comparison beermon.py @ 2:17bc5535bbb7

Wait on a condition var instead of using time.sleep() - this works better with signals in a multithreaded environment (ie the signal will interrupt the wait) A few other misc tidy ups.
author darius
date Mon, 24 Sep 2007 13:31:15 +0000
parents c0b01c8c63eb
children 1af7c85d5a0e
comparison
equal deleted inserted replaced
1:c0b01c8c63eb 2:17bc5535bbb7
2 2
3 ############################################################################ 3 ############################################################################
4 # Monitor & control fermenter temperature 4 # Monitor & control fermenter temperature
5 # v1.0 5 # v1.0
6 # 6 #
7 # $Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $ 7 # $Id: beermon.py,v 1.3 2007/09/24 13:31:15 darius Exp $
8 # 8 #
9 # Depends on: Python 2.3 (I think) 9 # Depends on: Python 2.3 (I think)
10 # 10 #
11 ############################################################################ 11 ############################################################################
12 # 12 #
34 # SUCH DAMAGE. 34 # SUCH DAMAGE.
35 # 35 #
36 ############################################################################ 36 ############################################################################
37 37
38 38
39 import pexpect, re, threading, time, logging 39 import pexpect, re, threading, time, logging, sys, traceback
40 from logging.handlers import RotatingFileHandler 40 from logging.handlers import RotatingFileHandler
41 41
42 class ROMReadError(Exception): 42 class ROMReadError(Exception):
43 pass 43 pass
44 44
49 49
50 def __init__(self, m, _log): 50 def __init__(self, m, _log):
51 self.m = m 51 self.m = m
52 global log 52 global log
53 log = _log 53 log = _log
54 self.cv = threading.Condition()
55 self.cv.acquire()
54 56
55 def doit(self): 57 def doit(self):
56 log.debug("target temperature - %3.2f" % (self.targetTemp)) 58 log.debug("target temperature - %3.2f" % (self.targetTemp))
57 log.debug("fermenterId - %s" % (self.m.fermenterId)) 59 log.debug("fermenterId - %s" % (self.m.fermenterId))
58 log.debug("fridgeId - %s" % (self.m.fridgeId)) 60 log.debug("fridgeId - %s" % (self.m.fridgeId))
63 65
64 log.debug("=== Starting ===") 66 log.debug("=== Starting ===")
65 log.debug("Fermenter Fridge Ambient State New State") 67 log.debug("Fermenter Fridge Ambient State New State")
66 while True: 68 while True:
67 if (self.m.lastUpdate == 0): 69 if (self.m.lastUpdate == 0):
68 print "%s Invalid data" % (time.asctime()) 70 log.debug("Invalid data")
69 time.sleep(30) 71 self.cv.wait(self.pollInterval)
70 self.m.setState('idle') 72 self.m.setState('idle')
71 continue 73 continue
72 74
73 nextState = "-" 75 nextState = "-"
74 76
96 self.m.temps[self.m.fridgeId], self.m.temps[self.m.ambientId], 98 self.m.temps[self.m.fridgeId], self.m.temps[self.m.ambientId],
97 self.m.currState, nextState)) 99 self.m.currState, nextState))
98 if (nextState != "-"): 100 if (nextState != "-"):
99 self.m.setState(nextState) 101 self.m.setState(nextState)
100 102
101 time.sleep(self.pollInterval) 103 self.cv.wait(self.pollInterval)
102 104
103 105
104 class MonitorDev(threading.Thread): 106 class MonitorDev(threading.Thread):
105 # Match a ROM ID (eg 00:11:22:33:44:55:66:77) 107 # Match a ROM ID (eg 00:11:22:33:44:55:66:77)
106 romre = re.compile('([0-9a-f]{2}:){7}[0-9a-f]{2}') 108 romre = re.compile('([0-9a-f]{2}:){7}[0-9a-f]{2}')
276 while True: 278 while True:
277 self.updateTemps() 279 self.updateTemps()
278 280
279 def initLog(): 281 def initLog():
280 # Init our logging 282 # Init our logging
281 global log
282 log = logging.getLogger("monitor") 283 log = logging.getLogger("monitor")
283 284
284 # Default to warts and all logging 285 # Default to warts and all logging
285 log.setLevel(logging.DEBUG) 286 log.setLevel(logging.DEBUG)
286 287
300 log.addHandler(logfile) 301 log.addHandler(logfile)
301 log.addHandler(logstderr) 302 log.addHandler(logstderr)
302 return(log) 303 return(log)
303 304
304 def main(): 305 def main():
305 import sys, traceback, beermon 306 import beermon
306 307
308 global log
309 log = initLog()
310
311 log.debug("=== Initing ===")
312 log.debug("$Id: beermon.py,v 1.3 2007/09/24 13:31:15 darius Exp $")
313
314 m = None
307 exitCode = 0 315 exitCode = 0
308
309 initLog()
310
311 log.debug("=== Initing ===")
312 log.debug("$Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $")
313 m = beermon.MonitorDev()
314
315 try: 316 try:
317 m = beermon.MonitorDev()
318
316 c = beermon.Control(m, log) 319 c = beermon.Control(m, log)
317 # Wait for the first temperature readings to come through, saves 320 # Wait for the first temperature readings to come through, saves
318 # getting an 'invalid data' message 321 # getting an 'invalid data' message
319 time.sleep(3) 322 time.sleep(3)
320 c.doit() 323 c.doit()
321 log.debug("doit exited") 324 log.debug("doit exited")
322 325
323 except KeyboardInterrupt: 326 except KeyboardInterrupt:
324 log.debug("Exiting due to keyboard interrupt") 327 log.debug("Exiting due to keyboard interrupt")
325 328
326 #except Exception, e: 329 except Exception, e:
327 # log.debug("Something went wrong, details below") 330 log.debug("Something went wrong, details below")
328 # log.debug(e) 331 log.debug(e)
329 # #log.debug(reduce(lambda x, y: x + y, traceback.format_exception( 332 #log.debug(reduce(lambda x, y: x + y, traceback.format_exception(
330 # # sys.last_type, sys.last_value, sys.last_traceback))) 333 # sys.last_type, sys.last_value, sys.last_traceback)))
331 # exitCode = 1 334 exitCode = 1
332 335
333 finally: 336 finally:
334 # Make sure we try and turn it off if something goes wrong 337 # Make sure we try and turn it off if something goes wrong
335 m.setState('idle') 338 if (m != None):
339 m.setState('idle')
336 340
337 sys.exit(exitCode) 341 sys.exit(exitCode)
338 342
339 if __name__ == "__main__": 343 if __name__ == "__main__":
340 main() 344 main()