Mercurial > templog
comparison py/uploader.py @ 259:26eee8591f61
long polling works
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Tue, 09 Jun 2015 23:27:44 +0800 |
parents | 6d06795aefbb |
children | 78c542f03030 d15dda1b1f76 |
comparison
equal
deleted
inserted
replaced
256:6d06795aefbb | 259:26eee8591f61 |
---|---|
20 def run(self): | 20 def run(self): |
21 # wait for the first read | 21 # wait for the first read |
22 yield from asyncio.sleep(5) | 22 yield from asyncio.sleep(5) |
23 while True: | 23 while True: |
24 yield from self.do() | 24 yield from self.do() |
25 yield from self.server.sleep(config.UPLOAD_SLEEP) | 25 yield from asyncio.sleep(config.UPLOAD_SLEEP) |
26 | 26 |
27 def get_tosend(self, readings): | 27 def get_tosend(self, readings): |
28 tosend = {} | 28 tosend = {} |
29 | 29 |
30 tosend['fridge_on'] = self.server.fridge.is_on() | 30 tosend['fridge_on'] = self.server.fridge.is_on() |
60 @asyncio.coroutine | 60 @asyncio.coroutine |
61 def do(self): | 61 def do(self): |
62 try: | 62 try: |
63 readings = self.server.take_readings() | 63 readings = self.server.take_readings() |
64 tosend = self.get_tosend(readings) | 64 tosend = self.get_tosend(readings) |
65 D("tosend >>>%s<<<" % str(tosend)) | |
65 nreadings = len(readings) | 66 nreadings = len(readings) |
66 yield from self.send(tosend) | 67 yield from self.send(tosend) |
67 readings = None | 68 readings = None |
68 D("Sent updated %d readings" % nreadings) | 69 D("Sent updated %d readings" % nreadings) |
69 except Exception as e: | 70 except Exception as e: |
70 E("Error in uploader: %s" % str(e)) | 71 EX("Error in uploader: %s" % str(e)) |
71 finally: | 72 finally: |
72 if readings is not None: | 73 if readings is not None: |
73 self.server.pushfront(readings) | 74 self.server.pushfront(readings) |