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.

169 lines
4.2 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 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. default:
  15. return []models.EventHandler{}, []models.EventHandler{}
  16. }
  17. }
  18. func sceneDimmer(
  19. config FactoryConfig,
  20. scenes []models.Scene,
  21. existing []models.EventHandler,
  22. ) ([]models.EventHandler, []models.EventHandler) {
  23. kind, refValue := models.ParseFetchString(config.Target)
  24. toRemove := make([]models.EventHandler, 0, 8)
  25. for _, old := range existing {
  26. if old.Conditions["deviceId"].EQ == config.TriggerDeviceID {
  27. toRemove = append(toRemove, old)
  28. }
  29. }
  30. resolvedScenes := make([]models.Scene, 0, len(config.SceneNames))
  31. for _, name := range config.SceneNames {
  32. for _, scene := range scenes {
  33. if name == scene.Name {
  34. resolvedScenes = append(resolvedScenes, scene)
  35. break
  36. }
  37. }
  38. }
  39. if len(scenes) < 2 {
  40. return []models.EventHandler{}, []models.EventHandler{}
  41. }
  42. handlers := make([]models.EventHandler, 0, len(scenes)+4)
  43. handlers = append(handlers, models.EventHandler{
  44. EventName: "ButtonPressed",
  45. Conditions: map[string]models.EventCondition{
  46. "deviceId": {EQ: config.TriggerDeviceID},
  47. "buttonName": {EQ: "On"},
  48. },
  49. OneShot: false,
  50. Priority: 100,
  51. TargetKind: kind,
  52. TargetValue: refValue,
  53. Actions: models.EventAction{SetPower: &myTrue},
  54. From: -1,
  55. To: -1,
  56. })
  57. handlers = append(handlers, models.EventHandler{
  58. EventName: "ButtonPressed",
  59. Conditions: map[string]models.EventCondition{
  60. "deviceId": {EQ: config.TriggerDeviceID},
  61. "buttonName": {EQ: "Off"},
  62. },
  63. OneShot: false,
  64. Priority: 100,
  65. TargetKind: kind,
  66. TargetValue: refValue,
  67. Actions: models.EventAction{SetPower: &myFalse},
  68. From: -1,
  69. To: -1,
  70. })
  71. for i, scene := range resolvedScenes {
  72. if i > 0 {
  73. handlers = append(handlers, models.EventHandler{
  74. EventName: "ButtonPressed",
  75. Conditions: map[string]models.EventCondition{
  76. "deviceId": {EQ: config.TriggerDeviceID},
  77. "buttonName": {EQ: "DimDown"},
  78. "any.scene": {EQ: fmt.Sprintf("%d", scene.ID)},
  79. },
  80. OneShot: false,
  81. Priority: 100,
  82. TargetKind: kind,
  83. TargetValue: refValue,
  84. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  85. SceneID: resolvedScenes[i-1].ID,
  86. Group: refValue,
  87. DurationMS: 0,
  88. }},
  89. From: -1,
  90. To: -1,
  91. })
  92. }
  93. if i < len(resolvedScenes)-1 {
  94. handlers = append(handlers, models.EventHandler{
  95. EventName: "ButtonPressed",
  96. Conditions: map[string]models.EventCondition{
  97. "deviceId": {EQ: config.TriggerDeviceID},
  98. "buttonName": {EQ: "DimUp"},
  99. "any.scene": {EQ: fmt.Sprintf("%d", scene.ID)},
  100. },
  101. OneShot: false,
  102. Priority: 100,
  103. TargetKind: kind,
  104. TargetValue: refValue,
  105. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  106. SceneID: resolvedScenes[i+1].ID,
  107. Group: refValue,
  108. DurationMS: 0,
  109. }},
  110. From: -1,
  111. To: -1,
  112. })
  113. }
  114. }
  115. handlers = append(handlers, models.EventHandler{
  116. EventName: "ButtonPressed",
  117. Conditions: map[string]models.EventCondition{
  118. "deviceId": {EQ: config.TriggerDeviceID},
  119. "buttonName": {EQ: "DimDown"},
  120. },
  121. OneShot: false,
  122. Priority: 90,
  123. TargetKind: kind,
  124. TargetValue: refValue,
  125. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  126. SceneID: resolvedScenes[len(resolvedScenes)-1].ID,
  127. Group: refValue,
  128. DurationMS: 0,
  129. }},
  130. From: -1,
  131. To: -1,
  132. })
  133. handlers = append(handlers, models.EventHandler{
  134. EventName: "ButtonPressed",
  135. Conditions: map[string]models.EventCondition{
  136. "deviceId": {EQ: config.TriggerDeviceID},
  137. "buttonName": {EQ: "DimUp"},
  138. },
  139. OneShot: false,
  140. Priority: 90,
  141. TargetKind: kind,
  142. TargetValue: refValue,
  143. Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
  144. SceneID: resolvedScenes[0].ID,
  145. Group: refValue,
  146. DurationMS: 0,
  147. }},
  148. From: -1,
  149. To: -1,
  150. })
  151. return handlers, toRemove
  152. }
  153. var myTrue = true
  154. var myFalse = false