diff --git a/cmd/config.go b/cmd/config.go new file mode 100644 index 000000000..24c5f2d2e --- /dev/null +++ b/cmd/config.go @@ -0,0 +1,63 @@ +package cmd + +import ( + "fmt" + "strings" + + "github.com/evcc-io/evcc/util/config" + "github.com/evcc-io/evcc/util/templates" + "github.com/spf13/cobra" +) + +// configCmd represents the configure command +var configCmd = &cobra.Command{ + Use: "config", + Short: "Dump database configuration", + Run: runConfig, +} + +func init() { + rootCmd.AddCommand(configCmd) + configCmd.Flags().String("class", "", "Device class ("+strings.Join(templates.ClassStrings(), "|")+")") +} + +func runConfig(cmd *cobra.Command, args []string) { + // load config + if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil { + log.FATAL.Fatal(err) + } + + // setup environment + if err := configureEnvironment(cmd, &conf); err != nil { + log.FATAL.Fatal(err) + } + + cc := templates.ClassValues() + if c := cmd.Flag("class").Value.String(); c != "" { + class, err := templates.ClassString(c) + if err != nil { + log.FATAL.Fatal(err) + } + cc = []templates.Class{class} + } + + for _, class := range cc { + configurable, err := config.ConfigurationsByClass(class) + if err != nil { + log.FATAL.Fatal(err) + } + + if len(configurable) > 0 { + if len(cc) > 0 { + fmt.Println(class) + fmt.Println("---") + } + + for _, c := range configurable { + fmt.Println(config.NameForID(c.ID), "type:"+c.Type, c.Value) + } + + fmt.Println("") + } + } +} diff --git a/cmd/config_delete.go b/cmd/config_delete.go new file mode 100644 index 000000000..6410a71c1 --- /dev/null +++ b/cmd/config_delete.go @@ -0,0 +1,70 @@ +package cmd + +import ( + "fmt" + + "github.com/evcc-io/evcc/api" + "github.com/evcc-io/evcc/core/loadpoint" + "github.com/evcc-io/evcc/util/config" + "github.com/evcc-io/evcc/util/templates" + "github.com/spf13/cobra" +) + +// configDeleteCmd represents the configure command +var configDeleteCmd = &cobra.Command{ + Use: "delete ", + Short: "Delete device", + Run: runConfigDelete, + Args: cobra.ExactArgs(1), +} + +func init() { + configCmd.AddCommand(configDeleteCmd) +} + +func runConfigDelete(cmd *cobra.Command, args []string) { + // load config + if err := loadConfigFile(&conf, !cmd.Flag(flagIgnoreDatabase).Changed); err != nil { + log.FATAL.Fatal(err) + } + + // setup environment + if err := configureEnvironment(cmd, &conf); err != nil { + log.FATAL.Fatal(err) + } + + id, err := config.IDForName(args[0]) + if err != nil { + log.FATAL.Fatal(err) + } + + c, err := config.ConfigByID(id) + if err != nil { + log.FATAL.Fatal(err) + } + + fmt.Println(config.NameForID(c.ID), "type:"+c.Type, c.Value) + + switch c.Class { + case templates.Charger: + deleteDevice[api.Charger](c) + case templates.Meter: + deleteDevice[api.Meter](c) + case templates.Vehicle: + deleteDevice[api.Vehicle](c) + case templates.Tariff: + deleteDevice[api.Tariff](c) + case templates.Circuit: + deleteDevice[api.Circuit](c) + case templates.Loadpoint: + deleteDevice[loadpoint.API](c) + } +} + +func deleteDevice[T any](c config.Config) { + var zero T + dev := config.NewConfigurableDevice[T](&c, zero) + if err := dev.Delete(); err != nil { + log.FATAL.Fatal(err) + } +} diff --git a/util/config/config.go b/util/config/config.go index 870ee153e..17e62207e 100644 --- a/util/config/config.go +++ b/util/config/config.go @@ -3,6 +3,8 @@ package config import ( "encoding/json" "fmt" + "strconv" + "strings" "dario.cat/mergo" "github.com/evcc-io/evcc/util/templates" @@ -175,6 +177,11 @@ func NameForID(id int) string { return fmt.Sprintf("db:%d", id) } +// IDForName returns a unique config name for the given id +func IDForName(name string) (int, error) { + return strconv.Atoi(strings.TrimPrefix(name, "db:")) +} + // ConfigurationsByClass returns devices by class from the database func ConfigurationsByClass(class templates.Class) ([]Config, error) { var devices []Config