forked from iegomez/lds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
117 lines (101 loc) · 3.01 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
package main
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/BurntSushi/toml"
"github.com/brocaar/lorawan"
"github.com/iegomez/lds/lds"
log "github.com/sirupsen/logrus"
)
type redisConf struct {
Addr string `toml:"addr"`
Password string `toml:"password"`
DB int `toml:"db"`
}
type tomlConfig struct {
MQTT mqtt `toml:"mqtt"`
Forwarder forwarder `toml:"forwarder"`
Band band `toml:"band"`
Device device `toml:"device"`
GW gateway `toml:"gateway"`
DR dataRate `toml:"data_rate"`
RXInfo rxInfo `toml:"rx_info"`
RawPayload rawPayload `toml:"raw_payload"`
EncodedType []*encodedType `toml:"encoded_type"`
LogLevel string `toml:"log_level"`
RedisConf redisConf `toml:"redis"`
Provisioner provisioner `toml:"provisioner"`
}
// Configuration holders.
var (
confFile *string
config *tomlConfig
)
// Configuration files loading and saving.
var (
openFile bool
files []os.FileInfo
saveFile bool
saveFilename string
)
const defaultMaxExecTime = 100
func importConf() {
//When config hasn't been initialized we need to provide fresh zero instances with some defaults.
//Decoding the conf file will override any present option.
if config == nil {
config = &tomlConfig{
MQTT: mqtt{},
Forwarder: forwarder{},
Band: band{},
Device: device{MType: lorawan.UnconfirmedDataUp},
GW: gateway{},
DR: dataRate{},
RXInfo: rxInfo{},
RawPayload: rawPayload{MaxExecTime: defaultMaxExecTime},
EncodedType: []*encodedType{},
Provisioner: provisioner{},
}
}
if _, err := toml.DecodeFile(*confFile, &config); err != nil {
log.Println(err)
return
}
log.SetLevel(log.InfoLevel)
if l, err := log.ParseLevel(config.LogLevel); err != nil {
log.SetLevel(l)
}
//Try to set redis.
lds.StartRedis(config.RedisConf.Addr, config.RedisConf.Password, config.RedisConf.DB)
//Fill string representations of numeric values.
config.DR.BitRateS = strconv.Itoa(config.DR.BitRate)
config.RXInfo.ChannelS = strconv.Itoa(config.RXInfo.Channel)
config.RXInfo.CrcStatusS = strconv.Itoa(config.RXInfo.CrcStatus)
config.RXInfo.FrequencyS = strconv.Itoa(config.RXInfo.Frequency)
config.RXInfo.LoRASNRS = strconv.FormatFloat(config.RXInfo.LoRaSNR, 'f', -1, 64)
config.RXInfo.RfChainS = strconv.Itoa(config.RXInfo.RfChain)
config.RXInfo.RssiS = strconv.Itoa(config.RXInfo.Rssi)
//Set default script when it's not present.
if config.RawPayload.Script == "" {
config.RawPayload.Script = defaultScript
}
}
func exportConf(filename string) {
if !strings.Contains(filename, ".toml") {
filename = fmt.Sprintf("%s.toml", filename)
}
f, err := os.Create(filename)
if err != nil {
log.Errorf("export error: %s", err)
return
}
encoder := toml.NewEncoder(f)
err = encoder.Encode(config)
if err != nil {
log.Errorf("export error: %s", err)
return
}
log.Infof("exported conf file %s", f.Name())
*confFile = f.Name()
}