30 lines
892 B
Python
30 lines
892 B
Python
from MqttBase import AbstractMqttPublisher
|
|
from loguru import logger
|
|
from time import sleep
|
|
|
|
|
|
class RelaisSubscribe(AbstractMqttPublisher):
|
|
def __init__(self, config, modbusHandler):
|
|
super().__init__(config)
|
|
self.modbusHandler = modbusHandler
|
|
|
|
def localLoop(self):
|
|
while not self.killBill:
|
|
sleep(60.0)
|
|
|
|
def onMessage(self, topic, payload):
|
|
logger.info("mqtt message received: {} -> {}".format(topic, str(payload)))
|
|
if payload == b'On':
|
|
self.modbusHandler.writeCoil(1, 0, 1)
|
|
elif payload == b'Off':
|
|
self.modbusHandler.writeCoil(1, 0, 0)
|
|
else:
|
|
logger.warning(f"Illegal command {payload} received")
|
|
|
|
|
|
|
|
def onConnect(self):
|
|
logger.info("mqtt connected")
|
|
self.client.subscribe("{}".format(self.config["relaisSubscribeTopic"]))
|
|
logger.info("subscribed")
|