comparison py/fridge.py @ 228:d9e81a563923

porting to asyncio
author Matt Johnston <matt@ucc.asn.au>
date Fri, 20 Mar 2015 20:12:25 +0800
parents 720809f6c968
children a01b7bccccd3
comparison
equal deleted inserted replaced
227:efb5cad2e98b 228:d9e81a563923
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)
36 25
37 def off(self): 26 def off(self):
38 self.turn(False) 27 self.turn(False)
39 self.fridge_off_clock = self.server.now() 28 self.fridge_off_clock = self.server.now()
40 29
41 def is_on(self): 30 def is_on(self):
42 self.value_file.seek(0) 31 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 32
51 # greenlet subclassed 33 @asyncio.coroutine
52 def _run(self): 34 def run(self):
53 if self.server.params.disabled: 35 if self.server.params.disabled:
54 L("Fridge is disabled") 36 L("Fridge is disabled")
55 while True: 37 while True:
56 self.do() 38 self.do()
57 self.server.sleep(config.FRIDGE_SLEEP) 39 yield from self.server.sleep(config.FRIDGE_SLEEP)
58 40
59 def do(self): 41 def do(self):
60 """ this is the main fridge control logic """ 42 """ this is the main fridge control logic """
61 wort, fridge = self.server.current_temps() 43 wort, fridge = self.server.current_temps()
62 44