|
@@ -0,0 +1,56 @@
|
|
1
|
+import config
|
|
2
|
+import logging
|
|
3
|
+import mqtt
|
|
4
|
+import report
|
|
5
|
+import select
|
|
6
|
+from systemd import journal
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+try:
|
|
10
|
+ from config import APP_NAME as ROOT_LOGGER_NAME
|
|
11
|
+except ImportError:
|
|
12
|
+ ROOT_LOGGER_NAME = 'root'
|
|
13
|
+logger = logging.getLogger(ROOT_LOGGER_NAME).getChild('main')
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+PLAY = "New A2DP transport"
|
|
17
|
+STOP = "Closing A2DP transport"
|
|
18
|
+
|
|
19
|
+def send_state_msg_mqtt(mc, state):
|
|
20
|
+ topic = config.MQTT_TOPIC + "/state"
|
|
21
|
+ logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, str(state))
|
|
22
|
+ mc.send(topic, "true" if state else "false")
|
|
23
|
+
|
|
24
|
+def send_title_msg_mqtt(mc, title):
|
|
25
|
+ topic = config.MQTT_TOPIC + "/title"
|
|
26
|
+ logger.info("Sending BT-Audio status information to mqtt %s = %s", topic, title)
|
|
27
|
+ mc.send(topic, title)
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+if __name__ == "__main__":
|
|
31
|
+ report.stdoutLoggingConfigure(((config.APP_NAME, config.LOGLVL), ), fmt=config.formatter)
|
|
32
|
+
|
|
33
|
+ mc = mqtt.mqtt_client(config.APP_NAME, config.MQTT_SERVER, 1883, config.MQTT_USER, config.MQTT_PASS)
|
|
34
|
+
|
|
35
|
+ j = journal.Reader()
|
|
36
|
+ j.log_level(journal.LOG_INFO)
|
|
37
|
+ j.add_match(_SYSTEMD_UNIT="bluealsa.service")
|
|
38
|
+
|
|
39
|
+ j.seek_tail()
|
|
40
|
+ j.get_next()
|
|
41
|
+
|
|
42
|
+ playing = False
|
|
43
|
+ send_state_msg_mqtt(mc, playing)
|
|
44
|
+ while True:
|
|
45
|
+ while j.get_next():
|
|
46
|
+ for entry in j:
|
|
47
|
+ if entry['MESSAGE'] != "":
|
|
48
|
+ if playing:
|
|
49
|
+ if STOP in entry['MESSAGE']:
|
|
50
|
+ playing = False
|
|
51
|
+ send_state_msg_mqtt(mc, playing)
|
|
52
|
+ else:
|
|
53
|
+ if PLAY in entry['MESSAGE']:
|
|
54
|
+ playing = True
|
|
55
|
+ send_state_msg_mqtt(mc, playing)
|
|
56
|
+ logger.debug(str(entry['__REALTIME_TIMESTAMP'] )+ ' ' + entry['MESSAGE'])
|