From 64ae21da53200eb62950c221148915db1a1a1dfb Mon Sep 17 00:00:00 2001 From: Nicholas Thompson Date: Mon, 15 Jun 2020 12:47:27 +0200 Subject: [PATCH] Update config to be passed in as environment variables --- cmd/invertergui/config.go | 34 ++++++++++++++++++++++++++++++++++ cmd/invertergui/main.go | 32 ++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 10 deletions(-) create mode 100644 cmd/invertergui/config.go diff --git a/cmd/invertergui/config.go b/cmd/invertergui/config.go new file mode 100644 index 0000000..79de9a2 --- /dev/null +++ b/cmd/invertergui/config.go @@ -0,0 +1,34 @@ +package main + +import ( + "github.com/jessevdk/go-flags" +) + +type config struct { + Address string `long:"address" env:"ADDRESS" default:":8080" description:"The IP/DNS and port of the machine that the application is running on."` + Data struct { + Source string `long:"data.source" env:"DATA_SOURCE" default:"serial" description:"Set the source of data for the inverter gui. \"serial\", \"tcp\" or \"mock\""` + Host string `long:"data.host" env:"DATA_HOST" default:"localhost:8139" description:"Host to connect when source is set to tcp."` + Device string `long:"data.device" env:"DATA_DEVICE" default:"/dev/ttyUSB0" description:"TTY device to use when source is set to serial."` + } + Cli struct { + Enabled bool `long:"cli.enabled" env:"CLI_ENABLED" description:"Enable CLI output."` + } + MQTT struct { + Enabled bool `long:"mqtt.enabled" env:"MQTT_ENABLED" description:"Enable MQTT publishing."` + Broker string `long:"mqtt.broker" env:"MQTT_BROKER" default:"tcp://localhost:1883" description:"Set the host port and scheme of the MQTT broker."` + ClientID string `long:"mqtt.client_id" env:"MQTT_CLIENT_ID" default:"interter-gui" description:"Set the client ID for the MQTT connection."` + Topic string `long:"mqtt.topic" env:"MQTT_TOPIC" default:"invertergui/updates" description:"Set the MQTT topic updates published to."` + Username string `long:"mqtt.username" env:"MQTT_USERNAME" default:"" description:"Set the MQTT username"` + Password string `long:"mqtt.password" env:"MQTT_PASSWORD" default:"" description:"Set the MQTT password"` + } +} + +func parseConfig() (*config, error) { + conf := &config{} + parser := flags.NewParser(conf, flags.Default) + if _, err := parser.Parse(); err != nil { + return nil, err + } + return conf, nil +} diff --git a/cmd/invertergui/main.go b/cmd/invertergui/main.go index 100576f..d552a96 100644 --- a/cmd/invertergui/main.go +++ b/cmd/invertergui/main.go @@ -31,7 +31,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. package main import ( - "flag" "io" "log" "net" @@ -41,6 +40,7 @@ import ( "github.com/diebietse/invertergui/mk2core" "github.com/diebietse/invertergui/mk2driver" "github.com/diebietse/invertergui/plugins/cli" + "github.com/diebietse/invertergui/plugins/mqttclient" "github.com/diebietse/invertergui/plugins/munin" "github.com/diebietse/invertergui/plugins/prometheus" "github.com/diebietse/invertergui/plugins/webui" @@ -50,19 +50,17 @@ import ( ) func main() { - source := flag.String("source", "serial", "Set the source of data for the inverter gui. \"serial\", \"tcp\" or \"mock\"") - addr := flag.String("addr", ":8080", "TCP address to listen on.") - ip := flag.String("ip", "localhost:8139", "IP to connect when using tcp connection.") - dev := flag.String("dev", "/dev/ttyUSB0", "TTY device to use.") - cliEnable := flag.Bool("cli", false, "Enable CLI output") - flag.Parse() + conf, err := parseConfig() + if err != nil { + os.Exit(1) + } - mk2 := getMk2Device(*source, *ip, *dev) + mk2 := getMk2Device(conf.Data.Source, conf.Data.Host, conf.Data.Device) defer mk2.Close() core := mk2core.NewCore(mk2) - if *cliEnable { + if conf.Cli.Enabled { cli.NewCli(core.NewSubscription()) } @@ -80,7 +78,21 @@ func main() { prometheus.NewPrometheus(core.NewSubscription()) http.Handle("/metrics", promhttp.Handler()) - log.Fatal(http.ListenAndServe(*addr, nil)) + // MQTT + if conf.MQTT.Enabled { + mqttConf := mqttclient.Config{ + Broker: conf.MQTT.Broker, + Topic: conf.MQTT.Topic, + ClientID: conf.MQTT.ClientID, + Username: conf.MQTT.Username, + Password: conf.MQTT.Password, + } + if err := mqttclient.New(core.NewSubscription(), mqttConf); err != nil { + log.Printf("Could not setup MQTT client: %v", err) + } + } + + log.Fatal(http.ListenAndServe(conf.Address, nil)) } func getMk2Device(source, ip, dev string) mk2driver.Mk2 {