validate and publish always with all channels; simplifications

This commit is contained in:
Dirk Alders 2022-07-24 10:38:30 +02:00
parent c796d1d47a
commit 1ccc230b50

View File

@ -26,35 +26,48 @@ class sispmctl(object):
logger.error("sispm error code %d", grepexc.returncode) logger.error("sispm error code %d", grepexc.returncode)
else: else:
logger.info('sispmctl all channels switched off') logger.info('sispmctl all channels switched off')
self.publish_states()
def __filter_output_parameter__(self, output):
try:
return int(output)
except ValueError:
return output
def set_out_state(self, output, state): def set_out_state(self, output, state):
if output in range(1,5): if output == "all":
if self.get_out_state(output) != state: state = [state, state, state, state]
try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state else "-f", str(output)]).decode('UTF-8') if self.get_out_state(output) != state:
except subprocess.CalledProcessError as grepexc: try:
logger.error("sispm error code %d", grepexc.returncode) out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True or state == [True, True, True, True] else "-f", output]).decode('UTF-8')
else: except subprocess.CalledProcessError as grepexc:
logger.info('sispmctl channel %d changed to %s', output, state) logger.error("sispm error code %d", grepexc.returncode)
cnt = 0 else:
while cnt < 3 and self.__state__[output - 1] != state: logger.info('sispmctl channel %s changed to %s', output, state)
cnt += 1 self.publish_states()
try:
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", str(output)]) def publish_states(self):
except subprocess.CalledProcessError as grepexc: try:
logger.error("sispm error code %d", grepexc.returncode) out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
else: except subprocess.CalledProcessError as grepexc:
self.__state__[output - 1] = out_txt.decode("UTF-8").split("\n")[1].split("\t")[1] == "on" logger.error("sispm error code %d", grepexc.returncode)
time.sleep(.2) else:
self.send_state(output) print(out_txt)
for i in range(1, 5):
self.__state__[i-1] = out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on"
self.send_state(i)
def send_state(self, output): def send_state(self, output):
topic = config.MQTT_TOPIC + "/status/" + str(output) topic = config.MQTT_TOPIC + "/status/" + str(output)
logger.info("Sending Powerplug status information of plug %d to mqtt %s = %s", output, topic, str(self.get_out_state(output))) logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.get_out_state(output)))
self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false") self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false")
def get_out_state(self, output): def get_out_state(self, output):
return self.__state__[output - 1] if output in range(1,5):
return self.__state__[int(output) - 1]
else:
return self.__state__
class mqtt_powerplug(object): class mqtt_powerplug(object):
@ -62,7 +75,8 @@ class mqtt_powerplug(object):
"set/1", "set/1",
"set/2", "set/2",
"set/3", "set/3",
"set/4" "set/4",
"set/all"
] ]
def __init__(self): def __init__(self):
@ -79,9 +93,9 @@ class mqtt_powerplug(object):
def __receive__(self, client, userdata, message): def __receive__(self, client, userdata, message):
if message.topic in self.__topics__: if message.topic in self.__topics__:
output = int(message.topic.split("/")[-1]) output = message.topic.split("/")[-1]
state = message.payload == b"true" state = message.payload == b"true"
logger.info("Received request to set output channel %d to state %s", output, str(state)) logger.info("Received request to set output channel %s to state %s", output, str(state))
self.__sc__.set_out_state(output, state) self.__sc__.set_out_state(output, state)
else: else:
logger.warning("Ignoring unknown mqtt topic %s", message.topic) logger.warning("Ignoring unknown mqtt topic %s", message.topic)