This repository was archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathconfig.go
54 lines (51 loc) · 1.38 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
package main
import (
"github.com/pelletier/go-toml"
"io/ioutil"
"strings"
)
type Config struct {
Address string
Username string
Password string
BatteryTopic string `toml:"battery_topic"`
AvailabilityTopic string `toml:"availability_topic"`
PayloadAvailable string `toml:"payload_available"`
PayloadNotAvailable string `toml:"payload_not_available"`
ScooterNaming string `toml:"scooter_naming"`
IgnoredScooters []string `toml:"ignored_scooters"`
ignoredScootersMap map[string]struct{} `toml:"-"`
}
func getConfig() (config *Config, err error) {
config = &Config{}
data, err := ioutil.ReadFile("config.toml")
if err == nil {
err = toml.Unmarshal(data, config)
}
config.ignoredScootersMap = make(map[string]struct{})
if config.Address == "" {
config.Address = "127.0.0.1:1883"
}
if config.BatteryTopic == "" {
config.BatteryTopic = "ninebot/%s/battery"
}
if config.AvailabilityTopic == "" {
config.AvailabilityTopic = "ninebot/%s/available"
}
if config.AvailabilityTopic == "disable" {
config.AvailabilityTopic = ""
}
if config.PayloadAvailable == "" {
config.PayloadAvailable = "online"
}
if config.PayloadNotAvailable == "" {
config.PayloadNotAvailable = "offline"
}
if config.ScooterNaming == "" {
config.ScooterNaming = "name"
}
for _, ignored := range config.IgnoredScooters {
config.ignoredScootersMap[strings.ToLower(ignored)] = struct{}{}
}
return
}