2022-07-22 09:14:52 +02:00
|
|
|
import config
|
|
|
|
import logging
|
|
|
|
import paho.mqtt.client as mqtt
|
|
|
|
import report
|
|
|
|
import subprocess
|
|
|
|
import time
|
|
|
|
|
|
|
|
try:
|
|
|
|
from config import APP_NAME as ROOT_LOGGER_NAME
|
|
|
|
except ImportError:
|
|
|
|
ROOT_LOGGER_NAME = 'root'
|
|
|
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
|
|
|
|
|
|
|
|
|
|
|
class sispmctl(object):
|
|
|
|
AMP_CHANNEL = 0
|
|
|
|
|
2022-07-24 08:49:03 +02:00
|
|
|
def __init__(self, mqtt_client):
|
2022-07-22 09:14:52 +02:00
|
|
|
logger.info("Starting sispmctl module...")
|
2022-07-24 08:49:03 +02:00
|
|
|
self.__mqtt_client__ = mqtt_client
|
2022-07-22 09:14:52 +02:00
|
|
|
self.__state__ = [False, False, False, False]
|
|
|
|
self.__state_requests__ = [{}, {}, {}, {}]
|
|
|
|
try:
|
|
|
|
out_txt = subprocess.check_output(["sudo", "sispmctl", "-f", "all"]).decode('UTF-8')
|
|
|
|
except subprocess.CalledProcessError as grepexc:
|
|
|
|
logger.error("sispm error code %d", grepexc.returncode)
|
|
|
|
else:
|
|
|
|
logger.info('sispmctl all channels switched off')
|
2022-07-24 10:38:30 +02:00
|
|
|
self.publish_states()
|
|
|
|
|
|
|
|
def __filter_output_parameter__(self, output):
|
|
|
|
try:
|
|
|
|
return int(output)
|
|
|
|
except ValueError:
|
|
|
|
return output
|
2022-07-22 09:14:52 +02:00
|
|
|
|
|
|
|
def set_out_state(self, output, state):
|
2022-07-24 14:08:49 +02:00
|
|
|
output = self.__filter_output_parameter__(output)
|
2022-07-24 10:38:30 +02:00
|
|
|
if output == "all":
|
|
|
|
state = [state, state, state, state]
|
|
|
|
|
2022-07-24 14:08:49 +02:00
|
|
|
if self.__state__[output - 1] != state:
|
2022-07-24 10:38:30 +02:00
|
|
|
try:
|
2022-07-24 14:08:49 +02:00
|
|
|
out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True or state == [True, True, True, True] else "-f", str(output)]).decode('UTF-8')
|
2022-07-24 10:38:30 +02:00
|
|
|
except subprocess.CalledProcessError as grepexc:
|
|
|
|
logger.error("sispm error code %d", grepexc.returncode)
|
|
|
|
else:
|
|
|
|
logger.info('sispmctl channel %s changed to %s', output, state)
|
|
|
|
self.publish_states()
|
|
|
|
|
2022-07-24 14:08:49 +02:00
|
|
|
def get_out_states(self):
|
2022-07-24 10:38:30 +02:00
|
|
|
try:
|
|
|
|
out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
|
|
|
|
except subprocess.CalledProcessError as grepexc:
|
|
|
|
logger.error("sispm error code %d", grepexc.returncode)
|
|
|
|
else:
|
2022-07-24 14:08:49 +02:00
|
|
|
rv = []
|
2022-07-24 10:38:30 +02:00
|
|
|
for i in range(1, 5):
|
2022-07-24 14:08:49 +02:00
|
|
|
rv.append(out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on")
|
|
|
|
return rv
|
|
|
|
|
|
|
|
def publish_states(self):
|
|
|
|
self.__state__ = self.get_out_states()
|
|
|
|
for i in range(1, 5):
|
|
|
|
self.send_state(i)
|
2022-07-24 08:49:03 +02:00
|
|
|
|
|
|
|
def send_state(self, output):
|
2022-07-24 09:23:48 +02:00
|
|
|
topic = config.MQTT_TOPIC + "/status/" + str(output)
|
2022-07-24 14:08:49 +02:00
|
|
|
logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
|
|
|
|
self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
|
2022-07-22 09:14:52 +02:00
|
|
|
|
|
|
|
|
|
|
|
class mqtt_powerplug(object):
|
|
|
|
SUBTOPICS = [
|
|
|
|
"set/1",
|
|
|
|
"set/2",
|
|
|
|
"set/3",
|
2022-07-24 10:38:30 +02:00
|
|
|
"set/4",
|
|
|
|
"set/all"
|
2022-07-22 09:14:52 +02:00
|
|
|
]
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.__client__ = mqtt.Client("mqtt_powerplug") # create client object
|
|
|
|
self.__client__.on_message = self.__receive__ # attach function to callback
|
2022-07-23 21:52:18 +02:00
|
|
|
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
|
|
|
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
2022-07-22 09:14:52 +02:00
|
|
|
self.__client__.loop_start() # start the loop
|
|
|
|
self.__topics__ = []
|
|
|
|
for subtopic in self.SUBTOPICS:
|
|
|
|
self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
|
|
|
|
self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
|
2022-07-24 08:49:03 +02:00
|
|
|
self.__sc__ = sispmctl(self.__client__)
|
2022-07-22 09:14:52 +02:00
|
|
|
|
|
|
|
def __receive__(self, client, userdata, message):
|
|
|
|
if message.topic in self.__topics__:
|
2022-07-24 10:38:30 +02:00
|
|
|
output = message.topic.split("/")[-1]
|
2022-07-22 09:14:52 +02:00
|
|
|
state = message.payload == b"true"
|
2022-07-24 10:38:30 +02:00
|
|
|
logger.info("Received request to set output channel %s to state %s", output, str(state))
|
2022-07-22 09:14:52 +02:00
|
|
|
self.__sc__.set_out_state(output, state)
|
|
|
|
else:
|
|
|
|
logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
|
|
|
|
|
|
|
def __del__(self):
|
|
|
|
self.__client__.loop_stop() # stop the loop
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
|
|
|
#
|
|
|
|
mp = mqtt_powerplug()
|
|
|
|
|
|
|
|
while True:
|
2022-07-24 14:08:49 +02:00
|
|
|
time.sleep(30)
|
|
|
|
mp.__sc__.publish_states()
|