Compare commits

...

2 Commits

Author SHA1 Message Date
ee201df901 Exception handling for MQTT 2022-08-24 14:31:18 +01:00
4ee21b692f Systemctl service improved 2022-08-24 14:30:52 +01:00
3 changed files with 19 additions and 11 deletions

View File

@ -6,7 +6,8 @@ import sys
SERVICE_FILE = """ SERVICE_FILE = """
[Unit] [Unit]
Description=MPD Monitor Service Description=MPD Monitor Service
After=mosquitto.target After=network-online.target
Wants=network-online.target
[Service] [Service]
User=%(UID)d User=%(UID)d
Group=%(GID)d Group=%(GID)d

View File

@ -13,10 +13,10 @@ MQTT_TOPIC = "hifi/mpd"
# #
__BASEPATH__ = os.path.abspath(os.path.dirname(__file__)) __BASEPATH__ = os.path.abspath(os.path.dirname(__file__))
APP_NAME = "mpd" APP_NAME = "mpd"
LOGTARGET = 'logfile' # possible choices are: 'logfile' or 'stdout' LOGTARGET = 'stdout' # possible choices are: 'logfile' or 'stdout'
LOGLVL = 'DEBUG' LOGLVL = 'DEBUG'
LOGHOST = 'cutelog' LOGHOST = 'cutelog'
LOGPORT = 19996 LOGPORT = 19996
formatter = report.LONG_FMT formatter = report.SHORT_FMT

View File

@ -52,18 +52,25 @@ class mpc(object):
def send_state_msg_mqtt(state): def send_state_msg_mqtt(state):
client= paho.Client("mpd") client= paho.Client("mpd")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
client.connect(config.MQTT_SERVER, 1883) try:
topic = config.MQTT_TOPIC + "/state" client.connect(config.MQTT_SERVER, 1883)
logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state)) topic = config.MQTT_TOPIC + "/state"
client.publish(topic, "true" if state else "false") logger.info("Sending MPD status information to mqtt %s = %s", topic, str(state))
client.publish(topic, "true" if state else "false")
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending mpd state information")
def send_title_msg_mqtt(title): def send_title_msg_mqtt(title):
client= paho.Client("mpd") client= paho.Client("mpd")
client.username_pw_set(config.MQTT_USER, config.MQTT_PASS) client.username_pw_set(config.MQTT_USER, config.MQTT_PASS)
client.connect(config.MQTT_SERVER, 1883) try:
topic = config.MQTT_TOPIC + "/title" client.connect(config.MQTT_SERVER, 1883)
logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title) topic = config.MQTT_TOPIC + "/title"
client.publish(topic, title) logger.info("Sending MPD title information to mqtt %s = \"%s\"", topic, title)
client.publish(topic, title)
except (socket.timeout, OSError) as e:
logger.warning("Erro while sending mpd title information")
if __name__ == '__main__': if __name__ == '__main__':