forked from kvantetore/igrill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokencube.py
94 lines (76 loc) · 3.35 KB
/
tokencube.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
class TokenCubeHandler(object):
def __init__(self, device_settings):
self.device_settings = device_settings
self.devices = {}
def handleDiscovery(self, scanEntry, isNewDev, isNewData):
data = bytearray(scanEntry.scanData.get(255, []))
#print "Handling", data
# make sure we have a tokencube device
if len(data) < 7 or data[0] != 0xee or data[1] != 0xff:
return
dev = self.devices.get(scanEntry.addr, None)
if dev is None:
dev = self.devices[scanEntry.addr] = {}
#read sensor data
sensor_index = 5
while sensor_index < len(data):
sensor = data[sensor_index]
if sensor == 0x01 or sensor == 0x81:
dev["temperature"] = get_float(data, sensor_index + 1, 2)
sensor_index += 3
elif sensor == 0x04 or sensor == 0x84:
dev["humidity"] = get_float(data, sensor_index + 1, 2)
sensor_index += 3
elif sensor == 0x05 or sensor == 0x85:
dev["pressure"] = get_float(data, sensor_index + 1, 4)
sensor_index += 5
elif sensor == 0x06 or sensor == 0x86:
dev["orientation_x"] = get_int(data, sensor_index + 1, 2)
dev["orientation_y"] = get_int(data, sensor_index + 3, 2)
dev["orientation_z"] = get_int(data, sensor_index + 5, 2)
sensor_index += 7
elif sensor == 0x07 or sensor == 0x87:
dev["pir_motion"] = data[sensor_index + 1]
sensor_index += 2
elif sensor == 0x08 or sensor == 0x88:
dev["sensor_motion"] = data[sensor_index + 1]
sensor_index += 2
elif sensor == 0x09 or sensor == 0x89:
dev["shock_x"] = data[sensor_index + 1]
dev["shock_y"] = data[sensor_index + 2]
dev["shock_z"] = data[sensor_index + 3]
sensor_index += 4
elif sensor == 0x0A or sensor == 0x8A:
dev["battery"] = float(data[sensor_index + 1])
sensor_index += 2
else:
print "Unknown sensor", sensor, "at index", sensor_index, "data = ", [x for x in data]
break
print "Got sensor data:", dev
def persist_stats(self, persistence):
for addr, stats in self.devices.items():
settings = self.device_settings.get(addr, None)
if settings is None:
settings = {
"device": "TokenCube " + addr,
"addr": addr,
"type": "ambient"
}
#save
fields = {}
for field_name in ["temperature", "humidity", "pressure"]:
if field_name in stats:
fields[field_name] = stats[field_name]
if len(fields) > 0:
persistence.save("sensordata", fields, settings)
if "battery" in stats:
persistence.save_battery_level(stats["battery"], **settings)
#reset device stats
self.devices = {}
def get_float(data, offset, length):
return get_int(data, offset, length) / 100.0
def get_int(data, offset, length):
ret = 0
for i in range(length):
ret = ret + data[offset + length - i - 1] * 255**i
return ret