commit 0db6b84e092c81a831d117aa5b62cd0e141c83f6 Author: Knut Ahlers Date: Sun Jul 5 01:21:48 2020 +0200 First working prototype diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3d8d293 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +config.yaml +.env diff --git a/config.go b/config.go new file mode 100644 index 0000000..618599e --- /dev/null +++ b/config.go @@ -0,0 +1,45 @@ +package main + +import ( + "os" + "strings" + + "github.com/pkg/errors" + "gopkg.in/yaml.v2" +) + +type configFile struct { + CommandPrefix string `yaml:"command_prefix"` + StatPrefix string `yaml:"stat_prefix"` + + Settings map[string]interface{} `yaml:"settings"` + + Devices map[string]deviceConfig `yaml:"devices"` +} + +type deviceConfig struct { + Topic string `yaml:"topic"` + Settings map[string]interface{} `yaml:"settings"` +} + +func (d deviceConfig) constructTopic(prefix, suffix string) string { + return strings.Join([]string{ + strings.Trim(prefix, "/"), + strings.Trim(d.Topic, "/"), + suffix, + }, "/") +} + +func loadConfig(p string) (*configFile, error) { + f, err := os.Open(p) + if err != nil { + return nil, errors.Wrap(err, "Unable to open config file") + } + defer f.Close() + + var out = &configFile{ + CommandPrefix: "cmnd", + StatPrefix: "stat", + } + return out, errors.Wrap(yaml.NewDecoder(f).Decode(out), "Unable to decode config file") +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..4a4fa92 --- /dev/null +++ b/go.mod @@ -0,0 +1,12 @@ +module github.com/Luzifer/tasmota-config + +go 1.14 + +require ( + github.com/Luzifer/rconfig/v2 v2.2.1 + github.com/eclipse/paho.mqtt.golang v1.2.0 + github.com/pkg/errors v0.9.1 + github.com/sirupsen/logrus v1.6.0 + golang.org/x/net v0.0.0-20200625001655-4c5254603344 // indirect + gopkg.in/yaml.v2 v2.2.2 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1615007 --- /dev/null +++ b/go.sum @@ -0,0 +1,33 @@ +github.com/Luzifer/rconfig v1.2.0 h1:waD1sqasGVSQSrExpLrQ9Q1JmMaltrS391VdOjWXP/I= +github.com/Luzifer/rconfig/v2 v2.2.1 h1:zcDdLQlnlzwcBJ8E0WFzOkQE1pCMn3EbX0dFYkeTczg= +github.com/Luzifer/rconfig/v2 v2.2.1/go.mod h1:OKIX0/JRZrPJ/ZXXWklQEFXA6tBfWaljZbW37w+sqBw= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/eclipse/paho.mqtt.golang v1.2.0 h1:1F8mhG9+aO5/xpdtFkW4SxOJB67ukuDC3t2y2qayIX0= +github.com/eclipse/paho.mqtt.golang v1.2.0/go.mod h1:H9keYFcgq3Qr5OUJm/JZI/i6U7joQ8SYLhZwfeOo6Ts= +github.com/konsorten/go-windows-terminal-sequences v1.0.3 h1:CE8S1cTafDpPvMhIxNJKvHsGVBgn1xWYf1NbHQhywc8= +github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I= +github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88= +github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg= +github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20200625001655-4c5254603344 h1:vGXIOMxbNfDTk/aXCmfdLgkrSV+Z2tcbze+pEc3v5W4= +golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= +golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884= +golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19 h1:WB265cn5OpO+hK3pikC9hpP1zI/KTwmyMFKloW9eOVc= +gopkg.in/validator.v2 v2.0.0-20180514200540-135c24b11c19/go.mod h1:o4V0GXN9/CAmCsvJ0oXYZvrZOe7syiDZSN1GWGZTGzc= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= diff --git a/main.go b/main.go new file mode 100644 index 0000000..3cd16d4 --- /dev/null +++ b/main.go @@ -0,0 +1,178 @@ +package main + +import ( + "fmt" + "os" + "strings" + "time" + + mqtt "github.com/eclipse/paho.mqtt.golang" + "github.com/pkg/errors" + log "github.com/sirupsen/logrus" + + "github.com/Luzifer/rconfig/v2" +) + +var ( + cfg = struct { + Config string `flag:"config,c" default:"config.yaml" description:"Configuration file with settings"` + Device string `flag:"device,d" default:"" description:"Limit execution to specific device by name"` + DryRun bool `flag:"dry-run,n" default:"false" description:"Do not execute write actions, just print changes"` + LogLevel string `flag:"log-level" default:"info" description:"Log level (debug, info, warn, error, fatal)"` + MQTTBroker string `flag:"mqtt-broker" default:"tcp://localhost:1883" description:"MQTT Broker to connect to"` + MQTTCommandTimeout time.Duration `flag:"mqtt-command-timeout" default:"2s" description:"How long to wait for commands to succeed"` + MQTTPassword string `flag:"mqtt-password" default:"" description:"Credentials for MQTT-Broker"` + MQTTUsername string `flag:"mqtt-username" default:"" description:"Credentials for MQTT-Broker"` + VersionAndExit bool `flag:"version" default:"false" description:"Prints current version and exits"` + }{} + + version = "dev" +) + +func init() { + rconfig.AutoEnv(true) + if err := rconfig.ParseAndValidate(&cfg); err != nil { + log.Fatalf("Unable to parse commandline options: %s", err) + } + + if cfg.VersionAndExit { + fmt.Printf("tasmota-config %s\n", version) + os.Exit(0) + } + + if l, err := log.ParseLevel(cfg.LogLevel); err != nil { + log.WithError(err).Fatal("Unable to parse log level") + } else { + log.SetLevel(l) + } +} + +func main() { + mqttConfig := mqtt.NewClientOptions(). + AddBroker(cfg.MQTTBroker). + SetPassword(cfg.MQTTPassword). + SetUsername(cfg.MQTTUsername) + mqttClient := mqtt.NewClient(mqttConfig) + + if err := checkToken(mqttClient.Connect()); err != nil { + log.WithError(err).Fatal("Unable to connect to broker due to error or timeout") + } + + config, err := loadConfig(cfg.Config) + if err != nil { + log.WithError(err).Fatal("Unable to load config file") + } + + for devName, devConfig := range config.Devices { + if cfg.Device != "" && devName != cfg.Device { + log.WithField("name", devName).Debug("Skipping device as requested") + continue + } + + if err := processDevice(mqttClient, config, devName, devConfig); err != nil { + log.WithField("name", devName).WithError(err).Error("Unable to process device") + } + } +} + +func processDevice(mqttClient mqtt.Client, config *configFile, devName string, devConfig deviceConfig) error { + log.WithField("name", devName).Info("Starting device config") + defer log.WithField("name", devName).Info("Finished device config") + + var ( + responses = make(chan []byte, 30) + updates []string + ) + defer close(responses) + + if err := checkToken(mqttClient.Subscribe( + devConfig.constructTopic(config.StatPrefix, "RESULT"), + 1, + func(c mqtt.Client, m mqtt.Message) { responses <- m.Payload() }, + )); err != nil { + return errors.Wrap(err, "Unable to subscribe to topic due to error or timeout") + } + defer mqttClient.Unsubscribe(devConfig.constructTopic(config.StatPrefix, "RESULT")) + + for setName, setValue := range mergeSettings(config.Settings, devConfig.Settings) { + if err := checkToken(mqttClient.Publish( + devConfig.constructTopic(config.CommandPrefix, setName), + 1, false, + "", + )); err != nil { + return errors.Wrap(err, "Unable to send request command") + } + + resp, err := extractSettingValue(setName, responses) + if err != nil { + return errors.Wrap(err, "Unable to extract settings value") + } + + if resp == setValue { + log.WithFields(log.Fields{ + "name": devName, + "expected": setValue, + "setting": setName, + }).Debug("Value is fine") + continue + } + + log.WithFields(log.Fields{ + "actual": fmt.Sprintf("%#v (%T)", resp, resp), + "expected": fmt.Sprintf("%#v (%T)", setValue, setValue), + "name": devName, + "setting": setName, + }).Warn("Value needs adjustment") + + updates = append(updates, fmt.Sprintf("%s %v", setName, setValue)) + } + + if len(updates) == 0 { + log.WithField("name", devName).Info("Device looks good, nothing to do") + } + + if cfg.DryRun { + log.WithField("name", devName).Info("Device needs updates but requested dry-run") + return nil + } + + log.WithField("name", devName).Infof("Requesting %d updates", len(updates)) + + log.WithField("name", devName).Tracef("Sending BackLog: %q", strings.Join(updates, "; ")) + + if err := checkToken(mqttClient.Publish( + devConfig.constructTopic(config.CommandPrefix, "BackLog"), + 1, false, + strings.Join(updates, "; "), + )); err != nil { + return errors.Wrap(err, "Unable to send BackLog command") + } + + return nil +} + +func checkToken(tok mqtt.Token) error { + if !tok.WaitTimeout(cfg.MQTTCommandTimeout) { + return errors.New("Command timed out") + } + + return errors.Wrap(tok.Error(), "Command errored") +} + +func mergeSettings(global, local map[string]interface{}) map[string]interface{} { + var out = map[string]interface{}{} + + if global != nil { + for k, v := range global { + out[k] = v + } + } + + if local != nil { + for k, v := range local { + out[k] = v + } + } + + return out +} diff --git a/settings.go b/settings.go new file mode 100644 index 0000000..f4788f5 --- /dev/null +++ b/settings.go @@ -0,0 +1,85 @@ +package main + +import ( + "encoding/json" + "strconv" + "strings" + "time" + + "github.com/pkg/errors" +) + +type settingExtractor func([]byte) (interface{}, error) + +var extractors = map[string]settingExtractor{ + "devicename": func(p []byte) (interface{}, error) { return extractGenericJSONValue("DeviceName", p) }, + "ledstate": func(p []byte) (interface{}, error) { return extractFloatToInt("LedState", p) }, + "module": extractModule, + "otaurl": func(p []byte) (interface{}, error) { return extractGenericJSONValue("OtaUrl", p) }, + "teleperiod": func(p []byte) (interface{}, error) { return extractFloatToInt("TelePeriod", p) }, + "timezone": func(p []byte) (interface{}, error) { return extractGenericJSONValue("Timezone", p) }, + "topic": func(p []byte) (interface{}, error) { return extractGenericJSONValue("Topic", p) }, +} + +func extractSettingValue(setting string, payloadChan chan []byte) (interface{}, error) { + e, ok := extractors[strings.ToLower(setting)] + if !ok { + // Default extractor: Full value + e = func(in []byte) (interface{}, error) { return string(in), nil } + } + + var deadline = time.NewTimer(cfg.MQTTCommandTimeout) + for { + select { + + case payload := <-payloadChan: + return e(payload) + + case <-deadline.C: + return nil, errors.New("Read timed out") + + } + } +} + +func extractGenericJSONValue(setting string, payload []byte) (interface{}, error) { + var data = map[string]interface{}{} + if err := json.Unmarshal(payload, &data); err != nil { + return nil, errors.Wrap(err, "Unable to map payload into map[string]interface{}") + } + + return data[setting], nil +} + +func extractFloatToInt(setting string, payload []byte) (interface{}, error) { + v, err := extractGenericJSONValue(setting, payload) + if err != nil { + return nil, err + } + + if _, ok := v.(float64); !ok { + return nil, errors.Errorf("Expected float value, got %T in %s", v, setting) + } + + return int(v.(float64)), nil +} + +func extractModule(payload []byte) (interface{}, error) { + var v = &struct { + Module map[string]string `json:"Module"` + }{} + if err := json.Unmarshal(payload, v); err != nil { + return nil, err + } + + var values []string + for k := range v.Module { + values = append(values, k) + } + + if len(values) != 1 { + return nil, errors.New("Unexpected number of module definitions found") + } + + return strconv.Atoi(values[0]) +}