Pārlūkot izejas kodu

Enable / disable implemented

master
Dirk Alders 2 gadus atpakaļ
vecāks
revīzija
5974ec1c8b
2 mainītis faili ar 26 papildinājumiem un 5 dzēšanām
  1. 2
    0
      example_config/config.py
  2. 24
    5
      exec_command.py

+ 2
- 0
example_config/config.py Parādīt failu

@@ -9,6 +9,8 @@ MQTT_USER = "user"
9 9
 MQTT_PASS = "pass"
10 10
 MQTT_SERVER = "host"
11 11
 
12
+ENABLED_TOPIC = 'topic'
13
+
12 14
 EXEC_LIST = {
13 15
     'topic_1': {
14 16
         'key': 'key',                   # Give a key or None to compare plain DATA

+ 24
- 5
exec_command.py Parādīt failu

@@ -24,12 +24,24 @@ bell_log.addHandler(my_handler)
24 24
 
25 25
 class exec_command(mqtt.mqtt_client):
26 26
     def __init__(self):
27
-        self.__block_execution__ = False
27
+        self.__enabled__ = True
28 28
         mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
29
+        self.add_callback(config.ENABLED_TOPIC + '/set', self.set_enabled)
29 30
         for topic in config.EXEC_LIST:
30 31
             self.add_callback(topic, self.mqtt_rx)
31 32
         # Start a pseudo process
32 33
         self.process = subprocess.Popen(["sleep", "0"])
34
+        self.publish_states()
35
+
36
+    def set_enabled(self, client, userdata, message):
37
+        try:
38
+            payload = json.loads(message.payload)
39
+        except:
40
+            logger.exception("Error decoding json mqtt message")
41
+        else:
42
+            if self.__enabled__ != payload:
43
+                self.__enabled__ = payload
44
+                self.publish_states()
33 45
 
34 46
     def exec_command(self, cmd, message):
35 47
         self.process = subprocess.Popen(cmd.split(" "))
@@ -56,10 +68,16 @@ class exec_command(mqtt.mqtt_client):
56 68
                 except AttributeError:
57 69
                     logger.exception("payload seems to be no dictionary")
58 70
         if data is None or payload == data:
59
-            if self.process.poll() is None:
60
-                self.process.kill()
61
-            logger.debug("Starting execution in background...")
62
-            self.exec_command(config.EXEC_LIST[message.topic]['command'], message)
71
+            if self.__enabled__:
72
+                if self.process.poll() is None:
73
+                    self.process.kill()
74
+                logger.debug("Starting execution in background...")
75
+                self.exec_command(config.EXEC_LIST[message.topic]['command'], message)
76
+            else:
77
+                logger.info("Execution is disabled")
78
+
79
+    def publish_states(self):
80
+        self.send(config.ENABLED_TOPIC, json.dumps(self.__enabled__))
63 81
 
64 82
 
65 83
 if __name__ == '__main__': 
@@ -69,6 +87,7 @@ if __name__ == '__main__':
69 87
     #
70 88
     while True:
71 89
         time.sleep(30)
90
+        ec.publish_states()
72 91
     try:
73 92
         ec.join()
74 93
     finally:

Notiek ielāde…
Atcelt
Saglabāt