annotate beermon.py @ 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
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 #
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
7 # $Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $
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
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
39 import pexpect, re, threading, time, logging
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
40 from logging.handlers import RotatingFileHandler
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
41
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
42 class ROMReadError(Exception):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
43 pass
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
44
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
45 class Control():
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
46 targetTemp = 18
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
47 hysteresis = 1
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
48 pollInterval = 30
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
49
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
50 def __init__(self, m, _log):
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
51 self.m = m
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
52 global log
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
53 log = _log
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
54
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
55 def doit(self):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
56 log.debug("target temperature - %3.2f" % (self.targetTemp))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
57 log.debug("fermenterId - %s" % (self.m.fermenterId))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
58 log.debug("fridgeId - %s" % (self.m.fridgeId))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
59 log.debug("ambientId - %s" % (self.m.ambientId))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
60 log.debug("minCoolOnTime - %d, minCoolOffTime - %d" % (self.m.minCoolOnTime, self.m.minCoolOffTime))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
61 log.debug("minHeatOnTime - %d, minHeatOffTime - %d" % (self.m.minHeatOnTime, self.m.minHeatOffTime))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
62 log.debug("pollInterval - %d" % (self.pollInterval))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
63
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
64 log.debug("=== Starting ===")
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
65 log.debug("Fermenter Fridge Ambient State New State")
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
66 while True:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
67 if (self.m.lastUpdate == 0):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
68 print "%s Invalid data" % (time.asctime())
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
69 time.sleep(30)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
70 self.m.setState('idle')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
71 continue
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
72
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
73 nextState = "-"
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
74
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
75 diff = self.m.temps[self.m.fermenterId] - self.targetTemp
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
76 if (self.m.currState == 'idle'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
77 # If we're idle then only heat or cool if the temperate difference is out of the
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
78 # hysteresis range
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
79 if (abs(diff) > self.hysteresis):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
80 if (diff < 0 and self.m.minHeatOffTime + self.m.lastHeatOff < time.time()):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
81 nextState = 'heat'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
82 elif (diff > 0 and self.m.minHeatOffTime + self.m.lastHeatOff < time.time()):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
83 nextState = 'cool'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
84 elif (self.m.currState == 'cool'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
85 # Go idle as soon as we can, there will be overshoot anyway
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
86 if (diff < 0 and self.m.minCoolOnTime + self.m.lastCoolOn < time.time()):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
87 nextState = 'idle'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
88 elif (self.m.currState == 'heat'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
89 if (diff > 0 and self.m.minHeatOnTime + self.m.lastHeatOn < time.time()):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
90 nextState = 'idle'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
91 else:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
92 raise KeyError
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
93
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
94 log.debug("%3.2f %3.2f %3.2f %s %s" %
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
95 (self.m.temps[self.m.fermenterId],
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
96 self.m.temps[self.m.fridgeId], self.m.temps[self.m.ambientId],
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
97 self.m.currState, nextState))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
98 if (nextState != "-"):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
99 self.m.setState(nextState)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
100
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
101 time.sleep(self.pollInterval)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
102
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
103
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
104 class MonitorDev(threading.Thread):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
105 # Match a ROM ID (eg 00:11:22:33:44:55:66:77)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
106 romre = re.compile('([0-9a-f]{2}:){7}[0-9a-f]{2}')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
107 # Match the prompt
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
108 promptre = re.compile('> ')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
109
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
110 coolRelay = 7
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
111 heatRelay = 6
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
112
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
113 fermenterId = '10:eb:48:21:01:08:00:df'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
114 fridgeId = '10:a6:2a:c4:00:08:00:11'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
115 ambientId = '10:97:1b:fe:00:08:00:d1'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
116
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
117 # minimum time the cooler must spend on/off
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
118 minCoolOnTime = 10 * 60
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
119 minCoolOffTime = 10 * 60
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
120
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
121 # minimum time the heater must spend on/off
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
122 minHeatOnTime = 60
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
123 minHeatOffTime = 60
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
124
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
125 temps = {}
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
126 lastUpdate = 0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
127
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
128 # Lock to gate access to the comms
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
129 commsLock = None
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
130
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
131 currState = 'idle'
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
132
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
133 lastHeatOn = 0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
134 lastHeatOff = 0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
135 lastCoolOn = 0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
136 lastCoolOff = 0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
137
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
138 def __init__(self):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
139 threading.Thread.__init__(self)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
140 self.commsLock = threading.Lock()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
141 self.p = pexpect.spawn('/usr/bin/ssh', ['-xt', '-enone', '-i', '/home/darius/.ssh/id_wrt', 'root@wrt', '(echo logged in; microcom -D/dev/cua/1)'])
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
142 assert(self.p.expect('logged in') == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
143 self.p.timeout = 3
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
144 self.setspeed()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
145 self.devs = self.find1wire()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
146 self.temps = filter(self.istemp, self.devs)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
147
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
148 self.start()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
149
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
150 def setspeed(self):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
151 self.commsLock.acquire()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
152 self.p.send('~')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
153 assert(self.p.expect('t - set terminal') == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
154 self.p.send('t')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
155 assert(self.p.expect('p - set speed') == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
156 self.p.send('p')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
157 assert(self.p.expect('f - 38400') == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
158 self.p.send('f')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
159 assert(self.p.expect('done!') == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
160 self.commsLock.release()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
161
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
162 def find1wire(self):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
163 self.commsLock.acquire()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
164 self.p.sendline('')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
165 assert(self.p.expect('> ') == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
166 self.p.sendline('sr')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
167 # Echo
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
168 assert(self.p.expect('sr') == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
169
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
170 # Send a new line which will give us a command prompt to stop on
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
171 # later. We could use read() but that would make the code a lot
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
172 # uglier
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
173 self.p.sendline('')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
174
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
175 devlist = []
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
176
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
177 # Loop until we get the command prompt (> ) collecting ROM IDs
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
178 while True:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
179 idx = self.p.expect([self.romre, self.promptre])
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
180 if (idx == 0):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
181 # Matched a ROM
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
182 #print "Found ROM " + self.p.match.group()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
183 devlist.append(self.p.match.group(0))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
184 elif (idx == 1):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
185 # Matched prompt, exit
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
186 break
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
187 else:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
188 # Unpossible!
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
189 self.commsLock.release()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
190 raise SystemError()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
191
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
192 self.commsLock.release()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
193
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
194 return(devlist)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
195
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
196 def istemp(self, id):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
197 [family, a, b, c, d, e, f, g] = id.split(':')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
198 if (family == '10'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
199 return True
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
200 else:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
201 return False
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
202
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
203 def updateTemps(self):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
204 tmp = {}
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
205 for i in self.temps:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
206 tmp[i] = float(self.readTemp(i))
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
207
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
208 self.temps = tmp
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
209 self.lastUpdate = time.time()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
210 return(self.temps)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
211
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
212 def readTemp(self, id):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
213 self.commsLock.acquire()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
214 cmd = 'te ' + id
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
215 self.p.sendline(cmd)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
216 # Echo
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
217 assert(self.p.expect(cmd) == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
218 # Eat EOL left from expect
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
219 self.p.readline()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
220
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
221 line = self.p.readline().strip()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
222 self.commsLock.release()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
223 # 'CRC mismatch' can mean that we picked the wrong ROM..
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
224 if (re.match('CRC mismatch', line) != None):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
225 raise ROMReadError
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
226
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
227 return(line)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
228
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
229 def setState(self, state):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
230 if (state == 'cool'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
231 relay = 1 << self.coolRelay
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
232 elif (state == 'heat'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
233 relay = 1 << self.heatRelay
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
234 elif (state == 'idle'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
235 relay = 0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
236 else:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
237 raise(ValueError)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
238
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
239 if (state == self.currState):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
240 return
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
241
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
242 # Keep track of when we last turned off or on
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
243 if (state == 'cool'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
244 if (self.currState == 'heat'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
245 self.lastHeatOff = time.time()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
246 self.lastCoolOn = time.time()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
247 elif (state == 'heat'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
248 if (self.currState == 'cool'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
249 self.lastCoolOff = time.time()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
250 self.lastHeatOn = time.time()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
251 else:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
252 if (self.currState == 'cool'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
253 self.lastCoolOff = time.time()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
254 if (self.currState == 'heat'):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
255 self.lastHeatOff = time.time()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
256
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
257 self.currState = state
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
258
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
259 self.commsLock.acquire()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
260 # Need the extra spaces cause the parser in the micro is busted
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
261 cmd = 'out c %02x' % relay
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
262 self.p.sendline(cmd)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
263 # Echo
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
264 assert(self.p.expect(cmd) == 0)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
265 self.commsLock.release()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
266
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
267 def polltemps(self, temps):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
268 while True:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
269 for d in temps:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
270 #print d
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
271 t = gettemp(p, d)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
272 print "%s -> %s" % (d, t)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
273 print
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
274
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
275 def run(self):
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
276 while True:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
277 self.updateTemps()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
278
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
279 def initLog():
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
280 # Init our logging
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
281 global log
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
282 log = logging.getLogger("monitor")
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
283
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
284 # Default to warts and all logging
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
285 log.setLevel(logging.DEBUG)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
286
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
287 # Log to this file
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
288 logfile = logging.handlers.RotatingFileHandler(filename = "/tmp/beermon.log",
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
289 maxBytes = 1000000, backupCount = 3)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
290
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
291 # And stderr
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
292 logstderr = logging.StreamHandler()
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
293
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
294 # Format it nicely
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
295 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
296
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
297 # Glue it all together
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
298 logfile.setFormatter(formatter)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
299 logstderr.setFormatter(formatter)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
300 log.addHandler(logfile)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
301 log.addHandler(logstderr)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
302 return(log)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
303
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
304 def main():
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
305 import sys, traceback, beermon
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
306
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
307 exitCode = 0
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
308
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
309 initLog()
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
310
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
311 log.debug("=== Initing ===")
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
312 log.debug("$Id: beermon.py,v 1.2 2007/09/24 04:05:52 darius Exp $")
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
313 m = beermon.MonitorDev()
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
314
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
315 try:
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
316 c = beermon.Control(m, log)
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
317 # Wait for the first temperature readings to come through, saves
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
318 # getting an 'invalid data' message
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
319 time.sleep(3)
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
320 c.doit()
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
321 log.debug("doit exited")
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
322
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
323 except KeyboardInterrupt:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
324 log.debug("Exiting due to keyboard interrupt")
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
325
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
326 #except Exception, e:
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
327 # log.debug("Something went wrong, details below")
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
328 # log.debug(e)
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
329 # #log.debug(reduce(lambda x, y: x + y, traceback.format_exception(
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
330 # # sys.last_type, sys.last_value, sys.last_traceback)))
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
331 # exitCode = 1
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
332
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
333 finally:
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
334 # Make sure we try and turn it off if something goes wrong
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
335 m.setState('idle')
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
336
1
c0b01c8c63eb Log version at startup, add copyright verbiage, etc..
darius
parents: 0
diff changeset
337 sys.exit(exitCode)
0
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
338
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
339 if __name__ == "__main__":
1c6f5a0281c7 Initial revision
darius
parents:
diff changeset
340 main()