Skip to content

Commit

Permalink
smlx plugin: introduce crontab as poll trigger, update webinterface p…
Browse files Browse the repository at this point in the history
…oll source
  • Loading branch information
bmxp committed Jan 28, 2024
1 parent 8949714 commit c8fe266
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 23 deletions.
59 changes: 41 additions & 18 deletions smlx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# vim: set encoding=utf-8 tabstop=4 softtabstop=4 shiftwidth=4 expandtab
#########################################################################
# Copyright 2012-2014 Oliver Hinckel [email protected]
# Copyright 2018-2022 Bernd Meiners [email protected]
# Copyright 2018-2024 Bernd Meiners [email protected]
# Copyright 2022- Michael Wenzel [email protected]
#########################################################################
#
Expand Down Expand Up @@ -55,7 +55,7 @@ class Smlx(SmartPlugin):
the update functions for the items
"""

PLUGIN_VERSION = '1.1.7'
PLUGIN_VERSION = '1.1.8'

# Lookup table for smartmeter names to data format
_devices = {
Expand All @@ -70,7 +70,33 @@ def __init__(self, sh):
# Call init code of parent class (SmartPlugin)
super().__init__()

#load parameters from config
self.load_parameters()

self.connected = False
self.alive = False
self._serial = None
self._sock = None
self._target = None
self._dataoffset = 0
self._items = {}
self._item_dict = {}
self._lock = threading.Lock()
self._parse_lock = threading.Lock()

self.init_webinterface(WebInterface)

def load_parameters(self):
self.cycle = self.get_parameter_value('cycle')
if self.cycle == 0:
self.cycle = None

self.crontab = self.get_parameter_value('crontab') # the more complex way to specify the device query frequency
if self.crontab == '':
self.crontab = None
if not (self.cycle or self.crontab):
self.logger.error(f"{self.get_fullname()}: no update cycle or crontab set. The smartmeter will not be queried automatically")

self.host = self.get_parameter_value('host') # None
self.port = self.get_parameter_value('port') # 0
self.serialport = self.get_parameter_value('serialport') # None
Expand All @@ -87,17 +113,6 @@ def __init__(self, sh):
self.xor_out = self.get_parameter_value('xor_out') # 0xffff
self.swap_crc_bytes = self.get_parameter_value('swap_crc_bytes') # False

self.connected = False
self.alive = False
self._serial = None
self._sock = None
self._target = None
self._dataoffset = 0
self._items = {}
self._item_dict = {}
self._lock = threading.Lock()
self._parse_lock = threading.Lock()

if device in self._devices:
device = self._devices[device]

Expand All @@ -111,24 +126,32 @@ def __init__(self, sh):

self.logger.debug(f"Using CRC params poly={self.poly}, reflect_in={self.reflect_in}, xor_in={self.xor_in}, reflect_out={self.reflect_out}, xor_out={self.xor_out}, swap_crc_bytes={self.swap_crc_bytes}")

self.init_webinterface(WebInterface)

def run(self):
"""
Run method for the plugin
"""
self.logger.debug(f"Plugin '{self.get_fullname()}': run method called")
self.load_parameters()
self.alive = True

# Setup scheduler for device poll loop
self.scheduler_add(SML_SCHEDULER_NAME, self.poll_device, cycle=self.cycle)
if self.cycle or self.crontab:
if self.crontab:
next = None # adhere to the crontab
else:
# no crontab given so we might just query immediately
next = shtime.now()
self.scheduler_add(self.get_fullname(), self.poll_device, prio=5, cycle=self.cycle, cron=self.crontab, next=next)
self.logger.debug(f"Plugin '{self.get_fullname()}': run method finished")

self.alive = True

def stop(self):
"""
Stop method for the plugin
"""
self.logger.debug(f"Plugin '{self.get_fullname()}': stop method called")
self.scheduler_remove(SML_SCHEDULER_NAME)
self.scheduler_remove(self.get_fullname())
self.alive = False
self.disconnect()

Expand Down Expand Up @@ -168,7 +191,7 @@ def update_item(self, item, caller=None, source=None, dest=None):
:param source: if given it represents the source
:param dest: if given it represents the dest
"""
if caller != self.get_shortname():
if caller != self.get_fullname():
# Code to execute, only if the item has not been changed by this plugin:
self.logger.info("Update item: {}, item has been changed outside this plugin".format(item.id()))
pass
Expand Down
13 changes: 10 additions & 3 deletions smlx/plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ plugin:
documentation: https://www.smarthomeng.de/developer/plugins/smlx/user_doc.html
support: https://knx-user-forum.de/forum/supportforen/smarthome-py/39119-sml-plugin-datenblock-größenfehler
restartable: True
version: 1.1.7 # Plugin version
version: 1.1.8 # Plugin version
sh_minversion: 1.4.2 # minimum shNG version to use this plugin
# sh_maxversion: # maximum shNG version to use this plugin (leave empty if latest)
# py_minversion: 3.6 # minimum Python version to use for this plugin
Expand Down Expand Up @@ -53,10 +53,17 @@ parameters:

cycle:
type: int
valid_min: 0
default: 60
description:
de: 'Zeitlicher Abstand in Sekunden zwischen zwei Abfragen des Smartmeters'
en: 'Time in seconds between two queries of Smartmeter'
de: 'Zeitlicher Abstand in Sekunden zwischen zwei Abfragen des Smartmeters, wird 0 angegeben so wird der Smartmeter über cycle nicht abgefragt'
en: 'Time in seconds between two queries of Smartmeter- If given 0 the smartmeter will not be queried by means of cycle'

crontab:
type: str
description:
de: 'Abfragen des Smartmeters mit Festlegung via Crontab'
en: 'Queries of smartmeter by means of a crontab'

device:
type: str
Expand Down
4 changes: 2 additions & 2 deletions smlx/webif/templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@
<td class="py-1"><strong>Verbunden</strong></td>
<td class="py-1">{% if p.connected %}{{ _('Ja') }}{% else %}{{ _('Nein') }}{% endif %}</td>
<td></td>
<td class="py-1"><strong>Zykluszeit</strong></td>
<td class="py-1">{{ p.cycle }}s</td>
<td class="py-1"><strong>Abfrageintervall</strong></td>
<td class="py-1">{% if p.cycle %}{{ p.cycle }}s{% endif %}{% if p.crontab %}{{ p.crontab }}{% endif %}</td>
<td></td>
</tr>
</tbody>
Expand Down

0 comments on commit c8fe266

Please sign in to comment.