summaryrefslogtreecommitdiff
path: root/tools/websocket.go
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2026-03-22 10:12:18 +0100
committerxengineering <me@xengineering.eu>2026-03-22 10:23:46 +0100
commit637c168aa164b0420c3f63fa818019b171ac92fc (patch)
treef6b30eeb31e33b21a88edbf24def969403dcd657 /tools/websocket.go
parentf752ba8296fa95f2e76977e4c5031af0ddecd501 (diff)
downloadsia-server-637c168aa164b0420c3f63fa818019b171ac92fc.tar
sia-server-637c168aa164b0420c3f63fa818019b171ac92fc.tar.zst
sia-server-637c168aa164b0420c3f63fa818019b171ac92fc.zip
tools: websocket: Log pretty-printed TX JSON
This allows to more easily see what is sent to the Shelly device.
Diffstat (limited to 'tools/websocket.go')
-rw-r--r--tools/websocket.go34
1 files changed, 33 insertions, 1 deletions
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")
}