// handle user input for a device modbus configuration
handle user input for a device modbus configuration
// handle user input for a device modbus configuration
func (c *CmdConfigure) processModbusConfig(param templates.Param, deviceCategory DeviceCategory, additionalConfig map[string]interface{}) {
	var selectedModbusKey string
	deviceDefaultBaudrate := templates.ModbusParamValueBaudrate
	deviceDefaultComset := templates.ModbusParamValueComset
	deviceDefaultPort := templates.ModbusParamValuePort
	deviceDefaultId := templates.ModbusParamValueId
	if param.Baudrate != 0 {
		deviceDefaultBaudrate = param.Baudrate
	}
	if param.Comset != "" {
		deviceDefaultComset = param.Comset
	}
	if param.Port != 0 {
		deviceDefaultPort = param.Port
	}
	if param.ID != 0 {
		deviceDefaultId = param.ID
	}
	var choices []string
	var choiceKeys []string
	for _, choice := range param.Choice {
		switch choice {
		case templates.ModbusChoiceRS485:
			choices = append(choices, "Serial (USB-RS485 Adapter)")
			choiceKeys = append(choiceKeys, templates.ModbusKeyRS485Serial)
			choices = append(choices, "Serial (Ethernet-RS485 Adapter)")
			choiceKeys = append(choiceKeys, templates.ModbusKeyRS485TCPIP)
		case templates.ModbusChoiceTCPIP:
			choices = append(choices, "TCP/IP")
			choiceKeys = append(choiceKeys, templates.ModbusKeyTCPIP)
		}
	}
	if len(choices) > 0 {
		id := c.askValue(question{
			label:        "ID",
			help:         "Modbus ID",
			defaultValue: deviceDefaultId,
			valueType:    templates.ParamValueTypeNumber,
			required:     true})
		additionalConfig[templates.ModbusParamNameId] = id
		var index int
		if len(choices) > 1 {
			index, _ = c.askChoice(c.localizedString("Config_ModbusInterface", nil), choices)
		}
		selectedModbusKey = choiceKeys[index]
		additionalConfig[templates.ParamModbus] = selectedModbusKey
		switch selectedModbusKey {
		case templates.ModbusKeyRS485Serial:
			device := c.askValue(question{
				label:        c.localizedString("UserFriendly_Device_Name", nil),
				help:         c.localizedString("UserFriendly_Device_Help", nil),
				exampleValue: templates.ModbusParamValueDevice,
				required:     true})
			additionalConfig[templates.ModbusParamNameDevice] = device
			baudrate := c.askValue(question{
				label:        c.localizedString("UserFriendly_Baudrate_Name", nil),
				help:         c.localizedString("UserFriendly_Baudrate_Help", nil),
				defaultValue: deviceDefaultBaudrate,
				valueType:    templates.ParamValueTypeNumber,
				required:     true})
			additionalConfig[templates.ModbusParamNameBaudrate] = baudrate
			comset := c.askValue(question{
				label:        c.localizedString("UserFriendly_ComSet_Name", nil),
				defaultValue: deviceDefaultComset,
				required:     true})
			additionalConfig[templates.ModbusParamNameComset] = comset
		case templates.ModbusKeyRS485TCPIP, templates.ModbusKeyTCPIP:
			if selectedModbusKey == templates.ModbusKeyRS485TCPIP {
				additionalConfig[templates.ModbusParamNameRTU] = "true"
			}
			host := c.askValue(question{
				label:        c.localizedString("UserFriendly_Host_Name", nil),
				exampleValue: templates.ModbusParamValueHost,
				required:     true})
			additionalConfig[templates.ModbusParamNameHost] = host
			port := c.askValue(question{
				label:        c.localizedString("UserFriendly_Port_Name", nil),
				defaultValue: deviceDefaultPort,
				valueType:    templates.ParamValueTypeNumber,
				required:     true})
			additionalConfig[templates.ModbusParamNamePort] = port
		}
	}
}
func ( c * CmdConfigure ) processModbusConfig ( param templates . Param , deviceCategory DeviceCategory , additionalConfig map [ string ] interface { } ) { var selectedModbusKey string deviceDefaultBaudrate := templates . ModbusParamValueBaudrate deviceDefaultComset := templates . ModbusParamValueComset deviceDefaultPort := templates . ModbusParamValuePort deviceDefaultId := templates . ModbusParamValueId if param . Baudrate != 0 { deviceDefaultBaudrate = param . Baudrate } if param . Comset != " " { deviceDefaultComset = param . Comset } if param . Port != 0 { deviceDefaultPort = param . Port } if param . ID != 0 { deviceDefaultId = param . ID } var choices [ ] string var choiceKeys [ ] string for _ , choice := range param . Choice { switch choice { case templates . ModbusChoiceRS485 : choices = append ( choices , " " ) choiceKeys = append ( choiceKeys , templates . ModbusKeyRS485Serial ) choices = append ( choices , " " ) choiceKeys = append ( choiceKeys , templates . ModbusKeyRS485TCPIP ) case templates . ModbusChoiceTCPIP : choices = append ( choices , " " ) choiceKeys = append ( choiceKeys , templates . ModbusKeyTCPIP ) } } if len ( choices ) > 0 { id := c . askValue ( question { label : " " , help : " " , defaultValue : deviceDefaultId , valueType : templates . ParamValueTypeNumber , required : true } ) additionalConfig [ templates . ModbusParamNameId ] = id var index int if len ( choices ) > 1 { index , _ = c . askChoice ( c . localizedString ( " " , nil ) , choices ) } selectedModbusKey = choiceKeys [ index ] additionalConfig [ templates . ParamModbus ] = selectedModbusKey switch selectedModbusKey { case templates . ModbusKeyRS485Serial : device := c . askValue ( question { label : c . localizedString ( " " , nil ) , help : c . localizedString ( " " , nil ) , exampleValue : templates . ModbusParamValueDevice , required : true } ) additionalConfig [ templates . ModbusParamNameDevice ] = device baudrate := c . askValue ( question { label : c . localizedString ( " " , nil ) , help : c . localizedString ( " " , nil ) , defaultValue : deviceDefaultBaudrate , valueType : templates . ParamValueTypeNumber , required : true } ) additionalConfig [ templates . ModbusParamNameBaudrate ] = baudrate comset := c . askValue ( question { label : c . localizedString ( " " , nil ) , defaultValue : deviceDefaultComset , required : true } ) additionalConfig [ templates . ModbusParamNameComset ] = comset case templates . ModbusKeyRS485TCPIP , templates . ModbusKeyTCPIP : if selectedModbusKey == templates . ModbusKeyRS485TCPIP { additionalConfig [ templates . ModbusParamNameRTU ] = " " } host := c . askValue ( question { label : c . localizedString ( " " , nil ) , exampleValue : templates . ModbusParamValueHost , required : true } ) additionalConfig [ templates . ModbusParamNameHost ] = host port := c . askValue ( question { label : c . localizedString ( " " , nil ) , defaultValue : deviceDefaultPort , valueType : templates . ParamValueTypeNumber , required : true } ) additionalConfig [ templates . ModbusParamNamePort ] = port } } }
handle user input for a device modbus configuration
handle user input for a device modbus configuration
// baudrate and comset defaults can be overwritten, as they are device specific // ask for modbus address // ask for modbus interface type