comparison py/fridge.py @ 220:251524081924

make the fridge off timer more robust add a few comments
author Matt Johnston <matt@ucc.asn.au>
date Fri, 19 Dec 2014 22:32:59 +0800
parents 16a83e2c97a0
children 720809f6c968
comparison
equal deleted inserted replaced
219:16a83e2c97a0 220:251524081924
11 gevent.Greenlet.__init__(self) 11 gevent.Greenlet.__init__(self)
12 self.server = server 12 self.server = server
13 self.setup_gpio() 13 self.setup_gpio()
14 self.wort_valid_clock = 0 14 self.wort_valid_clock = 0
15 self.fridge_on_clock = 0 15 self.fridge_on_clock = 0
16 self.fridge_off_clock = server.now() 16 self.off()
17 17
18 def setup_gpio(self): 18 def setup_gpio(self):
19 dir_fn = '%s/direction' % config.FRIDGE_GPIO 19 dir_fn = '%s/direction' % config.FRIDGE_GPIO
20 with open(dir_fn, 'w') as f: 20 with open(dir_fn, 'w') as f:
21 f.write('low') 21 f.write('low')
22 val_fn = '%s/value' % config.FRIDGE_GPIO 22 val_fn = '%s/value' % config.FRIDGE_GPIO
23 # XXX - Fridge should have __enter__/__exit__, close the file there.
23 self.value_file = open(val_fn, 'r+') 24 self.value_file = open(val_fn, 'r+')
24 25
25 def turn(self, value): 26 def turn(self, value):
26 self.value_file.seek(0) 27 self.value_file.seek(0)
27 if value: 28 if value:
33 def on(self): 34 def on(self):
34 self.turn(True) 35 self.turn(True)
35 36
36 def off(self): 37 def off(self):
37 self.turn(False) 38 self.turn(False)
39 self.fridge_off_clock = self.server.now()
38 40
39 def is_on(self): 41 def is_on(self):
40 self.value_file.seek(0) 42 self.value_file.seek(0)
41 buf = self.value_file.read().strip() 43 buf = self.value_file.read().strip()
42 if buf == '0': 44 if buf == '0':
69 off_time = self.server.now() - self.fridge_off_clock 71 off_time = self.server.now() - self.fridge_off_clock
70 72
71 if wort is not None: 73 if wort is not None:
72 self.wort_valid_clock = self.server.now() 74 self.wort_valid_clock = self.server.now()
73 75
74 if off_time < config.FRIDGE_DELAY: 76 # Safety to avoid bad things happening to the fridge motor (?)
77 # When it turns off don't start up again for at least FRIDGE_DELAY
78 if self.is_off() and off_time < config.FRIDGE_DELAY:
75 L("fridge skipping, too early") 79 L("fridge skipping, too early")
76 return 80 return
77 81
78 if params.disabled: 82 if params.disabled:
79 if self.is_on(): 83 if self.is_on():
114 turn_off = True 118 turn_off = True
115 119
116 if turn_off: 120 if turn_off:
117 L("Turning fridge off") 121 L("Turning fridge off")
118 self.off() 122 self.off()
119 self.fridge_off_clock = self.server.now()
120 123
121 else: 124 else:
122 # fridge is off 125 # fridge is off
123 turn_on = False 126 turn_on = False
124 if not params.nowort \ 127 if not params.nowort \