Bläddra i källkod

Initial functional commit

master
Dirk Alders 3 år sedan
förälder
incheckning
e7ec8ceed1
7 ändrade filer med 111 tillägg och 0 borttagningar
  1. 3
    0
      .gitignore
  2. 3
    0
      .gitmodules
  3. 1
    0
      activate
  4. 79
    0
      amplifier.py
  5. 23
    0
      config_example/config.py
  6. 1
    0
      report
  7. 1
    0
      requirements.txt

+ 3
- 0
.gitignore Visa fil

@@ -1,3 +1,6 @@
1
+config.py
2
+messages.*
3
+
1 4
 # ---> Linux
2 5
 *~
3 6
 

+ 3
- 0
.gitmodules Visa fil

@@ -0,0 +1,3 @@
1
+[submodule "report"]
2
+	path = report
3
+	url = https://git.mount-mockery.de/pylib/report.git

+ 1
- 0
activate Visa fil

@@ -0,0 +1 @@
1
+venv/bin/activate

+ 79
- 0
amplifier.py Visa fil

@@ -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.)

+ 23
- 0
config_example/config.py Visa fil

@@ -0,0 +1,23 @@
1
+import report
2
+
3
+APP_NAME = 'amplifier'
4
+
5
+#####################################################################
6
+
7
+METHOD_MONITOR = 'monitor'
8
+METHOD_KODI = 'kodi'
9
+METHOD_BT_AUDIO = 'bt_audio'
10
+
11
+METHOD_ACTIVE = [METHOD_BT_AUDIO, ]
12
+
13
+#####################################################################
14
+
15
+LOGTARGET = 'logfile'   # possible choices are: 'logfile' or 'stdout'
16
+LOGLVL = 'DEBUG'
17
+
18
+LOGHOST = '192.168.0.100'
19
+LOGPORT = 19996
20
+
21
+formatter = report.SHORT_FMT
22
+
23
+#####################################################################

+ 1
- 0
report

@@ -0,0 +1 @@
1
+Subproject commit 25889f225b3593d515e37bebffef21458c961f64

+ 1
- 0
requirements.txt Visa fil

@@ -0,0 +1 @@
1
+kodi-json

Laddar…
Avbryt
Spara