Usage of dbus for bt audio information and control
This commit is contained in:
parent
ae44e9495e
commit
8cf4e1da2c
@ -1,58 +1,92 @@
|
|||||||
import config
|
import config
|
||||||
|
import dbus
|
||||||
|
import dbus.mainloop.glib
|
||||||
|
from gi.repository import GLib
|
||||||
import logging
|
import logging
|
||||||
import mqtt
|
import mqtt
|
||||||
import report
|
import report
|
||||||
import select
|
import sys
|
||||||
from systemd import journal
|
|
||||||
import time
|
import time
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||||
except ImportError:
|
except ImportError:
|
||||||
ROOT_LOGGER_NAME = 'root'
|
ROOT_LOGGER_NAME = 'root'
|
||||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
||||||
|
|
||||||
|
player_state = False
|
||||||
|
current_title = ''
|
||||||
|
|
||||||
PLAY = "New A2DP transport"
|
def send_state_mqtt(state):
|
||||||
STOP = "Closing A2DP transport"
|
|
||||||
|
|
||||||
def send_state_msg_mqtt(mc, state):
|
|
||||||
topic = config.MQTT_TOPIC + "/state"
|
topic = config.MQTT_TOPIC + "/state"
|
||||||
logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
|
logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
|
||||||
mc.send(topic, "true" if state else "false")
|
mc.send(topic, "true" if state else "false")
|
||||||
|
|
||||||
def send_title_msg_mqtt(mc, title):
|
|
||||||
|
def send_title_mqtt(title):
|
||||||
topic = config.MQTT_TOPIC + "/title"
|
topic = config.MQTT_TOPIC + "/title"
|
||||||
logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
|
logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
|
||||||
mc.send(topic, title)
|
mc.send(topic, title)
|
||||||
|
|
||||||
|
|
||||||
|
def on_property_changed(interface, changed, invalidated):
|
||||||
|
global player_state
|
||||||
|
global current_title
|
||||||
|
#
|
||||||
|
if interface != 'org.bluez.MediaPlayer1':
|
||||||
|
return
|
||||||
|
for prop, value in changed.items():
|
||||||
|
if prop == 'Status':
|
||||||
|
logger.debug("BT-PLAYER status changed to %s", repr(value))
|
||||||
|
player_state = value == "playing"
|
||||||
|
send_state_mqtt(player_state)
|
||||||
|
send_title_mqtt(current_title if player_state else '')
|
||||||
|
elif prop == 'Track':
|
||||||
|
logger.debug("BT-PLAYER track information changed to %s", repr(value))
|
||||||
|
if 'Title' in value:
|
||||||
|
current_title = value.get('Title', '')
|
||||||
|
if player_state:
|
||||||
|
send_title_mqtt(current_title)
|
||||||
|
|
||||||
|
|
||||||
|
def on_mqtt_ctrl(client, userdata, message):
|
||||||
|
if message.topic == config.MQTT_TOPIC + "/PLAY":
|
||||||
|
player_iface.Play()
|
||||||
|
elif message.topic == config.MQTT_TOPIC + "/PAUSE":
|
||||||
|
player_iface.Pause()
|
||||||
|
elif message.topic == config.MQTT_TOPIC + "/TRACK_PREV":
|
||||||
|
player_iface.Previous()
|
||||||
|
elif message.topic == config.MQTT_TOPIC + "/TRACK_NEXT":
|
||||||
|
player_iface.Next()
|
||||||
|
else:
|
||||||
|
logger.info(message.topic)
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
|
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)
|
mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
||||||
|
mc.add_callback(config.MQTT_TOPIC + '/#', on_mqtt_ctrl)
|
||||||
j = journal.Reader()
|
#
|
||||||
j.log_level(journal.LOG_INFO)
|
send_state_mqtt(player_state)
|
||||||
j.add_match(_SYSTEMD_UNIT="bluealsa.service")
|
send_title_mqtt(current_title)
|
||||||
|
#
|
||||||
j.seek_tail()
|
|
||||||
j.get_next()
|
|
||||||
|
|
||||||
playing = False
|
|
||||||
send_state_msg_mqtt(mc, playing)
|
|
||||||
while True:
|
while True:
|
||||||
while j.get_next():
|
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
||||||
for entry in j:
|
bus = dbus.SystemBus()
|
||||||
if entry['MESSAGE'] != "":
|
obj = bus.get_object('org.bluez', "/")
|
||||||
if playing:
|
mgr = dbus.Interface(obj, 'org.freedesktop.DBus.ObjectManager')
|
||||||
if STOP in entry['MESSAGE']:
|
player_iface = None
|
||||||
playing = False
|
transport_prop_iface = None
|
||||||
send_state_msg_mqtt(mc, playing)
|
for path, ifaces in mgr.GetManagedObjects().items():
|
||||||
else:
|
if 'org.bluez.MediaPlayer1' in ifaces:
|
||||||
if PLAY in entry['MESSAGE']:
|
player_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.bluez.MediaPlayer1')
|
||||||
playing = True
|
elif 'org.bluez.MediaTransport1' in ifaces:
|
||||||
send_state_msg_mqtt(mc, playing)
|
transport_prop_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.freedesktop.DBus.Properties')
|
||||||
logger.debug(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])
|
if player_iface and transport_prop_iface:
|
||||||
|
break
|
||||||
time.sleep(0.1)
|
time.sleep(0.1)
|
||||||
|
|
||||||
|
bus.add_signal_receiver(on_property_changed, bus_name='org.bluez', signal_name='PropertiesChanged', dbus_interface='org.freedesktop.DBus.Properties')
|
||||||
|
GLib.MainLoop().run()
|
||||||
|
|
||||||
|
@ -1 +1,4 @@
|
|||||||
systemd-python
|
dbus-python
|
||||||
|
pycairo
|
||||||
|
PyGObject
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user