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.

92 lines
2.4 KiB

2 years ago
2 years ago
  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) Empty() bool {
  15. return s.Power == nil &&
  16. s.Temperature == nil &&
  17. s.Intensity == nil &&
  18. s.Color == nil
  19. }
  20. func (s State) String() string {
  21. parts := make([]string, 0, 4)
  22. if s.Power != nil {
  23. if *s.Power {
  24. parts = append(parts, "on")
  25. } else {
  26. parts = append(parts, "off")
  27. }
  28. }
  29. if s.Temperature != nil {
  30. parts = append(parts, fmt.Sprintf("%f°C", *s.Temperature))
  31. }
  32. if s.Intensity != nil {
  33. parts = append(parts, fmt.Sprintf("%.1f%%", *s.Intensity*100))
  34. }
  35. if s.Color != nil {
  36. parts = append(parts, s.Color.String())
  37. }
  38. return fmt.Sprint("(", strings.Join(parts, ", "), ")")
  39. }
  40. func (s State) Interpolate(s2 State, f float64) State {
  41. newState := State{}
  42. if s.Color != nil && s2.Color != nil {
  43. newState.Color = gentools.Ptr(s.Color.Interpolate(*s2.Color, f))
  44. } else if s.Color != nil {
  45. newState.Color = gentools.ShallowCopy(s.Color)
  46. } else if s2.Color != nil {
  47. newState.Color = gentools.ShallowCopy(s2.Color)
  48. }
  49. if s.Intensity != nil && s2.Intensity != nil {
  50. newState.Intensity = gentools.Ptr((*s2.Intensity * f) + (*s.Intensity * (1.0 - f)))
  51. } else if s.Intensity != nil {
  52. newState.Intensity = gentools.ShallowCopy(s.Intensity)
  53. } else if s2.Intensity != nil {
  54. newState.Intensity = gentools.ShallowCopy(s2.Intensity)
  55. }
  56. if s.Temperature != nil && s2.Temperature != nil {
  57. newState.Temperature = gentools.Ptr((*s2.Temperature * f) + (*s.Temperature * (1.0 - f)))
  58. } else if s.Temperature != nil {
  59. newState.Temperature = gentools.ShallowCopy(s.Temperature)
  60. } else if s2.Temperature != nil {
  61. newState.Temperature = gentools.ShallowCopy(s2.Temperature)
  62. }
  63. if s.Power != nil && s2.Power != nil {
  64. if *s.Power == *s2.Power {
  65. newState.Power = s.Power
  66. } else if *s.Power {
  67. newState.Power = gentools.Ptr(f < 0.95)
  68. } else {
  69. newState.Power = gentools.Ptr(f > 0.05)
  70. }
  71. } else if s.Power != nil {
  72. newState.Power = gentools.ShallowCopy(s.Power)
  73. } else if s2.Power != nil {
  74. newState.Power = gentools.ShallowCopy(s2.Power)
  75. }
  76. return newState
  77. }
  78. type PresenceState struct {
  79. Present bool `json:"present"`
  80. AbsenceSeconds float64 `json:"absenceSeconds,omitempty"`
  81. }