From 2c6cf94ce394b5ab39e8c7ef31bba943ac8a86b5 Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Fri, 22 Jul 2022 09:14:52 +0200 Subject: [PATCH] Initial powerplug implementation --- .gitignore | 5 +++ .gitmodules | 3 ++ config_example/config.py | 21 ++++++++++ powerplug.py | 82 ++++++++++++++++++++++++++++++++++++++++ report | 1 + requirements.txt | 1 + 6 files changed, 113 insertions(+) create mode 100644 .gitmodules create mode 100644 config_example/config.py create mode 100644 powerplug.py create mode 160000 report create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index c7ca2f3..35b217f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +# ---> powerplug-energenie +# +config.py + + # ---> Python # Byte-compiled / optimized / DLL files __pycache__/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..98f8bb5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "report"] + path = report + url = https://git.mount-mockery.de/pylib/report.git diff --git a/config_example/config.py b/config_example/config.py new file mode 100644 index 0000000..b7cd8ef --- /dev/null +++ b/config_example/config.py @@ -0,0 +1,21 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +import os +import report + + +MQTT_SERVER = "multimedia" +MQTT_TOPIC = "hifi/powerplug" + +# +# Logging +# +__BASEPATH__ = os.path.abspath(os.path.dirname(__file__)) +APP_NAME = "powerplug" +LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout' +LOGLVL = 'DEBUG' + +LOGHOST = 'localhost' +LOGPORT = 19996 + +formatter = report.LONG_FMT diff --git a/powerplug.py b/powerplug.py new file mode 100644 index 0000000..4c4c45c --- /dev/null +++ b/powerplug.py @@ -0,0 +1,82 @@ +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 + + def __init__(self): + logger.info("Starting sispmctl module...") + 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') + + def set_out_state(self, output, state): + if output in range(1,5): + if self.__state__[output - 1] != 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) + + def get_out_state(self, output): + return self.__state__[output - 1] + + +class mqtt_powerplug(object): + SUBTOPICS = [ + "set/1", + "set/2", + "set/3", + "set/4" + ] + + def __init__(self): + self.__client__ = mqtt.Client("mqtt_powerplug") # create client object + self.__client__.on_message = self.__receive__ # attach function to callback + self.__client__.connect("192.168.0.131", 1883) # establish connection + 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 + self.__sc__ = sispmctl() + + def __receive__(self, client, userdata, message): + if message.topic in self.__topics__: + output = int(message.topic.split("/")[-1]) + state = message.payload == b"true" + logger.info("Received request to set output channel %d to state %s", output, str(state)) + 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: + time.sleep(2) diff --git a/report b/report new file mode 160000 index 0000000..21bac82 --- /dev/null +++ b/report @@ -0,0 +1 @@ +Subproject commit 21bac82e0c459ebf6d34783c9249526a657a6bbd diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..8579e8b --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +paho-mqtt