Parcourir la source

Module mqtt implemented

master
Dirk Alders il y a 2 ans
Parent
révision
0e2a063bd1
4 fichiers modifiés avec 33 ajouts et 31 suppressions
  1. 3
    0
      .gitmodules
  2. 1
    1
      leyk.py
  3. 1
    0
      mqtt
  4. 28
    30
      piface_function.py

+ 3
- 0
.gitmodules Voir le fichier

@@ -10,3 +10,6 @@
10 10
 [submodule "task"]
11 11
 	path = task
12 12
 	url = https://git.mount-mockery.de/pylib/task.git
13
+[submodule "mqtt"]
14
+	path = mqtt
15
+	url = https://git.mount-mockery.de/pylib/mqtt.git

+ 1
- 1
leyk.py Voir le fichier

@@ -20,4 +20,4 @@ if __name__ == '__main__':
20 20
 
21 21
     while True:
22 22
         time.sleep(30)
23
-        #l.publish_states()
23
+        l.publish_states()

+ 1
- 0
mqtt

@@ -0,0 +1 @@
1
+Subproject commit cf97fa066cdff0e2f7eda0ff4d3c8c0f59c3f2ec

+ 28
- 30
piface_function.py Voir le fichier

@@ -9,7 +9,7 @@ import config
9 9
 import geo
10 10
 import json
11 11
 import logging
12
-import paho.mqtt.client as mqtt
12
+import mqtt
13 13
 import state_machine
14 14
 import task
15 15
 import random
@@ -309,7 +309,7 @@ class leyk(object):
309 309
     RX_TOPIC_MODE = config.MQTT_TOPIC + '/set/mode'
310 310
     RX_TOPIC_PLOENLEIN = config.MQTT_TOPIC + '/set/Ploenlein'
311 311
     RX_TOPIC_REESE_HOUSE = config.MQTT_TOPIC + '/set/Reese House'
312
-    
312
+
313 313
     RX_TOPICS = [
314 314
         RX_TOPIC_BAKE_HOUSE,
315 315
         RX_TOPIC_BAKERY,
@@ -319,18 +319,15 @@ class leyk(object):
319 319
         RX_TOPIC_REESE_HOUSE
320 320
     ]
321 321
 
322
+
322 323
     def __init__(self):
323
-        self.__client__ = mqtt.Client("mqtt_leyk")                              # create client object
324
-        self.__client__.on_message = self.__receive__                           # attach function to callback
325
-        self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS)     # login with credentials
326
-        try:
327
-            self.__client__.connect(config.MQTT_SERVER, 1883)                       # establish connection
328
-            self.__client__.loop_start()                                            # start the loop
329
-            self.__topics__ = []
330
-            for topic in self.RX_TOPICS:
331
-                self.__client__.subscribe(topic)          # subscibe a topic
332
-        except (socket.timeout, OSError) as e:
333
-            logger.warning("Error while setting up mqtt instance and listener")
324
+        self.__client__ = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
325
+        self.__client__.add_callback(self.RX_TOPIC_BAKE_HOUSE, self.__rx_bake_house__)
326
+        self.__client__.add_callback(self.RX_TOPIC_BAKERY, self.__rx_bakery__)
327
+        self.__client__.add_callback(self.RX_TOPIC_MILL, self.__rx_mill__)
328
+        self.__client__.add_callback(self.RX_TOPIC_PLOENLEIN, self.__rx_ploenlein__)
329
+        self.__client__.add_callback(self.RX_TOPIC_REESE_HOUSE, self.__rx_reese_house__)
330
+        self.__client__.add_callback(self.RX_TOPIC_MODE, self.__rx_mode__)
334 331
 
335 332
         self.TOPIC_DATA = {
336 333
             self.TOPIC_BAKE_HOUSE: self.get_bake_house,
@@ -368,22 +365,23 @@ class leyk(object):
368 365
         self.publish(self.TOPIC_MODE_BOOL)
369 366
         self.publish(self.TOPIC_STATE)
370 367
 
371
-    def __receive__(self, client, userdata, message):
372
-        logger.info("Received message %s with %s", message.topic, str(message.payload))
373
-        if message.topic == self.RX_TOPIC_MODE:
374
-            self.set_mode(self.sm_mode.STATE_AUTOMATIC if message.payload == b'true' else self.sm_mode.STATE_MANUAL)
375
-        elif message.topic == self.RX_TOPIC_BAKE_HOUSE:
376
-            self.set_bake_house(message.payload == b'true')
377
-        elif message.topic == self.RX_TOPIC_BAKERY:
378
-            self.set_bakery(message.payload == b'true')
379
-        elif message.topic == self.RX_TOPIC_MILL:
380
-            self.set_mill(message.payload == b'true')
381
-        elif message.topic == self.RX_TOPIC_PLOENLEIN:
382
-            self.set_ploenlein(message.payload == b'true')
383
-        elif message.topic == self.RX_TOPIC_REESE_HOUSE:
384
-            self.set_reese_house(message.payload == b'true')
385
-        else:
386
-            logger.warning("Ignoring unknown mqtt topic %s", message.topic)
368
+    def __rx_bake_house__(self, client, userdata, message):
369
+        self.set_bake_house(message.payload == b'true')
370
+
371
+    def __rx_bakery__(self, client, userdata, message):
372
+        self.set_bakery(message.payload == b'true')
373
+
374
+    def __rx_mill__(self, client, userdata, message):
375
+        self.set_mill(message.payload == b'true')
376
+
377
+    def __rx_ploenlein__(self, client, userdata, message):
378
+        self.set_ploenlein(message.payload == b'true')
379
+
380
+    def __rx_reese_house__(self, client, userdata, message):
381
+        self.set_reese_house(message.payload == b'true')
382
+
383
+    def __rx_mode__(self, client, userdata, message):
384
+        self.set_mode(self.sm_mode.STATE_AUTOMATIC if message.payload == b'true' else self.sm_mode.STATE_MANUAL)
387 385
 
388 386
     def publish_states(self):
389 387
         for name in self.TOPIC_DATA:
@@ -392,7 +390,7 @@ class leyk(object):
392 390
     def publish(self, topic):
393 391
         logger.info("Sending Leyk status information to mqtt %s = %s", topic, json.dumps(self.TOPIC_DATA[topic]()))
394 392
         try:
395
-            self.__client__.publish(topic, json.dumps(self.TOPIC_DATA[topic]()))
393
+            self.__client__.send(topic, json.dumps(self.TOPIC_DATA[topic]()))
396 394
         except (socket.timeout, OSError) as e:
397 395
             logger.warning("Error while sending state information information")
398 396
 

Chargement…
Annuler
Enregistrer