changeset 1:c0b01c8c63eb

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 1c6f5a0281c7
children 17bc5535bbb7
files beermon.py
diffstat 1 files changed, 82 insertions(+), 33 deletions(-) [+]
line wrap: on
line diff
--- a/beermon.py	Sun Sep 23 03:17:47 2007 +0000
+++ b/beermon.py	Mon Sep 24 04:05:52 2007 +0000
@@ -1,5 +1,41 @@
 #!/usr/bin/env python
 
+############################################################################
+# Monitor & control fermenter temperature 
+# v1.0
+#
+# $Id: beermon.py,v 1.2 2007/09/24 04:05:52 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 pexpect, re, threading, time, logging
 from logging.handlers import RotatingFileHandler
 
@@ -8,40 +44,15 @@
 
 class Control():
     targetTemp = 18
-    hysteresis = 4
+    hysteresis = 1
     pollInterval = 30
     
-    def __init__(self, m):
+    def __init__(self, m, _log):
         self.m = m
-        self.initLog()
-        
-    def initLog(self):
-        # Init our logging
         global log
-        log = logging.getLogger("monitor")
-
-        # Default to warts and all logging
-        log.setLevel(logging.DEBUG)
-
-        # Log to this file
-        logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/monitor.log",
-                                                       maxBytes = 10000, 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)
-
+        log = _log
+        
     def doit(self):
-        log.debug("=== Inited ===")
         log.debug("target temperature - %3.2f" % (self.targetTemp))
         log.debug("fermenterId - %s" % (self.m.fermenterId))
         log.debug("fridgeId - %s" % (self.m.fridgeId))
@@ -265,13 +276,44 @@
         while True:
             self.updateTemps()
 
+def initLog():
+    # Init our logging
+    global log
+    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():
-    import time, monitor
+    import sys, traceback, beermon
 
-    m = monitor.MonitorDev()
+    exitCode = 0
+    
+    initLog()
+
+    log.debug("=== Initing ===")
+    log.debug("$Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $")
+    m = beermon.MonitorDev()
 
     try:
-        c = monitor.Control(m)
+        c = beermon.Control(m, log)
         # Wait for the first temperature readings to come through, saves
         # getting an 'invalid data' message
         time.sleep(3)
@@ -280,12 +322,19 @@
 
     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.last_type, sys.last_value, sys.last_traceback)))
+    #    exitCode = 1
         
     finally:
         # Make sure we try and turn it off if something goes wrong
         m.setState('idle')
 
-    sys.exit(0)
+    sys.exit(exitCode)
     
 if __name__ == "__main__":  
     main()