-
Notifications
You must be signed in to change notification settings - Fork 0
/
telemetry.py
50 lines (41 loc) · 1.7 KB
/
telemetry.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
import threading
from time import sleep
import boto
from boto.s3.bucket import Bucket
from boto.s3.key import Key
from boto.cloudfront import CloudFrontConnection
import json, datetime, signal, os
class Telemetry(threading.Thread):
def __init__(self, ergometer=None, session=None):
threading.Thread.__init__(self)
self.ergometer = ergometer
self.session = session
self.running = True
self.connection = boto.connect_s3()
self.bucket = Bucket(self.connection, 'ergometria.async.fi')
self.k = Key(self.bucket)
self.k.key = 'live/latest.json'
def write_data(self, timestamp=datetime.datetime.utcnow, pulse=0, target_pulse=0, power=0, rpm=0):
data = {
'timestamp': timestamp.strftime('%Y-%m-%dT%H:%M:%S+0000'),
'pulse': pulse,
'target_pulse': target_pulse,
'power': power,
'rpm': rpm
}
f = open('www/' + self.k.key, 'w')
f.write(json.dumps(data))
f.close()
self.k.set_contents_from_filename('www/' + self.k.key, headers={'Cache-Control': 'max-age=0'})
def run(self):
while True:
self.write_data(timestamp = datetime.datetime.utcnow(),
pulse = self.ergometer.device.pulse,
target_pulse = self.session.step(self.ergometer.pc)['pulse'],
power = self.ergometer.device.power,
rpm = self.ergometer.device.rpm)
sleep(0.5)
if not self.running:
os.remove('www/live/latest.json')
self.k.delete()
return