comparison py/fridge.py @ 141:4755e6f9a5b8

python raspberry pi rewrite
author Matt Johnston <matt@ucc.asn.au>
date Mon, 19 Nov 2012 22:46:34 +0800
parents
children 0895f5ad7731
comparison
equal deleted inserted replaced
139:94330d90f11f 141:4755e6f9a5b8
1 from utils import L,W,E
2 import config
3
4 class Fridge(object):
5 def __init__(self):
6 self.setup_gpio()
7 self.wort_valid_clock = 0
8
9 def setup_gpio(self):
10 fn = '%s/direction' % config.FRIDGE_GPIO
11 f = open(fn, 'w')
12 f.write('low')
13 f.close()
14 # .off() shouldn't do anything, but tests that "value" is writable
15 self.off()
16
17 def turn(self, value):
18 fn = '%s/value' % config.FRIDGE_GPIO
19 f = open(fn, 'w')
20 if value:
21 f.write('1')
22 else:
23 f.write('0')
24 f.close()
25
26 def on(self):
27 self.turn(1)
28
29 def off(self):
30 self.turn(0)
31
32 def do(self):
33 wort, fridge, ambient = server.current_temps()
34
35 if server.uptime() < config.FRIDGE_DELAY:
36 L("fridge skipping, too early")
37 return
38
39 # handle broken wort sensor
40 if wort is not None:
41 self.wort_valid_clock = server.now()
42 else:
43 W("Invalid wort sensor")
44 invalid_time = server.now() - self.wort_valid_clock
45 if invalid_time < config.FRIDGE_WORT_INVALID_TIME:
46 W("Has only been invalid for %d, waiting" % invalid_time)
47 return
48
49 if fridge is None:
50 W("Invalid fridge sensor")
51
52
53
54
55
56
57
58 def run(self, server):
59 self.server = server
60
61 while True:
62 self.do()
63 gevent.sleep(config.FRIDGE_SLEEP)