-
Notifications
You must be signed in to change notification settings - Fork 0
/
record.py
executable file
·167 lines (138 loc) · 5.56 KB
/
record.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/usr/bin/env python3
import sys
import datetime
import time
import argparse
import csv
import os
import json
import statistics
from collections import defaultdict
import bluetooth._bluetooth as bluez
import blescan
TILTS = {
'a495bb10c5b14b44b5121370f02d74de': 'red',
'a495bb20c5b14b44b5121370f02d74de': 'green',
'a495bb30c5b14b44b5121370f02d74de': 'black',
'a495bb40c5b14b44b5121370f02d74de': 'purple',
'a495bb50c5b14b44b5121370f02d74de': 'orange',
'a495bb60c5b14b44b5121370f02d74de': 'blue',
'a495bb70c5b14b44b5121370f02d74de': 'yellow',
'a495bb80c5b14b44b5121370f02d74de': 'pink',
}
def distinct(objects):
seen = set()
unique = []
for obj in objects:
if obj['uuid'] not in seen:
unique.append(obj)
seen.add(obj['uuid'])
return unique
def to_celsius(fahrenheit):
return round((fahrenheit - 32.0) / 1.8, 1)
def keep_going(cutoff):
return time.time() < cutoff
def epoch_to_timestamp(epoch):
return datetime.datetime.fromtimestamp(epoch).isoformat(timespec='seconds')
def collect_data(config0, verbose):
cutoff = time.time() + 60 * config0['give_up_minutes']
start_epoch = round(time.time())
# used later for missing readings
nbr_readings = config0['readings']
wait_seconds = config0['wait_seconds']
recordings = defaultdict(list)
# output: str color -> list of (epoch, gravity, fahrenheit) tuples
for i in range(nbr_readings):
if verbose:
print('Reading', i + 1, 'of', nbr_readings)
found = False
while keep_going(cutoff) and (not found):
beacons = distinct(blescan.parse_events(sock, 10))
if verbose:
print('Found', len(beacons), 'beacons')
for beacon in beacons:
if beacon['uuid'] in TILTS.keys():
found = True
color = TILTS[beacon['uuid']]
epoch = round(time.time())
gravity = beacon['minor']
fahrenheit = beacon['major']
recordings[color].append((epoch, gravity, fahrenheit))
if verbose:
print(color, epoch, gravity, fahrenheit)
if (i < nbr_readings - 1) and keep_going(cutoff):
if verbose:
print('Waiting', wait_seconds, '...')
time.sleep(wait_seconds)
return start_epoch, recordings
def process_data(config0, recordings, default_epoch0, verbose):
default_timestamp = epoch_to_timestamp(default_epoch0)
results = []
# output: list of lists:
# [color, epoch, timestamp, gravity, celsius, fahrenheit, readings, raw_gravity]
water = config0.get('water', 1000)
for color in config0['hydrometers'].keys():
if color in recordings:
readings = len(recordings[color])
epochs = [t[0] for t in recordings[color]]
gravities = [t[1] for t in recordings[color]]
fahrenheits = [t[2] for t in recordings[color]]
epoch = round(statistics.mean(epochs))
timestamp = epoch_to_timestamp(max(epochs))
raw_gravity = round(statistics.median(gravities), 1)
gravity = raw_gravity + 1000 - water
fahrenheit = round(statistics.median(fahrenheits), 1)
celsius = to_celsius(fahrenheit)
results.append([color, epoch, timestamp, gravity, celsius, fahrenheit, readings, raw_gravity])
else:
# Missing readings: the empty string will be a Nan in pandas
results.append([color, default_epoch0, default_timestamp, '', '', '', 0])
if verbose:
for result in results:
print(*result)
return results
def store_data(config0, base_dir0, verbose, data_lines):
for data_line in data_lines:
color = data_line[0]
output_file = config0.get('hydrometers', []).get(color, None)
if output_file:
output_path = os.path.join(base_dir0, output_file)
if verbose:
print(f'Output: {output_path}')
with open(output_path, 'a') as f0:
writer = csv.writer(f0, lineterminator='\n')
writer.writerow(data_line)
if verbose:
print('Got', *data_line)
else:
print('Output', *data_line)
return
if __name__ == '__main__':
oparser = argparse.ArgumentParser(description="record Tilt hydrometer data",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
oparser.add_argument("-c", dest="config_file",
required=True,
metavar="JSON",
help="JSON config file")
oparser.add_argument("-v", dest="verbose",
default=False,
action='store_true',
help='verbose for debugging')
options = oparser.parse_args()
base_dir = os.path.dirname(options.config_file)
if options.verbose:
print(f'Config: {options.config_file}')
print(f'Basedir: {base_dir}')
with open(options.config_file, 'r') as f:
config = json.load(f)
dev_id = 0
try:
sock = bluez.hci_open_dev(dev_id)
except:
print('error accessing bluetooth device...')
sys.exit(1)
blescan.hci_le_set_scan_parameters(sock)
blescan.hci_enable_le_scan(sock)
default_epoch, raw_data = collect_data(config, options.verbose)
processed_data = process_data(config, raw_data, default_epoch, options.verbose)
store_data(config, base_dir, options.verbose, processed_data)