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.

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