Mercurial > ~darius > hgwebdir.cgi > beermon.old
annotate beermon.py @ 10:a1e7bb27f6d5
Logfile plotter
author | darius |
---|---|
date | Sat, 29 Sep 2007 14:40:33 +0000 |
parents | 483375ca5d10 |
children | c029d2195d19 |
rev | line source |
---|---|
1 | 1 #!/usr/bin/env python |
2 | |
3 | 3 ############################################################################ |
4 # Monitor & control fermenter temperature | |
5 # v1.0 | |
6 # | |
8 | 7 # $Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $ |
3 | 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 | |
8 | 39 import time, logging, sys, traceback, ConfigParser, MonitorDev, Control |
1 | 40 from logging.handlers import RotatingFileHandler |
41 | |
3 | 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 | |
1 | 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 | 69 |
8 | 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 | |
3 | 78 log.debug("=== Initing ===") |
8 | 79 log.debug("$Id: beermon.py,v 1.7 2007/09/29 14:39:59 darius Exp $") |
1 | 80 |
8 | 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 | |
4
618372f83862
Wait on a condition var instead of using time.sleep() - this works
darius
parents:
3
diff
changeset
|
90 exitCode = 0 |
1 | 91 try: |
92 # Wait for the first temperature readings to come through, saves | |
93 # getting an 'invalid data' message | |
8 | 94 # XXX: sleep on condvar holding data? |
1 | 95 time.sleep(3) |
96 c.doit() | |
97 log.debug("doit exited") | |
98 | |
99 except KeyboardInterrupt: | |
100 log.debug("Exiting due to keyboard interrupt") | |
3 | 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 | 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 | 105 log.debug(reduce(lambda x, y: x + y, traceback.format_exception( |
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 | 108 |
1 | 109 finally: |
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 | 113 |
3 | 114 sys.exit(exitCode) |
1 | 115 |
116 if __name__ == "__main__": | |
117 main() |