Mercurial > templog
annotate py/uploader.py @ 513:56cdea43b366
use requests rather than urllib2, better ipv6->ipv4 fallback
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Fri, 25 Jul 2014 23:53:38 +0800 |
parents | 1865674794c3 |
children | 16a83e2c97a0 |
rev | line source |
---|---|
444 | 1 import json |
2 import hmac | |
3 import zlib | |
459 | 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 | 6 |
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 | 9 |
10 import config | |
11 from utils import L,D,EX,W,E | |
12 import utils | |
444 | 13 |
14 class Uploader(gevent.Greenlet): | |
15 def __init__(self, server): | |
16 gevent.Greenlet.__init__(self) | |
17 self.server = server | |
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 | 22 def _run(self): |
459 | 23 gevent.sleep(5) |
444 | 24 while True: |
25 self.do() | |
26 gevent.sleep(config.UPLOAD_SLEEP) | |
27 | |
28 def get_tosend(self, readings): | |
29 tosend = {} | |
30 | |
31 tosend['fridge_on'] = self.server.fridge.is_on() | |
32 | |
33 tosend['now'] = self.server.now() | |
34 tosend['readings'] = readings | |
35 | |
36 tosend['wort_name'] = self.server.wort_name | |
459 | 37 tosend['fridge_name'] = self.server.wort_name |
444 | 38 |
490 | 39 tosend['current_params'] = dict(self.server.params) |
444 | 40 |
41 tosend['start_time'] = self.server.start_time | |
42 tosend['uptime'] = utils.uptime() | |
43 | |
44 return tosend | |
45 | |
46 def send(self, tosend): | |
47 js = json.dumps(tosend) | |
48 js_enc = binascii.b2a_base64(zlib.compress(js)) | |
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 | 53 if result != 'OK': |
54 raise Exception("Server returned %s" % result) | |
55 | |
459 | 56 def do(self): |
444 | 57 readings = self.server.take_readings() |
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 | 60 nreadings = len(readings) |
444 | 61 self.send(tosend) |
459 | 62 readings = None |
462 | 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 | 65 E("Error in uploader: %s" % str(e)) |
444 | 66 except Exception, e: |
459 | 67 EX("Error in uploader: %s" % str(e)) |
444 | 68 finally: |
69 if readings is not None: | |
70 self.server.pushfront(readings) |