39 lines
1.2 KiB
Python
39 lines
1.2 KiB
Python
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
|
|
GPIO.setup(config.GPIO_PIN_VALVE, GPIO.OUT)
|
|
|
|
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()
|
|
change = self.states.get(self.remote_state['out_valve'], None )
|
|
if change is not None:
|
|
change()
|
|
return self.local_state
|
|
|
|
def open_valve(self):
|
|
GPIO.output(config.GPIO_PIN_VALVE, GPIO.HIGH)
|
|
self.local_state['out_valve'] = 'open'
|
|
|
|
def close_valve(self):
|
|
GPIO.output(config.GPIO_PIN_VALVE, GPIO.LOW)
|
|
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
|