finish EventHandler model for now.
This commit is contained in:
+28
-3
@@ -11,8 +11,11 @@ type EventHandler struct {
|
|||||||
ID int `json:"id"`
|
ID int `json:"id"`
|
||||||
EventName string `json:"eventName"`
|
EventName string `json:"eventName"`
|
||||||
Conditions map[string]EventCondition `json:"conditions"`
|
Conditions map[string]EventCondition `json:"conditions"`
|
||||||
|
OneShot bool `json:"oneShot"`
|
||||||
|
Priority int `json:"priority"`
|
||||||
TargetKind ReferenceKind `json:"targetType"`
|
TargetKind ReferenceKind `json:"targetType"`
|
||||||
TargetValue string `json:"targetValue"`
|
TargetValue string `json:"targetValue"`
|
||||||
|
Actions EventAction `json:"actions"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type EventHandlerRepository interface {
|
type EventHandlerRepository interface {
|
||||||
@@ -49,9 +52,9 @@ func (h *EventHandler) MatchesEvent(event Event, targets []Device) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *EventCondition) check(key, value string, targets []Device) bool {
|
func (c *EventCondition) check(key, value string, targets []Device) bool {
|
||||||
any := strings.Index(key, "any.") == 0
|
any := strings.HasPrefix(key, "any.")
|
||||||
all := strings.Index(key, "all.") == 0
|
all := strings.HasPrefix(key, "all.")
|
||||||
if any || all && len(key) > 4 {
|
if any || all {
|
||||||
count := 0
|
count := 0
|
||||||
for _, target := range targets {
|
for _, target := range targets {
|
||||||
if c.checkDevice(key[4:], target) {
|
if c.checkDevice(key[4:], target) {
|
||||||
@@ -119,3 +122,25 @@ func (c *EventCondition) matches(value string) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type EventAction struct {
|
||||||
|
SetPower *bool `json:"setPower"`
|
||||||
|
SetColor *string `json:"setColor"`
|
||||||
|
SetIntensity *float64 `json:"setIntensity"`
|
||||||
|
AddIntensity *float64 `json:"addIntensity"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (action *EventAction) Apply(other EventAction) {
|
||||||
|
if action.SetPower == nil {
|
||||||
|
action.SetPower = other.SetPower
|
||||||
|
}
|
||||||
|
if action.SetColor == nil {
|
||||||
|
action.SetColor = other.SetColor
|
||||||
|
}
|
||||||
|
if action.SetIntensity == nil && other.SetIntensity != nil {
|
||||||
|
action.SetIntensity = other.SetIntensity
|
||||||
|
}
|
||||||
|
if action.AddIntensity == nil && action.SetIntensity == nil {
|
||||||
|
action.AddIntensity = other.AddIntensity
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user