annotate beermon.py @ 1:af88c0f96295 BEERMON_0_1

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