Skip to content
Snippets Groups Projects
Commit a9391c02 authored by KERDREUX Jerome's avatar KERDREUX Jerome
Browse files

Added cleaner MQTT display

parent 527b323e
Branches
No related tags found
No related merge requests found
package main
import (
"log/slog"
"math"
"gitlab.imt-atlantique.fr/xaal/code/go/core/schemas"
"gitlab.imt-atlantique.fr/xaal/code/go/core/uuid"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
......@@ -50,3 +53,40 @@ func newDevices(gw *gateway, bDevice *BridgeDevice) {
}
}
}
func updateXAALDevice(bDevice *BridgeDevice, payload map[string]interface{}) {
slog.Info("Updating device:", "name", bDevice.FriendlyName)
for _, dev := range bDevice.XAALDevice {
for key, value := range payload {
if key == "contact" && dev.DevType == "contact.basic" {
switch value {
case true:
dev.GetAttribute("detected").SetValue(false)
case false:
dev.GetAttribute("detected").SetValue(true)
}
}
if key == "battery" && dev.DevType == "battery.basic" {
dev.GetAttribute("level").SetValue(value)
}
if key == "linkquality" && dev.DevType == "linkquality.basic" {
value = math.Round(value.(float64) / 255 * 100)
dev.GetAttribute("level").SetValue(value)
}
if key == "temperature" && dev.DevType == "thermometer.basic" {
dev.GetAttribute("temperature").SetValue(value)
}
if key == "power" && dev.DevType == "powermeter.basic" {
dev.GetAttribute("power").SetValue(value)
}
if key == "state" && dev.DevType == "powerrelay.basic" {
switch value {
case "ON":
dev.GetAttribute("power").SetValue(true)
case "OFF":
dev.GetAttribute("power").SetValue(false)
}
}
}
}
}
......@@ -4,11 +4,11 @@ import (
"encoding/json"
"fmt"
"log/slog"
"math"
"slices"
"sort"
MQTT "github.com/eclipse/paho.mqtt.golang"
"github.com/jedib0t/go-pretty/v6/table"
"gitlab.imt-atlantique.fr/xaal/code/go/core/xaal"
)
......@@ -107,28 +107,6 @@ func parseDeviceJSON(jsonData []byte) {
}
}
func updateXAALDevice(bDevice *BridgeDevice, payload map[string]interface{}) {
slog.Info("Updating device:", "name", bDevice.FriendlyName)
for _, dev := range bDevice.XAALDevice {
for key, value := range payload {
if key == "contact" && dev.DevType == "contact.basic" {
dev.GetAttribute("detected").SetValue(value)
}
if key == "battery" && dev.DevType == "battery.basic" {
dev.GetAttribute("level").SetValue(value)
}
if key == "linkquality" && dev.DevType == "linkquality.basic" {
value = math.Round(value.(float64) / 255 * 100)
dev.GetAttribute("level").SetValue(value)
}
if key == "temperature" && dev.DevType == "thermometer.basic" {
dev.GetAttribute("temperature").SetValue(value)
}
}
}
}
func deviceHandler(device *BridgeDevice, msg MQTT.Message) {
var data map[string]interface{}
err := json.Unmarshal(msg.Payload(), &data)
......@@ -151,11 +129,14 @@ func dumpMessage(msg MQTT.Message) {
keys = append(keys, key)
}
sort.Strings(keys)
tab := table.NewWriter()
tab.SetTitle("MQTT update")
tab.SetStyle(table.StyleRounded)
for _, key := range keys {
fmt.Printf("%s => \t%v\n", key, data[key])
tab.AppendRow(table.Row{key, data[key]})
}
fmt.Printf("------------------\n\n")
fmt.Println(tab.Render())
}
func publishHander(client MQTT.Client, msg MQTT.Message) {
......@@ -182,7 +163,6 @@ func publishHander(client MQTT.Client, msg MQTT.Message) {
deviceHandler(dev, msg)
dumpMessage(msg)
}
fmt.Printf("------------------\n\n")
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment