annotate beermon.py @ 8:483375ca5d10

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