ソースを参照

Initial bt-audio implementation

master
Dirk Alders 10ヶ月前
コミット
09d8dc6ccb

+ 3
- 0
.gitignore ファイルの表示

@@ -1,3 +1,6 @@
1
+# bt-audiopaser
2
+bt-audioparser/config.py
3
+
1 4
 # ---> Linux
2 5
 *~
3 6
 

+ 6
- 0
.gitmodules ファイルの表示

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

+ 3
- 0
bt-audioparser/README.md ファイルの表示

@@ -0,0 +1,3 @@
1
+# Prerequires
2
+- Make executing user member of group systemd-journal to be able to parse the journal
3
+- Install python3-dev, libsystemd-dev to be able to create venv

+ 56
- 0
bt-audioparser/bt-audioparser.py ファイルの表示

@@ -0,0 +1,56 @@
1
+import config
2
+import logging
3
+import mqtt
4
+import report
5
+import select
6
+from systemd import journal
7
+
8
+
9
+try:
10
+    from config import APP_NAME as ROOT_LOGGER_NAME
11
+except ImportError:
12
+    ROOT_LOGGER_NAME = 'root'
13
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
14
+
15
+
16
+PLAY = "New A2DP transport"
17
+STOP = "Closing A2DP transport"
18
+
19
+def send_state_msg_mqtt(mc, state):
20
+    topic = config.MQTT_TOPIC + "/state"
21
+    logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
22
+    mc.send(topic, "true" if state else "false")
23
+
24
+def send_title_msg_mqtt(mc, title):
25
+    topic = config.MQTT_TOPIC + "/title"
26
+    logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
27
+    mc.send(topic, title)
28
+
29
+
30
+if __name__ == "__main__":
31
+    report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
32
+
33
+    mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
34
+
35
+    j = journal.Reader()
36
+    j.log_level(journal.LOG_INFO)
37
+    j.add_match(_SYSTEMD_UNIT="bluealsa.service")
38
+
39
+    j.seek_tail()
40
+    j.get_next()
41
+
42
+    playing = False
43
+    send_state_msg_mqtt(mc, playing)
44
+    while True:
45
+        while j.get_next():
46
+            for entry in j:
47
+                if entry['MESSAGE'] != "":
48
+                    if playing:
49
+                        if STOP in entry['MESSAGE']:
50
+                            playing = False
51
+                            send_state_msg_mqtt(mc, playing)
52
+                    else:
53
+                        if PLAY in entry['MESSAGE']:
54
+                            playing = True
55
+                            send_state_msg_mqtt(mc, playing)
56
+                    logger.debug(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])

+ 5
- 0
bt-audioparser/bt-audioparser.sh ファイルの表示

@@ -0,0 +1,5 @@
1
+#!/bin/sh
2
+#
3
+BASEPATH=`dirname $0`
4
+$BASEPATH/venv/bin/python $BASEPATH/bt-audioparser.py
5
+

+ 22
- 0
bt-audioparser/config_example/config.py ファイルの表示

@@ -0,0 +1,22 @@
1
+#!/usr/bin/env python
2
+# -*- coding: UTF-8 -*-
3
+import os
4
+import report
5
+
6
+MQTT_USER = "<mqtt_smarthome_username>"
7
+MQTT_PASS = "<mqtt_smarthome_password>"
8
+MQTT_SERVER = "<mqtt_smarthome_hostname>"
9
+MQTT_TOPIC = "<mqtt_bt_audio_topic>"
10
+
11
+#
12
+# Logging
13
+#
14
+__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
15
+APP_NAME = "btaudio"
16
+LOGTARGET = 'stdout'   # possible choices are: 'logfile' or 'stdout'
17
+LOGLVL = 'INFO'
18
+
19
+LOGHOST = 'cutelog'
20
+LOGPORT = 19996
21
+
22
+formatter = report.SHORT_FMT

+ 8
- 0
bt-audioparser/init_venv ファイルの表示

@@ -0,0 +1,8 @@
1
+#!/bin/sh
2
+#
3
+BASEPATH=`realpath $(dirname $0)`
4
+
5
+python3 -m venv $BASEPATH/venv
6
+$BASEPATH/venv/bin/pip install --upgrade pip
7
+find $BASEPATH -name requirements.txt | xargs -L 1 $BASEPATH/venv/bin/pip install -r
8
+$BASEPATH/venv/bin/pip list --outdated --format=json | jq -r '.[] | .name'|xargs -n1 $BASEPATH/venv/bin/pip install -U

+ 1
- 0
bt-audioparser/mqtt

@@ -0,0 +1 @@
1
+Subproject commit 328d3471a748472695a61193becdda76c7eefe69

+ 1
- 0
bt-audioparser/report

@@ -0,0 +1 @@
1
+Subproject commit b53dd30eae1d679b7eec4999bec50aed55bc105b

+ 1
- 0
bt-audioparser/requirements.txt ファイルの表示

@@ -0,0 +1 @@
1
+systemd-python

+ 1
- 0
bt-receiver/bt-pins ファイルの表示

@@ -0,0 +1 @@
1
+* *

+ 23
- 0
bt-receiver/bt-receiver ファイルの表示

@@ -0,0 +1,23 @@
1
+#!/bin/sh -e
2
+#
3
+
4
+if [[ $UID != 0 ]]; then
5
+    echo "This script needs to be executes as root"
6
+    exit 13
7
+fi
8
+
9
+BASEPATH=$(dirname `readlink -f "$0"`)
10
+
11
+
12
+# Make BT discoverable:
13
+sudo bluetoothctl <<EOF
14
+power on
15
+agent off
16
+agent NoInputNoOutput
17
+default-agent
18
+discoverable on
19
+pairable on
20
+EOF
21
+
22
+# Start bt-agent
23
+sudo bt-agent --capability=DisplayOnly -p $BASEPATH/bt-pins

読み込み中…
キャンセル
保存