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.

179 lines
3.8 KiB

  1. package hue
  2. import (
  3. "git.aiterp.net/lucifer/new-server/models"
  4. "strconv"
  5. "time"
  6. )
  7. type hueLightState struct {
  8. index int
  9. uniqueID string
  10. externalID int
  11. info LightData
  12. input LightStateInput
  13. stale bool
  14. }
  15. func (s *hueLightState) Update(state models.DeviceState) {
  16. input := LightStateInput{}
  17. if state.Power {
  18. input.On = ptrBool(true)
  19. if state.Color.IsKelvin() {
  20. input.CT = ptrInt(1000000 / state.Color.Kelvin)
  21. if s.input.CT == nil || *s.input.CT != *input.CT {
  22. s.stale = true
  23. }
  24. } else {
  25. input.Hue = ptrInt(int(state.Color.Hue*(65536/360)) % 65536)
  26. if s.input.Hue == nil || *s.input.Hue != *input.Hue {
  27. s.stale = true
  28. }
  29. input.Sat = ptrInt(int(state.Color.Hue * 255))
  30. if *input.Sat > 254 {
  31. *input.Sat = 254
  32. }
  33. if *input.Sat < 0 {
  34. *input.Sat = 0
  35. }
  36. if s.input.Sat == nil || *s.input.Sat != *input.Sat {
  37. s.stale = true
  38. }
  39. }
  40. input.Bri = ptrInt(int(state.Intensity * 255))
  41. if *input.Bri > 254 {
  42. *input.Bri = 254
  43. } else if *input.Bri < 0 {
  44. *input.Bri = 0
  45. }
  46. if s.input.Bri == nil || *s.input.Bri != *input.Bri {
  47. s.stale = true
  48. }
  49. } else {
  50. input.On = ptrBool(false)
  51. }
  52. if s.input.On == nil || *s.input.On != *input.On {
  53. s.stale = true
  54. }
  55. input.TransitionTime = ptrInt(1)
  56. s.input = input
  57. }
  58. func (s *hueLightState) CheckStaleness(state LightState) {
  59. if state.ColorMode == "xy" {
  60. s.stale = true
  61. if s.input.CT == nil && s.input.Hue == nil {
  62. s.input.Hue = ptrInt(state.Hue)
  63. s.input.Sat = ptrInt(state.Sat)
  64. s.input.Bri = ptrInt(state.Bri)
  65. }
  66. return
  67. } else if state.ColorMode == "ct" {
  68. if s.input.CT == nil || state.CT != *s.input.CT {
  69. s.stale = true
  70. }
  71. } else {
  72. if s.input.Hue == nil || state.Hue != *s.input.Hue || s.input.Sat == nil || state.Sat == *s.input.Sat {
  73. s.stale = true
  74. }
  75. }
  76. }
  77. type hueSensorState struct {
  78. index int
  79. externalID int
  80. uniqueID string
  81. prevData *SensorData
  82. prevTime time.Time
  83. }
  84. func (state *hueSensorState) Update(newData SensorData) *models.Event {
  85. stateTime, err := time.ParseInLocation("2006-01-02T15:04:05", newData.State.LastUpdated, time.UTC)
  86. if err != nil {
  87. // Invalid time is probably "none".
  88. return nil
  89. }
  90. defer func() {
  91. state.prevData = &newData
  92. state.prevTime = stateTime
  93. }()
  94. if state.uniqueID != newData.UniqueID {
  95. state.uniqueID = newData.UniqueID
  96. }
  97. if state.prevData != nil && newData.Type != state.prevData.Type {
  98. return nil
  99. }
  100. if time.Since(stateTime) > time.Second*3 {
  101. return nil
  102. }
  103. switch newData.Type {
  104. case "ZLLSwitch":
  105. {
  106. pe := state.prevData.State.ButtonEvent
  107. ce := newData.State.ButtonEvent
  108. td := stateTime.Sub(state.prevTime) >= time.Second
  109. pIdx := (pe / 1000) - 1
  110. cIdx := (ce / 1000) - 1
  111. pBtn := pe % 1000 / 2 // 0 = pressed, 1 = released
  112. cBtn := ce % 1000 / 2 // 0 = pressed, 1 = released
  113. if pIdx == cIdx && cBtn == 1 && pBtn == 0 {
  114. // Do not allow 4002 after 4000.
  115. // 4000 after 4002 is fine, though.
  116. break
  117. }
  118. if cBtn != pBtn || cIdx != pIdx || td {
  119. return &models.Event{
  120. Name: models.ENButtonPressed,
  121. Payload: map[string]string{
  122. "buttonIndex": strconv.Itoa(cIdx),
  123. "buttonName": buttonNames[cIdx],
  124. "deviceId": strconv.Itoa(state.externalID),
  125. "hueButtonEvent": strconv.Itoa(ce),
  126. },
  127. }
  128. }
  129. }
  130. case "ZLLPresence":
  131. {
  132. // TODO: Test this!
  133. if state.prevData != nil && state.prevData.State.Presence != newData.State.Presence {
  134. name := models.ENSensorPresenceStarted
  135. if !newData.State.Presence {
  136. name = models.ENSensorPresenceEnded
  137. }
  138. return &models.Event{
  139. Name: name,
  140. Payload: map[string]string{
  141. "deviceId": strconv.Itoa(state.externalID),
  142. "deviceInternalId": newData.UniqueID,
  143. },
  144. }
  145. }
  146. }
  147. }
  148. return nil
  149. }
  150. func ptrBool(v bool) *bool {
  151. return &v
  152. }
  153. func ptrInt(v int) *int {
  154. return &v
  155. }