Kaynağa Gözat

State feedback to mqtt implemented

master
Dirk Alders 2 yıl önce
ebeveyn
işleme
404455ab1c
1 değiştirilmiş dosya ile 20 ekleme ve 4 silme
  1. 20
    4
      powerplug.py

+ 20
- 4
powerplug.py Dosyayı Görüntüle

@@ -15,8 +15,9 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
15 15
 class sispmctl(object):
16 16
     AMP_CHANNEL = 0
17 17
 
18
-    def __init__(self):
18
+    def __init__(self, mqtt_client):
19 19
         logger.info("Starting sispmctl module...")
20
+        self.__mqtt_client__ = mqtt_client
20 21
         self.__state__ = [False, False, False, False]
21 22
         self.__state_requests__ = [{}, {}, {}, {}]
22 23
         try:
@@ -28,14 +29,29 @@ class sispmctl(object):
28 29
 
29 30
     def set_out_state(self, output, state):
30 31
         if output in range(1,5):
31
-            if self.__state__[output - 1] != state:
32
+            if self.get_out_state(output) != state:
32 33
                 try:
33 34
                     out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state else "-f", str(output)]).decode('UTF-8')
34 35
                 except subprocess.CalledProcessError as grepexc:                                                                                                   
35 36
                     logger.error("sispm error code %d", grepexc.returncode)
36 37
                 else:
37
-                    self.__state__[output - 1] = state
38 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)
50
+
51
+    def send_state(self, output):
52
+        topic = config.MQTT_TOPIC + "/get/" + str(output)
53
+        logger.info("Sending Powerplug status information of plug %d to mqtt %s = %s", output, topic, str(self.get_out_state(output)))
54
+        self.__mqtt_client__.publish(topic, "true" if self.get_out_state(output) else "false")
39 55
 
40 56
     def get_out_state(self, output):
41 57
         return self.__state__[output - 1]
@@ -59,7 +75,7 @@ class mqtt_powerplug(object):
59 75
         for subtopic in self.SUBTOPICS:
60 76
             self.__topics__.append(config.MQTT_TOPIC + "/" + subtopic)
61 77
             self.__client__.subscribe(self.__topics__[-1])                      # subscibe a topic
62
-        self.__sc__ = sispmctl()
78
+        self.__sc__ = sispmctl(self.__client__)
63 79
 
64 80
     def __receive__(self, client, userdata, message):
65 81
         if message.topic in self.__topics__:

Loading…
İptal
Kaydet