Skip to content

Commit 12bec6f

Browse files
committed
Some minor changes
Architectural changes.
1 parent fa46119 commit 12bec6f

4 files changed

Lines changed: 179 additions & 131 deletions

File tree

LighthouseV2PowerControl/Class1.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LighthouseV2PowerControl
6+
{
7+
class Class1
8+
{
9+
}
10+
}

LighthouseV2PowerControl/Form1.cs

Lines changed: 13 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,40 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.ComponentModel;
4-
using System.Data;
1+
using System.Collections.Generic;
52
using System.Drawing;
63
using System.Linq;
7-
using System.Text;
8-
using System.Text.RegularExpressions;
9-
using System.Threading.Tasks;
104
using System.Windows.Forms;
11-
using Windows.Devices.Bluetooth;
12-
using Windows.Devices.Bluetooth.GenericAttributeProfile;
13-
using Windows.Devices.Enumeration;
14-
using Windows.Storage.Streams;
155

166

177
namespace LighthouseV2PowerControl
188
{
199
public partial class Form1 : Form
2010
{
21-
private Guid service = Guid.Parse("00001523-1212-efde-1523-785feabcd124");
22-
private Guid characteristic = Guid.Parse("00001525-1212-efde-1523-785feabcd124");
23-
private byte activateByte = 0x01;
24-
private byte deactivateByte = 0x00;
25-
private static List<GattCharacteristic> listGattCharacteristics = new List<GattCharacteristic>();
26-
27-
private Regex regex = new Regex("^LHB-.{8}");
28-
29-
public Form1(string[] args)
11+
public Form1()
3012
{
3113
InitializeComponent();
32-
btnStart.Click += (obj, e) => SendOnLighthouseAsync(activateByte);
33-
btnStop.Click += (obj, e) => SendOnLighthouseAsync(deactivateByte);
34-
if (args.Length > 0)
35-
{
36-
UseArgumentsAsync(args);
37-
}
38-
else
39-
{
40-
GetGattCharacteristicsAsync();
41-
}
4214
}
4315

44-
private async void UseArgumentsAsync(string[] args)
16+
public void Log(object msg, LogType type = LogType.log)
4517
{
46-
await GetGattCharacteristicsAsync();
47-
for (int i = 0; i < args.Length; ++i)
18+
if (type == LogType.error)
4819
{
49-
Log(args[i] + " " + listGattCharacteristics.Count.ToString());
50-
if (args[i] == "--powerOn")
51-
{
52-
await SendOnLighthouseAsync(activateByte);
53-
break;
54-
}
55-
else if (args[i] == "--powerOff")
56-
{
57-
await SendOnLighthouseAsync(deactivateByte);
58-
break;
59-
}
20+
lvStatus.Items.Add(msg.ToString());
21+
lvStatus.Items[lvStatus.Items.Count - 1].ForeColor = Color.DarkRed;
6022
}
61-
Close();
62-
}
63-
64-
#region Body
65-
/// <summary>
66-
/// Call once at startup to get the characteristics of all base stations.
67-
/// </summary>
68-
/// <returns></returns>
69-
private async Task GetGattCharacteristicsAsync()
70-
{
71-
DeviceInformationCollection GatDevices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(service));
72-
for (int id = 0; id < GatDevices.Count; ++id)
73-
{
74-
if (!regex.IsMatch(GatDevices[id].Name)) continue;
75-
76-
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(GatDevices[id].Id);
77-
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
78-
79-
if (result.Status == GattCommunicationStatus.Success)
80-
{
81-
IReadOnlyList<GattDeviceService> serviceList = result.Services;
82-
for (int i = 0; i < serviceList.Count; ++i)
83-
{
84-
if (serviceList[i].Uuid != service) continue;
85-
GattCharacteristicsResult gattRes = await serviceList[i].GetCharacteristicsForUuidAsync(characteristic);
86-
if (gattRes.Status == GattCommunicationStatus.Success)
87-
{
88-
var openStatus = await serviceList[i].OpenAsync(GattSharingMode.SharedReadAndWrite);
89-
IReadOnlyList<GattCharacteristic> characteristics = gattRes.Characteristics;
90-
for (int j = 0; j < characteristics.Count; ++j)
91-
{
92-
if (characteristics[j].Uuid != characteristic) continue;
93-
listGattCharacteristics.Add(characteristics[j]);
94-
}
95-
}
96-
else
97-
{
98-
LogError($"Characteristics {gattRes.Status};");
99-
}
100-
}
101-
}
102-
else
103-
{
104-
LogError($"Sevices {result.Status};");
105-
}
106-
}
107-
108-
Log($"lighthouses found: {listGattCharacteristics.Count};");
109-
if (listGattCharacteristics.Count > 0)
110-
{
111-
btnStop.Enabled = btnStart.Enabled = true;
112-
}
113-
}
114-
115-
/// <summary>
116-
/// Called to write the value to the characteristic for all found base stations.
117-
/// </summary>
118-
/// <param name="byte4send"></param>
119-
/// <returns></returns>
120-
private async Task SendOnLighthouseAsync(byte byte4send)
121-
{
122-
for (int i = 0; i < listGattCharacteristics.Count; ++i)
23+
else if(type == LogType.log)
12324
{
124-
DataWriter writer = new DataWriter();
125-
writer.WriteByte(byte4send);
126-
GattCommunicationStatus resWrite = await listGattCharacteristics[i].WriteValueAsync(writer.DetachBuffer());
127-
if (resWrite == GattCommunicationStatus.Success)
128-
{
129-
Log($"Success;");
130-
}
131-
else
132-
{
133-
LogError($"lighthouse {i + 1}: {resWrite};");
134-
}
25+
lvStatus.Items.Add(msg.ToString());
26+
lvStatus.Items[lvStatus.Items.Count - 1].ForeColor = Color.Green;
13527
}
13628
}
137-
#endregion
13829

139-
private void Log(object msg)
30+
public void BtnActive(bool active)
14031
{
141-
lvStatus.Items.Add(msg.ToString());
142-
lvStatus.Items[lvStatus.Items.Count - 1].ForeColor = Color.Green;
32+
btnStop.Enabled = btnStart.Enabled = active;
14333
}
14434

145-
private void LogError(object msg)
35+
public IEnumerable<Button> GetButtons()
14636
{
147-
lvStatus.Items.Add(msg.ToString());
148-
lvStatus.Items[lvStatus.Items.Count - 1].ForeColor = Color.DarkRed;
37+
return Controls.OfType<Button>();
14938
}
15039
}
15140
}

