Mercurial > templog
annotate py/tempserver.py @ 459:c34083c078db
uploader works
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Thu, 03 Jan 2013 22:37:33 +0800 |
parents | 3db3665af2e2 |
children | d73077e8cd67 |
rev | line source |
---|---|
447 | 1 #!/home/matt/templog/venv/bin/python |
439 | 2 |
3 import sys | |
4 import os | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
5 import logging |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
6 |
439 | 7 import gevent |
444 | 8 import gevent.monkey |
439 | 9 |
10 import utils | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
11 from utils import L,D,EX,W |
444 | 12 import fridge |
13 import config | |
14 import sensor_ds18b20 | |
15 import params | |
459 | 16 import uploader |
444 | 17 |
439 | 18 |
19 class Tempserver(object): | |
20 def __init__(self): | |
21 self.readings = [] | |
444 | 22 self.current = (None, None) |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
23 self.fridge = None |
444 | 24 |
25 # don't patch os, fork() is used by daemonize | |
26 gevent.monkey.patch_all(os=False, thread=False) | |
439 | 27 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
28 def __enter__(self): |
444 | 29 self.params = params.Params() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
30 self.fridge = fridge.Fridge(self) |
459 | 31 self.uploader = uploader.Uploader(self) |
444 | 32 self.params.load() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
33 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
34 return self |
439 | 35 |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
36 def __exit__(self, exc_type, exc_value, traceback): |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
37 L("Exiting, cleanup handler"); |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
38 self.fridge.off() |
439 | 39 |
444 | 40 def run(self): |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
41 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
42 if self.fridge is None: |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
43 raise Exception("Tempserver.run() must be within 'with Tempserver() as server'") |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
44 |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
45 # XXX do these go here or in __enter_() ? |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
46 self.start_time = self.now() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
47 self.fridge.start() |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
48 self.sensors.start() |
459 | 49 self.uploader.start() |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
50 |
444 | 51 # won't return. |
52 while True: | |
456 | 53 try: |
54 gevent.sleep(60) | |
55 except KeyboardInterrupt: | |
56 break | |
444 | 57 |
58 def now(self): | |
59 return utils.monotonic_time() | |
60 | |
443 | 61 def set_sensors(self, sensors): |
447 | 62 if hasattr(self, 'sensors'): |
443 | 63 self.sensors.kill() |
64 self.sensors = sensors | |
65 self.wort_name = sensors.wort_name() | |
66 self.fridge_name = sensors.fridge_name() | |
439 | 67 |
68 def take_readings(self): | |
69 ret = self.readings | |
70 self.readings = [] | |
71 return ret | |
72 | |
73 def pushfront(self, readings): | |
74 """ used if a caller of take_readings() fails """ | |
459 | 75 self.readings = readings + self.readings |
439 | 76 |
443 | 77 # a reading is a map of {sensorname: value}. temperatures |
78 # are float degrees | |
439 | 79 def add_reading(self, reading): |
80 """ adds a reading at the current time """ | |
454 | 81 D("add_reading(%s)" % str(reading)) |
444 | 82 self.readings.append( (reading, self.now())) |
443 | 83 self.current = (reading.get(self.wort_name, None), |
84 reading.get(self.fridge_name, None)) | |
459 | 85 if len(self.readings) > config.MAX_READINGS: |
86 self.readings = self.readings[-config.MAX_READINGS:] | |
439 | 87 |
88 def current_temps(self): | |
443 | 89 """ returns (wort_temp, fridge_temp) tuple """ |
447 | 90 return self.current |
444 | 91 |
92 def setup_logging(): | |
93 logging.basicConfig(format='%(asctime)s %(message)s', | |
94 datefmt='%m/%d/%Y %I:%M:%S %p', | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
95 level=logging.DEBUG) |
444 | 96 |
97 def main(): | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
98 setup_logging() |
444 | 99 |
100 if '--daemon' in sys.argv: | |
101 utils.cheap_daemon() | |
448
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
102 with Tempserver() as server: |
fe729664a5e6
working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents:
447
diff
changeset
|
103 server.run() |
444 | 104 |
105 if __name__ == '__main__': | |
106 main() |