123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 |
- 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, mqtt_client):
- logger.info("Starting sispmctl module...")
- self.__mqtt_client__ = mqtt_client
- 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.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:
- 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]
-
-
- 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__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
- self.__client__.connect(config.MQTT_SERVER, 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(self.__client__)
-
- 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)
|