-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.go
90 lines (74 loc) · 2.67 KB
/
command.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package axisevents
import (
"context"
"fmt"
"net/http"
"github.com/AndersMagnusson/axisevents/actions"
)
// Command executes the request.
type Command interface {
// With specify your Do function.
With(do func(req *http.Request)) Command
// ExecuteOn the event on the specified device.
ExecuteOn(ctx context.Context, device Device) error
}
type command struct {
collector collector
do func(req *http.Request)
}
// With specify your Do function.
func (h *command) With(do func(req *http.Request)) Command {
h.do = do
return h
}
// ExecuteOn the event on the specified device.
func (h *command) ExecuteOn(ctx context.Context, device Device) error {
handler := actions.NewActionHandler(ctx, device.Username, device.Password, device.Address)
existingRule, found, err := handler.GetActionRule(h.collector.name)
if found {
err = handler.RemoveActionRule(existingRule.RuleID)
if err != nil {
return fmt.Errorf("Failed to remove existing ActionRule, %s", err.Error())
}
}
result, found, err := handler.GetActionConfiguration(h.collector.name)
if found {
err = handler.RemoveActionConfigurations(result.ConfigurationID)
if err != nil {
return fmt.Errorf("Failed to remove existing ActionConfiguration, %s", err.Error())
}
}
configID, err := handler.AddActionConfigurations(h.collector.name, h.collector.templateToken, getParameters(h.collector.properties))
if err != nil {
return fmt.Errorf("Failed to add ActionConfiguration, %s", err.Error())
}
messageContent := actions.NewMessageContent(h.collector.messageContent)
if h.collector.version < 4 {
topixExpression := actions.NewTopicExpression(h.collector.topicExpression)
startEvent := actions.NewStartEvent(topixExpression, messageContent)
_, err = handler.AddActionRule(h.collector.name, h.collector.enabled, startEvent, configID)
if err != nil {
return fmt.Errorf("Failed to add ActionRule, %s", err.Error())
}
} else {
topixExpression := actions.NewTopicOasisTopicExpression(h.collector.topicExpression)
c1 := actions.ConditionRequest{
TopicExpression: topixExpression,
MessageContent: messageContent,
}
_, err = handler.AddActionConditionRule(
h.collector.name, h.collector.enabled, []actions.ConditionRequest{c1}, configID)
if err != nil {
return fmt.Errorf("Failed to add ActionRule, %s", err.Error())
}
// c1 := actions.NewActionConditionRule(name string, enabled bool, conditions []actions.ConditionRequest, primaryActionID int)
}
return nil
}
func getParameters(parameters map[string]interface{}) []actions.Parameter {
result := make([]actions.Parameter, 0)
for k, v := range parameters {
result = append(result, actions.Parameter{Name: k, Value: v})
}
return result
}