|
|
@ -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 { |
|
|
|