annotate py/uploader.py @ 206:82603c0ded37

forgot text
author Matt Johnston <matt@ucc.asn.au>
date Sun, 30 Mar 2014 20:35:30 +0800
parents f4aabbf0da67
children 4d82099d1fe0
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
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import zlib
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
4 import binascii
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
5 import urllib
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
6 import urllib2
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
7
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
8 import gevent
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
9
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
10 import config
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
11 from utils import L,D,EX,W,E
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
12 import utils
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 class Uploader(gevent.Greenlet):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 def __init__(self, server):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 gevent.Greenlet.__init__(self)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 self.server = server
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 def _run(self):
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
20 gevent.sleep(5)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 while True:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 self.do()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 gevent.sleep(config.UPLOAD_SLEEP)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 def get_tosend(self, readings):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 tosend = {}
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 tosend['fridge_on'] = self.server.fridge.is_on()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 tosend['now'] = self.server.now()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 tosend['readings'] = readings
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['wort_name'] = self.server.wort_name
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
34 tosend['fridge_name'] = self.server.wort_name
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
190
f4aabbf0da67 params in their own bit
Matt Johnston <matt@ucc.asn.au>
parents: 163
diff changeset
36 tosend['current_params'] = dict(self.server.params)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 tosend['start_time'] = self.server.start_time
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 tosend['uptime'] = utils.uptime()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 return tosend
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 def send(self, tosend):
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 js = json.dumps(tosend)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 js_enc = binascii.b2a_base64(zlib.compress(js))
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 mac = hmac.new(config.HMAC_KEY, js_enc).hexdigest()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 url_data = urllib.urlencode( {'data': js_enc, 'hmac': mac} )
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 con = urllib2.urlopen(config.UPDATE_URL, url_data)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 result = con.read(100)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 if result != 'OK':
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 raise Exception("Server returned %s" % result)
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
53 def do(self):
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 readings = self.server.take_readings()
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 try:
146
3b4277aaed3c update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
56 tosend = self.get_tosend(readings)
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
57 nreadings = len(readings)
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 self.send(tosend)
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
59 readings = None
163
bf2a17873ba1 less verbose logging
Matt Johnston <matt@ucc.asn.au>
parents: 160
diff changeset
60 D("Sent updated %d readings" % nreadings)
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
61 except urllib2.HTTPError, e:
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
62 E("Error in uploader: %s" % str(e))
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 except Exception, e:
160
256505f98c4d uploader works
Matt Johnston <matt@ucc.asn.au>
parents: 146
diff changeset
64 EX("Error in uploader: %s" % str(e))
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 finally:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 if readings is not None:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 self.server.pushfront(readings)