Просмотр исходного кода

cyclic state publish via mqtt

master
Dirk Alders 2 лет назад
Родитель
Сommit
587de798c2
1 измененных файлов: 16 добавлений и 15 удалений
  1. 16
    15
      powerplug.py

+ 16
- 15
powerplug.py Просмотреть файл

@@ -35,39 +35,39 @@ class sispmctl(object):
35 35
             return output
36 36
 
37 37
     def set_out_state(self, output, state):
38
+        output = self.__filter_output_parameter__(output)
38 39
         if output == "all":
39 40
             state = [state, state, state, state]
40 41
 
41
-        if self.get_out_state(output) != state:
42
+        if self.__state__[output - 1] != state:
42 43
             try:
43
-                out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True or state == [True, True, True, True] else "-f", output]).decode('UTF-8')
44
+                out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True or state == [True, True, True, True] else "-f", str(output)]).decode('UTF-8')
44 45
             except subprocess.CalledProcessError as grepexc:                                                                                                   
45 46
                 logger.error("sispm error code %d", grepexc.returncode)
46 47
             else:
47 48
                 logger.info('sispmctl channel %s changed to %s', output, state)
48 49
                 self.publish_states()
49 50
 
50
-    def publish_states(self):
51
+    def get_out_states(self):
51 52
         try:
52 53
             out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
53 54
         except subprocess.CalledProcessError as grepexc:
54 55
             logger.error("sispm error code %d", grepexc.returncode)
55 56
         else:
56
-            print(out_txt)
57
+            rv = []
57 58
             for i in range(1, 5):
58
-                self.__state__[i-1] = out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on"
59
-                self.send_state(i)
59
+                rv.append(out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on")
60
+            return rv
61
+
62
+    def publish_states(self):
63
+        self.__state__ = self.get_out_states()
64
+        for i in range(1, 5):
65
+            self.send_state(i)
60 66
 
61 67
     def send_state(self, output):
62 68
         topic = config.MQTT_TOPIC + "/status/" + str(output)
63
-        logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.get_out_state(output)))
64
-        self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false")
65
-
66
-    def get_out_state(self, output):
67
-        if output in range(1,5):
68
-            return self.__state__[int(output) - 1]
69
-        else:
70
-            return self.__state__
69
+        logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
70
+        self.__mqtt_client__.publish(topic, "true" if self.__state__[output - 1] else "false")
71 71
 
72 72
 
73 73
 class mqtt_powerplug(object):
@@ -110,4 +110,5 @@ if __name__ == '__main__':
110 110
     mp = mqtt_powerplug()
111 111
 
112 112
     while True:
113
-        time.sleep(2)
113
+        time.sleep(30)
114
+        mp.__sc__.publish_states()

Загрузка…
Отмена
Сохранить