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.

235 lines
6.2 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package handlerfactory
  2. import (
  3. "fmt"
  4. "git.aiterp.net/lucifer/new-server/models"
  5. )
  6. func CreateHandlers(
  7. config FactoryConfig,
  8. scenes []models.Scene,
  9. existing []models.EventHandler,
  10. ) ([]models.EventHandler, []models.EventHandler) {
  11. switch config.Mode {
  12. case SceneDimmer:
  13. return sceneDimmer(config, scenes, existing)
  14. case MotionSensor:
  15. return motionSensor(config, existing)
  16. default:
  17. return []models.EventHandler{}, []models.EventHandler{}
  18. }
  19. }
  20. func sceneDimmer(
  21. config FactoryConfig,
  22. scenes []models.Scene,
  23. existing []models.EventHandler,
  24. ) ([]models.EventHandler, []models.EventHandler) {
  25. kind, refValue := models.ParseFetchString(config.Target)
  26. toRemove := make([]models.EventHandler, 0, 8)
  27. for _, old := range existing {
  28. if old.Conditions["deviceId"].EQ == config.TriggerDeviceID {
  29. toRemove = append(toRemove, old)
  30. }
  31. }
  32. resolvedScenes := make([]models.Scene, 0, len(config.SceneNames))
  33. for _, name := range config.SceneNames {
  34. for _, scene := range scenes {
  35. if name == scene.Name {
  36. resolvedScenes = append(resolvedScenes, scene)
  37. break
  38. }
  39. }
  40. }
  41. if len(scenes) < 2 {
  42. return []models.EventHandler{}, []models.EventHandler{}
  43. }
  44. handlers := make([]models.EventHandler, 0, len(scenes)+4)
  45. handlers = append(handlers, models.EventHandler{
  46. EventName: "ButtonPressed",
  47. Conditions: map[string]models.EventCondition{
  48. "deviceId": {EQ: config.TriggerDeviceID},
  49. "buttonName": {EQ: "On"},
  50. },
  51. OneShot: false,
  52. Priority: 100,
  53. TargetKind: kind,
  54. TargetValue: refValue,
  55. Actions: models.EventAction{SetPower: &myTrue},
  56. From: config.FromTime(),
  57. To: config.ToTime(),
  58. })
  59. handlers = append(handlers, models.EventHandler{
  60. EventName: "ButtonPressed",
  61. Conditions: map[string]models.EventCondition{
  62. "deviceId": {EQ: config.TriggerDeviceID},
  63. "buttonName": {EQ: "Off"},
  64. },
  65. OneShot: false,
  66. Priority: 100,
  67. TargetKind: kind,
  68. TargetValue: refValue,
  69. Actions: models.EventAction{SetPower: &myFalse},
  70. From: config.FromTime(),
  71. To: config.ToTime(),
  72. })
  73. for i, scene := range resolvedScenes {
  74. if i > 0 {
  75. handlers = append(handlers, models.EventHandler{
  76. EventName: "ButtonPressed",
  77. Conditions: map[string]models.EventCondition{
  78. "deviceId": {EQ: config.TriggerDeviceID},
  79. "buttonName": {EQ: "DimDown"},
  80. "any.scene": {EQ: fmt.Sprintf("%d", scene.ID)},
  81. },
  82. OneShot: false,
  83. Priority: 100,
  84. TargetKind: kind,
  85. TargetValue: refValue,
  86. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  87. SceneID: resolvedScenes[i-1].ID,
  88. Group: refValue,
  89. DurationMS: 0,
  90. }},
  91. From: config.FromTime(),
  92. To: config.ToTime(),
  93. })
  94. }
  95. if i < len(resolvedScenes)-1 {
  96. handlers = append(handlers, models.EventHandler{
  97. EventName: "ButtonPressed",
  98. Conditions: map[string]models.EventCondition{
  99. "deviceId": {EQ: config.TriggerDeviceID},
  100. "buttonName": {EQ: "DimUp"},
  101. "any.scene": {EQ: fmt.Sprintf("%d", scene.ID)},
  102. },
  103. OneShot: false,
  104. Priority: 100,
  105. TargetKind: kind,
  106. TargetValue: refValue,
  107. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  108. SceneID: resolvedScenes[i+1].ID,
  109. Group: refValue,
  110. DurationMS: 0,
  111. }},
  112. From: config.FromTime(),
  113. To: config.ToTime(),
  114. })
  115. }
  116. }
  117. handlers = append(handlers, models.EventHandler{
  118. EventName: "ButtonPressed",
  119. Conditions: map[string]models.EventCondition{
  120. "deviceId": {EQ: config.TriggerDeviceID},
  121. "buttonName": {EQ: "DimDown"},
  122. },
  123. OneShot: false,
  124. Priority: 90,
  125. TargetKind: kind,
  126. TargetValue: refValue,
  127. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  128. SceneID: resolvedScenes[len(resolvedScenes)-1].ID,
  129. Group: refValue,
  130. DurationMS: 0,
  131. }},
  132. From: config.FromTime(),
  133. To: config.ToTime(),
  134. })
  135. handlers = append(handlers, models.EventHandler{
  136. EventName: "ButtonPressed",
  137. Conditions: map[string]models.EventCondition{
  138. "deviceId": {EQ: config.TriggerDeviceID},
  139. "buttonName": {EQ: "DimUp"},
  140. },
  141. OneShot: false,
  142. Priority: 90,
  143. TargetKind: kind,
  144. TargetValue: refValue,
  145. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  146. SceneID: resolvedScenes[0].ID,
  147. Group: refValue,
  148. DurationMS: 0,
  149. }},
  150. From: config.FromTime(),
  151. To: config.ToTime(),
  152. })
  153. return optimizeLists(handlers, toRemove)
  154. }
  155. func motionSensor(
  156. config FactoryConfig,
  157. existing []models.EventHandler,
  158. ) ([]models.EventHandler, []models.EventHandler) {
  159. kind, refValue := models.ParseFetchString(config.Target)
  160. toRemove := make([]models.EventHandler, 0, 8)
  161. for _, old := range existing {
  162. if old.From != config.FromTime() || old.To != config.ToTime() {
  163. continue
  164. }
  165. if old.Conditions["deviceId"].EQ == config.TriggerDeviceID {
  166. toRemove = append(toRemove, old)
  167. }
  168. }
  169. handlers := make([]models.EventHandler, 0, len(config.After))
  170. for _, after := range config.After {
  171. if after.Minutes == 0 {
  172. handlers = append(handlers, models.EventHandler{
  173. EventName: "SensorPresenceStarted",
  174. Conditions: map[string]models.EventCondition{
  175. "deviceId": {EQ: config.TriggerDeviceID},
  176. },
  177. OneShot: false,
  178. Priority: 100,
  179. TargetKind: kind,
  180. TargetValue: refValue,
  181. Actions: models.EventAction{
  182. SetPower: after.NewState.Power,
  183. SetColor: after.NewState.Color,
  184. SetIntensity: after.NewState.Intensity,
  185. SetTemperature: after.NewState.Temperature,
  186. },
  187. From: config.FromTime(),
  188. To: config.ToTime(),
  189. })
  190. } else {
  191. handlers = append(handlers, models.EventHandler{
  192. EventName: "SensorPresenceEnded",
  193. Conditions: map[string]models.EventCondition{
  194. "deviceId": {EQ: config.TriggerDeviceID},
  195. "minutesElapsed": {EQ: fmt.Sprintf("%d", after.Minutes)},
  196. },
  197. OneShot: false,
  198. Priority: 100,
  199. TargetKind: kind,
  200. TargetValue: refValue,
  201. Actions: models.EventAction{
  202. SetPower: after.NewState.Power,
  203. SetColor: after.NewState.Color,
  204. SetIntensity: after.NewState.Intensity,
  205. SetTemperature: after.NewState.Temperature,
  206. },
  207. From: config.FromTime(),
  208. To: config.ToTime(),
  209. })
  210. }
  211. }
  212. return optimizeLists(handlers, toRemove)
  213. }
  214. var myTrue = true
  215. var myFalse = false