-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
353 lines (311 loc) · 10.4 KB
/
config.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"crypto/tls"
"io/ioutil"
"path/filepath"
"plugin"
"regexp"
"time"
"github.com/eclipse/paho.mqtt.golang"
"github.com/go-yaml/yaml"
log "github.com/sirupsen/logrus"
re "gopkg.in/gorethink/gorethink.v4"
es "gopkg.in/olivere/elastic.v5"
)
// Context type
type Context struct {
AppName string `yaml:"appname"`
Version int `yaml:"version"`
Decoders struct {
Path string `yaml:"path"`
} `yaml:"decoders"`
Owner struct {
ID string `yaml:"id"`
AppxBootstrapURI string `yaml:"appx_bootstrap_uri"`
StoragePrefList []string `yaml:"storage_pref_list"`
QueueFlushCount int `yaml:"queue_flush_count"`
QueueFlushTime int64 `yaml:"queue_flush_time"`
} `yaml:"owner"`
SSL struct {
Certificate string `yaml:"certificate"`
PrivateKey string `yaml:"private_key"`
TrustChain string `yaml:"trust_chain"`
} `yaml:"ssl"`
Mongo struct {
URI string `yaml:"uri"`
} `yaml:"mongo"`
RethinkDB struct {
URI string `yaml:"uri"`
URIs []string `yaml:"uris"`
DB string `yaml:"db"`
Collection string `yaml:"collection"`
InitialCap int `yaml:"initial_cap"`
MaxOpen int `yaml:"max_open"`
} `yaml:"rethinkdb"`
Elastic struct {
Hosts []string `yaml:"hosts"`
Index string `yaml:"index"`
} `yaml:"elastic"`
Mqtt struct {
Brokers []string `yaml:"brokers"`
Certificate string `yaml:"certificate"`
PrivateKey string `yaml:"private_key"`
User string `yaml:"user"`
Password string `yaml:"password"`
DnTopic string `yaml:"dntopic"`
UpTopic string `yaml:"uptopic"`
UpQoS byte `yaml:"upqos"`
DnQoS byte `yaml:"dnqos"`
} `yaml:"mqtt"`
Filters struct {
DevEui []string `yaml:"deveui"`
MsgType []string `yaml:"msg_type"`
} `yaml:"filters"`
Inventory map[string]string `yaml:"inventory"`
DecodingPlugins map[string]func(string) (interface{}, error)
Appxs TCIOInstance
CompilledFilters *DevEuiFilters
reSession *re.Session
esClient *es.Client
mqttClient mqtt.Client
mqttOptions *mqtt.ClientOptions
}
// TCIOInstance type
type TCIOInstance struct {
Error string `json:"error"`
Owner string `json:"owner"`
AppxList []ExchangePoint `json:"appx_list"`
Version uint32 `json:"version"`
Release uint32 `json:"release"`
}
// ExchangePoint type
type ExchangePoint struct {
Appxid string `json:"appxid"`
URI string `json:"uri"`
}
// DevEuiFilters type
type DevEuiFilters struct {
ReExpressions []*regexp.Regexp
//mu sync.Mutex
}
type Decoder struct {
Type string
Version string
}
//var devEuiFilters *DevEuiFilters
// CreateContext func
func CreateContext(config string) *Context {
ctx := Context{}
raw, err := ioutil.ReadFile(config)
if err != nil {
logger.WithFields(log.Fields{"config": config}).Fatalf("Can't load config file %+v", err)
}
err = yaml.Unmarshal(raw, &ctx)
if err != nil {
logger.WithFields(log.Fields{"config": config}).Fatalf("Can't parse config file %+v", err)
}
ctx.CompileFilters()
return &ctx
}
// InitBackends func
func (ctx *Context) InitBackends() {
for _, storage := range ctx.Owner.StoragePrefList {
var err error
switch storage {
case "rethinkdb":
if ctx.RethinkDB.URI != "" && ctx.RethinkDB.DB != "" && ctx.RethinkDB.Collection != "" {
ctx.reSession, err = re.Connect(re.ConnectOpts{
Address: ctx.RethinkDB.URI,
Addresses: ctx.RethinkDB.URIs,
InitialCap: ctx.RethinkDB.InitialCap,
MaxOpen: ctx.RethinkDB.MaxOpen,
})
if err != nil {
logger.Fatalf("Error connecting to %s %+v", storage, err)
}
break
}
logger.Fatalf("%s listed in pipeline but not configured: %+v", storage, ctx.RethinkDB)
break
case "elastic":
if len(ctx.Elastic.Hosts) != 0 && ctx.Elastic.Index != "" {
ctx.esClient, err = es.NewClient(es.SetURL(ctx.Elastic.Hosts...))
if err != nil {
logger.Fatalf("Error connecting to %s %+v", storage, err)
}
break
}
logger.Fatalf("%s listed in pipeline but not configured: %+v", storage, ctx.Elastic)
break
case "mqtt":
if len(ctx.Mqtt.Brokers) != 0 && ctx.Mqtt.User != "" && ctx.Mqtt.Password != "" && ctx.Mqtt.DnTopic != "" && ctx.Mqtt.UpTopic != "" {
for _, broker := range ctx.Mqtt.Brokers {
ctx.mqttOptions = mqtt.NewClientOptions().AddBroker(broker)
}
cer, err := tls.LoadX509KeyPair(ctx.Mqtt.Certificate, ctx.Mqtt.PrivateKey)
if err != nil {
logger.Fatalf("Something goes wrong with MQTT SSL certs loading %+v", err)
}
ctx.mqttOptions.SetUsername(ctx.Mqtt.User)
ctx.mqttOptions.SetPassword(ctx.Mqtt.Password)
ctx.mqttOptions.SetClientID(ctx.AppName)
ctx.mqttOptions.SetConnectTimeout(time.Second * 3)
ctx.mqttOptions.SetTLSConfig(&tls.Config{Certificates: []tls.Certificate{cer}, InsecureSkipVerify: true})
ctx.mqttOptions.SetConnectionLostHandler(func(c mqtt.Client, err error) {
logger.Errorln("Mqtt disconnected, trying to reconnect...")
ctx.reconnectMqtt()
})
ctx.mqttClient = mqtt.NewClient(ctx.mqttOptions)
if token := ctx.mqttClient.Connect(); token.Wait() && token.Error() != nil {
logger.Fatalf("Error connecting to %s %+v", storage, token.Error())
}
if token := ctx.mqttClient.Subscribe(ctx.Mqtt.DnTopic, ctx.Mqtt.UpQoS, pool.handleMqttDnMessage); token.Wait() && token.Error() != nil {
logger.Fatalf("Error subscribe to mqtt uptopic %s: %+v", ctx.Mqtt.UpTopic, token.Error())
}
break
}
logger.Fatalf("%s listed in pipeline but not configured: %+v", storage, ctx.Mqtt)
break
}
}
}
func (ctx *Context) reconnectMqtt() {
ticker := time.NewTicker(time.Second * 3)
if ctx.mqttClient.IsConnected() {
ctx.mqttClient.Disconnect(1000)
}
defer ticker.Stop()
for {
select {
case <-ticker.C:
logger.Infoln("Trying reconnect to MQTT...")
ctx.mqttClient = mqtt.NewClient(ctx.mqttOptions)
if token := ctx.mqttClient.Connect(); token.Wait() && token.Error() != nil {
logger.Errorf("Error connecting to mqtt %+v", token.Error())
} else {
if token := ctx.mqttClient.Subscribe(ctx.Mqtt.DnTopic, ctx.Mqtt.UpQoS, pool.handleMqttDnMessage); token.Wait() && token.Error() != nil {
logger.Errorf("Error subscribe to mqtt uptopic %s: %+v", ctx.Mqtt.UpTopic, token.Error())
}
ticker.Stop()
logger.Infoln("Mqtt reconnected")
return
}
}
}
}
// func (ctx *Context) reconnectMqtt() {
// ticker := time.NewTicker(time.Second * 3)
// if ctx.mqttClient.IsConnected() {
// ctx.mqttClient.Disconnect(1000)
// }
// defer ticker.Stop()
// for {
// select {
// case <-ticker.C:
// var opts *mqtt.ClientOptions
// for _, broker := range ctx.Mqtt.Brokers {
// opts = mqtt.NewClientOptions().AddBroker(broker)
// }
// /// ADD SSL AUTODETECT
// cer, err := tls.LoadX509KeyPair(ctx.Mqtt.Certificate, ctx.Mqtt.PrivateKey)
// if err != nil {
// logger.Fatalf("Something goes wrong with MQTT SSL certs loading %+v", err)
// }
// opts.SetUsername(ctx.Mqtt.User)
// opts.SetPassword(ctx.Mqtt.Password)
// opts.SetClientID(ctx.AppName)
// opts.SetConnectTimeout(time.Second * 3)
// opts.SetTLSConfig(&tls.Config{Certificates: []tls.Certificate{cer}, InsecureSkipVerify: true})
// opts.SetConnectionLostHandler(func(c mqtt.Client, err error) {
// logger.Errorln("Mqtt disconnected, trying to reconnect...")
// ctx.reconnectMqtt()
// })
// ctx.mqttClient = mqtt.NewClient(opts)
// if token := ctx.mqttClient.Connect(); token.Wait() && token.Error() != nil {
// logger.Errorf("Error connecting to mqtt %+v", token.Error())
// ticker = time.NewTicker(time.Second * 1)
// } else {
// logger.Infoln("Mqtt reconnected")
// if token := ctx.mqttClient.Subscribe(ctx.Mqtt.DnTopic, ctx.Mqtt.UpQoS, pool.handleMqttDnMessage); token.Wait() && token.Error() != nil {
// logger.Errorf("Error subscribe to mqtt uptopic %s: %+v", ctx.Mqtt.UpTopic, token.Error())
// }
// ticker.Stop()
// return
// }
// }
// }
// }
// LoadDecoders func
func (ctx *Context) LoadDecoders() {
// init decoders map
ctx.DecodingPlugins = make(map[string]func(string) (interface{}, error))
allDecoders, err := filepath.Glob(ctx.Decoders.Path + "/*.so")
if err != nil {
logger.WithFields(log.Fields{"path": ctx.Decoders.Path}).Fatalf("Can't list decoders dir: %v", err)
}
for _, decoder := range allDecoders {
// try to load decoder
p, err := plugin.Open(decoder)
if err != nil {
logger.WithFields(log.Fields{"decoder": decoder}).Fatalf("Can't load decoder: %v", err)
}
// import descriptive type of decoder
decoderType, err := p.Lookup("Decoder")
if err != nil {
logger.WithFields(log.Fields{"decoder": decoder}).Fatalf("Can't import type of decoder: %v", err)
}
// import decoder method
decodeMethod, err := p.Lookup("Decode")
if err != nil {
logger.WithFields(log.Fields{"decoder": decoder}).Fatalf("Can't import decoder method: %v", err)
}
ctx.DecodingPlugins[*decoderType.(*string)] = decodeMethod.(func(string) (interface{}, error))
}
for decoderType := range ctx.DecodingPlugins {
logger.Infof("Decoder loaded: %s", decoderType)
}
}
// CompileFilters func
func (ctx *Context) CompileFilters() /**DevEuiFilters*/ {
//ctx.CompilledFilters.mu.Lock()
//defer ctx.CompilledFilters.mu.Unlock()
df := DevEuiFilters{}
for _, expr := range ctx.Filters.DevEui {
df.ReExpressions = append(df.ReExpressions, regexp.MustCompile(expr))
}
ctx.CompilledFilters = &df
}
// ReloadConfig func
func (ctx *Context) ReloadConfig(config string) {
tmp := Context{}
raw, err := ioutil.ReadFile(config)
if err != nil {
logger.WithFields(log.Fields{"config": config}).Fatalf("Can't load config file %+v", err)
}
err = yaml.Unmarshal(raw, &tmp)
if err != nil {
logger.WithFields(log.Fields{"config": config}).Fatalf("Can't parse config file %+v", err)
}
ctx.Filters = tmp.Filters
ctx.Inventory = tmp.Inventory
ctx.CompileFilters()
}
// CheckRethinkAlive func
func (ctx *Context) CheckRethinkAlive() {
var err error
if !ctx.reSession.IsConnected() {
ctx.reSession, err = re.Connect(re.ConnectOpts{
Address: ctx.RethinkDB.URI,
Addresses: ctx.RethinkDB.URIs,
InitialCap: ctx.RethinkDB.InitialCap,
MaxOpen: ctx.RethinkDB.MaxOpen,
})
if err != nil {
logger.Fatalf("Error REconnecting to rethinkdb %+v", err)
}
logger.Warnln("Lost rethinkdb connect, reconnected")
}
}
// CheckElasticAlive func
func (ctx *Context) CheckElasticAlive() {
}