annotate beermon.py @ 4:618372f83862

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