// processDeviceValues processes the user provided values, create a device configuration and check if it is a valid device
processDeviceValues processes the user provided values, create a device configuration and check if it is a valid device
// processDeviceValues processes the user provided values create a device configuration and check if it is a valid device
func (c *CmdConfigure) processDeviceValues(values map[string]interface{}, templateItem templates.Template, device device, deviceCategory DeviceCategory) (device, error) {
	c.addedDeviceIndex++
	device.Name = fmt.Sprintf("%s%d", DeviceCategories[deviceCategory].defaultName, c.addedDeviceIndex)
	device.Title = templateItem.Description
	for item, value := range values {
		if strings.ToLower(item) != "title" {
			continue
		}
		if len(value.(string)) > 0 {
			device.Title = value.(string)
		}
	}
	categoryWithUsage := deviceCategory == DeviceCategoryPVMeter || deviceCategory == DeviceCategoryBatteryMeter || deviceCategory == DeviceCategoryGridMeter
	fmt.Println()
	if categoryWithUsage {
		fmt.Println(c.localizedString("TestingDevice_TitleUsage", localizeMap{"Device": templateItem.Description, "Usage": deviceCategory.String()}))
	} else {
		fmt.Println(c.localizedString("TestingDevice_Title", localizeMap{"Device": templateItem.Description}))
	}
	deviceTest := DeviceTest{
		DeviceCategory: deviceCategory,
		Template:       templateItem,
		ConfigValues:   values,
	}
	testResult, err := deviceTest.Test()
	if err != nil {
		fmt.Println("  ", c.localizedString("Error", localizeMap{"Error": err}))
		fmt.Println()
		question := c.localizedString("TestingDevice_AddFailed", localizeMap{"Device": templateItem.Description})
		if categoryWithUsage {
			question = c.localizedString("TestingDevice_AddFailedUsage", localizeMap{"Device": templateItem.Description, "Usage": deviceCategory.String()})
		}
		if !c.askYesNo(question) {
			c.addedDeviceIndex--
			return device, c.errDeviceNotValid
		}
	} else {
		if deviceCategory == DeviceCategoryCharger && testResult == DeviceTestResultValid {
			device.ChargerHasMeter = true
		}
	}
	templateItem.Params = append(templateItem.Params, templates.Param{Name: "name", Value: device.Name})
	if !c.expandedMode {
		b, err := templateItem.RenderProxyWithValues(values, false)
		if err != nil {
			c.addedDeviceIndex--
			return device, err
		}
		device.Yaml = string(b)
	} else {
		for _, p := range templateItem.Params {
			if p.Name == "name" {
				values["name"] = p.Value
				templateItem.Render = fmt.Sprintf("name: {{ .name }}\n%s", templateItem.Render)
			}
		}
		b, _, err := templateItem.RenderResult(false, values)
		if err != nil {
			c.addedDeviceIndex--
			return device, err
		}
		device.Yaml = string(b)
	}
	return device, nil
}
func ( c * CmdConfigure ) processDeviceValues ( values map [ string ] interface { } , templateItem templates . Template , device device , deviceCategory DeviceCategory ) ( device , error ) { c . addedDeviceIndex ++ device . Name = fmt . Sprintf ( " " , DeviceCategories [ deviceCategory ] . defaultName , c . addedDeviceIndex ) device . Title = templateItem . Description for item , value := range values { if strings . ToLower ( item ) != " " { continue } if len ( value . ( string ) ) > 0 { device . Title = value . ( string ) } } categoryWithUsage := deviceCategory == DeviceCategoryPVMeter || deviceCategory == DeviceCategoryBatteryMeter || deviceCategory == DeviceCategoryGridMeter fmt . Println ( ) if categoryWithUsage { fmt . Println ( c . localizedString ( " " , localizeMap { " " : templateItem . Description , " " : deviceCategory . String ( ) } ) ) } else { fmt . Println ( c . localizedString ( " " , localizeMap { " " : templateItem . Description } ) ) } deviceTest := DeviceTest { DeviceCategory : deviceCategory , Template : templateItem , ConfigValues : values , } testResult , err := deviceTest . Test ( ) if err != nil { fmt . Println ( " " , c . localizedString ( " " , localizeMap { " " : err } ) ) fmt . Println ( ) question := c . localizedString ( " " , localizeMap { " " : templateItem . Description } ) if categoryWithUsage { question = c . localizedString ( " " , localizeMap { " " : templateItem . Description , " " : deviceCategory . String ( ) } ) } if ! c . askYesNo ( question ) { c . addedDeviceIndex -- return device , c . errDeviceNotValid } } else { if deviceCategory == DeviceCategoryCharger && testResult == DeviceTestResultValid { device . ChargerHasMeter = true } } templateItem . Params = append ( templateItem . Params , templates . Param { Name : " " , Value : device . Name } ) if ! c . expandedMode { b , err := templateItem . RenderProxyWithValues ( values , false ) if err != nil { c . addedDeviceIndex -- return device , err } device . Yaml = string ( b ) } else { for _ , p := range templateItem . Params { if p . Name == " " { values [ " " ] = p . Value templateItem . Render = fmt . Sprintf ( " \n " , templateItem . Render ) } } b , _ , err := templateItem . RenderResult ( false , values ) if err != nil { c . addedDeviceIndex -- return device , err } device . Yaml = string ( b ) } return device , nil }
processDeviceValues processes the user provided values, create a device configuration and check if it is a valid device
processDeviceValues processes the user provided values create a device configuration and check if it is a valid device