48 lines
982 B
Go
48 lines
982 B
Go
package main
|
|
|
|
import (
|
|
"io/ioutil"
|
|
|
|
"gopkg.in/yaml.v2"
|
|
)
|
|
|
|
type Config struct {
|
|
Remote string `yaml:"remote"` // Remote IP address of the Smartbridge
|
|
Mqtt MqttConfig `yaml:"mqtt"` // mqtt configuration
|
|
Delay int `yaml:"delay"`
|
|
Verbose bool `yaml:"verbose"`
|
|
}
|
|
|
|
type MqttConfig struct {
|
|
Remote string `yaml:"remote"`
|
|
Port int `yaml:"port"`
|
|
ClientID string `yaml:"clientid"`
|
|
topic string `yaml:"topic"`
|
|
Username string `yaml:"username"`
|
|
Password string `yaml:"password"`
|
|
}
|
|
|
|
var cf Config
|
|
|
|
func (cf *Config) SetDefaults() {
|
|
cf.Remote = "127.0.0.1"
|
|
cf.Delay = 5
|
|
cf.Mqtt.Remote = "127.0.0.1"
|
|
cf.Mqtt.Port = 1883
|
|
cf.Mqtt.ClientID = "smartbridge"
|
|
cf.Mqtt.topic = "home/power"
|
|
cf.Mqtt.Username = ""
|
|
cf.Mqtt.Password = ""
|
|
}
|
|
|
|
func (cf *Config) LoadYAML(filename string) error {
|
|
content, err := ioutil.ReadFile(filename)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := yaml.Unmarshal(content, cf); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|