12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- #!./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.)
|