-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjukebox_mqtt_daemon.py
More file actions
executable file
·48 lines (39 loc) · 1.35 KB
/
jukebox_mqtt_daemon.py
File metadata and controls
executable file
·48 lines (39 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/python3
import logging
import paho.mqtt.client as mqtt
import sys
import yaml
from jukebox import Jukebox
from jukebox import load_yaml_config
if sys.version_info >= (3, 8):
from systemd.journal import JournalHandler
journald_handler = JournalHandler()
else:
from systemd.journal import JournaldLogHandler
journald_handler = JournaldLogHandler()
log = logging.getLogger(__name__)
log.addHandler(journald_handler)
log.setLevel(logging.DEBUG)
FILE_CONFIG_MQTT = 'configuration/mqtt.yaml'
mqtt_config = load_yaml_config(FILE_CONFIG_MQTT)
host = mqtt_config.get("host")
port = mqtt_config.get("port", 1883)
topic = mqtt_config.get("topic")
def on_message(client, userdata, message):
log.info('On message: %s.', message)
card = str(message.payload.decode("utf-8"))
jukebox = Jukebox()
jukebox.handle_card(card)
def on_connect(client, userdata, flags, rc):
log.info('Connected to MQTT Broker: %s.', host)
client.subscribe(topic)
if __name__ == "__main__":
if not host or not topic:
log.error("Missing MQTT broker configuration. Got: %s.", mqtt_config)
print("Please configure your MQTT broker properly.")
else:
mqttClient = mqtt.Client()
mqttClient.on_connect = on_connect
mqttClient.on_message = on_message
mqttClient.connect(host, port)
mqttClient.loop_forever()