/
eventTrigger.go
59 lines (48 loc) · 1.22 KB
/
eventTrigger.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package triggers
import (
"encoding/json"
things "github.com/galenliu/gateway/api/models/container"
"github.com/galenliu/gateway/pkg/bus/topic"
)
type EventTriggerDescription struct {
TriggerDescription
Thing string `json:"thing"`
Event string `json:"event"`
}
type EventTrigger struct {
*Trigger
thing string
event string
container things.Container
stopped bool
}
func NewEventTrigger(desc EventTriggerDescription, container things.Container) *EventTrigger {
return &EventTrigger{
Trigger: NewTrigger(desc.TriggerDescription),
thing: desc.Thing,
event: desc.Event,
stopped: true,
container: container,
}
}
func (t *EventTrigger) ToDescription() EventTriggerDescription {
return EventTriggerDescription{
TriggerDescription: t.Trigger.ToDescription(),
Thing: t.thing,
Event: t.event,
}
}
func (t *EventTrigger) MarshalJSON() ([]byte, error) {
return json.Marshal(t.ToDescription())
}
func (t *EventTrigger) Start() {
t.stopped = false
thing := t.container.GetThing(t.thing)
if thing != nil && !t.stopped {
thing.AddEventSubscription(t.onEvent)
}
}
func (t *EventTrigger) onEvent(message topic.ThingEventMessage) {
}
func (t *EventTrigger) Stop() {
}