Browse Source

Timout handling for lirc client implemented

master
Dirk Alders 2 years ago
parent
commit
934992d275
1 changed files with 30 additions and 12 deletions
  1. 30
    12
      remote_control.py

+ 30
- 12
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 mqtt
5
+import paho.mqtt.client as paho
6
 import report
6
 import report
7
 import socket
7
 import socket
8
 import time
8
 import time
16
 
16
 
17
 class remote_control(object):
17
 class remote_control(object):
18
     def __init__(self):
18
     def __init__(self):
19
-        self.__lirc_client__ = lirc.Client()
19
+        self.__lirc_client__ = None
20
+        self.__lirc_init__()
21
+        #
20
         self.__start_data__ = None
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
         self.__client__.on_message = self.__receive__                           # attach function to callback
25
         self.__client__.on_message = self.__receive__                           # attach function to callback
24
         self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS)     # login with credentials
26
         self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS)     # login with credentials
25
         try:
27
         try:
33
         except (socket.timeout, OSError) as e:
35
         except (socket.timeout, OSError) as e:
34
             logger.warning("Erro while setting up mqtt instance and listener")
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
     def __receive__(self, client, userdata, message):
45
     def __receive__(self, client, userdata, message):
37
         try:
46
         try:
38
             payload = json.loads(message.payload)
47
             payload = json.loads(message.payload)
41
         #
50
         #
42
         remote = message.topic.split("/")[-2]
51
         remote = message.topic.split("/")[-2]
43
         command = message.topic.split("/")[-1]
52
         command = message.topic.split("/")[-1]
53
+        #
44
         if payload is None:
54
         if payload is None:
45
             if self.__start_data__ is not None:
55
             if self.__start_data__ is not None:
46
                 self.__send_stop__(*self.__start_data__)
56
                 self.__send_stop__(*self.__start_data__)
62
     def __send_once__(self, remote, command):
72
     def __send_once__(self, remote, command):
63
         try:
73
         try:
64
             self.__lirc_client__.send_once(remote, command)
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
     def __send_start__(self, remote, command):
80
     def __send_start__(self, remote, command):
70
         try:
81
         try:
71
             self.__lirc_client__.send_start(remote, command)
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
     def __send_stop__(self, remote, command):
88
     def __send_stop__(self, remote, command):
77
         try:
89
         try:
78
             self.__lirc_client__.send_stop(remote, command)
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
 if __name__ == '__main__': 
101
 if __name__ == '__main__': 
84
     report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
102
     report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)

Loading…
Cancel
Save