State feedback to mqtt implemented

This commit is contained in:
Dirk Alders 2022-07-24 08:49:03 +02:00
parent c440b10bbe
commit 404455ab1c

View File

@ -15,8 +15,9 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
class sispmctl(object):
AMP_CHANNEL = 0
def __init__(self):
def __init__(self, mqtt_client):
logger.info("Starting sispmctl module...")
self.__mqtt_client__ = mqtt_client
self.__state__ = [False, False, False, False]
self.__state_requests__ = [{}, {}, {}, {}]
try:
@ -28,14 +29,29 @@ class sispmctl(object):
def set_out_state(self, output, state):
if output in range(1,5):
if self.__state__[output - 1] != state:
if self.get_out_state(output) != state:
try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state else "-f", str(output)]).decode('UTF-8')
except subprocess.CalledProcessError as grepexc:
logger.error("sispm error code %d", grepexc.returncode)
else:
self.__state__[output - 1] = state
logger.info('sispmctl channel %d changed to %s', output, state)
cnt = 0
while cnt < 3 and self.__state__[output - 1] != state:
cnt += 1
try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", str(output)])
except subprocess.CalledProcessError as grepexc:
logger.error("sispm error code %d", grepexc.returncode)
else:
self.__state__[output - 1] = out_txt.decode("UTF-8").split("\n")[1].split("\t")[1] == "on"
time.sleep(.2)
self.send_state(output)
def send_state(self, output):
topic = config.MQTT_TOPIC + "/get/" + str(output)
logger.info("Sending Powerplug status information of plug %d to mqtt %s = %s", output, topic, str(self.get_out_state(output)))
self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false")
def get_out_state(self, output):
return self.__state__[output - 1]
@ -59,7 +75,7 @@ class mqtt_powerplug(object):
for subtopic in self.SUBTOPICS:
self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
self.__sc__ = sispmctl()
self.__sc__ = sispmctl(self.__client__)
def __receive__(self, client, userdata, message):
if message.topic in self.__topics__: