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
169 lines
4.2 KiB
package handlerfactory
|
|
|
|
import (
|
|
"fmt"
|
|
"git.aiterp.net/lucifer/new-server/models"
|
|
)
|
|
|
|
func CreateHandlers(
|
|
config FactoryConfig,
|
|
scenes []models.Scene,
|
|
existing []models.EventHandler,
|
|
) ([]models.EventHandler, []models.EventHandler) {
|
|
switch config.Mode {
|
|
case SceneDimmer:
|
|
return sceneDimmer(config, scenes, existing)
|
|
default:
|
|
return []models.EventHandler{}, []models.EventHandler{}
|
|
}
|
|
}
|
|
|
|
func sceneDimmer(
|
|
config FactoryConfig,
|
|
scenes []models.Scene,
|
|
existing []models.EventHandler,
|
|
) ([]models.EventHandler, []models.EventHandler) {
|
|
kind, refValue := models.ParseFetchString(config.Target)
|
|
|
|
toRemove := make([]models.EventHandler, 0, 8)
|
|
for _, old := range existing {
|
|
if old.Conditions["deviceId"].EQ == config.TriggerDeviceID {
|
|
toRemove = append(toRemove, old)
|
|
}
|
|
}
|
|
|
|
resolvedScenes := make([]models.Scene, 0, len(config.SceneNames))
|
|
for _, name := range config.SceneNames {
|
|
for _, scene := range scenes {
|
|
if name == scene.Name {
|
|
resolvedScenes = append(resolvedScenes, scene)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
if len(scenes) < 2 {
|
|
return []models.EventHandler{}, []models.EventHandler{}
|
|
}
|
|
|
|
handlers := make([]models.EventHandler, 0, len(scenes)+4)
|
|
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "ButtonPressed",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
"buttonName": {EQ: "On"},
|
|
},
|
|
OneShot: false,
|
|
Priority: 100,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{SetPower: &myTrue},
|
|
From: -1,
|
|
To: -1,
|
|
})
|
|
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "ButtonPressed",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
"buttonName": {EQ: "Off"},
|
|
},
|
|
OneShot: false,
|
|
Priority: 100,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{SetPower: &myFalse},
|
|
From: -1,
|
|
To: -1,
|
|
})
|
|
|
|
for i, scene := range resolvedScenes {
|
|
if i > 0 {
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "ButtonPressed",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
"buttonName": {EQ: "DimDown"},
|
|
"any.scene": {EQ: fmt.Sprintf("%d", scene.ID)},
|
|
},
|
|
OneShot: false,
|
|
Priority: 100,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
|
|
SceneID: resolvedScenes[i-1].ID,
|
|
Group: refValue,
|
|
DurationMS: 0,
|
|
}},
|
|
From: -1,
|
|
To: -1,
|
|
})
|
|
}
|
|
|
|
if i < len(resolvedScenes)-1 {
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "ButtonPressed",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
"buttonName": {EQ: "DimUp"},
|
|
"any.scene": {EQ: fmt.Sprintf("%d", scene.ID)},
|
|
},
|
|
OneShot: false,
|
|
Priority: 100,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
|
|
SceneID: resolvedScenes[i+1].ID,
|
|
Group: refValue,
|
|
DurationMS: 0,
|
|
}},
|
|
From: -1,
|
|
To: -1,
|
|
})
|
|
}
|
|
}
|
|
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "ButtonPressed",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
"buttonName": {EQ: "DimDown"},
|
|
},
|
|
OneShot: false,
|
|
Priority: 90,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
|
|
SceneID: scenes[len(scenes)-1].ID,
|
|
Group: refValue,
|
|
DurationMS: 0,
|
|
}},
|
|
From: -1,
|
|
To: -1,
|
|
})
|
|
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "ButtonPressed",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
"buttonName": {EQ: "DimUp"},
|
|
},
|
|
OneShot: false,
|
|
Priority: 90,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{SetScene: &models.DeviceSceneAssignment{
|
|
SceneID: scenes[0].ID,
|
|
Group: refValue,
|
|
DurationMS: 0,
|
|
}},
|
|
From: -1,
|
|
To: -1,
|
|
})
|
|
|
|
return handlers, toRemove
|
|
}
|
|
|
|
var myTrue = true
|
|
var myFalse = false
|