forked from brocaar/chirpstack-application-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multi.go
185 lines (161 loc) · 5.43 KB
/
multi.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package multihandler provides a multi handler.
package multihandler
import (
"bytes"
"encoding/json"
"fmt"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/brocaar/lora-app-server/internal/config"
"github.com/brocaar/lora-app-server/internal/handler"
"github.com/brocaar/lora-app-server/internal/handler/httphandler"
"github.com/brocaar/lora-app-server/internal/handler/influxdbhandler"
"github.com/brocaar/lora-app-server/internal/storage"
)
// Handler kinds
const (
HTTPHandlerKind = "HTTP"
InfluxDBHandlerKind = "INFLUXDB"
)
// Handler wraps multiple handlers inside a single handler so that
// data can be sent to multiple endpoints simultaneously.
// Note that errors are logged, but not returned.
type Handler struct {
defaultHandler handler.Handler
}
// SendDataUp sends a data-up payload.
func (w Handler) SendDataUp(pl handler.DataUpPayload) error {
handlers, err := w.getHandlersForApplicationID(pl.ApplicationID)
if err != nil {
log.Errorf("get handlers for application-id error: %s", err)
handlers = []handler.IntegrationHandler{w.defaultHandler}
}
for _, h := range handlers {
if err := h.SendDataUp(pl); err != nil {
log.Errorf("handler %T error: %s", h, err)
}
}
return nil
}
// SendJoinNotification sends a join notification.
func (w Handler) SendJoinNotification(pl handler.JoinNotification) error {
handlers, err := w.getHandlersForApplicationID(pl.ApplicationID)
if err != nil {
log.Errorf("get handlers for application-id error: %s", err)
handlers = []handler.IntegrationHandler{w.defaultHandler}
}
for _, h := range handlers {
if err := h.SendJoinNotification(pl); err != nil {
log.Errorf("handler %T error: %s", h, err)
}
}
return nil
}
// SendACKNotification sends an ACK notification.
func (w Handler) SendACKNotification(pl handler.ACKNotification) error {
handlers, err := w.getHandlersForApplicationID(pl.ApplicationID)
if err != nil {
log.Errorf("get handlers for application-id error: %s", err)
handlers = []handler.IntegrationHandler{w.defaultHandler}
}
for _, h := range handlers {
if err := h.SendACKNotification(pl); err != nil {
log.Errorf("handler %T error: %s", h, err)
}
}
return nil
}
// SendErrorNotification sends an error notification.
func (w Handler) SendErrorNotification(pl handler.ErrorNotification) error {
handlers, err := w.getHandlersForApplicationID(pl.ApplicationID)
if err != nil {
log.Errorf("get handlers for application-id error: %s", err)
handlers = []handler.IntegrationHandler{w.defaultHandler}
}
for _, h := range handlers {
if err := h.SendErrorNotification(pl); err != nil {
log.Errorf("handler %T error: %s", h, err)
}
}
return nil
}
// SendStatusNotification sends a status notification.
func (w Handler) SendStatusNotification(pl handler.StatusNotification) error {
handlers, err := w.getHandlersForApplicationID(pl.ApplicationID)
if err != nil {
log.Errorf("get handlers for application-id error: %s", err)
handlers = []handler.IntegrationHandler{w.defaultHandler}
}
for _, h := range handlers {
if err := h.SendStatusNotification(pl); err != nil {
log.Errorf("handler %T error: %s", h, err)
}
}
return nil
}
// SendLocationNotification sends a location notification.
func (w Handler) SendLocationNotification(pl handler.LocationNotification) error {
handlers, err := w.getHandlersForApplicationID(pl.ApplicationID)
if err != nil {
log.Errorf("get handlers for application-id error: %s", err)
handlers = []handler.IntegrationHandler{w.defaultHandler}
}
for _, h := range handlers {
if err := h.SendLocationNotification(pl); err != nil {
log.Errorf("handler %T error: %s", h, err)
}
}
return nil
}
// Close closes the handlers.
func (w Handler) Close() error {
return w.defaultHandler.Close()
}
// getHandlersForApplicationID returns all handlers (including the default
// handler for the given application ID.
func (w Handler) getHandlersForApplicationID(id int64) ([]handler.IntegrationHandler, error) {
handlers := []handler.IntegrationHandler{w.defaultHandler}
// read integrations
integrations, err := storage.GetIntegrationsForApplicationID(config.C.PostgreSQL.DB, id)
if err != nil {
return nil, errors.Wrap(err, "get integrtions for application id error")
}
// map integration to handler + config
for _, intg := range integrations {
switch intg.Kind {
case HTTPHandlerKind:
var conf httphandler.HandlerConfig
if err := json.NewDecoder(bytes.NewReader(intg.Settings)).Decode(&conf); err != nil {
return nil, errors.Wrap(err, "decode http handler config error")
}
h, err := httphandler.NewHandler(conf)
if err != nil {
return nil, err
}
handlers = append(handlers, h)
case InfluxDBHandlerKind:
var conf influxdbhandler.HandlerConfig
if err := json.NewDecoder(bytes.NewReader(intg.Settings)).Decode(&conf); err != nil {
return nil, errors.Wrap(err, "decode influxdb handler config error")
}
h, err := influxdbhandler.NewHandler(conf)
if err != nil {
return nil, err
}
handlers = append(handlers, h)
default:
return nil, fmt.Errorf("unknown integration %s", intg.Kind)
}
}
return handlers, nil
}
// DataDownChan returns the channel containing the received DataDownPayload.
func (w Handler) DataDownChan() chan handler.DataDownPayload {
return w.defaultHandler.DataDownChan()
}
// NewHandler returns a new MultiHandler.
func NewHandler(defaultHandler handler.Handler) handler.Handler {
return Handler{
defaultHandler: defaultHandler,
}
}