From a83d6969262c6b68ea2eed04ab50f762b6a4dea4 Mon Sep 17 00:00:00 2001 From: Andreas Linde Date: Sat, 29 Jan 2022 16:30:11 +0100 Subject: [PATCH] Fix template handling with modbus params --- cmd/configure/flow.go | 7 +++---- cmd/configure/helper.go | 12 ++++++------ util/templates/template.go | 2 +- util/templates/template_documentation.go | 2 +- util/templates/template_modbus.go | 13 ++++++------- 5 files changed, 17 insertions(+), 19 deletions(-) diff --git a/cmd/configure/flow.go b/cmd/configure/flow.go index 3b88f32e8..feaad3a71 100644 --- a/cmd/configure/flow.go +++ b/cmd/configure/flow.go @@ -53,7 +53,7 @@ func (c *CmdConfigure) configureDeviceGuidedSetup() { // we only ask for the configuration for the first usage deviceCategory = supportedDeviceCategories[0] - values = c.processConfig(templateItem, deviceCategory) + values = c.processConfig(&templateItem, deviceCategory) deviceItem, err = c.processDeviceValues(values, templateItem, deviceItem, deviceCategory) if err != nil { @@ -158,7 +158,7 @@ func (c *CmdConfigure) configureLinkedTemplate(templateItem templates.Template, for ok := true; ok; { deviceItem := device{} - values := c.processConfig(templateItem, category) + values := c.processConfig(&templateItem, category) deviceItem, err := c.processDeviceValues(values, templateItem, deviceItem, category) if err != nil { if !errors.Is(err, c.errDeviceNotValid) { @@ -206,8 +206,7 @@ func (c *CmdConfigure) configureDeviceCategory(deviceCategory DeviceCategory) (d deviceDescription = templateItem.Title() capabilities = templateItem.Capabilities - values := c.processConfig(templateItem, deviceCategory) - + values := c.processConfig(&templateItem, deviceCategory) device, err = c.processDeviceValues(values, templateItem, device, deviceCategory) if err != nil { if err != c.errDeviceNotValid { diff --git a/cmd/configure/helper.go b/cmd/configure/helper.go index 85293dcab..a4b52c57a 100644 --- a/cmd/configure/helper.go +++ b/cmd/configure/helper.go @@ -340,23 +340,23 @@ func (c *CmdConfigure) paramChoiceValues(params []templates.Param, name string) // processConfig processes an EVCC configuration item // Returns: // a map with param name and values -func (c *CmdConfigure) processConfig(templateItem templates.Template, deviceCategory DeviceCategory) map[string]interface{} { +func (c *CmdConfigure) processConfig(templateItem *templates.Template, deviceCategory DeviceCategory) map[string]interface{} { fmt.Println() fmt.Println(c.localizedString("Config_Title", nil)) fmt.Println() - c.processModbusConfig(&templateItem, deviceCategory) + c.processModbusConfig(templateItem, deviceCategory) - return c.processParams(templateItem, templateItem.Params, deviceCategory) + return c.processParams(templateItem, deviceCategory) } // process a list of params -func (c *CmdConfigure) processParams(templateItem templates.Template, params []templates.Param, deviceCategory DeviceCategory) map[string]interface{} { +func (c *CmdConfigure) processParams(templateItem *templates.Template, deviceCategory DeviceCategory) map[string]interface{} { usageFilter := DeviceCategories[deviceCategory].categoryFilter additionalConfig := make(map[string]interface{}) - for _, param := range params { + for _, param := range templateItem.Params { if param.Dependencies != nil { valid := true for _, dep := range param.Dependencies { @@ -507,5 +507,5 @@ func (c *CmdConfigure) processModbusConfig(templateItem *templates.Template, dev // add the interface type specific modbus params templateItem.ModbusParams(choiceTypes[index], values) // Update the modbus default values - templateItem.ModbusValues(templates.TemplateRenderModeInstance, values) + _ = templateItem.ModbusValues(templates.TemplateRenderModeInstance, true, values) } diff --git a/util/templates/template.go b/util/templates/template.go index d724c214a..266d76899 100644 --- a/util/templates/template.go +++ b/util/templates/template.go @@ -317,7 +317,7 @@ func (t *Template) RenderResult(renderMode string, other map[string]interface{}) return nil, values, err } - t.ModbusValues(renderMode, values) + values = t.ModbusValues(renderMode, false, values) // add the common templates for _, v := range t.ConfigDefaults.Config.Presets { diff --git a/util/templates/template_documentation.go b/util/templates/template_documentation.go index b5a0924f9..75f022847 100644 --- a/util/templates/template_documentation.go +++ b/util/templates/template_documentation.go @@ -50,7 +50,7 @@ func (t *Template) RenderDocumentation(product Product, values map[string]interf } modbusData := map[string]interface{}{} - t.ModbusValues(TemplateRenderModeDocs, modbusData) + modbusData = t.ModbusValues(TemplateRenderModeDocs, true, modbusData) modbusOut := new(bytes.Buffer) diff --git a/util/templates/template_modbus.go b/util/templates/template_modbus.go index 7afa0bc00..25432884a 100644 --- a/util/templates/template_modbus.go +++ b/util/templates/template_modbus.go @@ -29,10 +29,10 @@ func (t *Template) ModbusParams(modbusType string, values map[string]interface{} } // set the modbus values required from modbus.tpl and and the template to the render -func (t *Template) ModbusValues(renderMode string, values map[string]interface{}) { +func (t *Template) ModbusValues(renderMode string, setDefaults bool, values map[string]interface{}) map[string]interface{} { choices := t.ModbusChoices() if len(choices) == 0 { - return + return values } // only add the template once, when testing multiple usages, it might already be present @@ -40,11 +40,8 @@ func (t *Template) ModbusValues(renderMode string, values map[string]interface{} t.Render = fmt.Sprintf("%s\n%s", t.Render, modbusTmpl) } - // either modbus param is defined, which means it ran through configuration - // or defaults for all modbus choices need to be set for rendering all cases for documentation - if modbusValue := values[ParamModbus]; renderMode != TemplateRenderModeInstance && modbusValue != nil && modbusValue != "" { - values[fmt.Sprintf("%s", modbusValue)] = true - return + if !setDefaults { + return values } modbusConfig := t.ConfigDefaults.Config.Modbus @@ -96,4 +93,6 @@ func (t *Template) ModbusValues(renderMode string, values map[string]interface{} values[iface] = true } } + + return values }