2022-09-04 12:58:52 +01:00
|
|
|
import config
|
|
|
|
import json
|
|
|
|
import lirc
|
|
|
|
import logging
|
2022-09-12 13:11:28 +01:00
|
|
|
import paho.mqtt.client as paho
|
2022-09-04 12:58:52 +01:00
|
|
|
import report
|
|
|
|
import socket
|
|
|
|
import time
|
|
|
|
|
|
|
|
try:
|
|
|
|
from config import APP_NAME as ROOT_LOGGER_NAME
|
|
|
|
except ImportError:
|
|
|
|
ROOT_LOGGER_NAME = 'root'
|
|
|
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
|
|
|
|
|
|
|
|
|
|
|
class remote_control(object):
|
|
|
|
def __init__(self):
|
2022-09-12 13:11:28 +01:00
|
|
|
self.__lirc_client__ = None
|
|
|
|
self.__lirc_init__()
|
|
|
|
#
|
2022-09-06 07:48:55 +01:00
|
|
|
self.__start_data__ = None
|
|
|
|
#
|
2022-09-12 13:11:28 +01:00
|
|
|
self.__client__ = paho.Client(config.APP_NAME) # create client object
|
2022-09-04 12:58:52 +01:00
|
|
|
self.__client__.on_message = self.__receive__ # attach function to callback
|
|
|
|
self.__client__.username_pw_set(config.MQTT_USER, config.MQTT_PASS) # login with credentials
|
|
|
|
try:
|
|
|
|
self.__client__.connect(config.MQTT_SERVER, 1883) # establish connection
|
|
|
|
self.__client__.loop_start() # start the loop
|
|
|
|
for remote in config.SUPPORTED_REMOTES:
|
|
|
|
for command in config.SUPPORTED_REMOTES[remote]:
|
|
|
|
topic = config.MQTT_TOPIC + "/" + remote + "/" + command
|
|
|
|
logger.debug("Subscribing \"%s\"", topic)
|
|
|
|
self.__client__.subscribe(topic) # subscibe a topic
|
|
|
|
except (socket.timeout, OSError) as e:
|
|
|
|
logger.warning("Erro while setting up mqtt instance and listener")
|
|
|
|
|
2022-09-12 13:11:28 +01:00
|
|
|
def __lirc_init__(self):
|
|
|
|
if self.__lirc_client__ is not None:
|
|
|
|
logger.info("Closing crashed lirc client instance")
|
|
|
|
self.__lirc_client__.close()
|
|
|
|
logger.info("Initiating lirc connection")
|
|
|
|
self.__lirc_client__ = lirc.Client()
|
|
|
|
|
2022-09-04 12:58:52 +01:00
|
|
|
def __receive__(self, client, userdata, message):
|
|
|
|
try:
|
|
|
|
payload = json.loads(message.payload)
|
|
|
|
except json.decoder.JSONDecodeError:
|
|
|
|
payload = None
|
2022-09-06 07:48:55 +01:00
|
|
|
#
|
|
|
|
remote = message.topic.split("/")[-2]
|
|
|
|
command = message.topic.split("/")[-1]
|
2022-09-12 13:11:28 +01:00
|
|
|
#
|
2022-09-04 12:58:52 +01:00
|
|
|
if payload is None:
|
2022-09-06 07:48:55 +01:00
|
|
|
if self.__start_data__ is not None:
|
|
|
|
self.__send_stop__(*self.__start_data__)
|
|
|
|
self.__start_data__ = None
|
|
|
|
self.__send_once__(remote, command)
|
2022-09-04 12:58:52 +01:00
|
|
|
elif payload is True:
|
2022-09-06 07:48:55 +01:00
|
|
|
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)
|
2022-09-04 12:58:52 +01:00
|
|
|
elif payload is False:
|
2022-09-06 07:48:55 +01:00
|
|
|
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)
|
2022-09-12 13:11:28 +01:00
|
|
|
except (TimeoutError, socket.timeout) as e:
|
|
|
|
self.__lirc_timeout_error__()
|
|
|
|
else:
|
|
|
|
logger.info("Sending once: %s to %s.", command, remote)
|
2022-09-04 12:58:52 +01:00
|
|
|
|
2022-09-06 07:48:55 +01:00
|
|
|
def __send_start__(self, remote, command):
|
|
|
|
try:
|
|
|
|
self.__lirc_client__.send_start(remote, command)
|
2022-09-12 13:11:28 +01:00
|
|
|
except (TimeoutError, socket.timeout) as e:
|
|
|
|
self.__lirc_timeout_error__()
|
|
|
|
else:
|
|
|
|
logger.info("Sending start: %s to %s.", command, remote)
|
2022-09-06 07:48:55 +01:00
|
|
|
|
|
|
|
def __send_stop__(self, remote, command):
|
|
|
|
try:
|
|
|
|
self.__lirc_client__.send_stop(remote, command)
|
2022-09-12 13:11:28 +01:00
|
|
|
except (TimeoutError, socket.timeout) as e:
|
|
|
|
self.__lirc_timeout_error__()
|
|
|
|
else:
|
|
|
|
logger.info("Sending stop: %s to %s.", command, remote)
|
|
|
|
|
|
|
|
def __lirc_timeout_error__(self):
|
|
|
|
logger.error("Timeout-Error while sending IR-Command.")
|
|
|
|
self.__lirc_init__()
|
|
|
|
|
2022-09-04 12:58:52 +01:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
report.appLoggingConfigure(config.__BASEPATH__, config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
|
|
|
|
#
|
|
|
|
rc = remote_control()
|
|
|
|
#
|
|
|
|
while (True):
|
|
|
|
time.sleep(30)
|