Skip to content

Commit e68d8f0

Browse files
committed
Restore last digital actuator state on reboot.
1 parent d4639ae commit e68d8f0

File tree

4 files changed

+41
-19
lines changed

4 files changed

+41
-19
lines changed

myDevices/devices/digital/gpio.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
class NativeGPIO(Singleton, GPIOPort):
3838
IN = 0
3939
OUT = 1
40+
OUT_LOW = 2
41+
OUT_HIGH = 3
4042

4143
ASUS_GPIO = 44
4244

@@ -295,11 +297,9 @@ def __setFunction__(self, channel, value):
295297
self.checkDigitalChannelExported(channel)
296298
self.checkPostingFunctionAllowed()
297299
try:
298-
if value == self.IN:
299-
value = 'in'
300-
else:
301-
value = 'out'
302-
try:
300+
value_dict = {self.IN: 'in', self.OUT: 'out', self.OUT_LOW: 'low', self.OUT_HIGH: 'high'}
301+
value = value_dict[value]
302+
try:
303303
self.functionFile[channel].write(value)
304304
self.functionFile[channel].seek(0)
305305
except:

myDevices/devices/digital/helper.py

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,17 @@ def __str__(self):
6363
return "MotionSensor"
6464

6565
class DigitalActuator(DigitalSensor):
66-
def __init__(self, gpio, channel, invert=False):
66+
def __init__(self, gpio, channel, invert=False, last_state=None):
6767
DigitalSensor.__init__(self, gpio, channel, invert)
68-
self.gpio.setFunction(self.channel, GPIO.OUT)
68+
function = GPIO.OUT
69+
if gpio == 'GPIO' and last_state is not None:
70+
if self.invert:
71+
last_state = int(not last_state)
72+
if last_state == 1:
73+
function = GPIO.OUT_HIGH
74+
elif last_state == 0:
75+
function = GPIO.OUT_LOW
76+
self.gpio.setFunction(self.channel, function)
6977

7078
def __str__(self):
7179
return "DigitalActuator"
@@ -82,30 +90,29 @@ def write(self, value):
8290
return self.read()
8391

8492
class LightSwitch(DigitalActuator):
85-
def __init__(self, gpio, channel, invert=False):
86-
DigitalActuator.__init__(self, gpio, channel, invert)
93+
def __init__(self, gpio, channel, invert=False, last_state=None):
94+
DigitalActuator.__init__(self, gpio, channel, invert, last_state)
8795

8896
def __str__(self):
8997
return "LightSwitch"
9098

9199
class MotorSwitch(DigitalActuator):
92-
def __init__(self, gpio, channel, invert=False):
93-
DigitalActuator.__init__(self, gpio, channel, invert)
100+
def __init__(self, gpio, channel, invert=False, last_state=None):
101+
DigitalActuator.__init__(self, gpio, channel, invert, last_state)
94102

95103
def __str__(self):
96104
return "MotorSwitch"
97105

98106
class RelaySwitch(DigitalActuator):
99-
def __init__(self, gpio, channel, invert=False):
100-
DigitalActuator.__init__(self, gpio, channel, invert)
107+
def __init__(self, gpio, channel, invert=False, last_state=None):
108+
DigitalActuator.__init__(self, gpio, channel, invert, last_state)
101109

102110
def __str__(self):
103111
return "RelaySwitch"
104112

105113
class ValveSwitch(DigitalActuator):
106-
def __init__(self, gpio, channel, invert=False):
107-
DigitalActuator.__init__(self, gpio, channel, invert)
114+
def __init__(self, gpio, channel, invert=False, last_state=None):
115+
DigitalActuator.__init__(self, gpio, channel, invert, last_state)
108116

109117
def __str__(self):
110118
return "ValveSwitch"
111-

myDevices/devices/manager.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def findDeviceClass(name):
4949
return getattr(module, name)
5050
return None
5151

52-
def saveDevice(name, install_date):
52+
def saveDevice(name, install_date=None):
5353
with mutex:
5454
logger.debug('saveDevice: ' + str(name))
5555
if name not in DEVICES:
@@ -58,7 +58,8 @@ def saveDevice(name, install_date):
5858
if DEVICES[name]['origin'] == 'manual':
5959
return
6060
DYNAMIC_DEVICES[name] = DEVICES[name]
61-
DEVICES[name]['install_date'] = install_date
61+
if install_date:
62+
DEVICES[name]['install_date'] = install_date
6263
json_devices = getJSON(DYNAMIC_DEVICES)
6364
with open(DEVICES_JSON_FILE, 'w') as outfile:
6465
outfile.write(json_devices)
@@ -129,6 +130,17 @@ def updateDevice(name, json):
129130

130131
return (c, d, t)
131132

133+
def updateDeviceState(name, value):
134+
with mutex:
135+
try:
136+
if not name in DEVICES:
137+
return
138+
device = DEVICES[name]
139+
device['args'].update({'last_state': value})
140+
saveDevice(name)
141+
except:
142+
pass
143+
132144
def addDevice(name, device, description, args, origin):
133145
with mutex:
134146
if name in DEVICES:

myDevices/sensors/sensors.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,13 +368,16 @@ def SensorCommand(self, command, sensorId, channel, value):
368368
info('Sensor not found')
369369
return result
370370
if command in commands:
371-
info('Sensor found: {}'.format(instance.DEVICES[sensorId]))
371+
device = instance.DEVICES[sensorId]
372+
info('Sensor found: {}'.format(device))
372373
func = getattr(sensor, commands[command]['function'])
373374
value = commands[command]['value_type'](value)
374375
if channel:
375376
result = self.CallDeviceFunction(func, int(channel), value)
376377
else:
377378
result = self.CallDeviceFunction(func, value)
379+
if 'DigitalActuator' in device['type']:
380+
manager.updateDeviceState(sensorId, value)
378381
return result
379382
warn('Command not implemented: {}'.format(command))
380383
return result

0 commit comments

Comments
 (0)