Skip to content

Commit 8da0ffc

Browse files
committed
timeoutパラメータが未実装だったのを修正
1 parent 50e994b commit 8da0ffc

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

Program.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Runtime.InteropServices.WindowsRuntime;
4-
using System.Text;
53
using System.Threading;
6-
using System.Threading.Tasks;
74
using Windows.Devices.Bluetooth;
85
using Windows.Devices.Bluetooth.Advertisement;
96
using Windows.Devices.Enumeration;
@@ -24,31 +21,34 @@ static void Main(string[] args)
2421
{
2522
int limit = 1;
2623
int timeout = 120;
27-
Commands cmd = Commands.GetTemp;
24+
Commands command = Commands.GetTemp;
2825

2926
for (var j = 0; j < args.Length; j++)
3027
{
3128
switch (args[j])
3229
{
3330
case "--limit":
34-
if (j + 1 >= args.Length) throw new Exception("-limit のパラメータが足りません");
31+
if (j + 1 >= args.Length) throw new Exception("--limit のパラメータが足りません");
3532
j++;
3633
limit = int.Parse(args[j]);
3734
break;
3835
case "--timeout":
36+
if (j + 1 >= args.Length) throw new Exception("--timeout のパラメータがありません");
37+
j++;
38+
timeout = int.Parse(args[j]);
3939
break;
4040
case "--list":
4141
case "-l":
42-
cmd = Commands.List;
42+
command = Commands.List;
4343
break;
4444
case "--help":
4545
case "-h":
46-
cmd = Commands.Help;
46+
command = Commands.Help;
4747
break;
4848
}
4949
}
5050

51-
switch (cmd)
51+
switch (command)
5252
{
5353
case Commands.GetTemp:
5454
GetTemp(limit, timeout);
@@ -64,7 +64,7 @@ static void Main(string[] args)
6464

6565
private static void Help()
6666
{
67-
Console.WriteLine("Usage: SwitchbotThermometerApp [options...]\n"
67+
Console.WriteLine("Usage: SwitchBotMeter [options...]\n"
6868
+ " Options:\n"
6969
+ " --limit N Limit results (default: 1, unlimited: 0)\n"
7070
+ " -t --timeout N Timeout seconds(default: 120, unlimited: 0)\n"
@@ -122,7 +122,8 @@ private static void GetTemp(int limit, int timeout)
122122
var data = ds.Data.ToArray(2, 6);
123123
double temp = ((data[3] & 0x0f) / 10.0 + (data[4] & 0x7f)) * ((data[4] & 0x80) != 0 ? 1 : -1);
124124
int humidity = data[5] & 0x7f;
125-
Console.WriteLine($"{args.BluetoothAddress:X} {temp:f1} {humidity:d}");
125+
int battery = data[2] & 0x7f;
126+
Console.WriteLine($"{args.BluetoothAddress:X} {temp:f1} {humidity:d} {battery:d}");
126127
if (limit > 0) countdownEvent.Signal();
127128
}
128129
}

README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,46 @@
11
# SwitchBotMeter について
22

33
SwitchBot の温度湿度計から温度と湿度を取得して表示するコンソールアプリケーション。
4+
45
Linux のツールはあったけど、Windows 用が見つけられなかったので作成。
56

7+
68
本当は Win32 で作りたかったけど、難しそうだったので諦めて .NET Core 3.1 を使用。
9+
710
とはいえ、C# も .NET Core 3.1 もほとんど経験がないので、とんでもない実装をしている可能性があります。
811

912
# 使い方
1013

11-
起動すると、空白切りでデバイスのアドレス、温度、湿度が表示されます。
14+
起動すると、空白切りでデバイスのアドレス、温度、湿度、バッテリー残量(%)が表示されます。
15+
1216
デフォルトでは1回取得したら終了します。
17+
1318
デフォルトのタイムアウトは120秒です。
19+
20+
1回だけ取得。
1421
```cmd
1522
C:\> SwitchBotMeter
16-
FEDBB31721C2 26.1 58
23+
FEDBB31721C2 27.8 56 100
24+
```
25+
26+
タイムアウトを10秒に設定して取得。
27+
```cmd
28+
C:\> SwitchBotMeter --timeout 10
1729
```
30+
31+
1分間取得し続ける。
32+
```cmd
33+
C:\> SwitchBotMeter --timeout 60 --limit 0
34+
```
35+
36+
無限に取得を繰り返す。
37+
```cmd
38+
C:\> SwitchBotMeter --timeout 0 --limit 0
39+
```
40+
41+
# 参考
42+
43+
以下の情報を参考にしました。ありがとうございます。
44+
45+
- https://github.com/OpenWonderLabs/python-host/wiki/Meter-BLE-open-API
46+
- https://qiita.com/warpzone/items/11ec9bef21f5b965bce3

0 commit comments

Comments
 (0)