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