annotate py/uploader.py @ 458:94932e7051e5

Fix the updated json web code to work
author Matt Johnston <matt@ucc.asn.au>
date Thu, 03 Jan 2013 22:35:54 +0800
parents 5b9dc87c988f
children 256505f98c4d
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 config
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import json
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import hmac
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import zlib
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 class Uploader(gevent.Greenlet):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 def __init__(self, server):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 gevent.Greenlet.__init__(self)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 self.server = server
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 def _run(self):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 while True:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 self.do()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 gevent.sleep(config.UPLOAD_SLEEP)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 def get_tosend(self, readings):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 tosend = {}
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 tosend['fridge_on'] = self.server.fridge.is_on()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 tosend['now'] = self.server.now()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 tosend['readings'] = readings
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 tosend['wort_name'] = self.server.wort_name
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 tosend['fridge_name'] = self.server.wort_fridge_name
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 tosend.update(dict(self.server.params))
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['start_time'] = self.server.start_time
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 tosend['uptime'] = utils.uptime()
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 return tosend
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 def send(self, tosend):
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 js = json.dumps(tosend)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 js_enc = binascii.b2a_base64(zlib.compress(js))
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 mac = hmac.new(config.HMAC_KEY, js_enc).hexdigest()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 url_data = urllib.urlencode( {'data': js_enc, 'hmac': mac} )
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 con = urllib2.urlopen(config.UPDATE_URL, url_data)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 result = con.read(100)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 if result != 'OK':
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 raise Exception("Server returned %s" % result)
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 def do():
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 readings = self.server.take_readings()
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 try:
445
5b9dc87c988f update web to handle new style params
Matt Johnston <matt@ucc.asn.au>
parents: 444
diff changeset
48 tosend = self.get_tosend(readings)
444
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 readings = None
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 self.send(tosend)
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 except Exception, e:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 EX"Error in uploader: %s" % str(e))
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 finally:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 if readings is not None:
c5629d79b4ac few more bits
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 self.server.pushfront(readings)