add status message on mqtt
This commit is contained in:
@ -2,6 +2,7 @@ from threading import Event
|
|||||||
from loguru import logger
|
from loguru import logger
|
||||||
from MqttBase import AbstractMqttPublisher
|
from MqttBase import AbstractMqttPublisher
|
||||||
from queue import Empty
|
from queue import Empty
|
||||||
|
import json
|
||||||
|
|
||||||
|
|
||||||
LOOP_SLICE = 0.1 # seconds
|
LOOP_SLICE = 0.1 # seconds
|
||||||
@ -11,8 +12,11 @@ class MqttPublish(AbstractMqttPublisher):
|
|||||||
super().__init__(config)
|
super().__init__(config)
|
||||||
self.queue = queue
|
self.queue = queue
|
||||||
self.topicPre = self.config["publishTopicPrefix"]
|
self.topicPre = self.config["publishTopicPrefix"]
|
||||||
|
self.statusTopic = self.config["statusTopic"]
|
||||||
|
self.statusThreshold = self.config["statusThreshold"]
|
||||||
|
|
||||||
def localLoop(self):
|
def localLoop(self):
|
||||||
|
cnt = 0
|
||||||
while not self.killBill:
|
while not self.killBill:
|
||||||
try:
|
try:
|
||||||
dataObject = self.queue.get(timeout=LOOP_SLICE)
|
dataObject = self.queue.get(timeout=LOOP_SLICE)
|
||||||
@ -22,7 +26,11 @@ class MqttPublish(AbstractMqttPublisher):
|
|||||||
topic = self.topicPre + '/' + dataObject.getTopicPost()
|
topic = self.topicPre + '/' + dataObject.getTopicPost()
|
||||||
payload = dataObject.getPayload()
|
payload = dataObject.getPayload()
|
||||||
self.client.publish(topic, payload)
|
self.client.publish(topic, payload)
|
||||||
|
cnt += 1
|
||||||
logger.debug("mqtt message sent: {} -> {}".format(topic, payload))
|
logger.debug("mqtt message sent: {} -> {}".format(topic, payload))
|
||||||
|
if cnt % 100 == 0:
|
||||||
|
payload = { "count": cnt }
|
||||||
|
self.client.publish(self.statusTopic, json.dumps(payload), retain=True)
|
||||||
except Empty:
|
except Empty:
|
||||||
if self.killBill:
|
if self.killBill:
|
||||||
logger.error("killbill received")
|
logger.error("killbill received")
|
||||||
|
@ -2,7 +2,9 @@
|
|||||||
"mqtt": {
|
"mqtt": {
|
||||||
"broker": "172.16.2.16",
|
"broker": "172.16.2.16",
|
||||||
"port": 1883,
|
"port": 1883,
|
||||||
"publishTopicPrefix": "opcua"
|
"publishTopicPrefix": "opcua",
|
||||||
|
"statusTopic": "opcua/bridge/status",
|
||||||
|
"statusThreshold": 100
|
||||||
},
|
},
|
||||||
"opcua": [
|
"opcua": [
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user