Mercurial > templog
annotate py/uploader.py @ 254:ffe25107d520
put more things inside "try"
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 08 Jun 2015 22:31:21 +0800 |
parents | 86e638d564b0 |
children | 6d06795aefbb |
rev | line source |
---|---|
145 | 1 import json |
2 import hmac | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
3 import hashlib |
145 | 4 import zlib |
160 | 5 import binascii |
215
4d82099d1fe0
use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents:
190
diff
changeset
|
6 import logging |
228 | 7 import asyncio |
160 | 8 |
228 | 9 import aiohttp |
160 | 10 |
11 import config | |
12 from utils import L,D,EX,W,E | |
13 import utils | |
145 | 14 |
228 | 15 class Uploader(object): |
145 | 16 def __init__(self, server): |
17 self.server = server | |
18 | |
228 | 19 @asyncio.coroutine |
20 def run(self): | |
21 # wait for the first read | |
22 yield from asyncio.sleep(5) | |
145 | 23 while True: |
228 | 24 yield from self.do() |
25 yield from self.server.sleep(config.UPLOAD_SLEEP) | |
145 | 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 | |
160 | 36 tosend['fridge_name'] = self.server.wort_name |
145 | 37 |
190 | 38 tosend['current_params'] = dict(self.server.params) |
145 | 39 |
40 tosend['start_time'] = self.server.start_time | |
41 tosend['uptime'] = utils.uptime() | |
42 | |
43 return tosend | |
44 | |
228 | 45 @asyncio.coroutine |
145 | 46 def send(self, tosend): |
47 js = json.dumps(tosend) | |
233
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
48 if self.server.test_mode(): |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
49 D("Would upload %s to %s" % (js, config.UPDATE_URL)) |
19569cb5ed46
better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents:
231
diff
changeset
|
50 return |
241 | 51 js_enc = binascii.b2a_base64(zlib.compress(js.encode())).strip() |
52 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest() | |
53 send_data = {'data': js_enc.decode(), 'hmac': mac} | |
228 | 54 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60) |
55 result = yield from asyncio.wait_for(r.text(), 60) | |
145 | 56 if result != 'OK': |
57 raise Exception("Server returned %s" % result) | |
58 | |
228 | 59 @asyncio.coroutine |
160 | 60 def do(self): |
145 | 61 try: |
254
ffe25107d520
put more things inside "try"
Matt Johnston <matt@ucc.asn.au>
parents:
241
diff
changeset
|
62 readings = self.server.take_readings() |
146
3b4277aaed3c
update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents:
145
diff
changeset
|
63 tosend = self.get_tosend(readings) |
160 | 64 nreadings = len(readings) |
228 | 65 yield from self.send(tosend) |
160 | 66 readings = None |
163 | 67 D("Sent updated %d readings" % nreadings) |
230 | 68 except Exception as e: |
231 | 69 E("Error in uploader: %s" % str(e)) |
145 | 70 finally: |
71 if readings is not None: | |
72 self.server.pushfront(readings) |