Mercurial > templog
annotate py/uploader.py @ 555:9dea75bd765f
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 08 Jun 2015 22:33:04 +0800 |
parents | 9499bd2f344b 39540ddacdca |
children | 26eee8591f61 |
rev | line source |
---|---|
444 | 1 import json |
2 import hmac | |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
3 import hashlib |
444 | 4 import zlib |
459 | 5 import binascii |
513
56cdea43b366
use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents:
490
diff
changeset
|
6 import logging |
527 | 7 import asyncio |
459 | 8 |
527 | 9 import aiohttp |
459 | 10 |
11 import config | |
12 from utils import L,D,EX,W,E | |
13 import utils | |
444 | 14 |
527 | 15 class Uploader(object): |
444 | 16 def __init__(self, server): |
17 self.server = server | |
18 | |
527 | 19 @asyncio.coroutine |
20 def run(self): | |
21 # wait for the first read | |
22 yield from asyncio.sleep(5) | |
444 | 23 while True: |
527 | 24 yield from self.do() |
25 yield from self.server.sleep(config.UPLOAD_SLEEP) | |
444 | 26 |
27 def get_tosend(self, readings): | |
28 tosend = {} | |
29 | |
30 tosend['fridge_on'] = self.server.fridge.is_on() | |
31 | |
32 tosend['now'] = self.server.now() | |
33 tosend['readings'] = readings | |
34 | |
35 tosend['wort_name'] = self.server.wort_name | |
459 | 36 tosend['fridge_name'] = self.server.wort_name |
444 | 37 |
490 | 38 tosend['current_params'] = dict(self.server.params) |
551
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
536
diff
changeset
|
39 tosend['current_params_epoch'] = self.server.params.get_epoch() |
444 | 40 |
41 tosend['start_time'] = self.server.start_time | |
42 tosend['uptime'] = utils.uptime() | |
43 | |
44 return tosend | |
45 | |
527 | 46 @asyncio.coroutine |
444 | 47 def send(self, tosend): |
48 js = json.dumps(tosend) | |
532
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
49 if self.server.test_mode(): |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
50 D("Would upload %s to %s" % (js, config.UPDATE_URL)) |
9b1d71310c83
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
530
diff
changeset
|
51 return |
536 | 52 js_enc = binascii.b2a_base64(zlib.compress(js.encode())).strip() |
53 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest() | |
54 send_data = {'data': js_enc.decode(), 'hmac': mac} | |
527 | 55 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60) |
56 result = yield from asyncio.wait_for(r.text(), 60) | |
551
9499bd2f344b
long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents:
536
diff
changeset
|
57 if r.status == 200 and result != 'OK': |
444 | 58 raise Exception("Server returned %s" % result) |
59 | |
527 | 60 @asyncio.coroutine |
459 | 61 def do(self): |
444 | 62 try: |
554
39540ddacdca
put more things inside "try"
Matt Johnston <matt@ucc.asn.au>
parents:
536
diff
changeset
|
63 readings = self.server.take_readings() |
445
5b9dc87c988f
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
444
diff
changeset
|
64 tosend = self.get_tosend(readings) |
459 | 65 nreadings = len(readings) |
527 | 66 yield from self.send(tosend) |
459 | 67 readings = None |
462 | 68 D("Sent updated %d readings" % nreadings) |
529 | 69 except Exception as e: |
530 | 70 E("Error in uploader: %s" % str(e)) |
444 | 71 finally: |
72 if readings is not None: | |
73 self.server.pushfront(readings) |