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.
 
 
 
 
 
 

79 lines
2.3 KiB

package device
import (
"fmt"
"git.aiterp.net/lucifer3/server/internal/color"
"git.aiterp.net/lucifer3/server/internal/gentools"
"strings"
)
type State struct {
Power *bool `json:"power"`
Temperature *float64 `json:"temperature"`
Intensity *float64 `json:"intensity"`
Color *color.Color `json:"color"`
}
func (s State) String() string {
parts := make([]string, 0, 4)
if s.Power != nil {
parts = append(parts, fmt.Sprintf("power:%t", *s.Power))
}
if s.Temperature != nil {
parts = append(parts, fmt.Sprintf("temperature:%f", *s.Temperature))
}
if s.Intensity != nil {
parts = append(parts, fmt.Sprintf("intensity:%.2f", *s.Intensity))
}
if s.Color != nil {
parts = append(parts, fmt.Sprintf("color:%s", s.Color.String()))
}
return fmt.Sprint("(", strings.Join(parts, ", "), ")")
}
func (s State) Interpolate(s2 State, f float64) State {
newState := State{}
if s.Color != nil && s2.Color != nil {
newState.Color = gentools.Ptr(s.Color.Interpolate(*s2.Color, f))
} else if s.Color != nil {
newState.Color = gentools.ShallowCopy(s.Color)
} else if s2.Color != nil {
newState.Color = gentools.ShallowCopy(s2.Color)
}
if s.Intensity != nil && s2.Intensity != nil {
newState.Intensity = gentools.Ptr((*s.Intensity * f) + (*s2.Intensity * (1.0 - f)))
} else if s.Intensity != nil {
newState.Intensity = gentools.ShallowCopy(s.Intensity)
} else if s2.Intensity != nil {
newState.Intensity = gentools.ShallowCopy(s2.Intensity)
}
if s.Temperature != nil && s2.Temperature != nil {
newState.Temperature = gentools.Ptr((*s.Temperature * f) + (*s2.Temperature * (1.0 - f)))
} else if s.Temperature != nil {
newState.Temperature = gentools.ShallowCopy(s.Temperature)
} else if s2.Temperature != nil {
newState.Temperature = gentools.ShallowCopy(s2.Temperature)
}
if s.Power != nil && s2.Power != nil {
if f < 0.5 {
newState.Power = gentools.ShallowCopy(s.Power)
} else {
newState.Power = gentools.ShallowCopy(s2.Power)
}
} else if s.Power != nil {
newState.Power = gentools.ShallowCopy(s.Power)
} else if s2.Power != nil {
newState.Power = gentools.ShallowCopy(s2.Power)
}
return newState
}
type PresenceState struct {
Present bool `json:"present"`
AbsenceSeconds float64 `json:"absenceSeconds,omitempty"`
}