adding untracked files
This commit is contained in:
39
controller/state/changer.py
Normal file
39
controller/state/changer.py
Normal file
@@ -0,0 +1,39 @@
|
||||
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(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()
|
||||
if self.local_state['out_valve'] != self.remote_state['out_valve']:
|
||||
change = self.states.get(self.remote_state['out_valve'], None )
|
||||
if change is not None:
|
||||
change()
|
||||
return self.local_state
|
||||
|
||||
def open_valve():
|
||||
GPIO.output(GPIO_PIN_VALVE, GPIO.HIGH)
|
||||
self.local_state['out_valve'] = 'open'
|
||||
|
||||
def close_valve():
|
||||
GPIO.output(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
|
||||
Reference in New Issue
Block a user