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.

206 lines
4.7 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.Saturation * 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. presenceCooldown bool
  84. }
  85. func (state *hueSensorState) Update(newData SensorData) *models.Event {
  86. stateTime, err := time.ParseInLocation("2006-01-02T15:04:05", newData.State.LastUpdated, time.UTC)
  87. if err != nil {
  88. // Invalid time is probably "none".
  89. return nil
  90. }
  91. defer func() {
  92. state.prevData = &newData
  93. state.prevTime = stateTime
  94. }()
  95. if state.uniqueID != newData.UniqueID {
  96. state.uniqueID = newData.UniqueID
  97. }
  98. if state.prevData != nil && newData.Type != state.prevData.Type {
  99. return nil
  100. }
  101. switch newData.Type {
  102. case "ZLLSwitch":
  103. {
  104. // Ignore old events.
  105. if time.Since(stateTime) > time.Second*3 {
  106. return nil
  107. }
  108. pe := state.prevData.State.ButtonEvent
  109. ce := newData.State.ButtonEvent
  110. td := stateTime.Sub(state.prevTime) >= time.Second
  111. pIdx := (pe / 1000) - 1
  112. cIdx := (ce / 1000) - 1
  113. pBtn := pe % 1000 / 2 // 0 = pressed, 1 = released
  114. cBtn := ce % 1000 / 2 // 0 = pressed, 1 = released
  115. if pIdx == cIdx && cBtn == 1 && pBtn == 0 {
  116. // Do not allow 4002 after 4000.
  117. // 4000 after 4002 is fine, though.
  118. break
  119. }
  120. if cBtn != pBtn || cIdx != pIdx || td {
  121. return &models.Event{
  122. Name: models.ENButtonPressed,
  123. Payload: map[string]string{
  124. "buttonIndex": strconv.Itoa(cIdx),
  125. "buttonName": buttonNames[cIdx],
  126. "deviceId": strconv.Itoa(state.externalID),
  127. "hueButtonEvent": strconv.Itoa(ce),
  128. },
  129. }
  130. }
  131. }
  132. case "ZLLPresence":
  133. {
  134. if state.prevData != nil && state.prevData.State.Presence != newData.State.Presence {
  135. name := models.ENSensorPresenceStarted
  136. if !newData.State.Presence {
  137. name = models.ENSensorPresenceEnding
  138. state.presenceCooldown = true
  139. }
  140. return &models.Event{
  141. Name: name,
  142. Payload: map[string]string{
  143. "deviceId": strconv.Itoa(state.externalID),
  144. "deviceInternalId": newData.UniqueID,
  145. },
  146. }
  147. } else if state.presenceCooldown && !newData.State.Presence && time.Since(stateTime) > time.Minute {
  148. state.presenceCooldown = false
  149. return &models.Event{
  150. Name: models.ENSensorPresenceEnded,
  151. Payload: map[string]string{
  152. "deviceId": strconv.Itoa(state.externalID),
  153. "deviceInternalId": newData.UniqueID,
  154. },
  155. }
  156. }
  157. }
  158. case "ZLLTemperature":
  159. if time.Since(stateTime) > (time.Minute * 15) {
  160. return nil
  161. }
  162. if !state.prevTime.Equal(stateTime) {
  163. return &models.Event{
  164. Name: models.ENSensorTemperature,
  165. Payload: map[string]string{
  166. "temperature": strconv.FormatFloat(float64(newData.State.Temperature)/100, 'f', 2, 64),
  167. "deviceId": strconv.Itoa(state.externalID),
  168. "deviceInternalId": newData.UniqueID,
  169. },
  170. }
  171. }
  172. }
  173. return nil
  174. }
  175. func ptrBool(v bool) *bool {
  176. return &v
  177. }
  178. func ptrInt(v int) *int {
  179. return &v
  180. }