From 32e3c7309835db302ef889bce780a64a3e57dcc1 Mon Sep 17 00:00:00 2001 From: PhatPhuckDave Date: Sun, 29 Dec 2024 00:22:55 +0100 Subject: [PATCH] Implement lamp api --- app.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/app.go b/app.go index 2c9cfee..9dfabf9 100644 --- a/app.go +++ b/app.go @@ -2,6 +2,11 @@ package main import ( "context" + "io" + "net/http" + "regexp" + "strconv" + "strings" "github.com/wailsapp/wails/v2/pkg/runtime" ) @@ -23,4 +28,49 @@ func (a *App) startup(ctx context.Context) { } func (a *App) Close() { runtime.Quit(a.ctx) -} \ No newline at end of file +} + +const IP = "192.168.1.83:5554" +// I really have to get rid of this dumb format... +// Just do one lamp per line, what the fuck is this "Lamp 1: " bullshit +var decancer = regexp.MustCompile(`Lamp \d: `) + +func (a *App) GetLamps() (lamps []string) { + resp, err := http.Get("http://" + IP) + if err != nil { + runtime.LogError(a.ctx, err.Error()) + return + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + runtime.LogError(a.ctx, err.Error()) + return + } + + lines := strings.Split(string(body), "\n") + for _, line := range lines { + line = strings.TrimSpace(line) + if line == "" { + continue + } + line = decancer.ReplaceAllString(line, "") + lamps = append(lamps, line) + } + return +} + +func (a *App) SetLampBrightness(lampId int, brightness int) string { + resp, err := http.Get("http://" + IP + "/lamp/" + strconv.Itoa(lampId) + "?n=" + strconv.Itoa(brightness)) + if err != nil { + runtime.LogError(a.ctx, err.Error()) + return "" + } + defer resp.Body.Close() + body, err := io.ReadAll(resp.Body) + if err != nil { + runtime.LogError(a.ctx, err.Error()) + return "" + } + return string(body) +}