refactor color system to be separate from models, also move errors out of models.
continuous-integration/drone/push Build is passing
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,6 +2,8 @@ package lifx
|
||||
|
||||
import (
|
||||
"context"
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"log"
|
||||
"sync"
|
||||
@@ -20,7 +22,7 @@ type Bridge struct {
|
||||
func (b *Bridge) StartSearch(ctx context.Context) error {
|
||||
c := b.getClient()
|
||||
if c == nil {
|
||||
return models.ErrBridgeRunningRequired
|
||||
return lerrors.ErrBridgeRunningRequired
|
||||
}
|
||||
|
||||
_, err := c.HorribleBroadcast(ctx, &GetService{})
|
||||
@@ -129,9 +131,9 @@ func (b *Bridge) Run(ctx context.Context, debug bool) error {
|
||||
|
||||
for {
|
||||
target, seq, payload, err := client.Recv(time.Millisecond * 200)
|
||||
if err == models.ErrInvalidPacketSize || err == models.ErrPayloadTooShort || err == models.ErrUnrecognizedPacketType {
|
||||
if err == lerrors.ErrInvalidPacketSize || err == lerrors.ErrPayloadTooShort || err == lerrors.ErrUnrecognizedPacketType {
|
||||
log.Println("LIFX udp socket received something weird:", err)
|
||||
} else if err != nil && err != models.ErrReadTimeout {
|
||||
} else if err != nil && err != lerrors.ErrReadTimeout {
|
||||
if ctx.Err() != nil {
|
||||
return ctx.Err()
|
||||
}
|
||||
@@ -168,8 +170,8 @@ func (b *Bridge) Run(ctx context.Context, debug bool) error {
|
||||
if state.deviceState == nil {
|
||||
state.deviceState = &models.DeviceState{
|
||||
Power: p.On,
|
||||
Color: models.ColorValue{
|
||||
HS: &models.ColorHS{
|
||||
Color: color.Color{
|
||||
HS: &color.HueSat{
|
||||
Hue: p.Hue,
|
||||
Sat: p.Sat,
|
||||
},
|
||||
|
||||
@@ -4,7 +4,7 @@ import (
|
||||
"context"
|
||||
"encoding/binary"
|
||||
"errors"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"log"
|
||||
"math/rand"
|
||||
"net"
|
||||
@@ -215,7 +215,7 @@ func (c *Client) Recv(timeout time.Duration) (target string, seq uint8, payload
|
||||
}
|
||||
if err != nil {
|
||||
if netErr, ok := err.(*net.OpError); ok && netErr.Timeout() {
|
||||
err = models.ErrReadTimeout
|
||||
err = lerrors.ErrReadTimeout
|
||||
return
|
||||
}
|
||||
|
||||
@@ -224,7 +224,7 @@ func (c *Client) Recv(timeout time.Duration) (target string, seq uint8, payload
|
||||
|
||||
packet := Packet(c.buf[:n])
|
||||
if n < 2 || packet.Size() != n && packet.Protocol() != 1024 {
|
||||
err = models.ErrInvalidAddress
|
||||
err = lerrors.ErrInvalidAddress
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ package lifx
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"log"
|
||||
"net"
|
||||
)
|
||||
@@ -60,7 +60,7 @@ func (p *Packet) SetTarget(v string) error {
|
||||
return err
|
||||
}
|
||||
if len(addr) != 6 {
|
||||
return models.ErrInvalidAddress
|
||||
return lerrors.ErrInvalidAddress
|
||||
}
|
||||
|
||||
copy((*p)[8:], addr)
|
||||
@@ -133,7 +133,7 @@ func (p *Packet) Payload() (res Payload, err error) {
|
||||
res = &SetLightPower{}
|
||||
err = res.Decode((*p)[36:])
|
||||
default:
|
||||
err = models.ErrUnrecognizedPacketType
|
||||
err = lerrors.ErrUnrecognizedPacketType
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
|
||||
@@ -3,7 +3,7 @@ package lifx
|
||||
import (
|
||||
"encoding/binary"
|
||||
"fmt"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"git.aiterp.net/lucifer/new-server/internal/lerrors"
|
||||
"math"
|
||||
"time"
|
||||
)
|
||||
@@ -82,7 +82,7 @@ type StateHostFirmware struct {
|
||||
|
||||
func (p *StateHostFirmware) Decode(data []byte) error {
|
||||
if len(data) < 20 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
ts := int64(binary.LittleEndian.Uint64(data[0:8]))
|
||||
@@ -123,7 +123,7 @@ type StateVersion struct {
|
||||
|
||||
func (p *StateVersion) Decode(data []byte) error {
|
||||
if len(data) < 8 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
p.Vendor = binary.LittleEndian.Uint32(data[0:4])
|
||||
@@ -207,7 +207,7 @@ type SetColor struct {
|
||||
|
||||
func (p *SetColor) Decode(data []byte) error {
|
||||
if len(data) < 13 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
hue := binary.LittleEndian.Uint16(data[1:3])
|
||||
@@ -263,7 +263,7 @@ type SetLightPower struct {
|
||||
|
||||
func (p *SetLightPower) Decode(data []byte) error {
|
||||
if len(data) < 6 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
level := binary.LittleEndian.Uint16(data[0:2])
|
||||
@@ -314,7 +314,7 @@ func (p *StateService) String() string {
|
||||
|
||||
func (p *StateService) Decode(data []byte) error {
|
||||
if len(data) < 5 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
p.Service = int(data[0])
|
||||
@@ -357,7 +357,7 @@ func (p *LightState) String() string {
|
||||
|
||||
func (p *LightState) Decode(data []byte) error {
|
||||
if len(data) < 52 {
|
||||
return models.ErrPayloadTooShort
|
||||
return lerrors.ErrPayloadTooShort
|
||||
}
|
||||
|
||||
hue := binary.LittleEndian.Uint16(data[0:2])
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package lifx
|
||||
|
||||
import (
|
||||
"git.aiterp.net/lucifer/new-server/internal/color"
|
||||
"git.aiterp.net/lucifer/new-server/models"
|
||||
"math"
|
||||
"time"
|
||||
@@ -38,7 +39,7 @@ func (s *State) generateUpdate() []Payload {
|
||||
|
||||
c, ok := s.deviceState.Color.ToHSK()
|
||||
if !ok {
|
||||
c, _ = models.ParseColorValue("hsk:0,0,4000")
|
||||
c, _ = color.Parse("hsk:0,0,4000")
|
||||
}
|
||||
|
||||
l := s.lightState
|
||||
@@ -76,16 +77,16 @@ func (s *State) handleAck(seq uint8) {
|
||||
prevLabel = s.lightState.Label
|
||||
}
|
||||
|
||||
color, ok := s.deviceState.Color.ToHSK()
|
||||
c, ok := s.deviceState.Color.ToHSK()
|
||||
if !ok {
|
||||
color, _ = models.ParseColorValue("hsk:0,0,4000")
|
||||
c, _ = color.Parse("hsk:0,0,4000")
|
||||
}
|
||||
|
||||
s.lightState = &LightState{
|
||||
Hue: color.HS.Hue,
|
||||
Sat: color.HS.Sat,
|
||||
Hue: c.HS.Hue,
|
||||
Sat: c.HS.Sat,
|
||||
Bri: s.deviceState.Intensity,
|
||||
Kelvin: *color.K,
|
||||
Kelvin: *c.K,
|
||||
On: s.deviceState.Power,
|
||||
Label: prevLabel,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user