diff --git a/.gitignore b/.gitignore index 275f520..42c3649 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +# bt-audiopaser +bt-audioparser/config.py + # ---> Linux *~ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..3ff8922 --- /dev/null +++ b/.gitmodules @@ -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 diff --git a/bt-audioparser/README.md b/bt-audioparser/README.md new file mode 100644 index 0000000..61a4a8e --- /dev/null +++ b/bt-audioparser/README.md @@ -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 diff --git a/bt-audioparser/bt-audioparser.py b/bt-audioparser/bt-audioparser.py new file mode 100644 index 0000000..e95520f --- /dev/null +++ b/bt-audioparser/bt-audioparser.py @@ -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']) \ No newline at end of file diff --git a/bt-audioparser/bt-audioparser.sh b/bt-audioparser/bt-audioparser.sh new file mode 100755 index 0000000..6024482 --- /dev/null +++ b/bt-audioparser/bt-audioparser.sh @@ -0,0 +1,5 @@ +#!/bin/sh +# +BASEPATH=`dirname $0` +$BASEPATH/venv/bin/python $BASEPATH/bt-audioparser.py + diff --git a/bt-audioparser/config_example/config.py b/bt-audioparser/config_example/config.py new file mode 100644 index 0000000..fc71cb6 --- /dev/null +++ b/bt-audioparser/config_example/config.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +# -*- coding: UTF-8 -*- +import os +import report + +MQTT_USER = "" +MQTT_PASS = "" +MQTT_SERVER = "" +MQTT_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 diff --git a/bt-audioparser/init_venv b/bt-audioparser/init_venv new file mode 100755 index 0000000..428ff04 --- /dev/null +++ b/bt-audioparser/init_venv @@ -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 diff --git a/bt-audioparser/mqtt b/bt-audioparser/mqtt new file mode 160000 index 0000000..328d347 --- /dev/null +++ b/bt-audioparser/mqtt @@ -0,0 +1 @@ +Subproject commit 328d3471a748472695a61193becdda76c7eefe69 diff --git a/bt-audioparser/report b/bt-audioparser/report new file mode 160000 index 0000000..b53dd30 --- /dev/null +++ b/bt-audioparser/report @@ -0,0 +1 @@ +Subproject commit b53dd30eae1d679b7eec4999bec50aed55bc105b diff --git a/bt-audioparser/requirements.txt b/bt-audioparser/requirements.txt new file mode 100644 index 0000000..d844bee --- /dev/null +++ b/bt-audioparser/requirements.txt @@ -0,0 +1 @@ +systemd-python diff --git a/bt-receiver/bt-pins b/bt-receiver/bt-pins new file mode 100644 index 0000000..0400b2d --- /dev/null +++ b/bt-receiver/bt-pins @@ -0,0 +1 @@ +* * diff --git a/bt-receiver/bt-receiver b/bt-receiver/bt-receiver new file mode 100755 index 0000000..ab00587 --- /dev/null +++ b/bt-receiver/bt-receiver @@ -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 <