-
Notifications
You must be signed in to change notification settings - Fork 1
/
tp357tool.py
executable file
·152 lines (121 loc) · 4.18 KB
/
tp357tool.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import sys
import time
from gi.repository import GLib
import pydbus
def get_device(bus, address):
try:
return bus.get("org.bluez", "/org/bluez/hci0/dev_" + address.replace(":", "_"))
except KeyError:
adapter = bus.get("org.bluez", "/org/bluez/hci0")
adapter.StartDiscovery()
N_TRIES = 12
N_TRY_LENGTH = 5
for i in range(N_TRIES):
time.sleep(N_TRY_LENGTH)
try:
device = bus.get("org.bluez", "/org/bluez/hci0/dev_" + address.replace(":", "_"))
break
except KeyError:
pass
print(f"Waiting for device... {i+1}/{N_TRIES}", file=sys.stderr)
else:
adapter.StopDiscovery()
print("Device not found", file=sys.stderr)
sys.exit(1)
adapter.StopDiscovery()
return device
def bt_setup(address):
bus = pydbus.SystemBus()
device = get_device(bus, address)
N_TRIES = 3
for i in range(N_TRIES):
try:
device.Connect()
break
except GLib.Error as e:
print(f"Connecting to device... {i+1}/{N_TRIES}", file=sys.stderr)
print(e, file=sys.stderr)
time.sleep(1)
else:
print("Connection failed", file=sys.stderr)
sys.exit(1)
time.sleep(2) # XXX: wait for services etc. to be populated
object_manager = bus.get("org.bluez", "/")["org.freedesktop.DBus.ObjectManager"]
uuid_write = "00010203-0405-0607-0809-0a0b0c0d2b11"
uuid_read = "00010203-0405-0607-0809-0a0b0c0d2b10"
def get_characteristic(uuid):
return [desc for desc in object_manager.GetManagedObjects().items()
if desc[0].startswith(device._path) and desc[1].get("org.bluez.GattCharacteristic1", {}).get("UUID") == uuid][0]
write = bus.get("org.bluez", get_characteristic(uuid_write)[0])
read = bus.get("org.bluez", get_characteristic(uuid_read)[0])
return device, read, write
def wait_for_temp(read, write):
raw = []
def temp_handler(iface, prop_changed, prop_removed):
if not 'Value' in prop_changed:
return
if prop_changed['Value'][0] == 194:
raw.extend(prop_changed['Value'])
mainloop.quit()
return
read.onPropertiesChanged = temp_handler
read.StartNotify()
mainloop = GLib.MainLoop()
mainloop.run()
temp = (raw[3] + raw[4] * 256) / 10
humid = raw[5]
return [temp], [humid]
def get_temperatures(read, write, mode):
raw = []
if mode == "day":
op_code = [b"\xa7", b"\x7a"]
elif mode == "week":
op_code = [b"\xa6", b"\x6a"]
elif mode == "year":
op_code = [b"\xa8", b"\x8a"]
else:
raise RuntimeError(f"Unknown mode: {mode}")
def temp_handler(iface, prop_changed, prop_removed):
if not 'Value' in prop_changed:
return
if prop_changed['Value'][0] == ord(op_code[0]):
raw.append(prop_changed['Value'])
elif raw:
mainloop.quit()
return
read.onPropertiesChanged = temp_handler
read.StartNotify()
write.AcquireWrite({})
write.WriteValue(op_code[0] + b"\x01\x00" + op_code[1], {})
mainloop = GLib.MainLoop()
mainloop.run()
temps = []
humids = []
for t in raw:
if t[0] != ord(op_code[0]):
continue
time = t[1] + t[2]*256
flag = t[3]
for i in range(5):
ofs = 4 + i * 3
if t[ofs] == 0xff and t[ofs + 1] == 0xff:
temps.append(float('nan'))
humids.append(float('nan'))
continue
temps.append((t[ofs] + t[ofs + 1] * 256) / 10)
humids.append(t[ofs + 2])
return temps, humids
if __name__ == "__main__":
device, read, write = bt_setup(sys.argv[1])
if sys.argv[2] == "now":
temps, humids = wait_for_temp(read, write)
else:
temps, humids = get_temperatures(read, write, sys.argv[2])
device.Disconnect()
import csv
writer = csv.writer(sys.stdout)
writer.writerow(["temp", "humid"])
for i in range(len(temps)):
writer.writerow([temps[i], humids[i]])