|
@@ -0,0 +1,82 @@
|
|
1
|
+import config
|
|
2
|
+import logging
|
|
3
|
+import paho.mqtt.client as mqtt
|
|
4
|
+import report
|
|
5
|
+import subprocess
|
|
6
|
+import time
|
|
7
|
+
|
|
8
|
+try:
|
|
9
|
+ from config import APP_NAME as ROOT_LOGGER_NAME
|
|
10
|
+except ImportError:
|
|
11
|
+ ROOT_LOGGER_NAME = 'root'
|
|
12
|
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+class sispmctl(object):
|
|
16
|
+ AMP_CHANNEL = 0
|
|
17
|
+
|
|
18
|
+ def __init__(self):
|
|
19
|
+ logger.info("Starting sispmctl module...")
|
|
20
|
+ self.__state__ = [False, False, False, False]
|
|
21
|
+ self.__state_requests__ = [{}, {}, {}, {}]
|
|
22
|
+ try:
|
|
23
|
+ out_txt = subprocess.check_output(["sudo", "sispmctl", "-f", "all"]).decode('UTF-8')
|
|
24
|
+ except subprocess.CalledProcessError as grepexc:
|
|
25
|
+ logger.error("sispm error code %d", grepexc.returncode)
|
|
26
|
+ else:
|
|
27
|
+ logger.info('sispmctl all channels switched off')
|
|
28
|
+
|
|
29
|
+ def set_out_state(self, output, state):
|
|
30
|
+ if output in range(1,5):
|
|
31
|
+ if self.__state__[output - 1] != state:
|
|
32
|
+ try:
|
|
33
|
+ out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state else "-f", str(output)]).decode('UTF-8')
|
|
34
|
+ except subprocess.CalledProcessError as grepexc:
|
|
35
|
+ logger.error("sispm error code %d", grepexc.returncode)
|
|
36
|
+ else:
|
|
37
|
+ self.__state__[output - 1] = state
|
|
38
|
+ logger.info('sispmctl channel %d changed to %s', output, state)
|
|
39
|
+
|
|
40
|
+ def get_out_state(self, output):
|
|
41
|
+ return self.__state__[output - 1]
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+class mqtt_powerplug(object):
|
|
45
|
+ SUBTOPICS = [
|
|
46
|
+ "set/1",
|
|
47
|
+ "set/2",
|
|
48
|
+ "set/3",
|
|
49
|
+ "set/4"
|
|
50
|
+ ]
|
|
51
|
+
|
|
52
|
+ def __init__(self):
|
|
53
|
+ self.__client__ = mqtt.Client("mqtt_powerplug") # create client object
|
|
54
|
+ self.__client__.on_message = self.__receive__ # attach function to callback
|
|
55
|
+ self.__client__.connect("192.168.0.131", 1883) # establish connection
|
|
56
|
+ self.__client__.loop_start() # start the loop
|
|
57
|
+ self.__topics__ = []
|
|
58
|
+ for subtopic in self.SUBTOPICS:
|
|
59
|
+ self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
|
|
60
|
+ self.__client__.subscribe(self.__topics__[-1]) # subscibe a topic
|
|
61
|
+ self.__sc__ = sispmctl()
|
|
62
|
+
|
|
63
|
+ def __receive__(self, client, userdata, message):
|
|
64
|
+ if message.topic in self.__topics__:
|
|
65
|
+ output = int(message.topic.split("/")[-1])
|
|
66
|
+ state = message.payload == b"true"
|
|
67
|
+ logger.info("Received request to set output channel %d to state %s", output, str(state))
|
|
68
|
+ self.__sc__.set_out_state(output, state)
|
|
69
|
+ else:
|
|
70
|
+ logger.warning("Ignoring unknown mqtt topic %s", message.topic)
|
|
71
|
+
|
|
72
|
+ def __del__(self):
|
|
73
|
+ self.__client__.loop_stop() # stop the loop
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+if __name__ == '__main__':
|
|
77
|
+ report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
|
78
|
+ #
|
|
79
|
+ mp = mqtt_powerplug()
|
|
80
|
+
|
|
81
|
+ while True:
|
|
82
|
+ time.sleep(2)
|