Add random plant id during configure (#4697)

This commit is contained in:
andig 2022-10-02 19:09:42 +02:00 • committed by GitHub
parent 880557594f
commit 8a655efce8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 22 additions and 4 deletions

View file

@ -6,6 +6,7 @@ import (
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/evcc-io/evcc/util/machine"
"github.com/evcc-io/evcc/util/templates"
)
@ -43,6 +44,8 @@ type config struct {
EEBUS string
MQTT string
SponsorToken string
Plant string
Telemetry bool
}
type Configure struct {
@ -120,6 +123,10 @@ func (c *Configure) RenderConfiguration() ([]byte, error) {
panic(err)
}
// Assign random plant id. Don't use actual machine-id as file might
// be copied around to a different machine.
c.config.Plant = machine.RandomID()
out := new(bytes.Buffer)
err = tmpl.Execute(out, c.config)

View file

@ -8,10 +8,17 @@ log: info
levels:
cache: error
# unique installation id
plant: {{ .Plant }}
interval: 10s # control cycle interval
{{- if ne (len .SponsorToken) 0 }}
sponsortoken: {{ .SponsorToken }}
# sponsors can set telemetry: true to enable anonymous data aggregation
# see https://github.com/evcc-io/evcc/discussions/4554
telemetry: {{ .Telemetry }}
{{- end}}
{{- if ne (len .Meters) 0 }}

View file

@ -30,16 +30,20 @@ func CustomID(cid string) error {
return nil
}
// RandomID creates a random id
func RandomID() string {
rnd := util.RandomString(512)
mac := hmac.New(sha256.New, []byte(rnd))
return hex.EncodeToString(mac.Sum(nil))
}
// ID returns the platform specific machine id of the current host OS.
// If ID cannot be generated, a random value is suggested.
func ID() (string, error) {
if id == "" {
var err error
if id, err = machineid.ID(); err != nil {
rnd := util.RandomString(512)
mac := hmac.New(sha256.New, []byte(rnd))
rid := hex.EncodeToString(mac.Sum(nil))
rid := RandomID()
return "", fmt.Errorf("could not get %w; for manual configuration use plant: %s", err, rid)
}
}