comparison beermon.py @ 3:f197f6716cd6

Log version at startup, add copyright verbiage, etc.. Move log init into the top level and pass it around (dunno why global doesn't do what I want tho..) Change hysteresis temperature to 1 degree.
author darius
date Mon, 24 Sep 2007 04:05:52 +0000
parents af88c0f96295
children 618372f83862
comparison
equal deleted inserted replaced
2:5e1453900d1c 3:f197f6716cd6
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2
3 ############################################################################
4 # Monitor & control fermenter temperature
5 # v1.0
6 #
7 # $Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $
8 #
9 # Depends on: Python 2.3 (I think)
10 #
11 ############################################################################
12 #
13 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
14 #
15 # Redistribution and use in source and binary forms, with or without
16 # modification, are permitted provided that the following conditions
17 # are met:
18 # 1. Redistributions of source code must retain the above copyright
19 # notice, this list of conditions and the following disclaimer.
20 # 2. Redistributions in binary form must reproduce the above copyright
21 # notice, this list of conditions and the following disclaimer in the
22 # documentation and/or other materials provided with the distribution.
23 #
24 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
25 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
28 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 # SUCH DAMAGE.
35 #
36 ############################################################################
37
2 38
3 import pexpect, re, threading, time, logging 39 import pexpect, re, threading, time, logging
4 from logging.handlers import RotatingFileHandler 40 from logging.handlers import RotatingFileHandler
5 41
6 class ROMReadError(Exception): 42 class ROMReadError(Exception):
7 pass 43 pass
8 44
9 class Control(): 45 class Control():
10 targetTemp = 18 46 targetTemp = 18
11 hysteresis = 4 47 hysteresis = 1
12 pollInterval = 30 48 pollInterval = 30
13 49
14 def __init__(self, m): 50 def __init__(self, m, _log):
15 self.m = m 51 self.m = m
16 self.initLog()
17
18 def initLog(self):
19 # Init our logging
20 global log 52 global log
21 log = logging.getLogger("monitor") 53 log = _log
22 54
23 # Default to warts and all logging
24 log.setLevel(logging.DEBUG)
25
26 # Log to this file
27 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/monitor.log",
28 maxBytes = 10000, backupCount = 3)
29
30 # And stderr
31 logstderr = logging.StreamHandler()
32
33 # Format it nicely
34 formatter = logging.Formatter(fmt = "%(asctime)s: %(message)s", datefmt = "%Y%m%d %H:%M:%S")
35
36 # Glue it all together
37 logfile.setFormatter(formatter)
38 logstderr.setFormatter(formatter)
39 log.addHandler(logfile)
40 log.addHandler(logstderr)
41 return(log)
42
43 def doit(self): 55 def doit(self):
44 log.debug("=== Inited ===")
45 log.debug("target temperature - %3.2f" % (self.targetTemp)) 56 log.debug("target temperature - %3.2f" % (self.targetTemp))
46 log.debug("fermenterId - %s" % (self.m.fermenterId)) 57 log.debug("fermenterId - %s" % (self.m.fermenterId))
47 log.debug("fridgeId - %s" % (self.m.fridgeId)) 58 log.debug("fridgeId - %s" % (self.m.fridgeId))
48 log.debug("ambientId - %s" % (self.m.ambientId)) 59 log.debug("ambientId - %s" % (self.m.ambientId))
49 log.debug("minCoolOnTime - %d, minCoolOffTime - %d" % (self.m.minCoolOnTime, self.m.minCoolOffTime)) 60 log.debug("minCoolOnTime - %d, minCoolOffTime - %d" % (self.m.minCoolOnTime, self.m.minCoolOffTime))
263 274
264 def run(self): 275 def run(self):
265 while True: 276 while True:
266 self.updateTemps() 277 self.updateTemps()
267 278
279 def initLog():
280 # Init our logging
281 global log
282 log = logging.getLogger("monitor")
283
284 # Default to warts and all logging
285 log.setLevel(logging.DEBUG)
286
287 # Log to this file
288 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/beermon.log",
289 maxBytes = 1000000, backupCount = 3)
290
291 # And stderr
292 logstderr = logging.StreamHandler()
293
294 # Format it nicely
295 formatter = logging.Formatter(fmt = "%(asctime)s: %(message)s", datefmt = "%Y/%m/%d %H:%M:%S")
296
297 # Glue it all together
298 logfile.setFormatter(formatter)
299 logstderr.setFormatter(formatter)
300 log.addHandler(logfile)
301 log.addHandler(logstderr)
302 return(log)
303
268 def main(): 304 def main():
269 import time, monitor 305 import sys, traceback, beermon
270 306
271 m = monitor.MonitorDev() 307 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()
272 314
273 try: 315 try:
274 c = monitor.Control(m) 316 c = beermon.Control(m, log)
275 # Wait for the first temperature readings to come through, saves 317 # Wait for the first temperature readings to come through, saves
276 # getting an 'invalid data' message 318 # getting an 'invalid data' message
277 time.sleep(3) 319 time.sleep(3)
278 c.doit() 320 c.doit()
279 log.debug("doit exited") 321 log.debug("doit exited")
280 322
281 except KeyboardInterrupt: 323 except KeyboardInterrupt:
282 log.debug("Exiting due to keyboard interrupt") 324 log.debug("Exiting due to keyboard interrupt")
325
326 #except Exception, e:
327 # log.debug("Something went wrong, details below")
328 # log.debug(e)
329 # #log.debug(reduce(lambda x, y: x + y, traceback.format_exception(
330 # # sys.last_type, sys.last_value, sys.last_traceback)))
331 # exitCode = 1
283 332
284 finally: 333 finally:
285 # Make sure we try and turn it off if something goes wrong 334 # Make sure we try and turn it off if something goes wrong
286 m.setState('idle') 335 m.setState('idle')
287 336
288 sys.exit(0) 337 sys.exit(exitCode)
289 338
290 if __name__ == "__main__": 339 if __name__ == "__main__":
291 main() 340 main()