Initial bt-audio implementation

This commit is contained in:
Dirk Alders 2024-02-11 10:34:59 +01:00
parent 71a05b8f98
commit 09d8dc6ccb
12 changed files with 130 additions and 0 deletions

3
.gitignore vendored
View File

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

6
.gitmodules vendored Normal file
View File

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

3
bt-audioparser/README.md Normal file
View File

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

View File

@ -0,0 +1,56 @@
import config
import logging
import mqtt
import report
import select
from systemd import journal
try:
from config import APP_NAME as ROOT_LOGGER_NAME
except ImportError:
ROOT_LOGGER_NAME = 'root'
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
PLAY = "New A2DP transport"
STOP = "Closing A2DP transport"
def send_state_msg_mqtt(mc, state):
topic = config.MQTT_TOPIC + "/state"
logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
mc.send(topic, "true" if state else "false")
def send_title_msg_mqtt(mc, title):
topic = config.MQTT_TOPIC + "/title"
logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
mc.send(topic, title)
if __name__ == "__main__":
report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
j = journal.Reader()
j.log_level(journal.LOG_INFO)
j.add_match(_SYSTEMD_UNIT="bluealsa.service")
j.seek_tail()
j.get_next()
playing = False
send_state_msg_mqtt(mc, playing)
while True:
while j.get_next():
for entry in j:
if entry['MESSAGE'] != "":
if playing:
if STOP in entry['MESSAGE']:
playing = False
send_state_msg_mqtt(mc, playing)
else:
if PLAY in entry['MESSAGE']:
playing = True
send_state_msg_mqtt(mc, playing)
logger.debug(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])

View File

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

View File

@ -0,0 +1,22 @@
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import report
MQTT_USER = "<mqtt_smarthome_username>"
MQTT_PASS = "<mqtt_smarthome_password>"
MQTT_SERVER = "<mqtt_smarthome_hostname>"
MQTT_TOPIC = "<mqtt_bt_audio_topic>"
#
# Logging
#
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
APP_NAME = "btaudio"
LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout'
LOGLVL = 'INFO'
LOGHOST = 'cutelog'
LOGPORT = 19996
formatter = report.SHORT_FMT

8
bt-audioparser/init_venv Executable file
View File

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

1
bt-audioparser/mqtt Submodule

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

1
bt-audioparser/report Submodule

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

View File

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

1
bt-receiver/bt-pins Normal file
View File

@ -0,0 +1 @@
* *

23
bt-receiver/bt-receiver Executable file
View File

@ -0,0 +1,23 @@
#!/bin/sh -e
#
if [[ $UID != 0 ]]; then
echo "This script needs to be executes as root"
exit 13
fi
BASEPATH=$(dirname `readlink -f "$0"`)
# Make BT discoverable:
sudo bluetoothctl <<EOF
power on
agent off
agent NoInputNoOutput
default-agent
discoverable on
pairable on
EOF
# Start bt-agent
sudo bt-agent --capability=DisplayOnly -p $BASEPATH/bt-pins