comparison py/uploader.py @ 160:256505f98c4d

uploader works
author Matt Johnston <matt@ucc.asn.au>
date Thu, 03 Jan 2013 22:37:33 +0800
parents 3b4277aaed3c
children bf2a17873ba1
comparison
equal deleted inserted replaced
159:683cfd134f6a 160:256505f98c4d
1 import config
2
3 import json 1 import json
4 import hmac 2 import hmac
5 import zlib 3 import zlib
4 import binascii
5 import urllib
6 import urllib2
7
8 import gevent
9
10 import config
11 from utils import L,D,EX,W,E
12 import utils
6 13
7 class Uploader(gevent.Greenlet): 14 class Uploader(gevent.Greenlet):
8 def __init__(self, server): 15 def __init__(self, server):
9 gevent.Greenlet.__init__(self) 16 gevent.Greenlet.__init__(self)
10 self.server = server 17 self.server = server
11 18
12 def _run(self): 19 def _run(self):
20 gevent.sleep(5)
13 while True: 21 while True:
14 self.do() 22 self.do()
15 gevent.sleep(config.UPLOAD_SLEEP) 23 gevent.sleep(config.UPLOAD_SLEEP)
16 24
17 def get_tosend(self, readings): 25 def get_tosend(self, readings):
21 29
22 tosend['now'] = self.server.now() 30 tosend['now'] = self.server.now()
23 tosend['readings'] = readings 31 tosend['readings'] = readings
24 32
25 tosend['wort_name'] = self.server.wort_name 33 tosend['wort_name'] = self.server.wort_name
26 tosend['fridge_name'] = self.server.wort_fridge_name 34 tosend['fridge_name'] = self.server.wort_name
27 35
28 tosend.update(dict(self.server.params)) 36 tosend.update(dict(self.server.params))
29 37
30 tosend['start_time'] = self.server.start_time 38 tosend['start_time'] = self.server.start_time
31 tosend['uptime'] = utils.uptime() 39 tosend['uptime'] = utils.uptime()
40 con = urllib2.urlopen(config.UPDATE_URL, url_data) 48 con = urllib2.urlopen(config.UPDATE_URL, url_data)
41 result = con.read(100) 49 result = con.read(100)
42 if result != 'OK': 50 if result != 'OK':
43 raise Exception("Server returned %s" % result) 51 raise Exception("Server returned %s" % result)
44 52
45 def do(): 53 def do(self):
46 readings = self.server.take_readings() 54 readings = self.server.take_readings()
47 try: 55 try:
48 tosend = self.get_tosend(readings) 56 tosend = self.get_tosend(readings)
57 nreadings = len(readings)
58 self.send(tosend)
49 readings = None 59 readings = None
50 self.send(tosend) 60 L("Sent updated %d readings" % nreadings)
61 except urllib2.HTTPError, e:
62 E("Error in uploader: %s" % str(e))
51 except Exception, e: 63 except Exception, e:
52 EX"Error in uploader: %s" % str(e)) 64 EX("Error in uploader: %s" % str(e))
53 finally: 65 finally:
54 if readings is not None: 66 if readings is not None:
55 self.server.pushfront(readings) 67 self.server.pushfront(readings)