-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidomctrl.py
executable file
·63 lines (47 loc) · 1.83 KB
/
pidomctrl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import gpio
import lightstate
class PidomCtrl:
def __init__(self, switches, persistent_state, redis_host, redis_port, redis_db, redis_password):
self.switches = switches
self.persistent_state = persistent_state
self.light_state = lightstate.LightState(
redis_host=redis_host, redis_port=redis_port,
redis_db=redis_db, redis_pass=redis_password)
self.output = gpio.Output()
def __getPersistedLocation(self, switch_id):
pos = 0
while pos < len(self.persistent_state):
if self.persistent_state[pos] == switch_id:
return pos
pos = pos + 1
if pos == len(self.persistent_state):
return -1 # not found
return pos
def pulse(self, switch_id, duration = None, set_state = True):
if duration:
self.output.pulse(self.switches[switch_id], duration)
else:
self.output.pulse(self.switches[switch_id])
# if switch state persisted, adjust state
if set_state and switch_id in self.persistent_state:
position = self.__getPersistedLocation(switch_id)
self.light_state.toggle(position)
def enforce0(self):
'''Set real life to state 0..0'''
self.pulse('persistedoff')
def enforce1(self):
'''Set real life to state 1..1'''
self.pulse('persistedon')
def getState(self):
return self.light_state.getState()
def setState(self, state):
self.light_state.setState(state)
# sync real life to 0..0
self.enforce0()
# Put on lights according to state
pos = 0
while pos < len(self.persistent_state):
if state[pos]:
self.pulse(self.persistent_state[pos], set_state=False)
pos = pos +1
return state