annotate beermon.py @ 0:1c6f5a0281c7

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