generated from dave/wails-template
77 lines
1.6 KiB
Go
77 lines
1.6 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/wailsapp/wails/v2/pkg/runtime"
|
|
)
|
|
|
|
// App struct
|
|
type App struct {
|
|
ctx context.Context
|
|
}
|
|
|
|
// NewApp creates a new App application struct
|
|
func NewApp() *App {
|
|
return &App{}
|
|
}
|
|
|
|
// startup is called when the app starts. The context is saved
|
|
// so we can call the runtime methods
|
|
func (a *App) startup(ctx context.Context) {
|
|
a.ctx = ctx
|
|
}
|
|
func (a *App) Close() {
|
|
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)
|
|
}
|