This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
global:
|
global:
|
||||||
scan_interval: 1
|
scan_interval: 1
|
||||||
log_level: INFO
|
log_level: DEBUG
|
||||||
|
|
||||||
mqtt:
|
mqtt:
|
||||||
broker: emqx01-anonymous-cluster-internal.broker.svc.cluster.local
|
broker: emqx01-anonymous-cluster-internal.broker.svc.cluster.local
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ class FromDevices(AbstractMqttPublisher):
|
|||||||
payload['cnt'] = cnt
|
payload['cnt'] = cnt
|
||||||
|
|
||||||
payloadStr = json.dumps(payload) if not device.raw_output else str(payload['output'])
|
payloadStr = json.dumps(payload) if not device.raw_output else str(payload['output'])
|
||||||
self.client.publish(device.publish_topic, payloadStr)
|
self.publish_with_cache(device.publish_topic, payloadStr)
|
||||||
logger.debug(f"mqtt message sent: {device.publish_topic} -> {payloadStr}")
|
logger.debug(f"mqtt message sent: {device.publish_topic} -> {payloadStr}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.error(f"Caught exception: {str(e)}")
|
logger.error(f"Caught exception: {str(e)}")
|
||||||
|
|||||||
@@ -25,6 +25,8 @@ class AbstractMqttPublisher(threading.Thread):
|
|||||||
logger.info(f"mqtt client id: {client_id}")
|
logger.info(f"mqtt client id: {client_id}")
|
||||||
self.client = mqtt.Client(client_id=client_id, userdata=self)
|
self.client = mqtt.Client(client_id=client_id, userdata=self)
|
||||||
|
|
||||||
|
self.cache = {}
|
||||||
|
|
||||||
# consider this flag in the localLoop
|
# consider this flag in the localLoop
|
||||||
self.killBill = False
|
self.killBill = False
|
||||||
self.killEvent = threading.Event()
|
self.killEvent = threading.Event()
|
||||||
@@ -62,3 +64,10 @@ class AbstractMqttPublisher(threading.Thread):
|
|||||||
def onMessage(self, topic, payload):
|
def onMessage(self, topic, payload):
|
||||||
logger.warning("mqtt unexpected message received: {} -> {}".format(topic, str(payload)))
|
logger.warning("mqtt unexpected message received: {} -> {}".format(topic, str(payload)))
|
||||||
|
|
||||||
|
def publish_with_cache(self, topic, payload):
|
||||||
|
if topic in self.cache and self.cache[topic] == payload:
|
||||||
|
logger.debug(f"mqtt message unchanged, not publishing: {topic} -> {payload}")
|
||||||
|
return
|
||||||
|
self.cache[topic] = payload
|
||||||
|
self.client.publish(topic, payload)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user