-
Notifications
You must be signed in to change notification settings - Fork 3
/
fs20f_logger.py
81 lines (60 loc) · 1.8 KB
/
fs20f_logger.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
import json
import sys
from bluepy import btle
from parser import Parser, ParseException
MAX_BUFFER_SIZE = 100
class HandleNotification(btle.DefaultDelegate):
def __init__(self,params):
btle.DefaultDelegate.__init__(self)
self.parser = Parser()
self.buffer = bytearray()
def handleNotification(self, cHandle, data):
self.buffer.extend(data)
assert(len(self.buffer) < MAX_BUFFER_SIZE)
result = True
while result is not None:
try:
result = self.process_buffer()
except ParseException as error:
continue
if result:
print(json.dumps(result))
def process_buffer(self):
msg_len = 0
# try to find start of valid message
for i in range(len(self.buffer)):
msg_len = self.parser.calc_msg_len(self.buffer[i:])
if msg_len:
break
else:
# no possible message found
self.buffer.clear()
return
# delete stuff before start
del self.buffer[:i]
# check if we have enough data
if len(self.buffer) < msg_len:
return
# pull out message from buffer
msg = self.buffer[:msg_len]
del self.buffer[:msg_len]
# parse message
res = self.parser.parse(msg)
return res
def start(mac_addr):
while True:
try:
p = btle.Peripheral(mac_addr)
p.setDelegate(HandleNotification(0))
while True:
if p.waitForNotifications(1.0):
continue
except KeyboardInterrupt:
break
except:
pass
if __name__ == '__main__':
if len(sys.argv) != 2:
print('missing MAC address')
sys.exit()
start(sys.argv[1])