LighthouseV2PowerControl/LighthouseV2PowerControl.csproj

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<OutputType>WinExe</OutputType>
4+
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<UseWindowsForms>true</UseWindowsForms>
77
<ApplicationIcon>newIcon.ico</ApplicationIcon>
8+
<Version>0.0.2</Version>
9+
<Authors>D0rG</Authors>
810
</PropertyGroup>
911

1012
<ItemGroup>
1113
<Reference Include="System.Runtime.WindowsRuntime">
12-
<HintPath>..\..\..\..\..\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
14+
<HintPath>C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETCore\v4.5\System.Runtime.WindowsRuntime.dll</HintPath>
1315
</Reference>
1416
<Reference Include="Windows">
1517
<HintPath>E:\Windows Kits\10\UnionMetadata\10.0.19041.0\Windows.winmd</HintPath>
Lines changed: 152 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,170 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Linq;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Text.RegularExpressions;
46
using System.Threading.Tasks;
57
using System.Windows.Forms;
8+
using Windows.ApplicationModel.Email.DataProvider;
9+
using Windows.Devices.Bluetooth;
10+
using Windows.Devices.Bluetooth.GenericAttributeProfile;
11+
using Windows.Devices.Enumeration;
12+
using Windows.Storage.Streams;
613

714
namespace LighthouseV2PowerControl
815
{
916
static class Program
1017
{
11-
/// <summary>
12-
/// The main entry point for the application.
13-
/// </summary>
18+
private static Guid service = Guid.Parse("00001523-1212-efde-1523-785feabcd124");
19+
private static Guid characteristic = Guid.Parse("00001525-1212-efde-1523-785feabcd124");
20+
private static byte activateByte = 0x01;
21+
private static byte deactivateByte = 0x00;
22+
private static List<GattCharacteristic> listGattCharacteristics = new List<GattCharacteristic>();
23+
24+
private static Regex regex = new Regex("^LHB-.{8}");
25+
26+
public delegate void LogHandler(object msg, LogType type);
27+
public static event LogHandler OnLog;
28+
private static Form1 app = null;
29+
1430
[STAThread]
1531
static void Main(string[] args)
1632
{
1733
Application.SetHighDpiMode(HighDpiMode.SystemAware);
1834
Application.EnableVisualStyles();
1935
Application.SetCompatibleTextRenderingDefault(false);
20-
Application.Run(new Form1(args));
36+
app = new Form1();
37+
OnLog += (msg, type) => app.Log(msg, type);
38+
Stack<EventHandler> eventHandlers = new Stack<EventHandler>();
39+
eventHandlers.Push(new EventHandler((obj, args) => SendActiveStatus(true)));
40+
eventHandlers.Push(new EventHandler((obj, args) => SendActiveStatus(false)));
41+
foreach (Button button in app.GetButtons())
42+
{
43+
button.Click += eventHandlers.Pop();
44+
}
45+
46+
if (args.Length > 0)
47+
{
48+
app.ShowInTaskbar = false;
49+
app.WindowState = FormWindowState.Minimized;
50+
UseArgumentsAsync(args);
51+
}
52+
else
53+
{
54+
GetGattCharacteristicsAsync();
55+
}
56+
57+
Application.Run(app);
58+
}
59+
60+
private static async Task GetGattCharacteristicsAsync()
61+
{
62+
DeviceInformationCollection GatDevices = await DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid(service));
63+
64+
for (int id = 0; id < GatDevices.Count; ++id)
65+
{
66+
if (!regex.IsMatch(GatDevices[id].Name)) continue;
67+
68+
BluetoothLEDevice bluetoothLeDevice = await BluetoothLEDevice.FromIdAsync(GatDevices[id].Id);
69+
GattDeviceServicesResult result = await bluetoothLeDevice.GetGattServicesAsync();
70+
71+
if (result.Status == GattCommunicationStatus.Success)
72+
{
73+
IReadOnlyList<GattDeviceService> serviceList = result.Services;
74+
for (int i = 0; i < serviceList.Count; ++i)
75+
{
76+
if (serviceList[i].Uuid != service) continue;
77+
GattCharacteristicsResult gattRes = await serviceList[i].GetCharacteristicsForUuidAsync(characteristic);
78+
if (gattRes.Status == GattCommunicationStatus.Success)
79+
{
80+
var openStatus = await serviceList[i].OpenAsync(GattSharingMode.SharedReadAndWrite);
81+
IReadOnlyList<GattCharacteristic> characteristics = gattRes.Characteristics;
82+
for (int j = 0; j < characteristics.Count; ++j)
83+
{
84+
if (characteristics[j].Uuid != characteristic) continue;
85+
listGattCharacteristics.Add(characteristics[j]);
86+
}
87+
}
88+
else
89+
{
90+
LogError($"Characteristics {gattRes.Status};");
91+
}
92+
}
93+
}
94+
else
95+
{
96+
LogError($"Sevices {result.Status};");
97+
}
98+
}
99+
100+
Log($"lighthouses found: {listGattCharacteristics.Count};");
101+
if (listGattCharacteristics.Count > 0 && app != null)
102+
{
103+
app.BtnActive(true);
104+
}
21105
}
106+
107+
/// <summary>
108+
/// Called to write the value to the characteristic for all found base stations.
109+
/// </summary>
110+
/// <param name="byte4send"></param>
111+
/// <returns></returns>
112+
private static async Task SendOnLighthouseAsync(byte byte4send)
113+
{
114+
app.BtnActive(false);
115+
for (int i = 0; i < listGattCharacteristics.Count; ++i)
116+
{
117+
DataWriter writer = new DataWriter();
118+
writer.WriteByte(byte4send);
119+
GattCommunicationStatus resWrite = await listGattCharacteristics[i].WriteValueAsync(writer.DetachBuffer());
120+
if (resWrite == GattCommunicationStatus.Success)
121+
{
122+
Log($"Success;");
123+
}
124+
else
125+
{
126+
LogError($"lighthouse {i + 1}: {resWrite};");
127+
}
128+
}
129+
app.BtnActive(true);
130+
}
131+
132+
133+
private static async void UseArgumentsAsync(string[] args)
134+
{
135+
await GetGattCharacteristicsAsync();
136+
if (args[0] == "--powerOn")
137+
{
138+
await SendOnLighthouseAsync(activateByte);
139+
}
140+
else if (args[0] == "--powerOff")
141+
{
142+
await SendOnLighthouseAsync(deactivateByte);
143+
}
144+
Application.Exit();
145+
}
146+
147+
private static void Log(object msg)
148+
{
149+
if (app == null) return;
150+
OnLog.Invoke(msg, LogType.log);
151+
}
152+
153+
private static void LogError(object msg)
154+
{
155+
if (app == null) return;
156+
OnLog.Invoke(msg, LogType.error);
157+
}
158+
159+
public static void SendActiveStatus(bool status)
160+
{
161+
SendOnLighthouseAsync((status)? activateByte : deactivateByte);
162+
}
163+
}
164+
165+
public enum LogType
166+
{
167+
log,
168+
error
22169
}
23170
}

0 commit comments

Comments
 (0)