annotate py/uploader.py @ 534:0cc7f2dda0e8

missing sensor.py
author Matt Johnston <matt@ucc.asn.au>
date Tue, 05 May 2015 21:29:19 +0800
parents 9b1d71310c83
children 86e638d564b0
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
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
19 @asyncio.coroutine
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
20 def run(self):
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
21 # wait for the first read
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
22 yield from asyncio.sleep(5)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 while True:
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
24 yield from self.do()
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
25 yield from self.server.sleep(config.UPLOAD_SLEEP)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def get_tosend(self, readings):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 tosend = {}
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 tosend['fridge_on'] = self.server.fridge.is_on()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 tosend['now'] = self.server.now()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 tosend['readings'] = readings
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 tosend['wort_name'] = self.server.wort_name
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
36 tosend['fridge_name'] = self.server.wort_name
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
490
1865674794c3 params in their own bit
Matt Johnston <matt@ucc.asn.au>
parents: 462
diff changeset
38 tosend['current_params'] = dict(self.server.params)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 tosend['start_time'] = self.server.start_time
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 tosend['uptime'] = utils.uptime()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 return tosend
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
45 @asyncio.coroutine
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 def send(self, tosend):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 js = json.dumps(tosend)
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
48 if self.server.test_mode():
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
49 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
50 return
530
73257bae83ea fix a few python3 issues
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
51 js_enc = binascii.b2a_base64(zlib.compress(js.encode()))
532
9b1d71310c83 better arg parser. seems close to ready
Matt Johnston <matt@ucc.asn.au>
parents: 530
diff changeset
52 mac = hmac.new(config.HMAC_KEY.encode(), js_enc, hashlib.sha1).hexdigest()
513
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
53 send_data = {'data': js_enc, 'hmac': mac}
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
54 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
55 result = yield from asyncio.wait_for(r.text(), 60)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if result != 'OK':
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 raise Exception("Server returned %s" % result)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
59 @asyncio.coroutine
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
60 def do(self):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 readings = self.server.take_readings()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 try:
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
63 tosend = self.get_tosend(readings)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
64 nreadings = len(readings)
527
dd8895652485 porting to asyncio
Matt Johnston <matt@ucc.asn.au>
parents: 518
diff changeset
65 yield from self.send(tosend)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
66 readings = None
462
a91adc95543d less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
67 D("Sent updated %d readings" % nreadings)
529
5201f441bf4a run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 527
diff changeset
68 except Exception as e:
530
73257bae83ea fix a few python3 issues
Matt Johnston <matt@ucc.asn.au>
parents: 529
diff changeset
69 E("Error in uploader: %s" % str(e))
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 finally:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 if readings is not None:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 self.server.pushfront(readings)