瀏覽代碼

Start and Stop sending via IR implemented

master
Dirk Alders 2 年之前
父節點
當前提交
1f591b6ca3
共有 1 個檔案被更改,包括 39 行新增10 行删除
  1. 39
    10
      remote_control.py

+ 39
- 10
remote_control.py 查看文件

@@ -17,7 +17,8 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
17 17
 class remote_control(object):
18 18
     def __init__(self):
19 19
         self.__lirc_client__ = lirc.Client()
20
-
20
+        self.__start_data__ = None
21
+        #
21 22
         self.__client__ = mqtt.Client(config.APP_NAME)                          # create client object
22 23
         self.__client__.on_message = self.__receive__                           # attach function to callback
23 24
         self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS)     # login with credentials
@@ -37,19 +38,47 @@ class remote_control(object):
37 38
             payload = json.loads(message.payload)
38 39
         except json.decoder.JSONDecodeError:
39 40
             payload = None
41
+        #
42
+        remote = message.topic.split("/")[-2]
43
+        command = message.topic.split("/")[-1]
40 44
         if payload is None:
41
-            remote = message.topic.split("/")[-2]
42
-            command = message.topic.split("/")[-1]
43
-            try:
44
-                self.__lirc_client__.send_once(remote, command)
45
-            except TimeoutError:
46
-                logger.exception("Timeout-Error while sending IR-Command.")
47
-            logger.info("Sending once: %s to %s.", command, remote)
45
+            if self.__start_data__ is not None:
46
+                self.__send_stop__(*self.__start_data__)
47
+                self.__start_data__ = None
48
+            self.__send_once__(remote, command)
48 49
         elif payload is True:
49
-            logger.warning("Start of remote command NYI!")
50
+            if self.__start_data__ is not None:
51
+                self.__send_stop__(*self.__start_data__)
52
+                self.__start_data__ = None
53
+            self.__start_data__ = remote, command
54
+            self.__send_start__(remote, command)
50 55
         elif payload is False:
51
-            logger.warning("Stop of remote command NYI!")
56
+            if self.__start_data__ is None:
57
+                self.__send_stop__(remote, command)
58
+            else:
59
+                self.__send_stop__(*self.__start_data__)
60
+            self.__start_data__ = None
61
+
62
+    def __send_once__(self, remote, command):
63
+        try:
64
+            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)
52 68
 
69
+    def __send_start__(self, remote, command):
70
+        try:
71
+            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)
75
+
76
+    def __send_stop__(self, remote, command):
77
+        try:
78
+            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)
53 82
 
54 83
 if __name__ == '__main__': 
55 84
     report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)

Loading…
取消
儲存