generated from dave/wails-template
Implement lamp api
This commit is contained in:
50
app.go
50
app.go
@@ -2,6 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"regexp"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v2/pkg/runtime"
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
||||||
)
|
)
|
||||||
@@ -24,3 +29,48 @@ func (a *App) startup(ctx context.Context) {
|
|||||||
func (a *App) Close() {
|
func (a *App) Close() {
|
||||||
runtime.Quit(a.ctx)
|
runtime.Quit(a.ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user