From 637c168aa164b0420c3f63fa818019b171ac92fc Mon Sep 17 00:00:00 2001 From: xengineering Date: Sun, 22 Mar 2026 10:12:18 +0100 Subject: tools: websocket: Log pretty-printed TX JSON This allows to more easily see what is sent to the Shelly device. --- tools/websocket.go | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) (limited to 'tools/websocket.go') diff --git a/tools/websocket.go b/tools/websocket.go index c882ce2..b956ee8 100644 --- a/tools/websocket.go +++ b/tools/websocket.go @@ -9,10 +9,12 @@ package main import ( + "encoding/json" "log" "net/url" "os" "os/signal" + "strings" "syscall" "github.com/gorilla/websocket" @@ -83,8 +85,38 @@ func getConfig(c *websocket.Conn) { } } ` - err := c.WriteMessage(websocket.TextMessage, []byte(request)) + + tx(c, request) +} + +func tx(c *websocket.Conn, d string) { + input := []byte(d) + + var parsed any + + err := json.Unmarshal(input, &parsed) + if err != nil { + log.Fatal(err) + } + + pretty, err := json.MarshalIndent(parsed, "", " ") if err != nil { log.Fatal(err) } + + log.Println(quote(string(pretty), "> ")) + err = c.WriteMessage(websocket.TextMessage, pretty) + if err != nil { + log.Fatal(err) + } +} + +func quote(input string, quotation string) string { + lines := strings.Split(input, "\n") + + for i, line := range lines { + lines[i] = quotation + line + } + + return strings.Join(lines, "\n") } -- cgit v1.3