Mercurial > templog
comparison py/fridge.py @ 293:d15dda1b1f76
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 06 Jul 2019 18:29:45 +0800 |
parents | 26eee8591f61 |
children | 20c89630be6c |
comparison
equal
deleted
inserted
replaced
292:28eb733cb803 | 293:d15dda1b1f76 |
---|---|
1 # -*- coding: utf-8 -*- | 1 # -*- coding: utf-8 -*- |
2 import asyncio | |
3 | |
2 from utils import L,W,E,EX,D | 4 from utils import L,W,E,EX,D |
3 import config | 5 import config |
4 import gevent | |
5 | 6 |
6 class Fridge(gevent.Greenlet): | 7 import gpio |
8 | |
9 class Fridge(object): | |
7 | 10 |
8 OVERSHOOT_MAX_DIV = 1800.0 # 30 mins | 11 OVERSHOOT_MAX_DIV = 1800.0 # 30 mins |
9 | 12 |
10 def __init__(self, server): | 13 def __init__(self, server): |
11 gevent.Greenlet.__init__(self) | |
12 self.server = server | 14 self.server = server |
13 self.setup_gpio() | 15 self.gpio = gpio.Gpio(config.FRIDGE_GPIO_PIN, "fridge") |
14 self.wort_valid_clock = 0 | 16 self.wort_valid_clock = 0 |
15 self.fridge_on_clock = 0 | 17 self.fridge_on_clock = 0 |
16 self.off() | 18 self.off() |
17 | 19 |
18 def setup_gpio(self): | |
19 dir_fn = '%s/direction' % config.FRIDGE_GPIO | |
20 with open(dir_fn, 'w') as f: | |
21 f.write('low') | |
22 val_fn = '%s/value' % config.FRIDGE_GPIO | |
23 # XXX - Fridge should have __enter__/__exit__, close the file there. | |
24 self.value_file = open(val_fn, 'r+') | |
25 | |
26 def turn(self, value): | 20 def turn(self, value): |
27 self.value_file.seek(0) | 21 self.gpio.turn(value) |
28 if value: | |
29 self.value_file.write('1') | |
30 else: | |
31 self.value_file.write('0') | |
32 self.value_file.flush() | |
33 | 22 |
34 def on(self): | 23 def on(self): |
35 self.turn(True) | 24 self.turn(True) |
25 pass | |
36 | 26 |
37 def off(self): | 27 def off(self): |
38 self.turn(False) | 28 self.turn(False) |
39 self.fridge_off_clock = self.server.now() | 29 self.fridge_off_clock = self.server.now() |
40 | 30 |
41 def is_on(self): | 31 def is_on(self): |
42 self.value_file.seek(0) | 32 return self.gpio.get_state() |
43 buf = self.value_file.read().strip() | |
44 if buf == '0': | |
45 return False | |
46 if buf != '1': | |
47 E("Bad value read from gpio '%s': '%s'" | |
48 % (self.value_file.name, buf)) | |
49 return True | |
50 | 33 |
51 # greenlet subclassed | 34 @asyncio.coroutine |
52 def _run(self): | 35 def run(self): |
53 if self.server.params.disabled: | 36 if self.server.params.disabled: |
54 L("Fridge is disabled") | 37 L("Fridge is disabled") |
55 while True: | 38 while True: |
56 self.do() | 39 try: |
57 self.server.sleep(config.FRIDGE_SLEEP) | 40 self.do() |
41 yield from self.server.sleep(config.FRIDGE_SLEEP) | |
42 except Exception as e: | |
43 EX("fridge failed") | |
58 | 44 |
59 def do(self): | 45 def do(self): |
60 """ this is the main fridge control logic """ | 46 """ this is the main fridge control logic """ |
61 wort, fridge = self.server.current_temps() | 47 wort, fridge = self.server.current_temps() |
62 | 48 |
94 return | 80 return |
95 | 81 |
96 if fridge is None: | 82 if fridge is None: |
97 W("Invalid fridge sensor") | 83 W("Invalid fridge sensor") |
98 | 84 |
85 D("fridge on %s" % self.is_on()) | |
86 | |
99 if self.is_on(): | 87 if self.is_on(): |
100 turn_off = False | 88 turn_off = False |
101 on_time = self.server.now() - self.fridge_on_clock | 89 on_time = self.server.now() - self.fridge_on_clock |
102 | 90 |
103 overshoot = 0 | 91 overshoot = 0 |
122 self.off() | 110 self.off() |
123 | 111 |
124 else: | 112 else: |
125 # fridge is off | 113 # fridge is off |
126 turn_on = False | 114 turn_on = False |
115 D("fridge %(fridge)s max %(fridge_max)s wort %(wort)s wort_max %(wort_max)s" % locals()) | |
127 if not params.nowort \ | 116 if not params.nowort \ |
128 and wort is not None \ | 117 and wort is not None \ |
129 and wort >= wort_max: | 118 and wort >= wort_max: |
130 L("Wort is too hot %f, max %f" % (wort, wort_max)) | 119 L("Wort is too hot %f, max %f" % (wort, wort_max)) |
131 turn_on = True | 120 turn_on = True |