|
@@ -2,7 +2,7 @@ import config
|
2
|
2
|
import json
|
3
|
3
|
import lirc
|
4
|
4
|
import logging
|
5
|
|
-import paho.mqtt.client as paho
|
|
5
|
+import mqtt
|
6
|
6
|
import report
|
7
|
7
|
import socket
|
8
|
8
|
import time
|
|
@@ -14,26 +14,19 @@ except ImportError:
|
14
|
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
|
18
|
def __init__(self):
|
19
|
19
|
self.__lirc_client__ = None
|
20
|
20
|
self.__lirc_init__()
|
21
|
21
|
#
|
22
|
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
|
31
|
def __lirc_init__(self):
|
39
|
32
|
if self.__lirc_client__ is not None:
|
|
@@ -42,7 +35,7 @@ class remote_control(object):
|
42
|
35
|
logger.info("Initiating lirc connection")
|
43
|
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
|
39
|
try:
|
47
|
40
|
payload = json.loads(message.payload)
|
48
|
41
|
except json.decoder.JSONDecodeError:
|