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 dbus
|
||||
import dbus.mainloop.glib
|
||||
from gi.repository import GLib
|
||||
import logging
|
||||
import mqtt
|
||||
import report
|
||||
import select
|
||||
from systemd import journal
|
||||
import sys
|
||||
import time
|
||||
|
||||
|
||||
try:
|
||||
from config import APP_NAME as ROOT_LOGGER_NAME
|
||||
except ImportError:
|
||||
ROOT_LOGGER_NAME = 'root'
|
||||
logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
||||
|
||||
player_state = False
|
||||
current_title = ''
|
||||
|
||||
PLAY = "New A2DP transport"
|
||||
STOP = "Closing A2DP transport"
|
||||
|
||||
def send_state_msg_mqtt(mc, state):
|
||||
def send_state_mqtt(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):
|
||||
|
||||
def send_title_mqtt(title):
|
||||
topic = config.MQTT_TOPIC + "/title"
|
||||
logger.info("Sending BT-Audio status information to mqtt %s = %s", 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__":
|
||||
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)
|
||||
mc.add_callback(config.MQTT_TOPIC + '/#', on_mqtt_ctrl)
|
||||
#
|
||||
send_state_mqtt(player_state)
|
||||
send_title_mqtt(current_title)
|
||||
#
|
||||
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'])
|
||||
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
|
||||
bus = dbus.SystemBus()
|
||||
obj = bus.get_object('org.bluez', "/")
|
||||
mgr = dbus.Interface(obj, 'org.freedesktop.DBus.ObjectManager')
|
||||
player_iface = None
|
||||
transport_prop_iface = None
|
||||
for path, ifaces in mgr.GetManagedObjects().items():
|
||||
if 'org.bluez.MediaPlayer1' in ifaces:
|
||||
player_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.bluez.MediaPlayer1')
|
||||
elif 'org.bluez.MediaTransport1' in ifaces:
|
||||
transport_prop_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.freedesktop.DBus.Properties')
|
||||
if player_iface and transport_prop_iface:
|
||||
break
|
||||
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