mirror of
https://github.com/naruxde/revpipyload.git
synced 2025-11-08 15:13:52 +01:00
Merge pull request #10 from kjkoster/master
MQTT feature request (with code)
This commit is contained in:
@@ -76,6 +76,7 @@ class MqttServer(Thread):
|
|||||||
self._loadrevpimodio()
|
self._loadrevpimodio()
|
||||||
|
|
||||||
# Topics konfigurieren
|
# Topics konfigurieren
|
||||||
|
self._basetopic = basetopic
|
||||||
self._mqtt_evt_io = join(basetopic, "event/{0}")
|
self._mqtt_evt_io = join(basetopic, "event/{0}")
|
||||||
self._mqtt_got_io = join(basetopic, "got/{0}")
|
self._mqtt_got_io = join(basetopic, "got/{0}")
|
||||||
self._mqtt_io = join(basetopic, "io/{0}")
|
self._mqtt_io = join(basetopic, "io/{0}")
|
||||||
@@ -222,16 +223,29 @@ class MqttServer(Thread):
|
|||||||
self._evt_data.set()
|
self._evt_data.set()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
lst_topic = msg.topic.split("/")
|
# The I/O name may contain forward slashes. Those look nice on the
|
||||||
|
# MQTT bus, but make parsing the topic for actions a bit harder since
|
||||||
|
# we cannot simply split the topic and know at what index to find the
|
||||||
|
# action. To find the action we first remove the base topic (keeping
|
||||||
|
# in mind that it may or may not have a trailing / in the
|
||||||
|
# configuration file), determine the I/O action and finally reassemble
|
||||||
|
# the I/O name with slashes.
|
||||||
|
|
||||||
|
lst_topic = msg.topic[len(self._basetopic):]
|
||||||
|
if lst_topic[0] == '/':
|
||||||
|
lst_topic = lst_topic[1:]
|
||||||
|
lst_topic = lst_topic.split("/")
|
||||||
|
proginit.logger.error("lst_topic {0}".format(lst_topic))
|
||||||
|
|
||||||
if len(lst_topic) < 2:
|
if len(lst_topic) < 2:
|
||||||
proginit.logger.info("wrong topic format - need ./get/ioname or ./set/ioname")
|
proginit.logger.error("wrong format for topic '{0}', expected '{1}/(get/set/reset)/<ioname>'".format(msg.topic, self._basetopic))
|
||||||
return
|
return
|
||||||
|
|
||||||
# Aktion und IO auswerten
|
# Aktion und IO auswerten
|
||||||
ioget = lst_topic[-2].lower() == "get"
|
ioget = lst_topic[0].lower() == "get"
|
||||||
ioset = lst_topic[-2].lower() == "set"
|
ioset = lst_topic[0].lower() == "set"
|
||||||
ioreset = lst_topic[-2].lower() == "reset"
|
ioreset = lst_topic[0].lower() == "reset"
|
||||||
ioname = lst_topic[-1]
|
ioname = '/'.join(lst_topic[1:])
|
||||||
coreio = ioname.find(".") != -1
|
coreio = ioname.find(".") != -1
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -245,7 +259,7 @@ class MqttServer(Thread):
|
|||||||
io = self._rpi.io[ioname]
|
io = self._rpi.io[ioname]
|
||||||
io_needbytes = type(io.value) == bytes
|
io_needbytes = type(io.value) == bytes
|
||||||
except Exception:
|
except Exception:
|
||||||
proginit.logger.error("can not find io '{0}' for MQTT".format(ioname))
|
proginit.logger.error("can not find io '{0}'".format(ioname))
|
||||||
return
|
return
|
||||||
|
|
||||||
# Aktion verarbeiten
|
# Aktion verarbeiten
|
||||||
@@ -261,7 +275,7 @@ class MqttServer(Thread):
|
|||||||
self._evt_io(io.name, io.value, requested=True)
|
self._evt_io(io.name, io.value, requested=True)
|
||||||
|
|
||||||
elif ioset and io.type != revpimodio2.OUT:
|
elif ioset and io.type != revpimodio2.OUT:
|
||||||
proginit.logger.error("can not write to inputs with MQTT")
|
proginit.logger.error("can not write to input '{0}'".format(ioname))
|
||||||
|
|
||||||
elif ioset:
|
elif ioset:
|
||||||
# Convert MQTT Payload to valid Output-Value
|
# Convert MQTT Payload to valid Output-Value
|
||||||
@@ -275,7 +289,7 @@ class MqttServer(Thread):
|
|||||||
try:
|
try:
|
||||||
value = value.to_bytes(io.length, io.byteorder)
|
value = value.to_bytes(io.length, io.byteorder)
|
||||||
except OverflowError:
|
except OverflowError:
|
||||||
proginit.logger.error("can not convert value '{0}' to fitting bytes".format(value))
|
proginit.logger.error("can not convert value '{0}' to fitting bytes for output '{1}'".format(value, ioname))
|
||||||
return
|
return
|
||||||
|
|
||||||
elif value.lower() == "false" and not io_needbytes:
|
elif value.lower() == "false" and not io_needbytes:
|
||||||
@@ -293,18 +307,18 @@ class MqttServer(Thread):
|
|||||||
if not self._send_events:
|
if not self._send_events:
|
||||||
io._parentdevice.writeprocimg()
|
io._parentdevice.writeprocimg()
|
||||||
except Exception:
|
except Exception:
|
||||||
proginit.logger.error("could not write '{0}' to Output '{1}'".format(value, ioname))
|
proginit.logger.error("could not write value '{0}' to output '{1}'".format(value, ioname))
|
||||||
|
|
||||||
elif ioreset:
|
elif ioreset:
|
||||||
# Counter zurücksetzen
|
# Counter zurücksetzen
|
||||||
if not isinstance(io, revpimodio2.io.IntIOCounter):
|
if not isinstance(io, revpimodio2.io.IntIOCounter):
|
||||||
proginit.logger.warning("this io has no counter")
|
proginit.logger.warning("io '{0}' is not a counter".format(ioname))
|
||||||
else:
|
else:
|
||||||
io.reset()
|
io.reset()
|
||||||
|
|
||||||
else:
|
else:
|
||||||
# Aktion nicht erkennbar
|
# Aktion nicht erkennbar
|
||||||
proginit.logger.warning("can not see get/set in topic '{0}'".format(msg.topic))
|
proginit.logger.warning("can not find get/set/reset in topic '{0}'".format(msg.topic))
|
||||||
|
|
||||||
def _send_pictory_conf(self):
|
def _send_pictory_conf(self):
|
||||||
"""Sendet piCtory Konfiguration per MQTT."""
|
"""Sendet piCtory Konfiguration per MQTT."""
|
||||||
|
|||||||
Reference in New Issue
Block a user