|
@@ -2,7 +2,7 @@ import config
|
2
|
2
|
import json
|
3
|
3
|
import lirc
|
4
|
4
|
import logging
|
5
|
|
-import paho.mqtt.client as mqtt
|
|
5
|
+import paho.mqtt.client as paho
|
6
|
6
|
import report
|
7
|
7
|
import socket
|
8
|
8
|
import time
|
|
@@ -16,10 +16,12 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
16
|
16
|
|
17
|
17
|
class remote_control(object):
|
18
|
18
|
def __init__(self):
|
19
|
|
- self.__lirc_client__ = lirc.Client()
|
|
19
|
+ self.__lirc_client__ = None
|
|
20
|
+ self.__lirc_init__()
|
|
21
|
+ #
|
20
|
22
|
self.__start_data__ = None
|
21
|
23
|
#
|
22
|
|
- self.__client__ = mqtt.Client(config.APP_NAME) # create client object
|
|
24
|
+ self.__client__ = paho.Client(config.APP_NAME) # create client object
|
23
|
25
|
self.__client__.on_message = self.__receive__ # attach function to callback
|
24
|
26
|
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
25
|
27
|
try:
|
|
@@ -33,6 +35,13 @@ class remote_control(object):
|
33
|
35
|
except (socket.timeout, OSError) as e:
|
34
|
36
|
logger.warning("Erro while setting up mqtt instance and listener")
|
35
|
37
|
|
|
38
|
+ def __lirc_init__(self):
|
|
39
|
+ if self.__lirc_client__ is not None:
|
|
40
|
+ logger.info("Closing crashed lirc client instance")
|
|
41
|
+ self.__lirc_client__.close()
|
|
42
|
+ logger.info("Initiating lirc connection")
|
|
43
|
+ self.__lirc_client__ = lirc.Client()
|
|
44
|
+
|
36
|
45
|
def __receive__(self, client, userdata, message):
|
37
|
46
|
try:
|
38
|
47
|
payload = json.loads(message.payload)
|
|
@@ -41,6 +50,7 @@ class remote_control(object):
|
41
|
50
|
#
|
42
|
51
|
remote = message.topic.split("/")[-2]
|
43
|
52
|
command = message.topic.split("/")[-1]
|
|
53
|
+ #
|
44
|
54
|
if payload is None:
|
45
|
55
|
if self.__start_data__ is not None:
|
46
|
56
|
self.__send_stop__(*self.__start_data__)
|
|
@@ -62,23 +72,31 @@ class remote_control(object):
|
62
|
72
|
def __send_once__(self, remote, command):
|
63
|
73
|
try:
|
64
|
74
|
self.__lirc_client__.send_once(remote, command)
|
65
|
|
- except TimeoutError:
|
66
|
|
- logger.exception("Timeout-Error while sending IR-Command.")
|
67
|
|
- logger.info("Sending once: %s to %s.", command, remote)
|
|
75
|
+ except (TimeoutError, socket.timeout) as e:
|
|
76
|
+ self.__lirc_timeout_error__()
|
|
77
|
+ else:
|
|
78
|
+ logger.info("Sending once: %s to %s.", command, remote)
|
68
|
79
|
|
69
|
80
|
def __send_start__(self, remote, command):
|
70
|
81
|
try:
|
71
|
82
|
self.__lirc_client__.send_start(remote, command)
|
72
|
|
- except TimeoutError:
|
73
|
|
- logger.exception("Timeout-Error while sending IR-Command.")
|
74
|
|
- logger.info("Sending start: %s to %s.", command, remote)
|
|
83
|
+ except (TimeoutError, socket.timeout) as e:
|
|
84
|
+ self.__lirc_timeout_error__()
|
|
85
|
+ else:
|
|
86
|
+ logger.info("Sending start: %s to %s.", command, remote)
|
75
|
87
|
|
76
|
88
|
def __send_stop__(self, remote, command):
|
77
|
89
|
try:
|
78
|
90
|
self.__lirc_client__.send_stop(remote, command)
|
79
|
|
- except TimeoutError:
|
80
|
|
- logger.exception("Timeout-Error while sending IR-Command.")
|
81
|
|
- logger.info("Sending stop: %s to %s.", command, remote)
|
|
91
|
+ except (TimeoutError, socket.timeout) as e:
|
|
92
|
+ self.__lirc_timeout_error__()
|
|
93
|
+ else:
|
|
94
|
+ logger.info("Sending stop: %s to %s.", command, remote)
|
|
95
|
+
|
|
96
|
+ def __lirc_timeout_error__(self):
|
|
97
|
+ logger.error("Timeout-Error while sending IR-Command.")
|
|
98
|
+ self.__lirc_init__()
|
|
99
|
+
|
82
|
100
|
|
83
|
101
|
if __name__ == '__main__':
|
84
|
102
|
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|