Initial functional commit
This commit is contained in:
parent
8fa9a65610
commit
e7ec8ceed1
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,6 @@
|
||||
config.py
|
||||
messages.*
|
||||
|
||||
# ---> Linux
|
||||
*~
|
||||
|
||||
|
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
[submodule "report"]
|
||||
path = report
|
||||
url = https://git.mount-mockery.de/pylib/report.git
|
79
amplifier.py
Normal file
79
amplifier.py
Normal file
@ -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.)
|
23
config_example/config.py
Normal file
23
config_example/config.py
Normal file
@ -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
|
||||
|
||||
#####################################################################
|
1
report
Submodule
1
report
Submodule
@ -0,0 +1 @@
|
||||
Subproject commit 25889f225b3593d515e37bebffef21458c961f64
|
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
kodi-json
|
Loading…
x
Reference in New Issue
Block a user