Browse Source

Module mqtt implemented

master
Dirk Alders 2 years ago
parent
commit
01fc7408ff
3 changed files with 13 additions and 16 deletions
  1. 3
    0
      .gitmodules
  2. 1
    0
      mqtt
  3. 9
    16
      remote_control.py

+ 3
- 0
.gitmodules View File

1
 [submodule "report"]
1
 [submodule "report"]
2
 	path = report
2
 	path = report
3
 	url = https://git.mount-mockery.de/pylib/report.git
3
 	url = https://git.mount-mockery.de/pylib/report.git
4
+[submodule "mqtt"]
5
+	path = mqtt
6
+	url = https://git.mount-mockery.de/pylib/mqtt.git

+ 1
- 0
mqtt

1
+Subproject commit 3d9fc164b4a2cca4a528bd71f04b87a50cd5a078

+ 9
- 16
remote_control.py View File

2
 import json
2
 import json
3
 import lirc
3
 import lirc
4
 import logging
4
 import logging
5
-import paho.mqtt.client as paho
5
+import mqtt
6
 import report
6
 import report
7
 import socket
7
 import socket
8
 import time
8
 import time
14
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
14
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
15
 
15
 
16
 
16
 
17
-class remote_control(object):
17
+class remote_control(mqtt.mqtt_client):
18
     def __init__(self):
18
     def __init__(self):
19
         self.__lirc_client__ = None
19
         self.__lirc_client__ = None
20
         self.__lirc_init__()
20
         self.__lirc_init__()
21
         #
21
         #
22
         self.__start_data__ = None
22
         self.__start_data__ = None
23
         #
23
         #
24
-        self.__client__ = paho.Client(config.APP_NAME)                          # create client object
25
-        self.__client__.on_message = self.__receive__                           # attach function to callback
26
-        self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS)     # login with credentials
27
-        try:
28
-            self.__client__.connect(config.MQTT_SERVER, 1883)                       # establish connection
29
-            self.__client__.loop_start()                                            # start the loop
30
-            for remote in config.SUPPORTED_REMOTES:
31
-                for command in config.SUPPORTED_REMOTES[remote]:
32
-                    topic = config.MQTT_TOPIC + "/" + remote + "/" + command
33
-                    logger.debug("Subscribing \"%s\"", topic)
34
-                    self.__client__.subscribe(topic)                                # subscibe a topic
35
-        except (socket.timeout, OSError) as e:
36
-            logger.warning("Erro while setting up mqtt instance and listener")
24
+        mqtt.mqtt_client.__init__(self, config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
25
+        #
26
+        for remote in config.SUPPORTED_REMOTES:
27
+            for command in config.SUPPORTED_REMOTES[remote]:
28
+                topic = config.MQTT_TOPIC + "/" + remote + "/" + command
29
+                self.add_callback(topic, self.mqtt_to_lirc)
37
 
30
 
38
     def __lirc_init__(self):
31
     def __lirc_init__(self):
39
         if self.__lirc_client__ is not None:
32
         if self.__lirc_client__ is not None:
42
         logger.info("Initiating lirc connection")
35
         logger.info("Initiating lirc connection")
43
         self.__lirc_client__ = lirc.Client()
36
         self.__lirc_client__ = lirc.Client()
44
 
37
 
45
-    def __receive__(self, client, userdata, message):
38
+    def mqtt_to_lirc(self, client, userdata, message):
46
         try:
39
         try:
47
             payload = json.loads(message.payload)
40
             payload = json.loads(message.payload)
48
         except json.decoder.JSONDecodeError:
41
         except json.decoder.JSONDecodeError:

Loading…
Cancel
Save