Skip to content

Commit

Permalink
Merge pull request edgexfoundry#443 from weichou1229/issue-438
Browse files Browse the repository at this point in the history
fix: fix protocol properties parsing error
  • Loading branch information
cloudxxx8 authored Mar 25, 2023
2 parents 9bc48e7 + 261bb0a commit 63f56b4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 19 deletions.
8 changes: 5 additions & 3 deletions internal/driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"strconv"

"github.com/edgexfoundry/go-mod-core-contracts/v3/models"

"github.com/spf13/cast"
)

// ConnectionInfo is device connection info
Expand Down Expand Up @@ -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
Expand Down
36 changes: 20 additions & 16 deletions internal/driver/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
},
}

Expand All @@ -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)
}
}
Expand All @@ -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),
},
}

Expand All @@ -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)
}
}
Expand Down

0 comments on commit 63f56b4

Please sign in to comment.