1
0
Fork 0
mirror of https://github.com/shouptech/tempgopher.git synced 2026-02-03 08:39:43 +00:00
tempgopher/config.go
Mike Shoup 446fb72189 Add configuration parameter for listen address
You can now specify :8080, 127.0.0.1:5555 or whatever
2018-10-03 20:05:00 -06:00

65 lines
1.7 KiB
Go

package main
import (
"errors"
"io/ioutil"
"gopkg.in/yaml.v2"
)
// Sensor defines configuration for a temperature sensor.
type Sensor struct {
ID string `json:"id" yaml:"id"`
Alias string `json:"alias" yaml:"alias"`
HighTemp float64 `json:"hightemp" yaml:"hightemp"`
LowTemp float64 `json:"lowtemp" yaml:"lowtemp"`
HeatGPIO int32 `json:"heatgpio" yaml:"heatgpio"`
HeatInvert bool `json:"heatinvert" yaml:"heatinvert"`
HeatMinutes float64 `json:"heatminutes" yaml:"heatminutes"`
CoolGPIO int32 `json:"coolgpio" yaml:"coolgpio"`
CoolInvert bool `json:"coolinvert" yaml:"coolinvert"`
CoolMinutes float64 `json:"coolminutes" yaml:"coolminutes"`
Verbose bool `json:"verbose" yaml:"verbose"`
}
// Config contains the applications configuration
type Config struct {
Sensors []Sensor `yaml:"sensors"`
BaseURL string `yaml:"baseurl"`
ListenAddr string `yaml:"listenaddr"`
}
// LoadConfig will loads a file and parses it into a Config struct
func LoadConfig(path string) (*Config, error) {
data, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config Config
yaml.Unmarshal(data, &config)
// Set a default listen address if not define.
if config.ListenAddr == "" {
config.ListenAddr = ":8080"
}
// Check for Duplicates
ids := make(map[string]bool)
aliases := make(map[string]bool)
for _, v := range config.Sensors {
if !ids[v.ID] {
ids[v.ID] = true
} else {
return nil, errors.New("Duplicate sensor ID found in configuration")
}
if !aliases[v.Alias] {
aliases[v.Alias] = true
} else {
return nil, errors.New("Duplicate sensor alias found in configuration")
}
}
return &config, nil
}