comparison MonitorDev.py @ 12:9d5b291cfd01

Add/correct docstrings & comments.
author darius
date Sat, 29 Sep 2007 14:51:20 +0000
parents 483375ca5d10
children f1832dec26e3
comparison
equal deleted inserted replaced
11:4b3d6022e6ea 12:9d5b291cfd01
1 #!/usr/bin/env python 1 #!/usr/bin/env python
2 2
3 ############################################################################ 3 ############################################################################
4 # Monitoring/control interface to hardware for beermon 4 # Monitoring/control interface to hardware for beermon
5 # 5 #
6 # $Id: MonitorDev.py,v 1.1 2007/09/29 14:39:59 darius Exp $ 6 # $Id: MonitorDev.py,v 1.2 2007/09/29 14:51:20 darius Exp $
7 # 7 #
8 # Depends on: Python 2.3 (I think) 8 # Depends on: Python 2.3 (I think)
9 # 9 #
10 ############################################################################ 10 ############################################################################
11 # 11 #
64 lastHeatOff = 0 64 lastHeatOff = 0
65 lastCoolOn = 0 65 lastCoolOn = 0
66 lastCoolOff = 0 66 lastCoolOff = 0
67 67
68 def __init__(self, _log, conf): 68 def __init__(self, _log, conf):
69 """_log is a logging object, conf is a ConfigParser object"""
69 global log 70 global log
70 log = _log 71 log = _log
71 threading.Thread.__init__(self) 72 threading.Thread.__init__(self)
72 73
73 # Collect parameters 74 # Collect parameters
104 log.debug("minHeatOvershoot - %3.2f, minCoolOvershoot - %3.2f" % 105 log.debug("minHeatOvershoot - %3.2f, minCoolOvershoot - %3.2f" %
105 (self.minHeatOvershoot, self.minCoolOvershoot)) 106 (self.minHeatOvershoot, self.minCoolOvershoot))
106 self.start() 107 self.start()
107 108
108 def setspeed(self): 109 def setspeed(self):
110 """Set the speed microcom talks to the serial port to 38400"""
109 self.commsLock.acquire() 111 self.commsLock.acquire()
110 self.p.send('~') 112 self.p.send('~')
111 assert(self.p.expect('t - set terminal') == 0) 113 assert(self.p.expect('t - set terminal') == 0)
112 self.p.send('t') 114 self.p.send('t')
113 assert(self.p.expect('p - set speed') == 0) 115 assert(self.p.expect('p - set speed') == 0)
116 self.p.send('f') 118 self.p.send('f')
117 assert(self.p.expect('done!') == 0) 119 assert(self.p.expect('done!') == 0)
118 self.commsLock.release() 120 self.commsLock.release()
119 121
120 def find1wire(self): 122 def find1wire(self):
123 """Scan the bus for 1-wire devices"""
121 self.commsLock.acquire() 124 self.commsLock.acquire()
122 self.p.sendline('') 125 self.p.sendline('')
123 assert(self.p.expect('> ') == 0) 126 assert(self.p.expect('> ') == 0)
124 self.p.sendline('sr') 127 self.p.sendline('sr')
125 # Echo 128 # Echo
150 self.commsLock.release() 153 self.commsLock.release()
151 154
152 return(devlist) 155 return(devlist)
153 156
154 def istemp(self, id): 157 def istemp(self, id):
158 """Returns true if the 1-wire device is a temperature sensor"""
155 [family, a, b, c, d, e, f, g] = id.split(':') 159 [family, a, b, c, d, e, f, g] = id.split(':')
156 if (family == '10'): 160 if (family == '10'):
157 return True 161 return True
158 else: 162 else:
159 return False 163 return False
160 164
161 def updateTemps(self): 165 def updateTemps(self):
166 """Update our cached copy of temperatures"""
162 for i in self.tempdevs: 167 for i in self.tempdevs:
163 try: 168 try:
164 self.temps[i] = float(self.readTemp(i)) 169 self.temps[i] = float(self.readTemp(i))
165 self.lastUpdate[i] = time.time() 170 self.lastUpdate[i] = time.time()
166 except OWReadError: 171 except OWReadError:
168 pass 173 pass
169 174
170 return(self.temps) 175 return(self.temps)
171 176
172 def readTemp(self, id): 177 def readTemp(self, id):
178 """Read the temperature of a sensor"""
173 self.commsLock.acquire() 179 self.commsLock.acquire()
174 cmd = 'te ' + id 180 cmd = 'te ' + id
175 self.p.sendline(cmd) 181 self.p.sendline(cmd)
176 # Echo 182 # Echo
177 assert(self.p.expect(cmd) == 0) 183 assert(self.p.expect(cmd) == 0)
185 raise OWReadError 191 raise OWReadError
186 192
187 return(line) 193 return(line)
188 194
189 def setState(self, state): 195 def setState(self, state):
196 """Set the heat/cool state, track the on/off time"""
190 if (state == 'cool'): 197 if (state == 'cool'):
191 relay = 1 << self.coolRelay 198 relay = 1 << self.coolRelay
192 elif (state == 'heat'): 199 elif (state == 'heat'):
193 relay = 1 << self.heatRelay 200 relay = 1 << self.heatRelay
194 elif (state == 'idle'): 201 elif (state == 'idle'):
222 self.p.sendline(cmd) 229 self.p.sendline(cmd)
223 # Echo 230 # Echo
224 assert(self.p.expect(cmd) == 0) 231 assert(self.p.expect(cmd) == 0)
225 self.commsLock.release() 232 self.commsLock.release()
226 233
227 def polltemps(self, temps):
228 while True:
229 for d in temps:
230 #print d
231 t = gettemp(p, d)
232 print "%s -> %s" % (d, t)
233 print
234
235 def run(self): 234 def run(self):
235 """Sit in a loop polling temperatures"""
236 while True: 236 while True:
237 self.updateTemps() 237 self.updateTemps()
238 238