diff --git a/models/eventhandler.go b/models/eventhandler.go index f233278..58cd91a 100644 --- a/models/eventhandler.go +++ b/models/eventhandler.go @@ -11,8 +11,11 @@ type EventHandler struct { ID int `json:"id"` EventName string `json:"eventName"` Conditions map[string]EventCondition `json:"conditions"` + OneShot bool `json:"oneShot"` + Priority int `json:"priority"` TargetKind ReferenceKind `json:"targetType"` TargetValue string `json:"targetValue"` + Actions EventAction `json:"actions"` } 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 { - any := strings.Index(key, "any.") == 0 - all := strings.Index(key, "all.") == 0 - if any || all && len(key) > 4 { + any := strings.HasPrefix(key, "any.") + all := strings.HasPrefix(key, "all.") + if any || all { count := 0 for _, target := range targets { if c.checkDevice(key[4:], target) { @@ -119,3 +122,25 @@ func (c *EventCondition) matches(value string) bool { 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 + } +}