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
235 lines
6.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)
|
|
case MotionSensor:
|
|
return motionSensor(config, 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: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
|
|
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: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
|
|
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: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
}
|
|
|
|
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: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
}
|
|
}
|
|
|
|
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: resolvedScenes[len(resolvedScenes)-1].ID,
|
|
Group: refValue,
|
|
DurationMS: 0,
|
|
}},
|
|
From: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
|
|
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: resolvedScenes[0].ID,
|
|
Group: refValue,
|
|
DurationMS: 0,
|
|
}},
|
|
From: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
|
|
return optimizeLists(handlers, toRemove)
|
|
}
|
|
|
|
func motionSensor(
|
|
config FactoryConfig,
|
|
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.From != config.FromTime() || old.To != config.ToTime() {
|
|
continue
|
|
}
|
|
|
|
if old.Conditions["deviceId"].EQ == config.TriggerDeviceID {
|
|
toRemove = append(toRemove, old)
|
|
}
|
|
}
|
|
|
|
handlers := make([]models.EventHandler, 0, len(config.After))
|
|
for _, after := range config.After {
|
|
if after.Minutes == 0 {
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "SensorPresenceStarted",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
},
|
|
OneShot: false,
|
|
Priority: 100,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{
|
|
SetPower: after.NewState.Power,
|
|
SetColor: after.NewState.Color,
|
|
SetIntensity: after.NewState.Intensity,
|
|
SetTemperature: after.NewState.Temperature,
|
|
},
|
|
From: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
} else {
|
|
handlers = append(handlers, models.EventHandler{
|
|
EventName: "SensorPresenceEnded",
|
|
Conditions: map[string]models.EventCondition{
|
|
"deviceId": {EQ: config.TriggerDeviceID},
|
|
"minutesElapsed": {EQ: fmt.Sprintf("%d", after.Minutes)},
|
|
},
|
|
OneShot: false,
|
|
Priority: 100,
|
|
TargetKind: kind,
|
|
TargetValue: refValue,
|
|
Actions: models.EventAction{
|
|
SetPower: after.NewState.Power,
|
|
SetColor: after.NewState.Color,
|
|
SetIntensity: after.NewState.Intensity,
|
|
SetTemperature: after.NewState.Temperature,
|
|
},
|
|
From: config.FromTime(),
|
|
To: config.ToTime(),
|
|
})
|
|
}
|
|
}
|
|
|
|
return optimizeLists(handlers, toRemove)
|
|
}
|
|
|
|
var myTrue = true
|
|
var myFalse = false
|