bt-audio/bt-audioparser/bt-audioparser.py

56 wiersze
1.7 KiB
Python

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'])