Bladeren bron

Usage of dbus for bt audio information and control

master
Dirk Alders 10 maanden geleden
bovenliggende
commit
8cf4e1da2c
2 gewijzigde bestanden met toevoegingen van 68 en 31 verwijderingen
  1. 64
    30
      bt-audioparser/bt-audioparser.py
  2. 4
    1
      bt-audioparser/requirements.txt

+ 64
- 30
bt-audioparser/bt-audioparser.py Bestand weergeven

@@ -1,58 +1,92 @@
1 1
 import config
2
+import dbus
3
+import dbus.mainloop.glib
4
+from gi.repository import GLib
2 5
 import logging
3 6
 import mqtt
4 7
 import report
5
-import select
6
-from systemd import journal
8
+import sys
7 9
 import time
8 10
 
9
-
10 11
 try:
11 12
     from config import APP_NAME as ROOT_LOGGER_NAME
12 13
 except ImportError:
13 14
     ROOT_LOGGER_NAME = 'root'
14 15
 logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
15 16
 
17
+player_state = False
18
+current_title = ''
16 19
 
17
-PLAY = "New A2DP transport"
18
-STOP = "Closing A2DP transport"
19
-
20
-def send_state_msg_mqtt(mc, state):
20
+def send_state_mqtt(state):
21 21
     topic = config.MQTT_TOPIC + "/state"
22 22
     logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
23 23
     mc.send(topic, "true" if state else "false")
24 24
 
25
-def send_title_msg_mqtt(mc, title):
25
+
26
+def send_title_mqtt(title):
26 27
     topic = config.MQTT_TOPIC + "/title"
27 28
     logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
28 29
     mc.send(topic, title)
29 30
 
30 31
 
31
-if __name__ == "__main__":
32
-    report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
32
+def on_property_changed(interface, changed, invalidated):
33
+    global player_state
34
+    global current_title
35
+    #
36
+    if interface != 'org.bluez.MediaPlayer1':
37
+        return
38
+    for prop, value in changed.items():
39
+        if prop == 'Status':
40
+            logger.debug("BT-PLAYER status changed to %s", repr(value))
41
+            player_state = value == "playing"
42
+            send_state_mqtt(player_state)
43
+            send_title_mqtt(current_title if player_state else '')
44
+        elif prop == 'Track':
45
+            logger.debug("BT-PLAYER track information changed to %s", repr(value))
46
+            if 'Title' in value:
47
+                current_title = value.get('Title', '')
48
+                if player_state:
49
+                    send_title_mqtt(current_title)
33 50
 
34
-    mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
35 51
 
36
-    j = journal.Reader()
37
-    j.log_level(journal.LOG_INFO)
38
-    j.add_match(_SYSTEMD_UNIT="bluealsa.service")
52
+def on_mqtt_ctrl(client, userdata, message):
53
+    if message.topic == config.MQTT_TOPIC + "/PLAY":
54
+        player_iface.Play()
55
+    elif message.topic == config.MQTT_TOPIC + "/PAUSE":
56
+        player_iface.Pause()
57
+    elif message.topic == config.MQTT_TOPIC + "/TRACK_PREV":
58
+        player_iface.Previous()
59
+    elif message.topic == config.MQTT_TOPIC + "/TRACK_NEXT":
60
+        player_iface.Next()
61
+    else:
62
+        logger.info(message.topic)
39 63
 
40
-    j.seek_tail()
41
-    j.get_next()
42 64
 
43
-    playing = False
44
-    send_state_msg_mqtt(mc, playing)
65
+if __name__ == "__main__":
66
+    report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
67
+    #
68
+    mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
69
+    mc.add_callback(config.MQTT_TOPIC + '/#', on_mqtt_ctrl)
70
+    #
71
+    send_state_mqtt(player_state)
72
+    send_title_mqtt(current_title)
73
+    #
45 74
     while True:
46
-        while j.get_next():
47
-            for entry in j:
48
-                if entry['MESSAGE'] != "":
49
-                    if playing:
50
-                        if STOP in entry['MESSAGE']:
51
-                            playing = False
52
-                            send_state_msg_mqtt(mc, playing)
53
-                    else:
54
-                        if PLAY in entry['MESSAGE']:
55
-                            playing = True
56
-                            send_state_msg_mqtt(mc, playing)
57
-                    logger.debug(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])
75
+        dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
76
+        bus = dbus.SystemBus()
77
+        obj = bus.get_object('org.bluez', "/")
78
+        mgr = dbus.Interface(obj, 'org.freedesktop.DBus.ObjectManager')
79
+        player_iface = None
80
+        transport_prop_iface = None
81
+        for path, ifaces in mgr.GetManagedObjects().items():
82
+            if 'org.bluez.MediaPlayer1' in ifaces:
83
+                player_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.bluez.MediaPlayer1')
84
+            elif 'org.bluez.MediaTransport1' in ifaces:
85
+                transport_prop_iface = dbus.Interface(bus.get_object('org.bluez', path), 'org.freedesktop.DBus.Properties')
86
+        if player_iface and transport_prop_iface:
87
+            break
58 88
         time.sleep(0.1)
89
+
90
+    bus.add_signal_receiver(on_property_changed, bus_name='org.bluez', signal_name='PropertiesChanged', dbus_interface='org.freedesktop.DBus.Properties')
91
+    GLib.MainLoop().run()
92
+

+ 4
- 1
bt-audioparser/requirements.txt Bestand weergeven

@@ -1 +1,4 @@
1
-systemd-python
1
+dbus-python
2
+pycairo
3
+PyGObject
4
+

Laden…
Annuleren
Opslaan