annotate py/uploader.py @ 516:a976793021d2

merge
author Matt Johnston <matt@ucc.asn.au>
date Fri, 25 Jul 2014 23:55:35 +0800
parents 56cdea43b366
children 16a83e2c97a0
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
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import zlib
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
4 import binascii
513
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
5 import logging
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
6
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
7 import gevent
513
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
8 import requests
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
9
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
10 import config
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
11 from utils import L,D,EX,W,E
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
12 import utils
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 class Uploader(gevent.Greenlet):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 def __init__(self, server):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 gevent.Greenlet.__init__(self)
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
513
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
19 requests_log = logging.getLogger("requests")
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
20 requests_log.setLevel(logging.WARNING)
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
21
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 def _run(self):
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
23 gevent.sleep(5)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 while True:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 self.do()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 gevent.sleep(config.UPLOAD_SLEEP)
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)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 tosend['start_time'] = self.server.start_time
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 tosend['uptime'] = utils.uptime()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 return tosend
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45
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)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 js_enc = binascii.b2a_base64(zlib.compress(js))
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 mac = hmac.new(config.HMAC_KEY, js_enc).hexdigest()
513
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
50 send_data = {'data': js_enc, 'hmac': mac}
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
51 r = requests.post(config.UPDATE_URL, data=send_data)
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
52 result = r.text
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if result != 'OK':
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 raise Exception("Server returned %s" % result)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
56 def do(self):
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 readings = self.server.take_readings()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 try:
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
59 tosend = self.get_tosend(readings)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
60 nreadings = len(readings)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 self.send(tosend)
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
62 readings = None
462
a91adc95543d less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 459
diff changeset
63 D("Sent updated %d readings" % nreadings)
513
56cdea43b366 use requests rather than urllib2, better ipv6->ipv4 fallback
Matt Johnston <matt@ucc.asn.au>
parents: 490
diff changeset
64 except requests.exceptions.RequestException, e:
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
65 E("Error in uploader: %s" % str(e))
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 except Exception, e:
459
c34083c078db uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 445
diff changeset
67 EX("Error in uploader: %s" % str(e))
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 finally:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 if readings is not None:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 self.server.pushfront(readings)