From f23dfab3ded420180c6a9e1b4dfe2b95df3d3fcd Mon Sep 17 00:00:00 2001 From: jkerdreux-imt <jerome.kerdreux@imt-atlantique.fr> Date: Thu, 21 Nov 2024 13:29:17 +0100 Subject: [PATCH] Refactoring --- gateway.go | 2 +- mqtt.go | 20 +------------------- utils.go | 21 +++++++++++++++++++++ xaal.go | 1 + 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/gateway.go b/gateway.go index f44ce43..82ca332 100644 --- a/gateway.go +++ b/gateway.go @@ -74,7 +74,7 @@ func (gw *Gateway) MQTTHandler(client mqtt.Client, msg mqtt.Message) { gw.Z2MParseDevices(msg) } else { zDev := gw.GetZDeviceByTopic(msg.Topic()) - // mqttDumpMsg(msg) + // MQTTDumpMsg(msg) if zDev != nil { zDev.HandleMessage(msg) } diff --git a/mqtt.go b/mqtt.go index 557869a..3745509 100644 --- a/mqtt.go +++ b/mqtt.go @@ -4,10 +4,8 @@ import ( "encoding/json" "fmt" "log/slog" - "sort" mqtt "github.com/eclipse/paho.mqtt.golang" - "github.com/jedib0t/go-pretty/v6/table" ) // MQTTSetup creates a new MQTT client @@ -36,21 +34,5 @@ func MQTTDumpMsg(msg mqtt.Message) { if err != nil { slog.Error("Error decoding JSON %v", "topic", msg.Topic(), "err", err) } - - // sort keys - keys := make([]string, 0, len(data)) - for key := range data { - keys = append(keys, key) - } - sort.Strings(keys) - // dump keys - tab := table.NewWriter() - tab.SetTitle("MQTT update") - tab.SetStyle(table.StyleRounded) - for _, key := range keys { - if key != "update" { - tab.AppendRow(table.Row{key, data[key]}) - } - } - fmt.Println(tab.Render()) + jsonDump(data) } diff --git a/utils.go b/utils.go index 011a924..acc2eb5 100644 --- a/utils.go +++ b/utils.go @@ -3,8 +3,10 @@ package main import ( "fmt" "math" + "sort" "strconv" + "github.com/jedib0t/go-pretty/v6/table" "github.com/lucasb-eyer/go-colorful" ) @@ -22,6 +24,25 @@ func hexStringToInteger(hexString string) (uint64, error) { return integerValue, nil } +func jsonDump(data map[string]interface{}) { + // sort keys + keys := make([]string, 0) + for key := range data { + keys = append(keys, key) + } + sort.Strings(keys) + // dump keys + tab := table.NewWriter() + tab.SetTitle("MQTT update") + tab.SetStyle(table.StyleRounded) + for _, key := range keys { + if key != "update" { + tab.AppendRow(table.Row{key, data[key]}) + } + } + fmt.Println(tab.Render()) +} + // xyToColor converts CIE XY color coordinates and brightness to an RGB color using the go-colorful library. // https://github.com/Koenkk/zigbee2mqtt/issues/3497 func xyToColor(x, y, brightness float64) colorful.Color { diff --git a/xaal.go b/xaal.go index c45cf4d..7a5c914 100644 --- a/xaal.go +++ b/xaal.go @@ -503,4 +503,5 @@ func NewDebugDevice(addr uuid.UUID, zDev *Z2MDevice, exp *Expose) XAALDeviceInte func (dev *DebugDevice) update(payload map[string]interface{}) { slog.Info("Debug Device update", "payload", payload) + jsonDump(payload) } -- GitLab