evcc-io/cmd/eebus.go
Andreas Linde 7e41a970d5
Experimentelle geführte Erstellung der Konfigurationsdatei (#1888)
Dies ist eine experimentelle Umsetzung im folgenden aufgeführten Ziele.

Wie es verwendet:
- Start über `evcc configure`
- Die Konfiguration wird in evcc.yaml geschrieben. Falls diese existiert kann ein alternativer Dateiname angegeben werden

Was es kann:
- Eine geführte Erstellung der Konfigurationsdatei
- Direktes Testen ob die Konfiguration jedes Gerätes auch funktioniert
- Konfigurationsabhängigkeiten durch direkte Konfiguration der Abhängigkeit lösen (z.B. Sponsorshipt required)
- Konfiguration in für Anwender bekannte Informationen und möglichst wenig Informationseingabe zu ermöglichen, z.B. anhand von Produkten anstatt aus der Implementierungssicht
- Bisherige Konfiguration funktioniert weiter
2021-11-30 14:29:09 +01:00

71 lines
1.6 KiB
Go

package cmd
import (
"crypto/x509/pkix"
"os"
"text/template"
"github.com/Masterminds/sprig/v3"
certhelper "github.com/evcc-io/eebus/cert"
"github.com/evcc-io/evcc/server"
"github.com/evcc-io/evcc/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// teslaCmd represents the vehicle command
var eebusCmd = &cobra.Command{
Use: "eebus-cert",
Short: "Generate EEBUS certificate for using EEBUS compatible chargers",
Run: runEEBUSCert,
}
func init() {
rootCmd.AddCommand(eebusCmd)
}
const tmpl = `
Add the following to the evcc config file:
eebus:
certificate:
public: |
{{ .public | indent 6 }}
private: |
{{ .private | indent 6 }}
`
func generateEEBUSCert() {
details := server.EEBUSDetails
subject := pkix.Name{
CommonName: details.DeviceCode,
Country: []string{"DE"},
Organization: []string{details.BrandName},
}
cert, err := certhelper.CreateCertificate(true, subject)
if err != nil {
log.FATAL.Fatal("could not create certificate")
}
pubKey, privKey, err := certhelper.GetX509KeyPair(cert)
if err != nil {
log.FATAL.Fatal("could not process generated certificate")
}
t := template.Must(template.New("out").Funcs(template.FuncMap(sprig.FuncMap())).Parse(tmpl))
if err := t.Execute(os.Stdout, map[string]interface{}{
"public": pubKey,
"private": privKey,
}); err != nil {
log.FATAL.Fatal("rendering failed", err)
}
}
func runEEBUSCert(cmd *cobra.Command, args []string) {
util.LogLevel(viper.GetString("log"), viper.GetStringMapString("levels"))
log.INFO.Printf("evcc %s (%s)", server.Version, server.Commit)
generateEEBUSCert()
}