Skip to content

Commit

Permalink
sdp: add version, cyclic error detection/reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Morg42 committed Mar 2, 2024
1 parent 0c1f65e commit ab4c8d5
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion lib/model/smartdeviceplugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ class SmartDevicePlugin(SmartPlugin):
described if changed/overwritten.
"""

# this is the internal SDP version
SDP_VERSION = '1.0.0'

# this is the placeholder version of the derived plugin, not of SDP
PLUGIN_VERSION = '0.0.1'

Expand Down Expand Up @@ -166,6 +169,8 @@ def __init__(self, sh, logger=None, **kwargs):
self._unknown_command = '.notify.'
self._initial_value_read_done = False
self._cyclic_update_active = False
self._cyclic_errors = 0
self._reconnect_on_cycle_error = True
# plugin-wide cycle interval, -1 is undefined
self._cycle = self.get_parameter_value(PLUGIN_ATTR_CYCLE)
if self._cycle is None:
Expand Down Expand Up @@ -1054,6 +1059,7 @@ def _create_cyclic_scheduler(self):
if self.scheduler_get(self.get_shortname() + '_cyclic'):
self.scheduler_remove(self.get_shortname() + '_cyclic')
self.scheduler_add(self.get_shortname() + '_cyclic', self._read_cyclic_values, cycle=workercycle, prio=5, offset=0)
self._cyclic_errors = 0
self.logger.info(f'Added cyclic worker thread {self.get_shortname()}_cyclic with {workercycle} s cycle. Shortest item update cycle found was {shortestcycle} s')

def _read_initial_values(self):
Expand Down Expand Up @@ -1084,10 +1090,20 @@ def _read_cyclic_values(self):
"""
# check if another cyclic cmd run is still active
if self._cyclic_update_active:
self.logger.warning('Triggered cyclic command read, but previous cyclic run is still active. Check device and cyclic configuration (too much/too short?)')
self._cyclic_errors += 1
if self._cyclic_errors >= 3 and self._reconnect_on_cycle_error:
self.logger.warning(f'Cyclic command read failed {self._cyclic_errors} times due to long previous cycle. Reconnecting... ')
self.disconnect()
# reconnect
if not self._parameters.get(PLUGIN_ATTR_CONN_AUTO_RECONN, False):
time.sleep(1)
self.connect()
else:
self.logger.warning('Triggered cyclic command read, but previous cyclic run is still active. Check device and cyclic configuration (too much/too short?)')
return
else:
self.logger.info('Triggering cyclic command read')
self._cyclic_errors = 0

# set lock
self._cyclic_update_active = True
Expand Down

0 comments on commit ab4c8d5

Please sign in to comment.