瀏覽代碼

Initial powerplug implementation

master
Dirk Alders 2 年之前
父節點
當前提交
2c6cf94ce3
共有 6 個文件被更改,包括 113 次插入0 次删除
  1. 5
    0
      .gitignore
  2. 3
    0
      .gitmodules
  3. 21
    0
      config_example/config.py
  4. 82
    0
      powerplug.py
  5. 1
    0
      report
  6. 1
    0
      requirements.txt

+ 5
- 0
.gitignore 查看文件

@@ -1,3 +1,8 @@
1
+# ---> powerplug-energenie
2
+#
3
+config.py
4
+
5
+
1 6
 # ---> Python
2 7
 # Byte-compiled / optimized / DLL files
3 8
 __pycache__/

+ 3
- 0
.gitmodules 查看文件

@@ -0,0 +1,3 @@
1
+[submodule "report"]
2
+	path = report
3
+	url = https://git.mount-mockery.de/pylib/report.git

+ 21
- 0
config_example/config.py 查看文件

@@ -0,0 +1,21 @@
1
+#!/usr/bin/env python
2
+# -*- coding: UTF-8 -*-
3
+import os
4
+import report
5
+
6
+
7
+MQTT_SERVER = "multimedia"
8
+MQTT_TOPIC = "hifi/powerplug"
9
+
10
+#
11
+# Logging
12
+#
13
+__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
14
+APP_NAME = "powerplug"
15
+LOGTARGET = 'stdout'   # possible choices are: 'logfile' or 'stdout'
16
+LOGLVL = 'DEBUG'
17
+
18
+LOGHOST = 'localhost'
19
+LOGPORT = 19996
20
+
21
+formatter = report.LONG_FMT

+ 82
- 0
powerplug.py 查看文件

@@ -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)

+ 1
- 0
report

@@ -0,0 +1 @@
1
+Subproject commit 21bac82e0c459ebf6d34783c9249526a657a6bbd

+ 1
- 0
requirements.txt 查看文件

@@ -0,0 +1 @@
1
+paho-mqtt

Loading…
取消
儲存