Amplifier Control
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!./venv/bin/python
  2. import config
  3. import subprocess
  4. import time
  5. from requests.exceptions import ConnectionError
  6. from kodijson import Kodi # sudo pip install kodi-json
  7. import logging
  8. import os
  9. import report
  10. try:
  11. from config import APP_NAME as ROOT_LOGGER_NAME
  12. except ImportError:
  13. ROOT_LOGGER_NAME = 'root'
  14. logger = logging.getLogger(ROOT_LOGGER_NAME)
  15. def monitor_state():
  16. mon_state = subprocess.check_output(['/usr/bin/tvservice', '-s'])
  17. if mon_state.split(' ')[1] in ['0xa', '0x12000a', '0x40002']:
  18. logger.debug('Connected Display detected')
  19. return True
  20. else:
  21. logger.debug('No connected Display detected')
  22. return False
  23. def kodi_state():
  24. try:
  25. kodi = Kodi("http://tv:8080/jsonrpc")
  26. ap = kodi.Player.GetActivePlayers()
  27. if len(ap['result']) > 0:
  28. logger.debug('Active KODI players detected')
  29. return True
  30. else:
  31. logger.debug('No active KODI players detected')
  32. return False
  33. except ConnectionError: # This is a dirty trick, if kodi is not yet ready to answer requests
  34. return False
  35. def bt_audio_state():
  36. pac_data = subprocess.check_output(['pactl', 'list', 'short', 'sinks'])
  37. rv = False
  38. for line in pac_data.splitlines():
  39. if line.decode('utf-8').split('\t')[4] == 'RUNNING':
  40. rv = True
  41. if rv is True:
  42. logger.debug('Running audio sink detected')
  43. else:
  44. logger.debug('No running audio sink detected')
  45. return rv
  46. if __name__ == '__main__':
  47. report.appLoggingConfigure(os.path.dirname(__file__), config.LOGTARGET, ((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter, host=config.LOGHOST, port=config.LOGPORT)
  48. prev_state = None
  49. while True:
  50. # second count
  51. curr_state = False
  52. if config.METHOD_MONITOR in config.METHOD_ACTIVE:
  53. curr_state |= monitor_state()
  54. if config.METHOD_KODI in config.METHOD_ACTIVE:
  55. curr_state |= kodi_state()
  56. if config.METHOD_BT_AUDIO in config.METHOD_ACTIVE:
  57. curr_state |= bt_audio_state()
  58. # check if there is a change in the screen state
  59. if curr_state != prev_state:
  60. if curr_state is True:
  61. logger.info('Switching on Amplifier')
  62. subprocess.check_output(['sudo', 'sispmctl', '-o', '1'])
  63. else:
  64. logger.info('Switching off Amplifier')
  65. subprocess.check_output(['sudo', 'sispmctl', '-f', '1'])
  66. prev_state = curr_state
  67. time.sleep(1.)