Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change timeout type from int to time.Duration #375

Open
wants to merge 2 commits into
base: jakarta
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions cmd/res/devices/modbus.test.devices.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Address = "0.0.0.0"
Port = "1502"
UnitID = "1"
Timeout = "5"
IdleTimeout = "5"
Timeout = "5s"
IdleTimeout = "5s"
[[DeviceList.AutoEvents]]
Interval = "20s"
OnChange = false
Expand All @@ -29,8 +29,8 @@
StopBits = "1"
Parity = "N"
UnitID = "1"
Timeout = "5"
IdleTimeout = "5"
Timeout = "5s"
IdleTimeout = "5s"

# Pre-define Devices
[[DeviceList]]
Expand All @@ -43,8 +43,8 @@
Address = "0.0.0.0"
Port = "1502"
UnitID = "1"
Timeout = "5"
IdleTimeout = "5"
Timeout = "5s"
IdleTimeout = "5s"
[[DeviceList.AutoEvents]]
Interval = "20s"
OnChange = false
Expand Down
26 changes: 19 additions & 7 deletions internal/driver/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package driver
import (
"fmt"
"strconv"

"time"
"github.com/edgexfoundry/go-mod-core-contracts/v2/models"
)

Expand All @@ -24,9 +24,9 @@ type ConnectionInfo struct {
Parity string
UnitID uint8
// Connect & Read timeout(seconds)
Timeout int
Timeout time.Duration
// Idle timeout(seconds) to close the connection
IdleTimeout int
IdleTimeout time.Duration
}

func createConnectionInfo(protocols map[string]models.ProtocolProperties) (info *ConnectionInfo, err error) {
Expand Down Expand Up @@ -66,6 +66,18 @@ func parseIntValue(properties map[string]string, key string) (int, error) {
return val, nil
}

func parseDurationValue(properties map[string]string, key string) (time.Duration, error) {
str, ok := properties[key]
if !ok {
return 0, fmt.Errorf("protocol config '%s' not exist", key)
}
val, err := time.ParseDuration(str)
if err != nil {
return 0, fmt.Errorf("fail to parse protocol config '%s', %v", key, err)
}
return val, nil
}

func createRTUConnectionInfo(rtuProtocol map[string]string) (info *ConnectionInfo, err error) {
errorMessage := "unable to create RTU connection info, protocol config '%s' not exist"
address, ok := rtuProtocol[Address]
Expand Down Expand Up @@ -105,12 +117,12 @@ func createRTUConnectionInfo(rtuProtocol map[string]string) (info *ConnectionInf
return nil, fmt.Errorf("invalid parity value, it should be N(None) or O(Odd) or E(Even)")
}

timeout, err := parseIntValue(rtuProtocol, Timeout)
timeout, err := parseDurationValue(rtuProtocol, Timeout)
if err != nil {
return nil, err
}

idleTimeout, err := parseIntValue(rtuProtocol, IdleTimeout)
idleTimeout, err := parseDurationValue(rtuProtocol, IdleTimeout)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -153,12 +165,12 @@ func createTcpConnectionInfo(tcpProtocol map[string]string) (info *ConnectionInf
return nil, fmt.Errorf("uintID value out of range(0–255). Error: %v", err)
}

timeout, err := parseIntValue(tcpProtocol, Timeout)
timeout, err := parseDurationValue(tcpProtocol, Timeout)
if err != nil {
return nil, err
}

idleTimeout, err := parseIntValue(tcpProtocol, IdleTimeout)
idleTimeout, err := parseDurationValue(tcpProtocol, IdleTimeout)
if err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions internal/driver/modbusclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,15 +129,15 @@ func NewDeviceClient(connectionInfo *ConnectionInfo) (*ModbusClient, error) {
if client.IsModbusTcp {
client.TCPClientHandler.Address = fmt.Sprintf("%s:%d", connectionInfo.Address, connectionInfo.Port)
client.TCPClientHandler.SlaveId = byte(connectionInfo.UnitID)
client.TCPClientHandler.Timeout = time.Duration(connectionInfo.Timeout) * time.Second
client.TCPClientHandler.IdleTimeout = time.Duration(connectionInfo.IdleTimeout) * time.Second
client.TCPClientHandler.Timeout = time.Duration(connectionInfo.Timeout)
client.TCPClientHandler.IdleTimeout = time.Duration(connectionInfo.IdleTimeout)
client.TCPClientHandler.Logger = log.New(os.Stdout, "", log.LstdFlags)
} else {
serialParams := strings.Split(connectionInfo.Address, ",")
client.RTUClientHandler.Address = serialParams[0]
client.RTUClientHandler.SlaveId = byte(connectionInfo.UnitID)
client.RTUClientHandler.Timeout = time.Duration(connectionInfo.Timeout) * time.Second
client.RTUClientHandler.IdleTimeout = time.Duration(connectionInfo.IdleTimeout) * time.Second
client.RTUClientHandler.Timeout = time.Duration(connectionInfo.Timeout)
client.RTUClientHandler.IdleTimeout = time.Duration(connectionInfo.IdleTimeout)
client.RTUClientHandler.BaudRate = connectionInfo.BaudRate
client.RTUClientHandler.DataBits = connectionInfo.DataBits
client.RTUClientHandler.StopBits = connectionInfo.StopBits
Expand Down