From 3bf8cbef2fe2b435d1a302f58182847e82a042db Mon Sep 17 00:00:00 2001 From: Stian Fredrik Aune Date: Sun, 12 Feb 2023 22:18:17 +0100 Subject: [PATCH] Mill option for local client --- internal/drivers/mill/bridge.go | 39 +++++++++++++++++++++++++++++++++ internal/drivers/mill/mill.go | 5 +++++ 2 files changed, 44 insertions(+) diff --git a/internal/drivers/mill/bridge.go b/internal/drivers/mill/bridge.go index 09b2de6..77288b7 100644 --- a/internal/drivers/mill/bridge.go +++ b/internal/drivers/mill/bridge.go @@ -6,6 +6,7 @@ import ( "crypto/rand" "crypto/sha1" "encoding/json" + "errors" "fmt" "git.aiterp.net/lucifer/new-server/internal/lerrors" "git.aiterp.net/lucifer/new-server/models" @@ -83,6 +84,10 @@ func (b *bridge) listDevices(ctx context.Context) ([]models.Device, error) { } func (b *bridge) pushStateChange(ctx context.Context, deviceModel models.Device) error { + if ip, ok := deviceModel.DriverProperties["ip"].(string); ok { + return b.pushWifiStateChange(ctx, ip, deviceModel.State.Temperature) + } + b.mu.Lock() if b.luciferMillIDMap == nil { b.luciferMillIDMap = make(map[int]int, 4) @@ -154,6 +159,40 @@ func (b *bridge) pushStateChange(ctx context.Context, deviceModel models.Device) return nil } +func (b *bridge) pushWifiStateChange(ctx context.Context, ip string, temperature int) error { + b.mu.Lock() + defer b.mu.Unlock() + + data, err := json.Marshal(wifiSetTemperatureBody{ + Type: "Normal", + Value: temperature, + }) + if err != nil { + return err + } + + req, err := http.NewRequestWithContext( + ctx, + "POST", + fmt.Sprintf("http://%s/set-temperature", ip), + bytes.NewReader(data), + ) + if err != nil { + return err + } + + res, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + + if res.StatusCode > 299 { + return errors.New("mill: negative response from " + ip) + } + + return nil +} + func (b *bridge) command(ctx context.Context, command string, payload interface{}, target interface{}) error { err := b.authenticate(ctx) if err != nil { diff --git a/internal/drivers/mill/mill.go b/internal/drivers/mill/mill.go index 81739d4..39545dc 100644 --- a/internal/drivers/mill/mill.go +++ b/internal/drivers/mill/mill.go @@ -73,6 +73,11 @@ type deviceControlGen3Body struct { HoldTemp int `json:"holdTemp,omitempty"` } +type wifiSetTemperatureBody struct { + Type string `json:"type"` + Value int `json:"value"` +} + var location *time.Location func init() {