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
+26 -12
View File
@@ -5,12 +5,13 @@ import (
"context"
"encoding/json"
"fmt"
"git.aiterp.net/lucifer/new-server/internal/color"
"git.aiterp.net/lucifer/new-server/internal/lerrors"
"git.aiterp.net/lucifer/new-server/models"
"github.com/lucasb-eyer/go-colorful"
"io"
"io/ioutil"
"log"
"math"
"net"
"net/http"
"strconv"
@@ -31,11 +32,12 @@ type bridge struct {
func (b *bridge) Devices() []models.Device {
results := make([]models.Device, 0, len(b.panels))
for i, panel := range b.panels {
// Find normalized RGB and intensity
red := float64(panel.ColorRGBA[0]) / 255.0
green := float64(panel.ColorRGBA[1]) / 255.0
blue := float64(panel.ColorRGBA[2]) / 255.0
hue, sat, value := colorful.LinearRgb(red, green, blue).Hsv()
hue, sat, value := colorful.Color{R: red, G: green, B: blue}.Hsv()
rgb := colorful.Hsv(hue, sat, 1)
shapeType, shapeTypeOK := shapeTypeMap[panel.ShapeType]
if !shapeTypeOK {
@@ -70,10 +72,11 @@ func (b *bridge) Devices() []models.Device {
UserProperties: nil,
State: models.DeviceState{
Power: panel.ColorRGBA[3] == 0,
Color: models.ColorValue{
Hue: math.Mod(hue, 360),
Saturation: sat,
},
Color: color.Color{RGB: &color.RGB{
Red: rgb.R,
Green: rgb.G,
Blue: rgb.B,
}},
Intensity: value,
Temperature: 0,
},
@@ -139,9 +142,9 @@ func (b *bridge) Overview(ctx context.Context) (*Overview, error) {
switch res.StatusCode {
case 400, 403, 500, 503:
return nil, models.ErrUnexpectedResponse
return nil, lerrors.ErrUnexpectedResponse
case 401:
return nil, models.ErrIncorrectToken
return nil, lerrors.ErrIncorrectToken
}
overview := Overview{}
@@ -172,9 +175,20 @@ func (b *bridge) Update(devices []models.Device) {
for _, panel := range b.panels {
if panel.ID == uint16(id) {
if device.State.Power {
color := colorful.Hsv(device.State.Color.Hue, device.State.Color.Saturation, device.State.Intensity)
rgbColor, ok := device.State.Color.ToRGB()
if !ok {
newColor := [4]byte{255, 255, 255, 255}
if newColor != panel.ColorRGBA {
panel.update(newColor, time.Now().Add(time.Millisecond*220))
}
red, green, blue := color.RGB255()
continue
}
rgb := rgbColor.RGB.AtIntensity(device.State.Intensity)
red := byte(rgb.Red * 255.9)
green := byte(rgb.Green * 255.9)
blue := byte(rgb.Blue * 255.9)
newColor := [4]byte{red, green, blue, 255}
if newColor != panel.ColorRGBA {
panel.update(newColor, time.Now().Add(time.Millisecond*220))
@@ -321,7 +335,7 @@ func (b *bridge) updateEffect(ctx context.Context) error {
defer res.Body.Close()
if res.StatusCode != 204 {
return models.ErrUnexpectedResponse
return lerrors.ErrUnexpectedResponse
}
b.mu.Lock()
+3 -2
View File
@@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"git.aiterp.net/lucifer/new-server/internal/lerrors"
"git.aiterp.net/lucifer/new-server/models"
"net/http"
"sync"
@@ -30,7 +31,7 @@ func (d *Driver) SearchBridge(ctx context.Context, address, _ string, dryRun boo
return nil, err
}
if deviceInfo.ModelNumber == "" {
return nil, models.ErrUnexpectedResponse
return nil, lerrors.ErrUnexpectedResponse
}
token := ""
@@ -47,7 +48,7 @@ func (d *Driver) SearchBridge(ctx context.Context, address, _ string, dryRun boo
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, models.ErrBridgeSearchFailed
return nil, lerrors.ErrBridgeSearchFailed
}
tokenResponse := TokenResponse{}