annotate beermon.py @ 7:860936fab75f

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