From 52852118529c7f4a0d87d435cbf84009ebc9e611 Mon Sep 17 00:00:00 2001 From: legobas Date: Fri, 3 May 2024 15:08:17 +0200 Subject: [PATCH] handle empty message --- README.md | 2 +- mqtt.go | 6 +++++- rclone.go | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 964a575..ca4c464 100644 --- a/README.md +++ b/README.md @@ -73,7 +73,7 @@ MQTT topic MQTT message mqtt4rclone/options/get -{} + or {} mqtt4rclone/options/set {"main":{"LogLevel":"DEBUG"}} diff --git a/mqtt.go b/mqtt.go index 0621545..5edd41b 100644 --- a/mqtt.go +++ b/mqtt.go @@ -2,6 +2,7 @@ package main import ( "os" + "strings" "time" MQTT "github.com/eclipse/paho.mqtt.golang" @@ -31,7 +32,10 @@ func receive(client MQTT.Client, msg MQTT.Message) { log.Trace().Msgf("MQTT Message: %s", msg) if topic != STATUS_TOPIC && topic != RESPONSE_TOPIC { command := topic[len(APPNAME):] - json := string(msg.Payload()[:]) + json := strings.TrimSpace(string(msg.Payload()[:])) + if len(json) == 0 { + json = "{}" + } response, err := sendToRclone(command, json) if err != nil { diff --git a/rclone.go b/rclone.go index d2f0c4a..0b09ad1 100644 --- a/rclone.go +++ b/rclone.go @@ -36,8 +36,8 @@ func sendToRclone(command string, payload string) (string, error) { payload = string(msg) } - log.Debug().Msgf("url: %s", url) - log.Debug().Msgf("json: %s", payload) + log.Debug().Msgf("Rclone url: %s", url) + log.Debug().Msgf("Rclone json: %s", payload) req, err := http.NewRequest(http.MethodPost, url, strings.NewReader(payload)) if err != nil {