annotate py/uploader.py @ 567:f93290b37abf

chown gpio files too
author Matt Johnston <matt@ucc.asn.au>
date Tue, 10 Nov 2015 21:10:57 +0800
parents 59cb8449ef97
children 6bacd8ca9f8f 424f1446f214
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import json
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
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
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import zlib
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
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
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
7 import asyncio
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
8
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
9 import aiohttp
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
10
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
11 import config
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
12 from utils import L,D,EX,W,E
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
13 import utils
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
15 class Uploader(object):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 def __init__(self, server):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 self.server = server
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
18 self.limitlog = utils.NotTooOften(600)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
20 @asyncio.coroutine
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
21 def run(self):
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
22 # wait for the first read
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
23 yield from asyncio.sleep(5)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 while True:
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
25 yield from self.do()
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 555
diff changeset
26 yield from asyncio.sleep(config.UPLOAD_SLEEP)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 def get_tosend(self, readings):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 tosend = {}
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 tosend['fridge_on'] = self.server.fridge.is_on()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 tosend['now'] = self.server.now()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 tosend['readings'] = readings
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 tosend['wort_name'] = self.server.wort_name
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
37 tosend['fridge_name'] = self.server.wort_name
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
490
1865674794c3 params in their own bit
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
39 tosend['current_params'] = dict(self.server.params)
551
9499bd2f344b long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 536
diff changeset
40 tosend['current_params_epoch'] = self.server.params.get_epoch()
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 tosend['start_time'] = self.server.start_time
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 tosend['uptime'] = utils.uptime()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 return tosend
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
47 class BadServerResponse(Exception):
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
48 pass
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
49
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
50 @asyncio.coroutine
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 def send(self, tosend):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 js = json.dumps(tosend)
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
53 if self.server.test_mode():
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
54 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
55 return
536
3a27b1a09d2e sha256 hash
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
56 js_enc = binascii.b2a_base64(zlib.compress(js.encode())).strip()
3a27b1a09d2e sha256 hash
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
57 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha256).hexdigest()
3a27b1a09d2e sha256 hash
Matt Johnston <matt@ucc.asn.au>
parents: 532
diff changeset
58 send_data = {'data': js_enc.decode(), 'hmac': mac}
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
59 r = yield from asyncio.wait_for(aiohttp.request('post', config.UPDATE_URL, data=send_data), 60)
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
60 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
61 if r.status == 200 and result != 'OK':
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
62 raise BadServerResponse("Server returned %s" % result)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
64 @asyncio.coroutine
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
65 def do(self):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 try:
554
39540ddacdca put more things inside "try"
Matt Johnston <matt@ucc.asn.au>
parents: 536
diff changeset
67 readings = self.server.take_readings()
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
68 tosend = self.get_tosend(readings)
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 555
diff changeset
69 D("tosend >>>%s<<<" % str(tosend))
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
70 nreadings = len(readings)
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
71 yield from self.send(tosend)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
72 readings = None
462
a91adc95543d less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
73 D("Sent updated %d readings" % nreadings)
564
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
74 except aiohttp.errors.ClientError as e:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
75 self.limitlog.log("Error with uploader: %s" % str(e))
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
76 except asyncio.TimeoutError as e:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
77 self.limitlog.log("uploader http timed out: %s" % str(e))
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
78 except self.BadServerResponse as e:
59cb8449ef97 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 556
diff changeset
79 self.limitlog.log("Bad reply with uploader: %s" % str(e))
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
80 except Exception as e:
556
c5f8375b12a2 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 555
diff changeset
81 EX("Error in uploader: %s" % str(e))
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 finally:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if readings is not None:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 self.server.pushfront(readings)