-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatus.py
executable file
·66 lines (54 loc) · 1.57 KB
/
status.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
#!/usr/bin/env python3
import sys
import time
import json
import arrow
import bluetooth._bluetooth as bluez
from pprint import pprint
from logger import open_bluetooth, process_packet
def save(data):
with open('seen.json', 'w') as outfile:
json.dump(data, outfile)
def load():
try:
with open('seen.json', 'r') as infile:
return(json.load(infile))
except FileNotFoundError:
return({})
def display(seen):
# Clear terminal
print(chr(27) + "[2J")
for mac in seen.keys():
print("{} {} {:04}".format(
mac,
seen[mac]['first_ts'],
seen[mac]['counter'],
seen[mac]['last_ts'] if 'last_ts' in seen[mac] else ''),
arrow.get(seen[mac]['last_ts']) - arrow.get(seen[mac]['first_ts']) if 'last_ts' in seen[mac] else ''
)
print()
seen = load()
sock = open_bluetooth()
try:
while True:
try:
packet = process_packet(sock)
# print(packet)
mac = packet['mac']
ts = packet['ts']
if not mac in seen:
seen[mac] = {}
seen[mac]['first_ts'] = ts
seen[mac]['counter'] = 1
else:
seen[mac]['last_ts'] = ts
seen[mac]['counter'] += 1
except bluez.error as e:
save(seen)
time.sleep(10)
print("Bluetooth failed with error {}, reconnecting".format(e), file=sys.stderr)
sock = open_bluetooth()
display(seen)
except KeyboardInterrupt:
save(seen)
pass