Mercurial > ~darius > hgwebdir.cgi > beermon.old
view beermon.py @ 12:9d5b291cfd01
Add/correct docstrings & comments.
author | darius |
---|---|
date | Sat, 29 Sep 2007 14:51:20 +0000 |
parents | 483375ca5d10 |
children | c029d2195d19 |
line wrap: on
line source
#!/usr/bin/env python ############################################################################ # Monitor & control fermenter temperature # v1.0 # # $Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $ # # Depends on: Python 2.3 (I think) # ############################################################################ # # Copyright (C) 2007 Daniel O'Connor. All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions # are met: # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF # SUCH DAMAGE. # ############################################################################ import time, logging, sys, traceback, ConfigParser, MonitorDev, Control from logging.handlers import RotatingFileHandler def initLog(): # Init our logging log = logging.getLogger("monitor") # Default to warts and all logging log.setLevel(logging.DEBUG) # Log to this file logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/beermon.log", maxBytes = 1000000, backupCount = 3) # And stderr logstderr = logging.StreamHandler() # Format it nicely formatter = logging.Formatter(fmt = "%(asctime)s: %(message)s", datefmt = "%Y/%m/%d %H:%M:%S") # Glue it all together logfile.setFormatter(formatter) logstderr.setFormatter(formatter) log.addHandler(logfile) log.addHandler(logstderr) return(log) def main(): global log log = initLog() conf = ConfigParser.ConfigParser() conf.read('beermon.ini') for s in ['control', 'hardware']: if (not conf.has_section(s)): log.debug("Mandatory '%s' section missing from config file, exiting" % (s)) sys.exit(1) log.debug("=== Initing ===") log.debug("$Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $") try: m = MonitorDev.MonitorDev(log, conf) c = Control.Control(log, m, conf) except ConfigParser.NoOptionError, e: log.debug("Mandatory option '%s' missing from section '%s'" % (e.option, e.section)) sys.exit(1) except ValueError, e: log.debug("Unable to parse option - " + str(e)) exitCode = 0 try: # Wait for the first temperature readings to come through, saves # getting an 'invalid data' message # XXX: sleep on condvar holding data? time.sleep(3) c.doit() log.debug("doit exited") except KeyboardInterrupt: log.debug("Exiting due to keyboard interrupt") except Exception, e: log.debug("Something went wrong, details below:") log.debug(e) log.debug(reduce(lambda x, y: x + y, traceback.format_exception( sys.exc_type, sys.exc_value, sys.exc_traceback))) exitCode = 1 finally: # Make sure we try and turn it off if something goes wrong if (m != None): m.setState('idle') sys.exit(exitCode) if __name__ == "__main__": main()