Skip to content

Commit

Permalink
Persistant states
Browse files Browse the repository at this point in the history
States and last values now saved across restarts.
  • Loading branch information
FlyingDiver committed Jan 26, 2020
1 parent 4d8add0 commit f80a6ce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 23 deletions.
2 changes: 1 addition & 1 deletion HTTPd2.indigoPlugin/Contents/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>PluginVersion</key>
<string>2.0.0</string>
<string>2.0.1</string>
<key>ServerApiVersion</key>
<string>2.0</string>
<key>IwsApiVersion</key>
Expand Down
3 changes: 2 additions & 1 deletion HTTPd2.indigoPlugin/Contents/Server Plugin/Devices.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<Device type="custom" id="serverDevice">
<Name>HTTP/HTTPS Server</Name>
<ConfigUI>
<Field id="protocol" type="menu" defaultValue="http">
<Field id="saved_states" type="textfield" hidden="true" defaultValue="{}"/>
<Field id="protocol" type="menu" defaultValue="http">
<Label>Protocol:</Label>
<List>
<Option value="http">HTTP</Option>
Expand Down
36 changes: 15 additions & 21 deletions HTTPd2.indigoPlugin/Contents/Server Plugin/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,21 @@ def do_setvar(self, request):
device = indigo.devices[self.server.devID]
self.logger.debug(u"MyRequestHandler: updating device {} (port {})".format(device.name, device.address))

old_states = device.pluginProps.get("states_list", indigo.List())
new_states = indigo.List()
saved_states = json.loads(device.pluginProps.get("saved_states", "{}"))
self.logger.debug(u"MyRequestHandler: saved_states = {} ({})".format(saved_states, type(saved_states)))

new_states = {}
state_list = []
for key in query:
value = query[key][0]
self.logger.debug(u"MyRequestHandler: setting state {} to '{}'".format(key, value))
new_states.append(key)
state_list.append({'key': unicode(key), 'value': value})
new_states[key] = value
self.logger.debug(u"MyRequestHandler: new_states = {} ({})".format(new_states, type(new_states)))

if old_states != new_states:
self.logger.threaddebug(u"{}: update, new_states: {}".format(device.name, new_states))
if saved_states != new_states:
newProps = device.pluginProps
newProps["states_list"] = new_states
newProps["saved_states"] = json.dumps(new_states)
device.replacePluginPropsOnServer(newProps)
device.stateListOrDisplayStateIdChanged()

Expand Down Expand Up @@ -373,11 +375,11 @@ def deviceStartComm(self, dev):
server.set_dev_id(dev.id)
self.servers[dev.id] = server

states_list = dev.pluginProps.get("states_list", indigo.List())
stateList = []
for key in states_list:
stateList.append({'key': key, 'value': ''})
dev.updateStatesOnServer(stateList)
saved_states = json.loads(dev.pluginProps.get("saved_states", "{}"))
state_list = []
for key in saved_states:
state_list.append({'key': key, 'value': saved_states[key]})
dev.updateStatesOnServer(state_list)

elif dev.deviceTypeId == 'proxyDevice':

Expand Down Expand Up @@ -428,22 +430,14 @@ def didDeviceCommPropertyChange(self, oldDevice, newDevice):
#
########################################

def getStateList(self, filter, valuesDict, typeId, deviceId):
self.logger.threaddebug(u"{}: getStateList, valuesDict = {}".format(device.name, valuesDict))
returnList = list()
if 'states_list' in valuesDict:
for topic in valuesDict['states_list']:
returnList.append(topic)
return returnList

def getDeviceStateList(self, device):
state_list = indigo.PluginBase.getDeviceStateList(self, device)
self.logger.threaddebug(u"{}: getDeviceStateList, base state_list = {}".format(device.name, state_list))
if device.deviceTypeId != "serverDevice":
return state_list

add_states = device.pluginProps.get("states_list", indigo.List())
for key in add_states:
saved_states = json.loads(device.pluginProps.get("saved_states", "{}"))
for key in saved_states:
dynamic_state = self.getDeviceStateDictForStringType(unicode(key), unicode(key), unicode(key))
state_list.append(dynamic_state)

Expand Down

2 comments on commit f80a6ce

@JaceJenkins
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have it installed. Thanks so much. IF we do need to purge old states is there a way to do so?

@FlyingDiver
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not without recreating the device. Create an issue and I'll look into it.

Please sign in to comment.