You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
1.8 KiB
87 lines
1.8 KiB
package lifx
|
|
|
|
import (
|
|
"git.aiterp.net/lucifer/new-server/models"
|
|
"math"
|
|
"time"
|
|
)
|
|
|
|
type State struct {
|
|
target string
|
|
externalId int
|
|
lightState *LightState
|
|
firmware *StateHostFirmware
|
|
version *StateVersion
|
|
|
|
deviceState *models.DeviceState
|
|
discoveredTime time.Time
|
|
lightStateTime time.Time
|
|
requestTime time.Time
|
|
updateTime time.Time
|
|
acksPending []uint8
|
|
}
|
|
|
|
func (s *State) generateUpdate() []Payload {
|
|
if s.deviceState == nil || s.lightState == nil {
|
|
return nil
|
|
}
|
|
|
|
results := make([]Payload, 0, 0)
|
|
if s.deviceState.Power != s.lightState.On {
|
|
results = append(results, &SetLightPower{On: s.deviceState.Power, TransitionTime: time.Millisecond * 100})
|
|
}
|
|
|
|
if !s.deviceState.Power {
|
|
return results
|
|
}
|
|
|
|
c := s.deviceState.Color
|
|
l := s.lightState
|
|
di := s.deviceState.Intensity
|
|
k := c.Kelvin
|
|
if k == 0 {
|
|
k = 4000
|
|
}
|
|
if !equalish(c.Hue, l.Hue) || !equalish(c.Saturation, l.Sat) || !equalish(di, l.Bri) || k != l.Kelvin {
|
|
results = append(results, &SetColor{
|
|
Hue: c.Hue,
|
|
Sat: c.Saturation,
|
|
Bri: di,
|
|
Kelvin: k,
|
|
TransitionTime: time.Millisecond * 150,
|
|
})
|
|
}
|
|
|
|
return results
|
|
}
|
|
|
|
func (s *State) handleAck(seq uint8) {
|
|
for i, pendingAck := range s.acksPending {
|
|
if pendingAck == seq {
|
|
s.acksPending = append(s.acksPending[:i], s.acksPending[i+1:]...)
|
|
break
|
|
}
|
|
}
|
|
|
|
if len(s.acksPending) == 0 {
|
|
s.lightStateTime = time.Now()
|
|
|
|
prevLabel := ""
|
|
if s.lightState != nil {
|
|
prevLabel = s.lightState.Label
|
|
}
|
|
|
|
s.lightState = &LightState{
|
|
Hue: s.deviceState.Color.Hue,
|
|
Sat: s.deviceState.Color.Saturation,
|
|
Bri: s.deviceState.Intensity,
|
|
Kelvin: s.deviceState.Color.Kelvin,
|
|
On: s.deviceState.Power,
|
|
Label: prevLabel,
|
|
}
|
|
}
|
|
}
|
|
|
|
func equalish(a, b float64) bool {
|
|
return math.Abs(a-b) < 0.005
|
|
}
|