Browse Source

sispm improvements

master
Dirk Alders 2 years ago
parent
commit
4ecd37f1ca
1 changed files with 16 additions and 12 deletions
  1. 16
    12
      powerplug.py

+ 16
- 12
powerplug.py View File

40
             try:
40
             try:
41
                 out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True else "-f", output]).decode('UTF-8')
41
                 out_txt = subprocess.check_output(["sudo", "sispmctl", "-o" if state == True else "-f", output]).decode('UTF-8')
42
             except subprocess.CalledProcessError as grepexc:                                                                                                   
42
             except subprocess.CalledProcessError as grepexc:                                                                                                   
43
-                logger.error("sispm error code %d", grepexc.returncode)
43
+                logger.error("sispm error code %d while changing state of channel %s", grepexc.returncode, output)
44
             else:
44
             else:
45
                 logger.info('sispmctl channel %s changed to %s', output, state)
45
                 logger.info('sispmctl channel %s changed to %s', output, state)
46
                 self.publish_states()
46
                 self.publish_states()
53
             self.set_out_state(output, not self.__state__[int(output) - 1])
53
             self.set_out_state(output, not self.__state__[int(output) - 1])
54
 
54
 
55
     def get_out_states(self):
55
     def get_out_states(self):
56
-        try:
57
-            out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
58
-        except subprocess.CalledProcessError as grepexc:
59
-            logger.error("sispm error code %d", grepexc.returncode)
60
-        else:
61
-            rv = []
62
-            for i in range(1, 5):
63
-                rv.append(out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on")
64
-            return rv
56
+        i = 0
57
+        while i < 10:	# number of tries
58
+            try:
59
+                out_txt = subprocess.check_output(["sudo", "sispmctl", "-g", "all"])
60
+            except subprocess.CalledProcessError as grepexc:
61
+                logger.error("sispm error code %d while getting states", grepexc.returncode)
62
+            else:
63
+                rv = []
64
+                for i in range(1, 5):
65
+                    rv.append(out_txt.decode("UTF-8").split("\n")[i].split("\t")[1] == "on")
66
+                return rv
67
+        logger.warning("Could not get state of powerplugs")
68
+        return self.__state__
65
 
69
 
66
     def publish_states(self):
70
     def publish_states(self):
67
         self.__state__ = self.get_out_states()
71
         self.__state__ = self.get_out_states()
70
 
74
 
71
     def send_state(self, output):
75
     def send_state(self, output):
72
         topic = config.MQTT_TOPIC + "/status/" + str(output)
76
         topic = config.MQTT_TOPIC + "/status/" + str(output)
73
-        logger.info("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
77
+        logger.debug("Sending Powerplug status information of plug %s to mqtt %s = %s", str(output), topic, str(self.__state__[output - 1]))
74
         try:
78
         try:
75
             self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
79
             self.__mqtt_client__.send(topic, "true" if self.__state__[output - 1] else "false")
76
         except (socket.timeout, OSError) as e:
80
         except (socket.timeout, OSError) as e:
77
-            logger.warning("Erro while sending state information information")
81
+            logger.warning("Error while sending state information via mqtt")
78
 
82
 
79
 
83
 
80
 class mqtt_powerplug(mqtt.mqtt_client):
84
 class mqtt_powerplug(mqtt.mqtt_client):

Loading…
Cancel
Save