comparison py/uploader.py @ 294:6bacd8ca9f8f

merge
author Matt Johnston <matt@ucc.asn.au>
date Sat, 06 Jul 2019 18:30:25 +0800
parents d15dda1b1f76 78c542f03030
children ca06059e76de
comparison
equal deleted inserted replaced
293:d15dda1b1f76 294:6bacd8ca9f8f
13 import utils 13 import utils
14 14
15 class Uploader(object): 15 class Uploader(object):
16 def __init__(self, server): 16 def __init__(self, server):
17 self.server = server 17 self.server = server
18 self.limitlog = utils.NotTooOften(600)
18 19
19 @asyncio.coroutine 20 @asyncio.coroutine
20 def run(self): 21 def run(self):
21 # wait for the first read 22 # wait for the first read
22 yield from asyncio.sleep(5) 23 yield from asyncio.sleep(5)
41 tosend['start_time'] = self.server.start_time 42 tosend['start_time'] = self.server.start_time
42 tosend['uptime'] = utils.uptime() 43 tosend['uptime'] = utils.uptime()
43 44
44 return tosend 45 return tosend
45 46
47 class BadServerResponse(Exception):
48 pass
49
46 @asyncio.coroutine 50 @asyncio.coroutine
47 def send(self, tosend): 51 def send(self, tosend):
48 js = json.dumps(tosend) 52 js = json.dumps(tosend)
49 if self.server.test_mode(): 53 if self.server.test_mode():
50 D("Would upload %s to %s" % (js, config.UPDATE_URL)) 54 D("Would upload %s to %s" % (js, config.UPDATE_URL))
53 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest() 57 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest()
54 send_data = {'data': js_enc.decode(), 'hmac': mac} 58 send_data = {'data': js_enc.decode(), 'hmac': mac}
55 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60) 59 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) 60 result = yield from asyncio.wait_for(r.text(), 60)
57 if r.status == 200 and result != 'OK': 61 if r.status == 200 and result != 'OK':
58 raise Exception("Server returned %s" % result) 62 raise BadServerResponse("Server returned %s" % result)
59 63
60 @asyncio.coroutine 64 @asyncio.coroutine
61 def do(self): 65 def do(self):
62 try: 66 try:
63 readings = self.server.take_readings() 67 readings = self.server.take_readings()
65 D("tosend >>>%s<<<" % str(tosend)) 69 D("tosend >>>%s<<<" % str(tosend))
66 nreadings = len(readings) 70 nreadings = len(readings)
67 yield from self.send(tosend) 71 yield from self.send(tosend)
68 readings = None 72 readings = None
69 D("Sent updated %d readings" % nreadings) 73 D("Sent updated %d readings" % nreadings)
74 except aiohttp.errors.ClientError as e:
75 self.limitlog.log("Error with uploader: %s" % str(e))
76 except asyncio.TimeoutError as e:
77 self.limitlog.log("uploader http timed out: %s" % str(e))
78 except self.BadServerResponse as e:
79 self.limitlog.log("Bad reply with uploader: %s" % str(e))
70 except Exception as e: 80 except Exception as e:
71 EX("Error in uploader: %s" % str(e)) 81 EX("Error in uploader: %s" % str(e))
72 finally: 82 finally:
73 if readings is not None: 83 if readings is not None:
74 self.server.pushfront(readings) 84 self.server.pushfront(readings)