-
Notifications
You must be signed in to change notification settings - Fork 7
/
config.go
69 lines (59 loc) · 1.79 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
package main
import (
"io/ioutil"
"github.com/maxatome/go-vitotrol"
"gopkg.in/yaml.v2"
)
type ConfigInflux struct {
Login string `yaml:"login"`
Password string `yaml:"password"`
Address string `yaml:"address"`
}
type ConfigVitotrol struct {
Login string `yaml:"login"`
Password string `yaml:"password"`
RetryTimeout uint `yaml:"retry_timeout"`
Frequency uint `yaml:"frequency"`
}
type ConfigDevice struct {
Name string `yaml:"name"`
Location string `yaml:"location"`
Database string `yaml:"database"`
Precision string `yaml:"precision"`
RetentionPolicy string `yaml:"retention_policy"`
WriteConsistency string `yaml:"write_consistency"`
Measurement string `yaml:"measurement"`
Tags map[string]string `yaml:"tags"`
Fields []string `yaml:"fields"`
attrs []vitotrol.AttrID
computedFields []string
}
type Config struct {
Influx ConfigInflux `yaml:"influx"`
Vitotrol ConfigVitotrol `yaml:"vitotrol"`
Devices []*ConfigDevice `yaml:"devices"`
}
// ReadConfig load a YAML config file and returns its contents.
func ReadConfig(filename string) (*Config, error) {
data, err := ioutil.ReadFile(filename)
if err != nil {
return nil, err
}
var conf Config
err = yaml.Unmarshal(data, &conf)
if err != nil {
return nil, err
}
return &conf, nil
}
// GetConfigDevice returns the ConfigDevice corresponding to the
// device name in location location.
func (c *Config) GetConfigDevice(name, location string) *ConfigDevice {
for _, device := range c.Devices {
if (device.Name == "*" || device.Name == name) &&
(device.Location == "*" || device.Location == location) {
return device
}
}
return nil
}