Browse Source

finish EventHandler model for now.

pull/1/head
Gisle Aune 3 years ago
parent
commit
b2c9cca400
  1. 31
      models/eventhandler.go

31
models/eventhandler.go

@ -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
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 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
}
}
Loading…
Cancel
Save