|
| 1 | +package config |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "os" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/lazy-electron-consulting/ve-direct-exporter/internal/util" |
| 10 | + "gopkg.in/yaml.v2" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + DefaultAddress = ":8000" |
| 15 | + DefaultBaudRate = 19200 |
| 16 | + DefaultDataBits = 8 |
| 17 | + DefaultStopBits = 1 |
| 18 | + DefaultParity = "N" |
| 19 | + DefaultTimeout = 5 * time.Second |
| 20 | +) |
| 21 | + |
| 22 | +type Serial struct { |
| 23 | + Path string `json:"path,omitempty" yaml:"path,omitempty"` |
| 24 | + BaudRate int `json:"baudRate,omitempty" yaml:"baudRate,omitempty"` |
| 25 | + DataBits int `json:"dataBits,omitempty" yaml:"dataBits,omitempty"` |
| 26 | + StopBits int `json:"stopBits,omitempty" yaml:"stopBits,omitempty"` |
| 27 | + Parity string `json:"parity,omitempty" yaml:"parity,omitempty"` |
| 28 | + Timeout time.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"` |
| 29 | +} |
| 30 | + |
| 31 | +func (s *Serial) defaults() { |
| 32 | + s.BaudRate = util.Default(s.BaudRate, DefaultBaudRate) |
| 33 | + s.DataBits = util.Default(s.DataBits, DefaultDataBits) |
| 34 | + s.StopBits = util.Default(s.StopBits, DefaultStopBits) |
| 35 | + s.Parity = util.Default(s.Parity, DefaultParity) |
| 36 | + s.Timeout = util.Default(s.Timeout, DefaultTimeout) |
| 37 | +} |
| 38 | + |
| 39 | +type Gauge struct { |
| 40 | + Name string `json:"name,omitempty" yaml:"name,omitempty"` |
| 41 | + Label string `json:"label,omitempty" yaml:"label,omitempty"` |
| 42 | + Help string `json:"help,omitempty" yaml:"help,omitempty"` |
| 43 | + Multiplier float64 `json:"multiplier,omitempty" yaml:"multiplier,omitempty"` |
| 44 | +} |
| 45 | + |
| 46 | +func (g *Gauge) defaults() { |
| 47 | + g.Multiplier = util.Default(g.Multiplier, 1) |
| 48 | +} |
| 49 | + |
| 50 | +type Config struct { |
| 51 | + Address string `json:"address,omitempty" yaml:"address,omitempty"` |
| 52 | + Subsystem string `json:"subsystem,omitempty" yaml:"subsystem,omitempty"` |
| 53 | + Serial Serial `json:"serial,omitempty" yaml:"serial,omitempty"` |
| 54 | + Gauges []Gauge `json:"gauges,omitempty" yaml:"gauges,omitempty"` |
| 55 | +} |
| 56 | + |
| 57 | +func (c *Config) defaults() { |
| 58 | + c.Address = util.Default(c.Address, DefaultAddress) |
| 59 | + c.Serial.defaults() |
| 60 | + for i, g := range c.Gauges { |
| 61 | + g.defaults() |
| 62 | + c.Gauges[i] = g |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func ParseYaml(r io.Reader) (*Config, error) { |
| 67 | + decoder := yaml.NewDecoder(r) |
| 68 | + decoder.SetStrict(true) |
| 69 | + |
| 70 | + var config Config |
| 71 | + if err := decoder.Decode(&config); err != nil { |
| 72 | + return nil, fmt.Errorf("failed to parse: %w", err) |
| 73 | + } |
| 74 | + config.defaults() |
| 75 | + return &config, nil |
| 76 | +} |
| 77 | + |
| 78 | +func ReadYaml(path string) (*Config, error) { |
| 79 | + f, err := os.Open(path) |
| 80 | + if err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + return ParseYaml(f) |
| 84 | +} |
0 commit comments