Squashed commit of the following:
continuous-integration/drone/push Build is passing

commit 62e7cb88f4
Author: Gisle Aune <dev@gisle.me>
Date:   Mon Feb 28 22:52:06 2022 +0100

    refactor color system to be separate from models, also move errors out of models.

commit df865574bf
Author: Gisle Aune <dev@gisle.me>
Date:   Mon Feb 21 19:27:42 2022 +0100

    rework color system and make it work with drivers.
This commit is contained in:
2022-02-28 22:52:25 +01:00
parent d5cf30ea4b
commit f1ed6da6d7
36 changed files with 681 additions and 376 deletions
+11 -10
View File
@@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"git.aiterp.net/lucifer/new-server/internal/color"
"git.aiterp.net/lucifer/new-server/models"
"golang.org/x/sync/errgroup"
"log"
@@ -269,7 +270,7 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
lightsOut := light.Power != nil && !device.State.Power
if !lightsOut {
if light.ColorTemperature != nil && device.State.Color.IsKelvin() {
mirek := 1000000 / device.State.Color.Kelvin
mirek := 1000000 / *device.State.Color.K
if mirek < light.ColorTemperature.MirekSchema.MirekMinimum {
mirek = light.ColorTemperature.MirekSchema.MirekMinimum
}
@@ -280,8 +281,8 @@ func (b *Bridge) MakeCongruent(ctx context.Context) (int, error) {
update.Mirek = &mirek
changed = true
}
} else if xy, ok := device.State.Color.ToXY(); ok && light.Color != nil {
xy = light.Color.Gamut.Conform(xy).Round()
} else if xyColor, ok := device.State.Color.ToXY(); ok && light.Color != nil {
xy := light.Color.Gamut.Conform(*xyColor.XY).Round()
if xy.DistanceTo(light.Color.XY) > 0.0009 || (light.ColorTemperature != nil && light.ColorTemperature.Mirek != nil) {
update.ColorXY = &xy
changed = true
@@ -419,9 +420,7 @@ func (b *Bridge) GenerateDevices() []models.Device {
}
if light.ColorTemperature != nil {
if light.ColorTemperature.Mirek != nil {
device.State.Color = models.ColorValue{
Kelvin: int(1000000 / *light.ColorTemperature.Mirek),
}
device.State.Color.SetK(1000000 / *light.ColorTemperature.Mirek)
}
device.Capabilities = append(device.Capabilities, models.DCColorKelvin)
device.DriverProperties["maxTemperature"] = 1000000 / light.ColorTemperature.MirekSchema.MirekMinimum
@@ -429,7 +428,7 @@ func (b *Bridge) GenerateDevices() []models.Device {
}
if light.Color != nil {
if device.State.Color.IsEmpty() {
device.State.Color = models.ColorValue{
device.State.Color = color.Color{
XY: &light.Color.XY,
}
}
@@ -530,9 +529,11 @@ func (b *Bridge) applyPatches(patches []ResourceData) {
resCopy.Color = &cp
resCopy.Color.XY = patch.Color.XY
cp2 := *resCopy.ColorTemperature
resCopy.ColorTemperature = &cp2
resCopy.ColorTemperature.Mirek = nil
if resCopy.ColorTemperature != nil {
cp2 := *resCopy.ColorTemperature
resCopy.ColorTemperature = &cp2
resCopy.ColorTemperature.Mirek = nil
}
}
if patch.ColorTemperature != nil && resCopy.ColorTemperature != nil {
cp := *resCopy.ColorTemperature
+2 -2
View File
@@ -8,7 +8,7 @@ import (
"encoding/json"
"errors"
"fmt"
"git.aiterp.net/lucifer/new-server/models"
"git.aiterp.net/lucifer/new-server/internal/lerrors"
"io"
"log"
"net"
@@ -52,7 +52,7 @@ func (c *Client) Register(ctx context.Context) (string, error) {
}
}
if result[0].Success == nil {
return "", models.ErrUnexpectedResponse
return "", lerrors.ErrUnexpectedResponse
}
c.token = result[0].Success.Username
+5 -5
View File
@@ -4,7 +4,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"git.aiterp.net/lucifer/new-server/models"
"git.aiterp.net/lucifer/new-server/internal/color"
"strings"
"time"
)
@@ -152,9 +152,9 @@ type LightDimming struct {
}
type LightColor struct {
Gamut models.ColorGamut `json:"gamut"`
GamutType string `json:"gamut_type"`
XY models.ColorXY `json:"xy"`
Gamut color.Gamut `json:"gamut"`
GamutType string `json:"gamut_type"`
XY color.XY `json:"xy"`
}
type LightCT struct {
@@ -182,7 +182,7 @@ type LightAlert struct {
type ResourceUpdate struct {
Name *string
Power *bool
ColorXY *models.ColorXY
ColorXY *color.XY
Brightness *float64
Mirek *int
TransitionDuration *time.Duration
+2 -1
View File
@@ -5,6 +5,7 @@ import (
"encoding/json"
"encoding/xml"
"fmt"
"git.aiterp.net/lucifer/new-server/internal/lerrors"
"git.aiterp.net/lucifer/new-server/models"
"net/http"
"sync"
@@ -19,7 +20,7 @@ type Driver struct {
func (d *Driver) SearchBridge(ctx context.Context, address, token string, dryRun bool) ([]models.Bridge, error) {
if address == "" {
if !dryRun {
return nil, models.ErrAddressOnlyDryRunnable
return nil, lerrors.ErrAddressOnlyDryRunnable
}
res, err := http.Get("https://discovery.meethue.com")