Kaynağa Gözat

validate and publish always with all channels; simplifications

master
Dirk Alders 2 yıl önce
ebeveyn
işleme
1ccc230b50
1 değiştirilmiş dosya ile 38 ekleme ve 24 silme
  1. 38
    24
      powerplug.py

+ 38
- 24
powerplug.py Dosyayı Görüntüle

@@ -26,35 +26,48 @@ class sispmctl(object):
26 26
             logger.error("sispm error code %d", grepexc.returncode)
27 27
         else:
28 28
             logger.info('sispmctl all channels switched off')
29
+        self.publish_states()
30
+
31
+    def __filter_output_parameter__(self, output):
32
+        try:
33
+            return int(output)
34
+        except ValueError:
35
+            return output
29 36
 
30 37
     def set_out_state(self, output, state):
31
-        if output in range(1,5):
32
-            if self.get_out_state(output) != state:
33
-                try:
34
-                    out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state else "-f", str(output)]).decode('UTF-8')
35
-                except subprocess.CalledProcessError as grepexc:                                                                                                   
36
-                    logger.error("sispm error code %d", grepexc.returncode)
37
-                else:
38
-                    logger.info('sispmctl channel %d changed to %s', output, state)
39
-                    cnt = 0
40
-                    while cnt < 3 and self.__state__[output - 1] != state:
41
-                        cnt += 1
42
-                        try:
43
-                            out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", str(output)])
44
-                        except subprocess.CalledProcessError as grepexc:
45
-                            logger.error("sispm error code %d", grepexc.returncode)
46
-                        else:
47
-                            self.__state__[output - 1] = out_txt.decode("UTF-8").split("\n")[1].split("\t")[1] == "on"
48
-                        time.sleep(.2)
49
-                self.send_state(output)
38
+        if output == "all":
39
+            state = [state, state, state, state]
40
+
41
+        if self.get_out_state(output) != state:
42
+            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
+            except subprocess.CalledProcessError as grepexc:                                                                                                   
45
+                logger.error("sispm error code %d", grepexc.returncode)
46
+            else:
47
+                logger.info('sispmctl channel %s changed to %s', output, state)
48
+                self.publish_states()
49
+
50
+    def publish_states(self):
51
+        try:
52
+            out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
53
+        except subprocess.CalledProcessError as grepexc:
54
+            logger.error("sispm error code %d", grepexc.returncode)
55
+        else:
56
+            print(out_txt)
57
+            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)
50 60
 
51 61
     def send_state(self, output):
52 62
         topic = config.MQTT_TOPIC + "/status/" + str(output)
53
-        logger.info("Sending Powerplug status information of plug %d to mqtt %s = %s", output, topic, str(self.get_out_state(output)))
63
+        logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.get_out_state(output)))
54 64
         self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false")
55 65
 
56 66
     def get_out_state(self, output):
57
-        return self.__state__[output - 1]
67
+        if output in range(1,5):
68
+            return self.__state__[int(output) - 1]
69
+        else:
70
+            return self.__state__
58 71
 
59 72
 
60 73
 class mqtt_powerplug(object):
@@ -62,7 +75,8 @@ class mqtt_powerplug(object):
62 75
         "set/1",
63 76
         "set/2",
64 77
         "set/3",
65
-        "set/4"
78
+        "set/4",
79
+        "set/all"
66 80
     ]
67 81
 
68 82
     def __init__(self):
@@ -79,9 +93,9 @@ class mqtt_powerplug(object):
79 93
 
80 94
     def __receive__(self, client, userdata, message):
81 95
         if message.topic in self.__topics__:
82
-            output = int(message.topic.split("/")[-1])
96
+            output = message.topic.split("/")[-1]
83 97
             state = message.payload == b"true"
84
-            logger.info("Received request to set output channel %d to state %s", output, str(state))
98
+            logger.info("Received request to set output channel %s to state %s", output, str(state))
85 99
             self.__sc__.set_out_state(output, state)
86 100
         else:
87 101
             logger.warning("Ignoring unknown mqtt topic %s", message.topic)

Loading…
İptal
Kaydet