Files
old-tfm/controller/state/changer.py

39 lines
1.2 KiB
Python
Raw Permalink Normal View History

2016-01-24 09:15:14 +01:00
import RPi.GPIO as GPIO
import config
class Changer(object):
def __init__(self, local_state, remote_state):
self.local_state = local_state
self.remote_state = remote_state
GPIO.setmode(GPIO.BCM) # Broadcom pin-numbering scheme
2016-01-24 09:18:14 +01:00
GPIO.setup(config.GPIO_PIN_VALVE, GPIO.OUT)
2016-01-24 09:15:14 +01:00
self.states = {
'opening': self.open_valve,
'closing': self.close_valve,
'open': self.open_valve,
'closed': self.close_valve
}
def process_change(self):
self.validate_states()
2016-01-24 09:37:02 +01:00
change = self.states.get(self.remote_state['out_valve'], None )
if change is not None:
change()
2016-01-24 09:15:14 +01:00
return self.local_state
2016-01-24 09:41:38 +01:00
def open_valve(self):
2016-01-24 09:18:14 +01:00
GPIO.output(config.GPIO_PIN_VALVE, GPIO.HIGH)
2016-01-24 09:15:14 +01:00
self.local_state['out_valve'] = 'open'
2016-01-24 09:41:38 +01:00
def close_valve(self):
2016-01-24 09:18:14 +01:00
GPIO.output(config.GPIO_PIN_VALVE, GPIO.LOW)
2016-01-24 09:15:14 +01:00
self.local_state['out_valve'] = 'closed'
def validate_states(self):
if self.local_state is None or self.remote_state is None:
raise ClassNotReadyException("Both local and remote states must be present!")
# TODO: add detailed validation