Mercurial > templog
comparison py/tempserver.py @ 465:a40f30fad8f6
Merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Mon, 21 Jan 2013 07:25:52 +0800 |
parents | a91adc95543d |
children | 5821c5fab919 |
comparison
equal
deleted
inserted
replaced
464:c3926e7cfb0c | 465:a40f30fad8f6 |
---|---|
4 import os | 4 import os |
5 import logging | 5 import logging |
6 | 6 |
7 import gevent | 7 import gevent |
8 import gevent.monkey | 8 import gevent.monkey |
9 import lockfile | |
10 import daemon | |
9 | 11 |
10 import utils | 12 import utils |
11 from utils import L,D,EX,W | 13 from utils import L,D,EX,W |
12 import fridge | 14 import fridge |
13 import config | 15 import config |
14 import sensor_ds18b20 | 16 import sensor_ds18b20 |
15 import params | 17 import params |
18 import uploader | |
16 | 19 |
17 | 20 |
18 class Tempserver(object): | 21 class Tempserver(object): |
19 def __init__(self): | 22 def __init__(self): |
20 self.readings = [] | 23 self.readings = [] |
25 gevent.monkey.patch_all(os=False, thread=False) | 28 gevent.monkey.patch_all(os=False, thread=False) |
26 | 29 |
27 def __enter__(self): | 30 def __enter__(self): |
28 self.params = params.Params() | 31 self.params = params.Params() |
29 self.fridge = fridge.Fridge(self) | 32 self.fridge = fridge.Fridge(self) |
33 self.uploader = uploader.Uploader(self) | |
30 self.params.load() | 34 self.params.load() |
31 self.set_sensors(sensor_ds18b20.DS18B20s(self)) | 35 self.set_sensors(sensor_ds18b20.DS18B20s(self)) |
32 return self | 36 return self |
33 | 37 |
34 def __exit__(self, exc_type, exc_value, traceback): | 38 def __exit__(self, exc_type, exc_value, traceback): |
42 | 46 |
43 # XXX do these go here or in __enter_() ? | 47 # XXX do these go here or in __enter_() ? |
44 self.start_time = self.now() | 48 self.start_time = self.now() |
45 self.fridge.start() | 49 self.fridge.start() |
46 self.sensors.start() | 50 self.sensors.start() |
51 self.uploader.start() | |
47 | 52 |
48 # won't return. | 53 # won't return. |
49 while True: | 54 while True: |
50 try: | 55 try: |
51 gevent.sleep(60) | 56 gevent.sleep(60) |
67 self.readings = [] | 72 self.readings = [] |
68 return ret | 73 return ret |
69 | 74 |
70 def pushfront(self, readings): | 75 def pushfront(self, readings): |
71 """ used if a caller of take_readings() fails """ | 76 """ used if a caller of take_readings() fails """ |
72 self.readings = pushback + self.readings | 77 self.readings = readings + self.readings |
73 | 78 |
74 # a reading is a map of {sensorname: value}. temperatures | 79 # a reading is a map of {sensorname: value}. temperatures |
75 # are float degrees | 80 # are float degrees |
76 def add_reading(self, reading): | 81 def add_reading(self, reading): |
77 """ adds a reading at the current time """ | 82 """ adds a reading at the current time """ |
78 D("add_reading(%s)" % str(reading)) | 83 D("add_reading(%s)" % str(reading)) |
79 self.readings.append( (reading, self.now())) | 84 self.readings.append( (reading, self.now())) |
80 self.current = (reading.get(self.wort_name, None), | 85 self.current = (reading.get(self.wort_name, None), |
81 reading.get(self.fridge_name, None)) | 86 reading.get(self.fridge_name, None)) |
87 if len(self.readings) > config.MAX_READINGS: | |
88 self.readings = self.readings[-config.MAX_READINGS:] | |
82 | 89 |
83 def current_temps(self): | 90 def current_temps(self): |
84 """ returns (wort_temp, fridge_temp) tuple """ | 91 """ returns (wort_temp, fridge_temp) tuple """ |
85 return self.current | 92 return self.current |
86 | 93 |
87 def setup_logging(): | 94 def setup_logging(): |
88 logging.basicConfig(format='%(asctime)s %(message)s', | 95 logging.basicConfig(format='%(asctime)s %(message)s', |
89 datefmt='%m/%d/%Y %I:%M:%S %p', | 96 datefmt='%m/%d/%Y %I:%M:%S %p', |
90 level=logging.DEBUG) | 97 level=logging.INFO) |
98 | |
99 def start(): | |
100 with Tempserver() as server: | |
101 server.run() | |
91 | 102 |
92 def main(): | 103 def main(): |
93 setup_logging() | 104 setup_logging() |
94 | 105 |
106 pidpath = os.path.join(os.path.dirname(__file__), 'tempserver-lock') | |
107 pidf = lockfile.FileLock(pidpath, threaded=False) | |
108 pidf.acquire(0) | |
109 | |
95 if '--daemon' in sys.argv: | 110 if '--daemon' in sys.argv: |
96 utils.cheap_daemon() | 111 logpath = os.path.join(os.path.dirname(__file__), 'tempserver.log') |
97 with Tempserver() as server: | 112 logf = open(logpath, 'a+') |
98 server.run() | 113 with daemon.DaemonContext(pidfile=pidf, stdout=logf, stderr = logf): |
114 start() | |
115 else: | |
116 with pidf: | |
117 start() | |
99 | 118 |
100 if __name__ == '__main__': | 119 if __name__ == '__main__': |
101 main() | 120 main() |