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) Empty() bool { return s.Power == nil && s.Temperature == nil && s.Intensity == nil && s.Color == nil } func (s State) String() string { parts := make([]string, 0, 4) if s.Power != nil { if *s.Power { parts = append(parts, "on") } else { parts = append(parts, "off") } } if s.Temperature != nil { parts = append(parts, fmt.Sprintf("%f°C", *s.Temperature)) } if s.Intensity != nil { parts = append(parts, fmt.Sprintf("%.1f%%", *s.Intensity*100)) } if s.Color != nil { parts = append(parts, 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((*s2.Intensity * f) + (*s.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((*s2.Temperature * f) + (*s.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 *s.Power == *s2.Power { newState.Power = s.Power } // With power, interpolate strongly in the direction of power if *s.Power { newState.Power = gentools.Ptr(f < 0.95) } if *s2.Power { newState.Power = gentools.Ptr(f > 0.05) } } 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"` }