93 líneas
3.2 KiB
Python
93 líneas
3.2 KiB
Python
import config
|
|
import dbus
|
|
import dbus.mainloop.glib
|
|
from gi.repository import GLib
|
|
import logging
|
|
import mqtt
|
|
import report
|
|
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 = ''
|
|
|
|
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_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)
|
|
mc.add_callback(config.MQTT_TOPIC + '/#', on_mqtt_ctrl)
|
|
#
|
|
send_state_mqtt(player_state)
|
|
send_title_mqtt(current_title)
|
|
#
|
|
while True:
|
|
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()
|
|
|