annotate beermon.py @ 6:45d9895a5020

Split into seperate files.
author darius
date Sat, 29 Sep 2007 14:39:59 +0000
parents 8d471840b153
children 17449d52d5e5
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
1 #!/usr/bin/env python
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
2
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
3 ############################################################################
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
4 # Monitor & control fermenter temperature
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
5 # v1.0
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
6 #
6
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
7 # $Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
8 #
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
9 # Depends on: Python 2.3 (I think)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
10 #
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
11 ############################################################################
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
12 #
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
13 # Copyright (C) 2007 Daniel O'Connor. All rights reserved.
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
14 #
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
15 # Redistribution and use in source and binary forms, with or without
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
16 # modification, are permitted provided that the following conditions
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
17 # are met:
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
18 # 1. Redistributions of source code must retain the above copyright
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
19 # notice, this list of conditions and the following disclaimer.
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
20 # 2. Redistributions in binary form must reproduce the above copyright
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
21 # notice, this list of conditions and the following disclaimer in the
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
22 # documentation and/or other materials provided with the distribution.
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
23 #
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
24 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
25 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
26 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
27 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
28 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
29 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
30 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
31 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
32 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
33 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
34 # SUCH DAMAGE.
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
35 #
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
36 ############################################################################
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
37
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
38
6
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
39 import time, logging, sys, traceback, ConfigParser, MonitorDev, Control
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
40 from logging.handlers import RotatingFileHandler
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
41
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
42 def initLog():
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
43 # Init our logging
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
44 log = logging.getLogger("monitor")
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
45
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
46 # Default to warts and all logging
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
47 log.setLevel(logging.DEBUG)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
48
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
49 # Log to this file
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
50 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/beermon.log",
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
51 maxBytes = 1000000, backupCount = 3)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
52
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
53 # And stderr
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
54 logstderr = logging.StreamHandler()
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
55
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
56 # Format it nicely
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
57 formatter = logging.Formatter(fmt = "%(asctime)s: %(message)s", datefmt = "%Y/%m/%d %H:%M:%S")
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
58
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
59 # Glue it all together
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
60 logfile.setFormatter(formatter)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
61 logstderr.setFormatter(formatter)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
62 log.addHandler(logfile)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
63 log.addHandler(logstderr)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
64 return(log)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
65
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
66 def main():
2
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
67 global log
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
68 log = initLog()
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
69
6
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
70 conf = ConfigParser.ConfigParser()
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
71 conf.read('beermon.ini')
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
72
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
73 for s in ['control', 'hardware']:
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
74 if (not conf.has_section(s)):
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
75 log.debug("Mandatory '%s' section missing from config file, exiting" % (s))
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
76 sys.exit(1)
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
77
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
78 log.debug("=== Initing ===")
6
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
79 log.debug("$Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $")
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
80
6
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
81 try:
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
82 m = MonitorDev.MonitorDev(log, conf)
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
83 c = Control.Control(log, m, conf)
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
84 except ConfigParser.NoOptionError, e:
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
85 log.debug("Mandatory option '%s' missing from section '%s'" % (e.option, e.section))
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
86 sys.exit(1)
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
87 except ValueError, e:
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
88 log.debug("Unable to parse option - " + str(e))
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
89
2
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
90 exitCode = 0
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
91 try:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
92 # Wait for the first temperature readings to come through, saves
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
93 # getting an 'invalid data' message
6
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
94 # XXX: sleep on condvar holding data?
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
95 time.sleep(3)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
96 c.doit()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
97 log.debug("doit exited")
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
98
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
99 except KeyboardInterrupt:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
100 log.debug("Exiting due to keyboard interrupt")
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
101
2
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
102 except Exception, e:
3
1af7c85d5a0e Log backtrace on unexpected exception.
darius
parents: 2
diff changeset
103 log.debug("Something went wrong, details below:")
2
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
104 log.debug(e)
3
1af7c85d5a0e Log backtrace on unexpected exception.
darius
parents: 2
diff changeset
105 log.debug(reduce(lambda x, y: x + y, traceback.format_exception(
1af7c85d5a0e Log backtrace on unexpected exception.
darius
parents: 2
diff changeset
106 sys.exc_type, sys.exc_value, sys.exc_traceback)))
2
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
107 exitCode = 1
6
45d9895a5020 Split into seperate files.
darius
parents: 5
diff changeset
108
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
109 finally:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
110 # Make sure we try and turn it off if something goes wrong
2
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
111 if (m != None):
17bc5535bbb7 Wait on a condition var instead of using time.sleep() - this works
darius
parents: 1
diff changeset
112 m.setState('idle')
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
113
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
114 sys.exit(exitCode)
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
115
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
116 if __name__ == "__main__":
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
117 main()