Mercurial > ~darius > hgwebdir.cgi > beermon
annotate beermon.py @ 8:cb03cea09bda
Logfile plotter
author | darius |
---|---|
date | Sat, 29 Sep 2007 14:40:33 +0000 |
parents | 45d9895a5020 |
children | 17449d52d5e5 |
rev | line source |
---|---|
0 | 1 #!/usr/bin/env python |
2 | |
1 | 3 ############################################################################ |
4 # Monitor & control fermenter temperature | |
5 # v1.0 | |
6 # | |
6 | 7 # $Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $ |
1 | 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 | |
38 | |
6 | 39 import time, logging, sys, traceback, ConfigParser, MonitorDev, Control |
0 | 40 from logging.handlers import RotatingFileHandler |
41 | |
1 | 42 def initLog(): |
43 # Init our logging | |
44 log = logging.getLogger("monitor") | |
45 | |
46 # Default to warts and all logging | |
47 log.setLevel(logging.DEBUG) | |
48 | |
49 # Log to this file | |
50 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/beermon.log", | |
51 maxBytes = 1000000, backupCount = 3) | |
52 | |
53 # And stderr | |
54 logstderr = logging.StreamHandler() | |
55 | |
56 # Format it nicely | |
57 formatter = logging.Formatter(fmt = "%(asctime)s: %(message)s", datefmt = "%Y/%m/%d %H:%M:%S") | |
58 | |
59 # Glue it all together | |
60 logfile.setFormatter(formatter) | |
61 logstderr.setFormatter(formatter) | |
62 log.addHandler(logfile) | |
63 log.addHandler(logstderr) | |
64 return(log) | |
65 | |
0 | 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 | 69 |
6 | 70 conf = ConfigParser.ConfigParser() |
71 conf.read('beermon.ini') | |
72 | |
73 for s in ['control', 'hardware']: | |
74 if (not conf.has_section(s)): | |
75 log.debug("Mandatory '%s' section missing from config file, exiting" % (s)) | |
76 sys.exit(1) | |
77 | |
1 | 78 log.debug("=== Initing ===") |
6 | 79 log.debug("$Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $") |
0 | 80 |
6 | 81 try: |
82 m = MonitorDev.MonitorDev(log, conf) | |
83 c = Control.Control(log, m, conf) | |
84 except ConfigParser.NoOptionError, e: | |
85 log.debug("Mandatory option '%s' missing from section '%s'" % (e.option, e.section)) | |
86 sys.exit(1) | |
87 except ValueError, e: | |
88 log.debug("Unable to parse option - " + str(e)) | |
89 | |
2
17bc5535bbb7
Wait on a condition var instead of using time.sleep() - this works
darius
parents:
1
diff
changeset
|
90 exitCode = 0 |
0 | 91 try: |
92 # Wait for the first temperature readings to come through, saves | |
93 # getting an 'invalid data' message | |
6 | 94 # XXX: sleep on condvar holding data? |
0 | 95 time.sleep(3) |
96 c.doit() | |
97 log.debug("doit exited") | |
98 | |
99 except KeyboardInterrupt: | |
100 log.debug("Exiting due to keyboard interrupt") | |
1 | 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 | 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 | 105 log.debug(reduce(lambda x, y: x + y, traceback.format_exception( |
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 | 108 |
0 | 109 finally: |
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 | 113 |
1 | 114 sys.exit(exitCode) |
0 | 115 |
116 if __name__ == "__main__": | |
117 main() |