diff --git a/internal/driver/config.go b/internal/driver/config.go index bdced1e4..04ab0bb3 100644 --- a/internal/driver/config.go +++ b/internal/driver/config.go @@ -11,6 +11,8 @@ import ( "strconv" "github.com/edgexfoundry/go-mod-core-contracts/v3/models" + + "github.com/spf13/cast" ) // ConnectionInfo is device connection info @@ -60,9 +62,9 @@ func parseIntValue(properties map[string]any, key string) (int, error) { return 0, fmt.Errorf("protocol config '%s' not exist", key) } - res, ok := value.(int) - if !ok { - return 0, fmt.Errorf("cannot transfrom protocol config %s value %v to int", key, value) + res, err := cast.ToIntE(value) + if err != nil { + return 0, fmt.Errorf("cannot transfrom protocol config %s value %v to int, %v", key, value, err) } return res, nil diff --git a/internal/driver/config_test.go b/internal/driver/config_test.go index 9121aa17..9c61fe69 100644 --- a/internal/driver/config_test.go +++ b/internal/driver/config_test.go @@ -20,16 +20,18 @@ func TestCreateRTUConnectionInfo_unitID255(t *testing.T) { stopBits := 1 parity := "N" unitID := uint8(255) + timeout := 5 + idleTimeout := 5 protocols := map[string]models.ProtocolProperties{ ProtocolRTU: { Address: address, - UnitID: 255, - BaudRate: 19200, - DataBits: 8, - StopBits: 1, - Parity: "N", - Timeout: 5, - IdleTimeout: 5, + UnitID: unitID, + BaudRate: float64(baudRate), + DataBits: float64(dataBits), + StopBits: float64(stopBits), + Parity: parity, + Timeout: float64(timeout), + IdleTimeout: float64(idleTimeout), }, } @@ -40,7 +42,7 @@ func TestCreateRTUConnectionInfo_unitID255(t *testing.T) { } if connectionInfo.Protocol != ProtocolRTU || connectionInfo.Address != address || connectionInfo.UnitID != unitID || connectionInfo.BaudRate != baudRate || connectionInfo.DataBits != dataBits || connectionInfo.StopBits != stopBits || - connectionInfo.Parity != parity { + connectionInfo.Parity != parity || connectionInfo.Timeout != timeout || connectionInfo.IdleTimeout != idleTimeout { t.Fatalf("Unexpect test result. %v should match to %v ", connectionInfo, protocols) } } @@ -52,16 +54,18 @@ func TestCreateConnectionInfo_unitID0(t *testing.T) { stopBits := 1 parity := "N" unitID := uint8(0) + timeout := 5 + idleTimeout := 5 protocols := map[string]models.ProtocolProperties{ ProtocolRTU: { Address: address, - UnitID: 0, - BaudRate: 19200, - DataBits: 8, - StopBits: 1, - Parity: "N", - Timeout: 5, - IdleTimeout: 5, + UnitID: unitID, + BaudRate: float64(baudRate), + DataBits: float64(dataBits), + StopBits: float64(stopBits), + Parity: parity, + Timeout: float64(timeout), + IdleTimeout: float64(idleTimeout), }, } @@ -72,7 +76,7 @@ func TestCreateConnectionInfo_unitID0(t *testing.T) { } if connectionInfo.Protocol != ProtocolRTU || connectionInfo.Address != address || connectionInfo.UnitID != unitID || connectionInfo.BaudRate != baudRate || connectionInfo.DataBits != dataBits || connectionInfo.StopBits != stopBits || - connectionInfo.Parity != parity { + connectionInfo.Parity != parity || connectionInfo.Timeout != timeout || connectionInfo.IdleTimeout != idleTimeout { t.Fatalf("Unexpect test result. %v should match to %v ", connectionInfo, protocols) } }