Start and Stop sending via IR implemented

This commit is contained in:
Dirk Alders 2022-09-06 07:48:55 +01:00
parent afc1509050
commit 1f591b6ca3

View File

@ -17,7 +17,8 @@ logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
class remote_control(object): class remote_control(object):
def __init__(self): def __init__(self):
self.__lirc_client__ = lirc.Client() self.__lirc_client__ = lirc.Client()
self.__start_data__ = None
#
self.__client__ = mqtt.Client(config.APP_NAME) # create client object self.__client__ = mqtt.Client(config.APP_NAME) # create client object
self.__client__.on_message = self.__receive__ # attach function to callback self.__client__.on_message = self.__receive__ # attach function to callback
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials 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) payload = json.loads(message.payload)
except json.decoder.JSONDecodeError: except json.decoder.JSONDecodeError:
payload = None payload = None
#
remote = message.topic.split("/")[-2]
command = message.topic.split("/")[-1]
if payload is None: if payload is None:
remote = message.topic.split("/")[-2] if self.__start_data__ is not None:
command = message.topic.split("/")[-1] self.__send_stop__(*self.__start_data__)
try: self.__start_data__ = None
self.__lirc_client__.send_once(remote, command) self.__send_once__(remote, command)
except TimeoutError:
logger.exception("Timeout-Error while sending IR-Command.")
logger.info("Sending once: %s to %s.", command, remote)
elif payload is True: 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: 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__': if __name__ == '__main__':
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT) report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)