annotate py/uploader.py @ 295:424f1446f214

fix for updated aiohttp
author Matt Johnston <matt@ucc.asn.au>
date Sat, 06 Jul 2019 18:35:52 +0800
parents 78c542f03030
children ca06059e76de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import json
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import zlib
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
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
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
7 import asyncio
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
8
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
9 import aiohttp
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
10
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
11 import config
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
12 from utils import L,D,EX,W,E
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
13 import utils
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
15 class Uploader(object):
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 def __init__(self, server):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 self.server = server
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
18 self.limitlog = utils.NotTooOften(600)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
20 @asyncio.coroutine
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
21 def run(self):
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
22 # wait for the first read
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
23 yield from asyncio.sleep(5)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 while True:
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
25 yield from self.do()
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 256
diff changeset
26 yield from asyncio.sleep(config.UPLOAD_SLEEP)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 def get_tosend(self, readings):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 tosend = {}
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 tosend['fridge_on'] = self.server.fridge.is_on()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 tosend['now'] = self.server.now()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 tosend['readings'] = readings
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 tosend['wort_name'] = self.server.wort_name
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
37 tosend['fridge_name'] = self.server.wort_name
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
190
f4aabbf0da67 params in their own bit
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
39 tosend['current_params'] = dict(self.server.params)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 241
diff changeset
40 tosend['current_params_epoch'] = self.server.params.get_epoch()
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 tosend['start_time'] = self.server.start_time
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 tosend['uptime'] = utils.uptime()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 return tosend
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
47 class BadServerResponse(Exception):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
48 pass
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
49
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
50 @asyncio.coroutine
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 def send(self, tosend):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 js = json.dumps(tosend)
233
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
53 if self.server.test_mode():
19569cb5ed46 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 231
diff changeset
54 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
55 return
241
86e638d564b0 sha256 hash
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
56 js_enc = binascii.b2a_base64(zlib.compress(js.encode())).strip()
86e638d564b0 sha256 hash
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
57 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest()
86e638d564b0 sha256 hash
Matt Johnston <matt@ucc.asn.au>
parents: 233
diff changeset
58 send_data = {'data': js_enc.decode(), 'hmac': mac}
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
59 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60)
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
60 result = yield from asyncio.wait_for(r.text(), 60)
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 241
diff changeset
61 if r.status == 200 and result != 'OK':
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
62 raise BadServerResponse("Server returned %s" % result)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
64 @asyncio.coroutine
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
65 def do(self):
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 try:
254
ffe25107d520 put more things inside "try"
Matt Johnston <matt@ucc.asn.au>
parents: 241
diff changeset
67 readings = self.server.take_readings()
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
68 tosend = self.get_tosend(readings)
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 256
diff changeset
69 D("tosend >>>%s<<<" % str(tosend))
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
70 nreadings = len(readings)
228
d9e81a563923 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 219
diff changeset
71 yield from self.send(tosend)
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
72 readings = None
163
bf2a17873ba1 less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
73 D("Sent updated %d readings" % nreadings)
295
424f1446f214 fix for updated aiohttp
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
74 except aiohttp.ClientResponseError as e:
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
75 self.limitlog.log("Error with uploader: %s" % str(e))
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
76 except asyncio.TimeoutError as e:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
77 self.limitlog.log("uploader http timed out: %s" % str(e))
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
78 except self.BadServerResponse as e:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
79 self.limitlog.log("Bad reply with uploader: %s" % str(e))
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 228
diff changeset
80 except Exception as e:
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 256
diff changeset
81 EX("Error in uploader: %s" % str(e))
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 finally:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if readings is not None:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 self.server.pushfront(readings)