-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
210 lines (192 loc) · 7.13 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using System.Threading;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Devices.Enumeration;
using SwitchBotUtil;
namespace SwitbotThermometerApp;
class Program
{
enum Commands
{
GetTemp,
List,
Help
}
static void Main(string[] args)
{
int limit = 1;
int timeout = 120;
Commands command = Commands.GetTemp;
for (var j = 0; j < args.Length; j++)
{
switch (args[j])
{
case "--limit":
if (j + 1 >= args.Length) throw new Exception("--limit のパラメータが足りません");
j++;
limit = int.Parse(args[j]);
break;
case "--timeout":
if (j + 1 >= args.Length) throw new Exception("--timeout のパラメータがありません");
j++;
timeout = int.Parse(args[j]);
break;
case "--list":
case "-l":
command = Commands.List;
break;
case "--help":
case "-h":
command = Commands.Help;
break;
}
}
switch (command)
{
case Commands.GetTemp:
GetTemp(limit, timeout);
break;
case Commands.List:
ListDevices();
break;
case Commands.Help:
Help();
break;
}
}
private static void Help()
{
Console.WriteLine("Usage: SwitchBotMeter [options...]\n"
+ " Options:\n"
+ " --limit N Limit results (default: 1, unlimited: 0)\n"
+ " -t --timeout N Timeout seconds(default: 120, unlimited: 0)\n"
+ " -l --list ListDevices\n"
+ " -h --help Show help\n"
);
}
private static void ListDevices()
{
// Query for extra properties you want returned
string[] requestedProperties = { "System.Devices.Aep.DeviceAddress", "System.Devices.Aep.IsConnected" };
DeviceWatcher deviceWatcher =
DeviceInformation.CreateWatcher(
BluetoothLEDevice.GetDeviceSelector(),
//BluetoothLEDevice.GetDeviceSelectorFromPairingState(false),
requestedProperties,
DeviceInformationKind.AssociationEndpoint);
deviceWatcher.Added += (sender, args) =>
{
Console.WriteLine(args.Name + " " + args.Id);
var e = args.Properties.GetEnumerator();
while (e.MoveNext())
{
Console.WriteLine("[" + e.Current.Key + "]" + e.Current.Value);
}
};
deviceWatcher.Start();
Thread.Sleep(5 * 1000);
deviceWatcher.Stop();
}
private static void PrintTemp(ulong address, double temp, int humidity, int battery, String device)
{
Console.WriteLine($"{address:X} {temp:f1} {humidity:d} {battery:d} {device}");
}
private static void GetTemp(int limit, int timeout)
{
var watcher = new BluetoothLEAdvertisementWatcher();
var countdownEvent = new CountdownEvent(limit > 0 ? limit : 1);
var meters = new Dictionary<ulong, byte[]>();
watcher.Received += (sender, args) =>
{
var address = $"{args.BluetoothAddress:X}";
if (args.Advertisement.ServiceUuids.Contains(Uuids.meter))
{
// 初代の温度湿度計
foreach (var ds in args.Advertisement.DataSections)
{
if (ds.DataType == 22 && ds.Data.Length == 8)
{
var data = ds.Data.ToArray(2, 6);
double temp = ((data[3] & 0x0f) / 10.0 + (data[4] & 0x7f)) * ((data[4] & 0x80) != 0 ? 1 : -1);
int humidity = data[5] & 0x7f;
int battery = data[2] & 0x7f;
PrintTemp(args.BluetoothAddress, temp, humidity, battery, "Meter");
if (limit > 0) countdownEvent.Signal();
}
}
}
else
{
if (meters.TryGetValue(args.BluetoothAddress, out var serviceData))
{
var mdl = args.Advertisement.GetManufacturerDataByCompanyId(SwitchBot.companyId);
if (mdl.Count == 1)
{
var md = mdl[0].Data.ToArray();
double temp = ((md[9] & 0x7f) + ((md[8] & 0x0f) / 10.0)) * ((md[9] & 0x80) == 0 ? -1 : 1);
int humidity = (md[10] & 0x7f);
int battery = serviceData[4] & 0x7f;
int deviceType = serviceData[2] & 0x7f;
PrintTemp(args.BluetoothAddress, temp, humidity, battery, GetDeviceTypeName(deviceType));
if (limit > 0) countdownEvent.Signal();
}
}
else
{
var sdl = args.Advertisement.GetSectionsByType(0x16);
foreach (var sd in sdl)
{
if (sd.Data.GetByte(0) == 0x3d && sd.Data.GetByte(1) == 0xfd)
{
var deviceType = (sd.Data.GetByte(2) & 0x7f);
switch (deviceType)
{
case SBDeviceTypes.OutdoorMeter:
case SBDeviceTypes.MeterPlus:
case SBDeviceTypes.MeterPro:
meters[args.BluetoothAddress] = sd.Data.ToArray();
break;
default:
// Console.Error.WriteLine($"Unknown deviceType:0x{deviceType:X}");
break;
}
}
}
}
}
};
watcher.ScanningMode = BluetoothLEScanningMode.Active;
watcher.Start();
if (timeout > 0)
{
countdownEvent.Wait(timeout * 1000);
}
else
{
countdownEvent.Wait();
}
watcher.Stop();
}
static String GetDeviceTypeName(int value)
{
switch (value)
{
case SBDeviceTypes.Bot:
return "Bot";
case SBDeviceTypes.Meter:
return "Meter";
case SBDeviceTypes.MeterPlus:
return "MeterPlus";
case SBDeviceTypes.OutdoorMeter:
return "OutdoorMeter";
case SBDeviceTypes.MeterPro:
return "MeterPro";
default:
return $"notImplemented(0x{value:X})";
}
}
}