input working

This commit is contained in:
2021-08-23 16:24:40 +02:00
parent a37f4945d8
commit 17e5b6b012
6 changed files with 167 additions and 17 deletions

41
src/MqttEventPublisher.py Normal file
View File

@@ -0,0 +1,41 @@
import paho.mqtt.client as mqtt
import threading
from loguru import logger
from AbstractMqttHandler import AbstractMqttPublisher
def mqttEventPublisherStart(config, processImage):
mqttEventPublisherThread = MqttEventPublisher(config, processImage)
mqttEventPublisherThread.start()
class MqttEventPublisher(AbstractMqttPublisher):
def __init__(self, config, processImage):
super().__init__(config, processImage)
def localLoop(self):
while True:
with self.processImage:
self.processImage.wait()
discreteInputChangeset = self.processImage.getChangedDiscreteInputs()
analogInputChangeset = self.processImage.getChangedAnalogsInputs()
for discreteInputChangeItem in discreteInputChangeset:
logger.debug("Discrete input {} changed from {} to {}"
.format(discreteInputChangeItem[0],
discreteInputChangeItem[1][1],
discreteInputChangeItem[1][0]))
self.client.publish("{}/{}".format(self.config["digitalInputTopicPrefix"], str(discreteInputChangeItem[0])),
str(discreteInputChangeItem[1][0]))
for analogInputChangeItem in analogInputChangeset:
logger.debug("Analog input {} changed from {} to {}"
.format(analogInputChangeItem[0],
analogInputChangeItem[1][1],
analogInputChangeItem[1][0]))
self.client.publish("{}/{}".format(self.config["analogInputEventTopicPrefix"], str(analogInputChangeItem[0])),
str(analogInputChangeItem[1][0]))