Repository: edgexfoundry/device-modbus-go (go)

Issue:
Got error "cannot transfrom protocol config Timeout value 60 to int" while sending get command
<!--🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅

Hello there! 😄

To expedite issue processing please search open and closed issues before submitting a new one.
Existing issues often contain information about workarounds, resolution, or progress updates.

Please do not remove sections of this template when submitting your issue.  If the section is not relevant to your issue, leave the section but enter "n/a" for not applicable.

Note some sections are marked "REQUIRED" and must be provided.  Never mark required sections "n/a".

🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅🔅-->


# 🐞 Bug Report

### Affected Services [**REQUIRED**]
<!-- Can you pin-point one or more EdgeX services (Data, Metadata, Command, etc...) as the source of the bug? -->
<!-- ✍️edit: --> The issue is located in: device-modbus


### Is this a regression?

<!-- Did this behavior work correctly in the previous version? -->
<!-- ✍️--> Yes

### Description and Minimal Reproduction [**REQUIRED**]
1. Deploy EdgeX by attached compose file
[docker-compose-no-secty.yml.zip](https://github.com/edgexfoundry/device-modbus-go/files/11035968/docker-compose-no-secty.yml.zip)

2. device-modbus create default profiles and devices with autoevent
3. Found error on device-modbus logs

## 🔥 Exception or Error
<pre><code>
level=ERROR ts=2023-03-22T03:22:16.766647886Z app=device-modbus source=executor.go:55 msg="AutoEvent - error occurs when reading resource HVACValues: error reading DeviceCommand HVACValues for Modbus-TCP-test-device -> cannot transfrom protocol config Timeout value 5 to int"



</code></pre>


## 🌍  Your Environment
**Deployment Environment:** Ubuntu/x86

**EdgeX Version [**REQUIRED**]:** Main


**Anything else relevant?**



Pull request:
fix: fix protocol properties parsing error
fix protocol property timeout and idleTimeout parsing error

Close #438

<!-- Expected Commit Message Description (imported automatically by GitHub) -->
<!-- Must conform to [conventional commits guidelines](https://github.com/edgexfoundry/device-modbus-go/blob/main/.github/Contributing.md) -->
<!-- Expected Commit message must contain Closes/Fixes #IssueNumber statement when there is a related issue -->

<!-- Add additional detailed description of need for change if no related issue -->

**If your build fails** due to your commit message not passing the build checks, please review the guidelines here: https://github.com/edgexfoundry/device-modbus-go/blob/main/.github/Contributing.md

## PR Checklist
Please check if your PR fulfills the following requirements:

- [x] I am not introducing a breaking change (if you are, flag in conventional commit message with `BREAKING CHANGE:` describing the break)
- [x] I am not introducing a new dependency (add notes below if you are)
- [ ] I have added unit tests for the new feature or bug fix (if not, why?) not impact
- [x] I have fully tested (add details below) this the new feature or bug fix (if not, why?)
- [ ] I have opened a PR for the related docs change (if not, why?) not impact
  <link to docs PR>

## Testing Instructions
<!-- How can the reviewers test your change? -->
Run unit test

## New Dependency Instructions (If applicable)
<!-- Please follow [vetting instructions](https://wiki.edgexfoundry.org/display/FA/Vetting+Process+for+3rd+Party+Dependencies) and place results here -->

Patch:
diff --git a/internal/driver/config.go b/internal/driver/config.go
index bdced1e..04ab0bb 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


Test patch:
diff --git a/internal/driver/config_test.go b/internal/driver/config_test.go
index 9121aa1..9c61fe6 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)
 	}
 }