|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
3 | | -using System.Linq; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.IO; |
| 5 | +using System.Text.RegularExpressions; |
4 | 6 | using System.Threading.Tasks; |
5 | 7 | 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; |
6 | 13 |
|
7 | 14 | namespace LighthouseV2PowerControl |
8 | 15 | { |
9 | 16 | static class Program |
10 | 17 | { |
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 | + |
14 | 30 | [STAThread] |
15 | 31 | static void Main(string[] args) |
16 | 32 | { |
17 | 33 | Application.SetHighDpiMode(HighDpiMode.SystemAware); |
18 | 34 | Application.EnableVisualStyles(); |
19 | 35 | 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 | + } |
21 | 105 | } |
| 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 |
22 | 169 | } |
23 | 170 | } |
0 commit comments