From e7ec8ceed166edd0d1ec04eb244fc8280ea5a85f Mon Sep 17 00:00:00 2001 From: Dirk Alders Date: Mon, 25 Jan 2021 18:59:50 +0100 Subject: [PATCH] Initial functional commit --- .gitignore | 3 ++ .gitmodules | 3 ++ activate | 1 + amplifier.py | 79 ++++++++++++++++++++++++++++++++++++++++ config_example/config.py | 23 ++++++++++++ report | 1 + requirements.txt | 1 + 7 files changed, 111 insertions(+) create mode 100644 .gitmodules create mode 120000 activate create mode 100644 amplifier.py create mode 100644 config_example/config.py create mode 160000 report create mode 100644 requirements.txt diff --git a/.gitignore b/.gitignore index f81017a..679598f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +config.py +messages.* + # ---> Linux *~ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..98f8bb5 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "report"] + path = report + url = https://git.mount-mockery.de/pylib/report.git diff --git a/activate b/activate new file mode 120000 index 0000000..9308d33 --- /dev/null +++ b/activate @@ -0,0 +1 @@ +venv/bin/activate \ No newline at end of file diff --git a/amplifier.py b/amplifier.py new file mode 100644 index 0000000..7bf750d --- /dev/null +++ b/amplifier.py @@ -0,0 +1,79 @@ +#!./venv/bin/python +import config +import subprocess +import time +from requests.exceptions import ConnectionError +from kodijson import Kodi # sudo pip install kodi-json +import logging +import os +import report + + +try: + from config import APP_NAME as ROOT_LOGGER_NAME +except ImportError: + ROOT_LOGGER_NAME = 'root' + +logger = logging.getLogger(ROOT_LOGGER_NAME) + + + +def monitor_state(): + mon_state = subprocess.check_output(['/usr/bin/tvservice', '-s']) + if mon_state.split(' ')[1] in ['0xa', '0x12000a', '0x40002']: + logger.debug('Connected Display detected') + return True + else: + logger.debug('No connected Display detected') + return False + + +def kodi_state(): + try: + kodi = Kodi("http://tv:8080/jsonrpc") + ap = kodi.Player.GetActivePlayers() + if len(ap['result']) > 0: + logger.debug('Active KODI players detected') + return True + else: + logger.debug('No active KODI players detected') + return False + except ConnectionError: # This is a dirty trick, if kodi is not yet ready to answer requests + return False + +def bt_audio_state(): + pac_data = subprocess.check_output(['pactl', 'list', 'short', 'sinks']) + rv = False + for line in pac_data.splitlines(): + if line.decode('utf-8').split('\t')[4] == 'RUNNING': + rv = True + if rv is True: + logger.debug('Running audio sink detected') + else: + logger.debug('No running audio sink detected') + return rv + + +if __name__ == '__main__': + report.appLoggingConfigure(os.path.dirname(__file__), config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT) + + prev_state = None + while True: + # second count + curr_state = False + if config.METHOD_MONITOR in config.METHOD_ACTIVE: + curr_state |= monitor_state() + if config.METHOD_KODI in config.METHOD_ACTIVE: + curr_state |= kodi_state() + if config.METHOD_BT_AUDIO in config.METHOD_ACTIVE: + curr_state |= bt_audio_state() + # check if there is a change in the screen state + if curr_state != prev_state: + if curr_state is True: + logger.info('Switching on Amplifier') + subprocess.check_output(['sudo', 'sispmctl', '-o', '1']) + else: + logger.info('Switching off Amplifier') + subprocess.check_output(['sudo', 'sispmctl', '-f', '1']) + prev_state = curr_state + time.sleep(1.) diff --git a/config_example/config.py b/config_example/config.py new file mode 100644 index 0000000..8cfe4c6 --- /dev/null +++ b/config_example/config.py @@ -0,0 +1,23 @@ +import report + +APP_NAME = 'amplifier' + +##################################################################### + +METHOD_MONITOR = 'monitor' +METHOD_KODI = 'kodi' +METHOD_BT_AUDIO = 'bt_audio' + +METHOD_ACTIVE = [METHOD_BT_AUDIO, ] + +##################################################################### + +LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout' +LOGLVL = 'DEBUG' + +LOGHOST = '192.168.0.100' +LOGPORT = 19996 + +formatter = report.SHORT_FMT + +##################################################################### diff --git a/report b/report new file mode 160000 index 0000000..25889f2 --- /dev/null +++ b/report @@ -0,0 +1 @@ +Subproject commit 25889f225b3593d515e37bebffef21458c961f64 diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..2b24f9a --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +kodi-json \ No newline at end of file