Browse Source

it has the trappings of a driver now, but essential features are missing: discovery and forgetting

feature-colorvalue2
Gisle Aune 3 years ago
parent
commit
107b29620d
  1. 2
      app/config/driver.go
  2. 22
      cmd/xy/main.go
  3. 296
      internal/drivers/hue2/bridge.go
  4. 156
      internal/drivers/hue2/client.go
  5. 125
      internal/drivers/hue2/data.go
  6. 116
      internal/drivers/hue2/driver.go
  7. 1
      models/bridge.go
  8. 8
      models/colorxy.go

2
app/config/driver.go

@ -3,6 +3,7 @@ package config
import ( import (
"git.aiterp.net/lucifer/new-server/internal/drivers" "git.aiterp.net/lucifer/new-server/internal/drivers"
"git.aiterp.net/lucifer/new-server/internal/drivers/hue" "git.aiterp.net/lucifer/new-server/internal/drivers/hue"
"git.aiterp.net/lucifer/new-server/internal/drivers/hue2"
"git.aiterp.net/lucifer/new-server/internal/drivers/lifx" "git.aiterp.net/lucifer/new-server/internal/drivers/lifx"
"git.aiterp.net/lucifer/new-server/internal/drivers/mill" "git.aiterp.net/lucifer/new-server/internal/drivers/mill"
"git.aiterp.net/lucifer/new-server/internal/drivers/nanoleaf" "git.aiterp.net/lucifer/new-server/internal/drivers/nanoleaf"
@ -18,6 +19,7 @@ func DriverProvider() models.DriverProvider {
dp = drivers.DriverMap{ dp = drivers.DriverMap{
models.DTNanoLeaf: &nanoleaf.Driver{}, models.DTNanoLeaf: &nanoleaf.Driver{},
models.DTHue: &hue.Driver{}, models.DTHue: &hue.Driver{},
models.DTHue2: &hue2.Driver{},
models.DTLIFX: &lifx.Driver{}, models.DTLIFX: &lifx.Driver{},
models.DTMill: &mill.Driver{}, models.DTMill: &mill.Driver{},
} }

22
cmd/xy/main.go

@ -20,20 +20,30 @@ func main() {
j, _ := json.Marshal(bridge.GenerateDevices()) j, _ := json.Marshal(bridge.GenerateDevices())
fmt.Println(string(j)) fmt.Println(string(j))
for _, device := range bridge.GenerateDevices() {
ch := make(chan models.Event)
go func() {
for event := range ch {
log.Println("EVENT", event.Name, event.Payload)
}
}()
for i, dev := range bridge.GenerateDevices() {
device := dev
switch device.InternalID { switch device.InternalID {
case "6d5a45b0-ec69-4417-8588-717358b05086": case "6d5a45b0-ec69-4417-8588-717358b05086":
c, _ := models.ParseColorValue("xy:0.22,0.18") c, _ := models.ParseColorValue("xy:0.22,0.18")
device.State.Color = c device.State.Color = c
device.State.Intensity = 1
bridge.Update(device)
device.State.Intensity = 0.3
case "a71128f4-5295-4ae4-9fbc-5541abc8739b": case "a71128f4-5295-4ae4-9fbc-5541abc8739b":
c, _ := models.ParseColorValue("k:2000")
c, _ := models.ParseColorValue("k:6500")
device.State.Color = c device.State.Color = c
device.State.Intensity = 0.2 device.State.Intensity = 0.2
bridge.Update(device)
} }
device.ID = i + 1
bridge.Update(device)
} }
fmt.Println(bridge.MakeCongruent(context.Background()))
err = bridge.Run(context.Background(), ch)
log.Println(err)
} }

296
internal/drivers/hue2/bridge.go

@ -2,9 +2,11 @@ package hue2
import ( import (
"context" "context"
"errors"
"fmt" "fmt"
"git.aiterp.net/lucifer/new-server/models" "git.aiterp.net/lucifer/new-server/models"
"golang.org/x/sync/errgroup" "golang.org/x/sync/errgroup"
"log"
"math" "math"
"strings" "strings"
"sync" "sync"
@ -13,7 +15,9 @@ import (
type Bridge struct { type Bridge struct {
mu sync.Mutex mu sync.Mutex
externalID int
client *Client client *Client
needsUpdate chan struct{}
devices map[string]models.Device devices map[string]models.Device
resources map[string]*ResourceData resources map[string]*ResourceData
} }
@ -21,20 +25,209 @@ type Bridge struct {
func NewBridge(client *Client) *Bridge { func NewBridge(client *Client) *Bridge {
return &Bridge{ return &Bridge{
client: client, client: client,
needsUpdate: make(chan struct{}, 4),
devices: make(map[string]models.Device, 64), devices: make(map[string]models.Device, 64),
resources: make(map[string]*ResourceData, 256), resources: make(map[string]*ResourceData, 256),
} }
} }
func (b *Bridge) Run(ctx context.Context, eventCh chan<- models.Event) error {
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Connecting SSE...")
sse := b.client.SSE(ctx)
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Refreshing...")
err := b.RefreshAll(ctx)
if err != nil {
return err
}
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Running updates...")
updated, err := b.MakeCongruent(ctx)
if err != nil {
return err
}
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updated", updated, "hue services at startup.")
lightRefreshTimer := time.NewTicker(time.Second * 5)
defer lightRefreshTimer.Stop()
motionTicker := time.NewTicker(time.Second * 10)
defer motionTicker.Stop()
absences := make(map[int]time.Time)
lastPress := make(map[string]time.Time)
needFull := false
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-lightRefreshTimer.C:
if needFull {
err := b.RefreshAll(ctx)
if err != nil {
return err
}
} else {
err := b.Refresh(ctx, "light")
if err != nil {
return err
}
}
updated, err := b.MakeCongruent(ctx)
if err != nil {
return err
}
if updated > 0 {
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updated", updated, "hue services (regular check)")
}
case <-b.needsUpdate:
updated, err := b.MakeCongruent(ctx)
if err != nil {
return err
}
if updated > 0 {
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updated", updated, "hue services (publish)")
}
case <-motionTicker.C:
for id, absenceTime := range absences {
seconds := int(time.Since(absenceTime).Seconds())
if seconds < 10 || seconds%60 >= 10 {
continue
}
eventCh <- models.Event{
Name: models.ENSensorPresenceEnded,
Payload: map[string]string{
"deviceId": fmt.Sprint(id),
"minutesElapsed": fmt.Sprint(seconds / 60),
"secondsElapsed": fmt.Sprint(seconds),
"lastUpdated": fmt.Sprint(absenceTime.Unix()),
},
}
}
case data, ok := <-sse:
if !ok {
return errors.New("SSE Disconnected")
}
b.applyPatches(data.Data)
updated, err := b.MakeCongruent(ctx)
if err != nil {
return err
}
if updated > 0 {
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updated", updated, "hue services (SSE)")
}
for _, patch := range data.Data {
if patch.Owner == nil {
continue
}
if b.resources[patch.Owner.ID] == nil {
needFull = true
}
device, deviceOK := b.devices[patch.Owner.ID]
if !deviceOK || device.ID == 0 {
continue
}
if patch.Button != nil {
valid := false
if patch.Button.LastEvent == "initial_press" || patch.Button.LastEvent == "repeat" {
valid = true
} else if patch.Button.LastEvent == "long_release" {
valid = false
} else {
valid = lastPress[patch.ID].Unix() != data.CreationTime.Unix()
}
if valid {
lastPress[patch.ID] = data.CreationTime
b.mu.Lock()
owner := b.resources[patch.Owner.ID]
b.mu.Unlock()
if owner != nil {
index := owner.ServiceIndex("button", patch.ID)
if index != -1 {
eventCh <- models.Event{
Name: models.ENButtonPressed,
Payload: map[string]string{
"deviceId": fmt.Sprint(device.ID),
"hueButtonEvent": patch.Button.LastEvent,
"buttonIndex": fmt.Sprint(index),
"buttonName": device.ButtonNames[index],
},
}
}
}
}
}
if patch.Temperature != nil && patch.Temperature.Valid {
eventCh <- models.Event{
Name: models.ENSensorTemperature,
Payload: map[string]string{
"deviceId": fmt.Sprint(device.ID),
"deviceInternalId": patch.Owner.ID,
"temperature": fmt.Sprint(patch.Temperature.Temperature),
"lastUpdated": fmt.Sprint(data.CreationTime.Unix()),
},
}
}
if patch.Motion != nil && patch.Motion.Valid {
if patch.Motion.Motion {
eventCh <- models.Event{
Name: models.ENSensorPresenceStarted,
Payload: map[string]string{
"deviceId": fmt.Sprint(device.ID),
"deviceInternalId": patch.Owner.ID,
},
}
delete(absences, device.ID)
} else {
eventCh <- models.Event{
Name: models.ENSensorPresenceEnded,
Payload: map[string]string{
"deviceId": fmt.Sprint(device.ID),
"deviceInternalId": device.InternalID,
"minutesElapsed": "0",
"secondsElapsed": "0",
"lastUpdated": fmt.Sprint(data.CreationTime.Unix()),
},
}
absences[device.ID] = data.CreationTime
}
}
}
}
}
}
func (b *Bridge) Update(devices ...models.Device) { func (b *Bridge) Update(devices ...models.Device) {
b.mu.Lock() b.mu.Lock()
for _, device := range devices { for _, device := range devices {
b.devices[device.InternalID] = device b.devices[device.InternalID] = device
} }
b.mu.Unlock() b.mu.Unlock()
select {
case b.needsUpdate <- struct{}{}:
default:
}
} }
func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) { func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
// Eat the event if there's a pending update.
select {
case <-b.needsUpdate:
default:
}
b.mu.Lock() b.mu.Lock()
dur := time.Millisecond * 200 dur := time.Millisecond * 200
updates := make(map[string]ResourceUpdate) updates := make(map[string]ResourceUpdate)
@ -45,6 +238,8 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
update := ResourceUpdate{TransitionDuration: &dur} update := ResourceUpdate{TransitionDuration: &dur}
changed := false changed := false
lightsOut := light.Power != nil && !device.State.Power
if !lightsOut {
if light.ColorTemperature != nil && device.State.Color.IsKelvin() { if light.ColorTemperature != nil && device.State.Color.IsKelvin() {
mirek := 1000000 / device.State.Color.Kelvin mirek := 1000000 / device.State.Color.Kelvin
if mirek < light.ColorTemperature.MirekSchema.MirekMinimum { if mirek < light.ColorTemperature.MirekSchema.MirekMinimum {
@ -59,18 +254,24 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
} }
} else if xy, ok := device.State.Color.ToXY(); ok && light.Color != nil { } else if xy, ok := device.State.Color.ToXY(); ok && light.Color != nil {
xy = light.Color.Gamut.Conform(xy).Round() xy = light.Color.Gamut.Conform(xy).Round()
if xy.DistanceTo(light.Color.XY) > 0.00015 || (light.ColorTemperature != nil && light.ColorTemperature.Mirek != nil) {
if xy.DistanceTo(light.Color.XY) > 0.0009 || (light.ColorTemperature != nil && light.ColorTemperature.Mirek != nil) {
update.ColorXY = &xy update.ColorXY = &xy
changed = true changed = true
} }
} }
if light.Power != nil && light.Power.On != device.State.Power {
update.Power = &device.State.Power
if light.Dimming != nil && math.Abs(light.Dimming.Brightness/100-device.State.Intensity) > 0.02 {
brightness := math.Abs(math.Min(device.State.Intensity*100, 100))
update.Brightness = &brightness
changed = true changed = true
} }
if light.Dimming != nil && math.Abs(light.Dimming.Brightness/100-device.State.Intensity) > 0.005 {
}
if light.Power != nil && light.Power.On != device.State.Power {
update.Power = &device.State.Power
if device.State.Power {
brightness := math.Abs(math.Min(device.State.Intensity*100, 100)) brightness := math.Abs(math.Min(device.State.Intensity*100, 100))
update.Brightness = &brightness update.Brightness = &brightness
}
changed = true changed = true
} }
@ -85,6 +286,8 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
return 0, nil return 0, nil
} }
log.Println(fmt.Sprintf("[Bridge %d]", b.externalID), "Updating", len(updates), "services...")
eg, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
for key := range updates { for key := range updates {
update := updates[key] update := updates[key]
@ -95,7 +298,28 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
}) })
} }
return len(updates), eg.Wait()
err := eg.Wait()
if err != nil {
return len(updates), err
}
if len(updates) > 0 {
// Optimistically apply the updates to the states, so that the driver assumes they are set until
// proven otherwise by the SSE client.
b.mu.Lock()
newResources := make(map[string]*ResourceData, len(b.resources))
for key, value := range b.resources {
newResources[key] = value
}
for key, update := range updates {
id := strings.SplitN(key, "/", 2)[1]
newResources[id] = newResources[id].WithUpdate(update)
}
b.resources = newResources
b.mu.Unlock()
}
return len(updates), nil
} }
func (b *Bridge) GenerateDevices() []models.Device { func (b *Bridge) GenerateDevices() []models.Device {
@ -243,7 +467,6 @@ func (b *Bridge) RefreshAll(ctx context.Context) error {
} }
resources := make(map[string]*ResourceData, len(allResources)) resources := make(map[string]*ResourceData, len(allResources))
for i := range allResources { for i := range allResources {
resource := allResources[i] resource := allResources[i]
resources[resource.ID] = &resource resources[resource.ID] = &resource
@ -255,3 +478,64 @@ func (b *Bridge) RefreshAll(ctx context.Context) error {
return nil return nil
} }
func (b *Bridge) applyPatches(patches []ResourceData) {
b.mu.Lock()
newResources := make(map[string]*ResourceData, len(b.resources))
for key, value := range b.resources {
newResources[key] = value
}
b.mu.Unlock()
for _, patch := range patches {
if res := newResources[patch.ID]; res != nil {
resCopy := *res
if patch.Power != nil && resCopy.Power != nil {
cp := *resCopy.Power
resCopy.Power = &cp
resCopy.Power.On = patch.Power.On
}
if patch.Color != nil && resCopy.Color != nil {
cp := *resCopy.Color
resCopy.Color = &cp
resCopy.Color.XY = patch.Color.XY
cp2 := *resCopy.ColorTemperature
resCopy.ColorTemperature = &cp2
resCopy.ColorTemperature.Mirek = nil
}
if patch.ColorTemperature != nil && resCopy.ColorTemperature != nil {
cp := *resCopy.ColorTemperature
resCopy.ColorTemperature = &cp
resCopy.ColorTemperature.Mirek = patch.ColorTemperature.Mirek
}
if patch.Dimming != nil && resCopy.Dimming != nil {
cp := *resCopy.Dimming
resCopy.Dimming = &cp
resCopy.Dimming.Brightness = patch.Dimming.Brightness
}
if patch.Dynamics != nil {
resCopy.Dynamics = patch.Dynamics
}
if patch.Alert != nil {
resCopy.Alert = patch.Alert
}
if patch.PowerState != nil {
resCopy.PowerState = patch.PowerState
}
if patch.Temperature != nil {
resCopy.Temperature = patch.Temperature
}
if patch.Status != nil {
resCopy.Status = patch.Status
}
newResources[patch.ID] = &resCopy
}
}
b.mu.Lock()
b.resources = newResources
b.mu.Unlock()
}

156
internal/drivers/hue2/client.go

@ -1,12 +1,16 @@
package hue2 package hue2
import ( import (
"bufio"
"bytes" "bytes"
"context" "context"
"crypto/tls" "crypto/tls"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"git.aiterp.net/lucifer/new-server/models"
"io" "io"
"log"
"net" "net"
"net/http" "net/http"
"strings" "strings"
@ -32,6 +36,30 @@ type Client struct {
ch chan struct{} ch chan struct{}
} }
func (c *Client) Register(ctx context.Context) (string, error) {
result := make([]CreateUserResponse, 0, 1)
err := c.post(ctx, "api/", CreateUserInput{DeviceType: "git.aiterp.net/lucifer"}, &result)
if err != nil {
return "", err
}
if len(result) == 0 || result[0].Error != nil {
select {
case <-ctx.Done():
return "", ctx.Err()
case <-time.After(time.Second):
return c.Register(ctx)
}
}
if result[0].Success == nil {
return "", models.ErrUnexpectedResponse
}
c.token = result[0].Success.Username
return result[0].Success.Username, nil
}
func (c *Client) AllResources(ctx context.Context) ([]ResourceData, error) { func (c *Client) AllResources(ctx context.Context) ([]ResourceData, error) {
res := struct { res := struct {
Error interface{} Error interface{}
@ -64,7 +92,107 @@ func (c *Client) UpdateResource(ctx context.Context, link ResourceLink, update R
return c.put(ctx, link.Path(), update, nil) return c.put(ctx, link.Path(), update, nil)
} }
func (c *Client) SSE(ctx context.Context) <-chan SSEUpdate {
ch := make(chan SSEUpdate, 4)
go func() {
defer close(ch)
reader, err := c.getReader(ctx, "/eventstream/clip/v2", map[string]string{
"Accept": "text/event-stream",
})
if err != nil {
log.Println("SSE Connect error:", err)
return
}
defer reader.Close()
br := bufio.NewReader(reader)
for {
line, err := br.ReadString('\n')
if err != nil {
log.Println("SSE Read error:", err)
return
}
line = strings.Trim(line, "  \t\r\n")
kv := strings.SplitN(line, ": ", 2)
if len(kv) < 2 {
continue
}
switch kv[0] {
case "data":
var data []SSEUpdate
err := json.Unmarshal([]byte(kv[1]), &data)
if err != nil {
log.Println("Parsing SSE event failed:", err)
log.Println(" json:", kv[1])
return
}
for _, obj := range data {
ch <- obj
}
}
}
}()
return ch
}
func (c *Client) getReader(ctx context.Context, path string, headers map[string]string) (io.ReadCloser, error) {
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-c.ch:
defer func() {
c.ch <- struct{}{}
}()
}
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/%s", c.host, path), nil)
if err != nil {
return nil, err
}
for key, value := range headers {
req.Header.Set(key, value)
}
req.Header.Set("hue-application-key", c.token)
client := httpClient
if headers["Accept"] == "text/event-stream" {
client = sseClient
}
res, err := client.Do(req.WithContext(ctx))
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
_ = res.Body.Close()
return nil, errors.New(res.Status)
}
return res.Body, nil
}
func (c *Client) get(ctx context.Context, path string, target interface{}) error { func (c *Client) get(ctx context.Context, path string, target interface{}) error {
body, err := c.getReader(ctx, path, map[string]string{})
if err != nil {
return err
}
defer body.Close()
if target == nil {
return nil
}
return json.NewDecoder(body).Decode(&target)
}
func (c *Client) put(ctx context.Context, path string, body interface{}, target interface{}) error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
@ -74,7 +202,12 @@ func (c *Client) get(ctx context.Context, path string, target interface{}) error
}() }()
} }
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/%s", c.host, path), nil)
rb, err := reqBody(body)
if err != nil {
return err
}
req, err := http.NewRequest("PUT", fmt.Sprintf("https://%s/%s", c.host, path), rb)
if err != nil { if err != nil {
return err return err
} }
@ -94,7 +227,7 @@ func (c *Client) get(ctx context.Context, path string, target interface{}) error
return json.NewDecoder(res.Body).Decode(&target) return json.NewDecoder(res.Body).Decode(&target)
} }
func (c *Client) put(ctx context.Context, path string, body interface{}, target interface{}) error {
func (c *Client) post(ctx context.Context, path string, body interface{}, target interface{}) error {
select { select {
case <-ctx.Done(): case <-ctx.Done():
return ctx.Err() return ctx.Err()
@ -109,12 +242,14 @@ func (c *Client) put(ctx context.Context, path string, body interface{}, target
return err return err
} }
req, err := http.NewRequest("PUT", fmt.Sprintf("https://%s/%s", c.host, path), rb)
req, err := http.NewRequest("POST", fmt.Sprintf("https://%s/%s", c.host, path), rb)
if err != nil { if err != nil {
return err return err
} }
if c.token != "" {
req.Header.Set("hue-application-key", c.token) req.Header.Set("hue-application-key", c.token)
}
res, err := httpClient.Do(req.WithContext(ctx)) res, err := httpClient.Do(req.WithContext(ctx))
if err != nil { if err != nil {
@ -151,6 +286,21 @@ func reqBody(body interface{}) (io.Reader, error) {
} }
} }
var sseClient = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: (&net.Dialer{
Timeout: time.Hour * 1000000,
KeepAlive: 30 * time.Second,
}).DialContext,
MaxIdleConns: 5,
MaxIdleConnsPerHost: 1,
IdleConnTimeout: 0,
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: time.Hour * 1000000,
}
var httpClient = &http.Client{ var httpClient = &http.Client{
Transport: &http.Transport{ Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment, Proxy: http.ProxyFromEnvironment,

125
internal/drivers/hue2/data.go

@ -1,6 +1,7 @@
package hue2 package hue2
import ( import (
"encoding/xml"
"fmt" "fmt"
"git.aiterp.net/lucifer/new-server/models" "git.aiterp.net/lucifer/new-server/models"
"strings" "strings"
@ -31,6 +32,13 @@ type DeviceProductData struct {
SoftwareVersion string `json:"software_version"` SoftwareVersion string `json:"software_version"`
} }
type SSEUpdate struct {
CreationTime time.Time `json:"creationTime"`
ID string `json:"id"`
Type string `json:"type"`
Data []ResourceData `json:"data"`
}
type ResourceData struct { type ResourceData struct {
ID string `json:"id"` ID string `json:"id"`
LegacyID string `json:"id_v1"` LegacyID string `json:"id_v1"`
@ -42,6 +50,7 @@ type ResourceData struct {
Owner *ResourceLink `json:"owner"` Owner *ResourceLink `json:"owner"`
ProductData *DeviceProductData `json:"product_data"` ProductData *DeviceProductData `json:"product_data"`
Services []ResourceLink `json:"services"` Services []ResourceLink `json:"services"`
Button *SensorButton `json:"button"`
Power *LightPower `json:"on"` Power *LightPower `json:"on"`
Color *LightColor `json:"color"` Color *LightColor `json:"color"`
ColorTemperature *LightCT `json:"color_temperature"` ColorTemperature *LightCT `json:"color_temperature"`
@ -49,6 +58,8 @@ type ResourceData struct {
Dynamics *LightDynamics `json:"dynamics"` Dynamics *LightDynamics `json:"dynamics"`
Alert *LightAlert `json:"alert"` Alert *LightAlert `json:"alert"`
PowerState *PowerState `json:"power_state"` PowerState *PowerState `json:"power_state"`
Temperature *SensorTemperature `json:"temperature"`
Motion *SensorMotion `json:"motion"`
Status *string `json:"status"` Status *string `json:"status"`
} }
@ -62,6 +73,61 @@ func (res *ResourceData) ServiceID(kind string) *string {
return nil return nil
} }
func (res *ResourceData) ServiceIndex(kind string, id string) int {
idx := 0
for _, link := range res.Services {
if link.ID == id {
return idx
} else if link.Kind == kind {
idx += 1
}
}
return -1
}
func (res *ResourceData) WithUpdate(update ResourceUpdate) *ResourceData {
resCopy := *res
if update.Power != nil {
cp := *resCopy.Power
resCopy.Power = &cp
resCopy.Power.On = *update.Power
}
if update.ColorXY != nil {
cp := *resCopy.Color
resCopy.Color = &cp
resCopy.Color.XY = *update.ColorXY
}
if update.Mirek != nil {
cp := *resCopy.ColorTemperature
resCopy.ColorTemperature = &cp
mirek := *update.Mirek
resCopy.ColorTemperature.Mirek = &mirek
}
if update.Brightness != nil {
cp := *resCopy.Dimming
resCopy.Dimming = &cp
resCopy.Dimming.Brightness = *update.Brightness
}
return &resCopy
}
type SensorButton struct {
LastEvent string `json:"last_event"`
}
type SensorMotion struct {
Motion bool `json:"motion"`
Valid bool `json:"motion_valid"`
}
type SensorTemperature struct {
Temperature float64 `json:"temperature"`
Valid bool `json:"temperature_valid"`
}
type PowerState struct { type PowerState struct {
BatteryState string `json:"battery_state"` BatteryState string `json:"battery_state"`
BatteryLevel float64 `json:"battery_level"` BatteryLevel float64 `json:"battery_level"`
@ -129,8 +195,6 @@ func (r ResourceUpdate) MarshalJSON() ([]byte, error) {
chunks = append(chunks, fmt.Sprintf(`"dynamics":{"duration":%d}`, r.TransitionDuration.Truncate(time.Millisecond*100).Milliseconds())) chunks = append(chunks, fmt.Sprintf(`"dynamics":{"duration":%d}`, r.TransitionDuration.Truncate(time.Millisecond*100).Milliseconds()))
} }
fmt.Println(fmt.Sprintf("{%s}", strings.Join(chunks, ",")))
return []byte(fmt.Sprintf("{%s}", strings.Join(chunks, ","))), nil return []byte(fmt.Sprintf("{%s}", strings.Join(chunks, ","))), nil
} }
@ -142,3 +206,60 @@ type ResourceLink struct {
func (rl *ResourceLink) Path() string { func (rl *ResourceLink) Path() string {
return fmt.Sprintf("/clip/v2/resource/%s/%s", rl.Kind, rl.ID) return fmt.Sprintf("/clip/v2/resource/%s/%s", rl.Kind, rl.ID)
} }
type CreateUserInput struct {
DeviceType string `json:"devicetype"`
}
type CreateUserResponse struct {
Success *struct {
Username string `json:"username"`
} `json:"success"`
Error *struct {
Type int `json:"type"`
Address string `json:"address"`
Description string `json:"description"`
} `json:"error"`
}
type DiscoveryEntry struct {
Id string `json:"id"`
InternalIPAddress string `json:"internalipaddress"`
}
type BridgeDeviceInfo struct {
XMLName xml.Name `xml:"root"`
Text string `xml:",chardata"`
Xmlns string `xml:"xmlns,attr"`
SpecVersion struct {
Text string `xml:",chardata"`
Major string `xml:"major"`
Minor string `xml:"minor"`
} `xml:"specVersion"`
URLBase string `xml:"URLBase"`
Device struct {
Text string `xml:",chardata"`
DeviceType string `xml:"deviceType"`
FriendlyName string `xml:"friendlyName"`
Manufacturer string `xml:"manufacturer"`
ManufacturerURL string `xml:"manufacturerURL"`
ModelDescription string `xml:"modelDescription"`
ModelName string `xml:"modelName"`
ModelNumber string `xml:"modelNumber"`
ModelURL string `xml:"modelURL"`
SerialNumber string `xml:"serialNumber"`
UDN string `xml:"UDN"`
PresentationURL string `xml:"presentationURL"`
IconList struct {
Text string `xml:",chardata"`
Icon struct {
Text string `xml:",chardata"`
Mimetype string `xml:"mimetype"`
Height string `xml:"height"`
Width string `xml:"width"`
Depth string `xml:"depth"`
URL string `xml:"url"`
} `xml:"icon"`
} `xml:"iconList"`
} `xml:"device"`
}

116
internal/drivers/hue2/driver.go

@ -0,0 +1,116 @@
package hue2
import (
"context"
"encoding/json"
"encoding/xml"
"fmt"
"git.aiterp.net/lucifer/new-server/models"
"net/http"
"sync"
"time"
)
type Driver struct {
mu sync.Mutex
bridges []*Bridge
}
func (d *Driver) SearchBridge(ctx context.Context, address, token string, dryRun bool) ([]models.Bridge, error) {
if address == "" {
if !dryRun {
return nil, models.ErrAddressOnlyDryRunnable
}
res, err := http.Get("https://discovery.meethue.com")
if err != nil {
return nil, err
}
defer res.Body.Close()
entries := make([]DiscoveryEntry, 0, 8)
err = json.NewDecoder(res.Body).Decode(&entries)
if err != nil {
return nil, err
}
bridges := make([]models.Bridge, 0, len(entries))
for _, entry := range entries {
bridges = append(bridges, models.Bridge{
ID: -1,
Name: entry.Id,
Driver: models.DTHue2,
Address: entry.InternalIPAddress,
Token: "",
})
}
return bridges, nil
}
deviceInfo := BridgeDeviceInfo{}
res, err := http.Get(fmt.Sprintf("http://%s/description.xml", address))
if err != nil {
return nil, err
}
defer res.Body.Close()
err = xml.NewDecoder(res.Body).Decode(&deviceInfo)
if err != nil {
return nil, err
}
bridge := models.Bridge{
ID: -1,
Name: deviceInfo.Device.FriendlyName,
Driver: models.DTHue2,
Address: address,
Token: "",
}
if !dryRun {
client := NewClient(address, "")
timeout, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()
bridge.Token, err = client.Register(timeout)
if err != nil {
return nil, err
}
}
return []models.Bridge{bridge}, nil
}
func (d *Driver) SearchDevices(ctx context.Context, bridge models.Bridge, timeout time.Duration) ([]models.Device, error) {
return nil, nil
}
func (d *Driver) ListDevices(_ context.Context, bridge models.Bridge) ([]models.Device, error) {
return d.ensureBridge(bridge).GenerateDevices(), nil
}
func (d *Driver) Publish(_ context.Context, bridge models.Bridge, devices []models.Device) error {
d.ensureBridge(bridge).Update(devices...)
return nil
}
func (d *Driver) Run(ctx context.Context, bridge models.Bridge, ch chan<- models.Event) error {
return d.ensureBridge(bridge).Run(ctx, ch)
}
func (d *Driver) ensureBridge(info models.Bridge) *Bridge {
d.mu.Lock()
for _, bridge := range d.bridges {
if bridge.client.host == info.Address {
d.mu.Unlock()
return bridge
}
}
bridge := NewBridge(NewClient(info.Address, info.Token))
bridge.externalID = info.ID
d.bridges = append(d.bridges, bridge)
d.mu.Unlock()
return bridge
}

1
models/bridge.go

@ -21,6 +21,7 @@ type DriverKind string
var ( var (
DTHue DriverKind = "Hue" DTHue DriverKind = "Hue"
DTHue2 DriverKind = "Hue2"
DTNanoLeaf DriverKind = "Nanoleaf" DTNanoLeaf DriverKind = "Nanoleaf"
DTLIFX DriverKind = "LIFX" DTLIFX DriverKind = "LIFX"
DTMill DriverKind = "Mill" DTMill DriverKind = "Mill"

8
models/colorxy.go

@ -127,8 +127,8 @@ func (cg *ColorGamut) Conform(color ColorXY) ColorXY {
return color return color
} }
for x := best.X - 0.001; x < best.X+0.001; x += 0.0001 {
for y := best.Y - 0.001; y < best.Y+0.001; y += 0.0001 {
for x := best.X - 0.001; x < best.X+0.001; x += 0.0002 {
for y := best.Y - 0.001; y < best.Y+0.001; y += 0.0002 {
color2 := ColorXY{X: x, Y: y} color2 := ColorXY{X: x, Y: y}
if cg.atTheEdge(color2) { if cg.atTheEdge(color2) {
@ -139,8 +139,8 @@ func (cg *ColorGamut) Conform(color ColorXY) ColorXY {
} }
} }
for x := best.X - 0.0001; x < best.X+0.0001; x += 0.00001 {
for y := best.Y - 0.0001; y < best.Y+0.0001; y += 0.00001 {
for x := best.X - 0.0001; x < best.X+0.0001; x += 0.00003 {
for y := best.Y - 0.0001; y < best.Y+0.0001; y += 0.00003 {
color2 := ColorXY{X: x, Y: y} color2 := ColorXY{X: x, Y: y}
if cg.atTheEdge(color2) { if cg.atTheEdge(color2) {

Loading…
Cancel
Save