evcc-io/util/templates/init.go
Andreas Linde 96a0475390
Template improvements (#2019)
Fix #2018

- Update go-e firmware requirements
- Change cfos devices not to use modbus config schema
- Add support for `onIdentify` params of vehicles
- Add support for `resetOnDisconnect` for loadpoints
- Add support for defining device specific modbus port, baudrate and comset defaults
- Handle fatal error when `evcc.yaml` exists and the process doesn't have write permissions
- Some bug fixes
2021-12-12 19:56:28 +01:00

76 lines
1.3 KiB
Go

package templates
import (
"fmt"
"io/fs"
"path"
"github.com/evcc-io/evcc/templates/definition"
"gopkg.in/yaml.v3"
)
var (
templates = make(map[string][]Template)
)
const (
Charger = "charger"
Meter = "meter"
Vehicle = "vehicle"
)
func loadTemplates(class string) {
if templates[class] != nil {
return
}
err := fs.WalkDir(definition.YamlTemplates, ".", func(filepath string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
return nil
}
b, err := fs.ReadFile(definition.YamlTemplates, filepath)
if err != nil {
return err
}
var tmpl Template
if err = yaml.Unmarshal(b, &tmpl); err != nil {
return fmt.Errorf("reading template '%s' failed: %w", filepath, err)
}
tmpl.ResolveParamBase()
if err = tmpl.Validate(); err != nil {
return err
}
path := path.Dir(filepath)
templates[path] = append(templates[path], tmpl)
return nil
})
if err != nil {
panic(err)
}
}
func ByClass(class string) []Template {
loadTemplates(class)
return templates[class]
}
func ByTemplate(t, class string) (Template, error) {
loadTemplates(class)
for _, tmpl := range templates[class] {
if tmpl.Template == t {
return tmpl, nil
}
}
return Template{}, fmt.Errorf("template not found: %s", t)
}