-
Notifications
You must be signed in to change notification settings - Fork 0
/
triggers.go
162 lines (127 loc) · 3.17 KB
/
triggers.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package main
import (
"fmt"
"net/http"
"strings"
"syscall"
"text/template"
)
const DefaultMethod = "PUT"
const DefaultTemplate = "{{if .RisingEdge}}1{{else}}0{{end}}"
var Client http.Client
type triggerInfo struct {
p TriggeringPin
onRising string
onFalling string
method string
tpl *template.Template
}
func (ti *triggerInfo) send(rising bool, url string) error {
var body strings.Builder
err := ti.tpl.Execute(&body, struct {
RisingEdge bool
FallingEdge bool
Pin PinMap
}{
RisingEdge: rising,
FallingEdge: !rising,
Pin: pinMap,
})
if err != nil {
return fmt.Errorf("Template execution failed: %v", err)
}
req, err := http.NewRequest(ti.method, url, strings.NewReader(body.String()))
if err != nil {
return fmt.Errorf("Can't create HTTP request: %v", err)
}
resp, err := Client.Do(req)
if err != nil {
return fmt.Errorf("Can't do HTTP request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("HTTP request failed: %v", resp.Status)
}
return nil
}
func (ti *triggerInfo) SendRising() error {
if ti.onRising == "" {
return nil
}
return ti.send(true, ti.onRising)
}
func (ti *triggerInfo) SendFalling() error {
if ti.onFalling == "" {
return nil
}
return ti.send(false, ti.onFalling)
}
type Triggers struct {
epollFd int
pins map[int]triggerInfo
}
func NewTriggers() (*Triggers, error) {
fd, err := syscall.EpollCreate1(syscall.EPOLL_CLOEXEC)
if err != nil {
return nil, err
}
return &Triggers{fd, make(map[int]triggerInfo)}, nil
}
func (t *Triggers) Add(p TriggeringPin, onRising string, onFalling string, method string, payload string) error {
if method == "" {
method = DefaultMethod
}
if payload == "" {
payload = DefaultTemplate
}
tpl, err := template.New("trigger").Parse(payload)
if err != nil {
return fmt.Errorf("cannot parse payload template: %v", err)
}
ev, err := p.GetEpollEvent(onRising != "", onFalling != "")
if err != nil {
return fmt.Errorf("cannot configure pin: %v", err)
}
err = syscall.EpollCtl(t.epollFd, syscall.EPOLL_CTL_ADD, int(ev.Fd), ev)
if err != nil {
return fmt.Errorf("epoll: %v", err)
}
t.pins[int(ev.Fd)] = triggerInfo{p, onRising, onFalling, method, tpl}
return nil
}
// pinMap is a map of pin value functions indexed by pin name
// It can be used when evaluating templates so multiple pins
// can be sampled as part of the same event.
type PinMap map[string]fmt.Stringer
var pinMap PinMap
// AddContext adds the pin to the context used during template evaluation
func (*Triggers) AddContext(p PinHandler) {
if pinMap == nil {
pinMap = make(PinMap)
}
pinMap[p.Endpoint()] = p
}
func (t *Triggers) Wait() {
events := make([]syscall.EpollEvent, 1)
for {
n, err := syscall.EpollWait(t.epollFd, events, -1)
if err != nil && err != syscall.EINTR {
fmt.Println("epoll_wait returned error:", err)
return
}
if n > 0 {
fd := int(events[0].Fd)
if ti, ok := t.pins[fd]; ok {
r, f := ti.p.IdentifyEdge(&events[0])
if r {
ti.SendRising()
}
if f {
ti.SendFalling()
}
} else {
fmt.Println("epoll returned event for unrecognised fd", fd)
}
}
}
}