diff --git a/remote_control.py b/remote_control.py index 4c84c0f..856f20f 100644 --- a/remote_control.py +++ b/remote_control.py @@ -17,7 +17,8 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main') class remote_control(object): def __init__(self): self.__lirc_client__ = lirc.Client() - + self.__start_data__ = None + # self.__client__ = mqtt.Client(config.APP_NAME) # create client object self.__client__.on_message = self.__receive__ # attach function to callback self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials @@ -37,19 +38,47 @@ class remote_control(object): payload = json.loads(message.payload) except json.decoder.JSONDecodeError: payload = None + # + remote = message.topic.split("/")[-2] + command = message.topic.split("/")[-1] if payload is None: - remote = message.topic.split("/")[-2] - command = message.topic.split("/")[-1] - try: - self.__lirc_client__.send_once(remote, command) - except TimeoutError: - logger.exception("Timeout-Error while sending IR-Command.") - logger.info("Sending once: %s to %s.", command, remote) + if self.__start_data__ is not None: + self.__send_stop__(*self.__start_data__) + self.__start_data__ = None + self.__send_once__(remote, command) elif payload is True: - logger.warning("Start of remote command NYI!") + if self.__start_data__ is not None: + self.__send_stop__(*self.__start_data__) + self.__start_data__ = None + self.__start_data__ = remote, command + self.__send_start__(remote, command) elif payload is False: - logger.warning("Stop of remote command NYI!") + if self.__start_data__ is None: + self.__send_stop__(remote, command) + else: + self.__send_stop__(*self.__start_data__) + self.__start_data__ = None + def __send_once__(self, remote, command): + try: + self.__lirc_client__.send_once(remote, command) + except TimeoutError: + logger.exception("Timeout-Error while sending IR-Command.") + logger.info("Sending once: %s to %s.", command, remote) + + def __send_start__(self, remote, command): + try: + self.__lirc_client__.send_start(remote, command) + except TimeoutError: + logger.exception("Timeout-Error while sending IR-Command.") + logger.info("Sending start: %s to %s.", command, remote) + + def __send_stop__(self, remote, command): + try: + self.__lirc_client__.send_stop(remote, command) + except TimeoutError: + logger.exception("Timeout-Error while sending IR-Command.") + logger.info("Sending stop: %s to %s.", command, remote) if __name__ == '__main__': report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)