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

  1. package device
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer3/server/internal/color"
  5. "git.aiterp.net/lucifer3/server/internal/gentools"
  6. "strings"
  7. )
  8. type State struct {
  9. Power *bool `json:"power"`
  10. Temperature *float64 `json:"temperature"`
  11. Intensity *float64 `json:"intensity"`
  12. Color *color.Color `json:"color"`
  13. }
  14. func (s State) String() string {
  15. parts := make([]string, 0, 4)
  16. if s.Power != nil {
  17. parts = append(parts, fmt.Sprintf("power:%t", *s.Power))
  18. }
  19. if s.Temperature != nil {
  20. parts = append(parts, fmt.Sprintf("temperature:%f", *s.Temperature))
  21. }
  22. if s.Intensity != nil {
  23. parts = append(parts, fmt.Sprintf("intensity:%.2f", *s.Intensity))
  24. }
  25. if s.Color != nil {
  26. parts = append(parts, fmt.Sprintf("color:%s", s.Color.String()))
  27. }
  28. return fmt.Sprint("(", strings.Join(parts, ", "), ")")
  29. }
  30. func (s State) Interpolate(s2 State, f float64) State {
  31. newState := State{}
  32. if s.Color != nil && s2.Color != nil {
  33. newState.Color = gentools.Ptr(s.Color.Interpolate(*s2.Color, f))
  34. } else if s.Color != nil {
  35. newState.Color = gentools.ShallowCopy(s.Color)
  36. } else if s2.Color != nil {
  37. newState.Color = gentools.ShallowCopy(s2.Color)
  38. }
  39. if s.Intensity != nil && s2.Intensity != nil {
  40. newState.Intensity = gentools.Ptr((*s.Intensity * f) + (*s2.Intensity * (1.0 - f)))
  41. } else if s.Intensity != nil {
  42. newState.Intensity = gentools.ShallowCopy(s.Intensity)
  43. } else if s2.Intensity != nil {
  44. newState.Intensity = gentools.ShallowCopy(s2.Intensity)
  45. }
  46. if s.Temperature != nil && s2.Temperature != nil {
  47. newState.Temperature = gentools.Ptr((*s.Temperature * f) + (*s2.Temperature * (1.0 - f)))
  48. } else if s.Temperature != nil {
  49. newState.Temperature = gentools.ShallowCopy(s.Temperature)
  50. } else if s2.Temperature != nil {
  51. newState.Temperature = gentools.ShallowCopy(s2.Temperature)
  52. }
  53. if s.Power != nil && s2.Power != nil {
  54. if f < 0.5 {
  55. newState.Power = gentools.ShallowCopy(s.Power)
  56. } else {
  57. newState.Power = gentools.ShallowCopy(s2.Power)
  58. }
  59. } else if s.Power != nil {
  60. newState.Power = gentools.ShallowCopy(s.Power)
  61. } else if s2.Power != nil {
  62. newState.Power = gentools.ShallowCopy(s2.Power)
  63. }
  64. return newState
  65. }
  66. type PresenceState struct {
  67. Present bool `json:"present"`
  68. AbsenceSeconds float64 `json:"absenceSeconds,omitempty"`
  69. }