add event handler API and logic.
This commit is contained in:
+51
-2
@@ -13,7 +13,7 @@ type EventHandler struct {
|
||||
Conditions map[string]EventCondition `json:"conditions"`
|
||||
OneShot bool `json:"oneShot"`
|
||||
Priority int `json:"priority"`
|
||||
TargetKind ReferenceKind `json:"targetType"`
|
||||
TargetKind ReferenceKind `json:"targetKind"`
|
||||
TargetValue string `json:"targetValue"`
|
||||
Actions EventAction `json:"actions"`
|
||||
}
|
||||
@@ -33,13 +33,58 @@ type EventCondition struct {
|
||||
LTE string `json:"lte,omitempty"`
|
||||
}
|
||||
|
||||
type EventHandlerUpdate struct {
|
||||
SetEventName *string `json:"setEventName"`
|
||||
SetPriority *int `json:"setPriority"`
|
||||
SetTargetKind *ReferenceKind `json:"setTargetKind"`
|
||||
SetTargetValue *string `json:"setTargetValue"`
|
||||
SetConditions map[string]EventCondition `json:"setConditions"`
|
||||
PatchConditions map[string]*EventCondition `json:"patchConditions"`
|
||||
SetActions *EventAction `json:"setActions"`
|
||||
}
|
||||
|
||||
func (h *EventHandler) ApplyUpdate(update EventHandlerUpdate) {
|
||||
if update.SetEventName != nil {
|
||||
h.EventName = *update.SetEventName
|
||||
}
|
||||
if update.SetPriority != nil {
|
||||
h.Priority = *update.SetPriority
|
||||
}
|
||||
if update.SetTargetKind != nil {
|
||||
h.TargetKind = *update.SetTargetKind
|
||||
}
|
||||
if update.SetTargetValue != nil {
|
||||
h.TargetValue = *update.SetTargetValue
|
||||
}
|
||||
if update.SetActions != nil {
|
||||
h.Actions = *update.SetActions
|
||||
}
|
||||
|
||||
if update.SetConditions != nil {
|
||||
h.Conditions = update.SetConditions
|
||||
}
|
||||
if update.PatchConditions != nil {
|
||||
if h.Conditions == nil {
|
||||
h.Conditions = make(map[string]EventCondition)
|
||||
}
|
||||
|
||||
for key, value := range update.PatchConditions {
|
||||
if value == nil {
|
||||
delete(h.Conditions, key)
|
||||
} else {
|
||||
h.Conditions[key] = *value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (h *EventHandler) MatchesEvent(event Event, targets []Device) bool {
|
||||
if event.Name != h.EventName {
|
||||
return false
|
||||
}
|
||||
|
||||
for key, condition := range h.Conditions {
|
||||
if !event.HasPayload(key) {
|
||||
if !strings.ContainsRune(key, '.') && !event.HasPayload(key) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -128,6 +173,7 @@ type EventAction struct {
|
||||
SetColor *string `json:"setColor"`
|
||||
SetIntensity *float64 `json:"setIntensity"`
|
||||
AddIntensity *float64 `json:"addIntensity"`
|
||||
FireEvent *Event `json:"fireEvent"`
|
||||
}
|
||||
|
||||
func (action *EventAction) Apply(other EventAction) {
|
||||
@@ -143,4 +189,7 @@ func (action *EventAction) Apply(other EventAction) {
|
||||
if action.AddIntensity == nil && action.SetIntensity == nil {
|
||||
action.AddIntensity = other.AddIntensity
|
||||
}
|
||||
if action.FireEvent == nil {
|
||||
action.FireEvent = other.FireEvent
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user