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.

204 lines
4.1 KiB

  1. package models
  2. import (
  3. "math"
  4. "math/rand"
  5. "sort"
  6. "strings"
  7. )
  8. type Scene struct {
  9. ID int `json:"id"`
  10. Name string `json:"name"`
  11. IntervalMS int64 `json:"interval"`
  12. Roles []SceneRole `json:"roles"`
  13. }
  14. func (s *Scene) Validate() error {
  15. if s.IntervalMS < 0 {
  16. return ErrSceneInvalidInterval
  17. }
  18. if len(s.Roles) == 0 {
  19. return ErrSceneNoRoles
  20. }
  21. for _, role := range s.Roles {
  22. err := role.Validate()
  23. if err != nil {
  24. return err
  25. }
  26. }
  27. return nil
  28. }
  29. func (s *Scene) Role(device *Device) *SceneRole {
  30. index := s.RoleIndex(device)
  31. if index == -1 {
  32. return nil
  33. }
  34. return &s.Roles[index]
  35. }
  36. func (s *Scene) RoleIndex(device *Device) int {
  37. for i, role := range s.Roles {
  38. if role.TargetKind.Matches(device, role.TargetValue) {
  39. return i
  40. }
  41. }
  42. return -1
  43. }
  44. type SceneEffect string
  45. const (
  46. SEStatic SceneEffect = "Plain"
  47. SERandom SceneEffect = "Random"
  48. SEGradient SceneEffect = "Gradient"
  49. SETransition SceneEffect = "Transition"
  50. )
  51. type SceneRole struct {
  52. Effect SceneEffect `json:"effect"`
  53. PowerMode ScenePowerMode `json:"overridePower"`
  54. TargetKind ReferenceKind `json:"targetKind"`
  55. TargetValue string `json:"targetValue"`
  56. Interpolate bool `json:"interpolate"`
  57. Order string `json:"order"`
  58. States []NewDeviceState `json:"states"`
  59. }
  60. func (r *SceneRole) Validate() error {
  61. if len(r.States) == 0 {
  62. return ErrSceneRoleNoStates
  63. }
  64. switch r.TargetKind {
  65. case RKTag, RKBridgeID, RKDeviceID, RKName, RKAll:
  66. default:
  67. return ErrBadInput
  68. }
  69. switch r.PowerMode {
  70. case SPScene, SPDevice, SPOverride:
  71. default:
  72. return ErrSceneRoleUnknownPowerMode
  73. }
  74. switch r.Effect {
  75. case SEStatic, SERandom, SEGradient, SETransition:
  76. default:
  77. return ErrSceneRoleUnknownEffect
  78. }
  79. switch r.Order {
  80. case "", "-name", "name", "+name", "-id", "id", "+id":
  81. default:
  82. return ErrSceneRoleUnsupportedOrdering
  83. }
  84. return nil
  85. }
  86. func (r *SceneRole) ApplyOrder(devices []Device) {
  87. if r.Order == "" {
  88. return
  89. }
  90. desc := strings.HasPrefix(r.Order, "-")
  91. orderKey := r.Order
  92. if desc || strings.HasPrefix(r.Order, "+") {
  93. orderKey = orderKey[1:]
  94. }
  95. switch orderKey {
  96. case "id":
  97. sort.Slice(devices, func(i, j int) bool {
  98. if desc {
  99. return devices[i].ID > devices[j].ID
  100. }
  101. return devices[i].ID < devices[j].ID
  102. })
  103. case "name":
  104. sort.Slice(devices, func(i, j int) bool {
  105. if desc {
  106. return strings.Compare(devices[i].Name, devices[j].Name) < 0
  107. }
  108. return strings.Compare(devices[i].Name, devices[j].Name) > 0
  109. })
  110. }
  111. }
  112. func (r *SceneRole) ApplyEffect(device *Device, intervalFac float64, positionFac float64) (newState NewDeviceState, deviceChanged bool) {
  113. switch r.Effect {
  114. case SEStatic:
  115. newState = r.States[0]
  116. case SERandom:
  117. newState = r.State(rand.Float64())
  118. case SEGradient:
  119. newState = r.State(positionFac)
  120. case SETransition:
  121. newState = r.State(intervalFac)
  122. }
  123. switch r.PowerMode {
  124. case SPDevice:
  125. newState.Power = nil
  126. case SPScene:
  127. // Do nothing
  128. case SPOverride:
  129. if newState.Power != nil && device.State.Power != *newState.Power {
  130. device.State.Power = *newState.Power
  131. deviceChanged = true
  132. }
  133. }
  134. return
  135. }
  136. func (r *SceneRole) State(fac float64) NewDeviceState {
  137. if len(r.States) == 0 {
  138. return NewDeviceState{}
  139. } else if len(r.States) == 1 {
  140. return r.States[0]
  141. }
  142. if r.Interpolate {
  143. if len(r.States) == 2 {
  144. return r.States[0].Interpolate(r.States[1], fac)
  145. } else {
  146. pos := fac * float64(len(r.States)-1)
  147. start := math.Floor(pos)
  148. localFac := pos - start
  149. return r.States[int(start)].Interpolate(r.States[int(start)+1], localFac)
  150. }
  151. } else {
  152. index := int(fac * float64(len(r.States)))
  153. if index == len(r.States) {
  154. index = len(r.States) - 1
  155. }
  156. return r.States[index]
  157. }
  158. }
  159. type ScenePowerMode string
  160. const (
  161. SPDevice ScenePowerMode = "Device" // Device state decides power. Scene state may only power off.
  162. SPScene ScenePowerMode = "Scene" // Scene state decide power.
  163. SPOverride ScenePowerMode = "Override" // Same as above, but Scene state set Device state' power. This is good for "wake up" scenes.
  164. )
  165. type SceneAssignment struct {
  166. Scene *Scene
  167. Device *Device
  168. Tag string
  169. }