Explorar el Código

Module mqtt implemented

master
Dirk Alders hace 2 años
padre
commit
0e2a063bd1
Se han modificado 4 ficheros con 33 adiciones y 31 borrados
  1. 3
    0
      .gitmodules
  2. 1
    1
      leyk.py
  3. 1
    0
      mqtt
  4. 28
    30
      piface_function.py

+ 3
- 0
.gitmodules Ver fichero

10
 [submodule "task"]
10
 [submodule "task"]
11
 	path = task
11
 	path = task
12
 	url = https://git.mount-mockery.de/pylib/task.git
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 Ver fichero

20
 
20
 
21
     while True:
21
     while True:
22
         time.sleep(30)
22
         time.sleep(30)
23
-        #l.publish_states()
23
+        l.publish_states()

+ 1
- 0
mqtt

1
+Subproject commit cf97fa066cdff0e2f7eda0ff4d3c8c0f59c3f2ec

+ 28
- 30
piface_function.py Ver fichero

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

Loading…
Cancelar
Guardar