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

Alphatec: Add configurable timeout #8523

Merged
merged 6 commits into from
Jun 18, 2023
Merged
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
18 changes: 14 additions & 4 deletions charger/alphatec.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package charger
import (
"encoding/binary"
"fmt"
"time"

"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
Expand Down Expand Up @@ -47,24 +48,33 @@ func init() {

// NewAlphatecFromConfig creates a Alphatec charger from generic config
func NewAlphatecFromConfig(other map[string]interface{}) (api.Charger, error) {
cc := modbus.Settings{
ID: 1,
cc := struct {
modbus.Settings `mapstructure:",squash"`
Timeout time.Duration
}{
Settings: modbus.Settings{
ID: 1,
},
}

if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}

return NewAlphatec(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID)
return NewAlphatec(cc.URI, cc.Device, cc.Comset, cc.Baudrate, modbus.ProtocolFromRTU(cc.RTU), cc.ID, cc.Timeout)
}

// NewAlphatec creates Alphatec charger
func NewAlphatec(uri, device, comset string, baudrate int, proto modbus.Protocol, slaveID uint8) (api.Charger, error) {
func NewAlphatec(uri, device, comset string, baudrate int, proto modbus.Protocol, slaveID uint8, timeout time.Duration) (api.Charger, error) {
conn, err := modbus.NewConnection(uri, device, comset, baudrate, proto, slaveID)
if err != nil {
return nil, err
}

if timeout > 0 {
conn.Timeout(timeout)
}

if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
Expand Down
2 changes: 2 additions & 0 deletions templates/definition/charger/alphatec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ params:
choice: ["rs485"]
baudrate: 9600
comset: 8N1
- name: timeout
render: |
type: alphatec
{{- include "modbus" . }}
timeout: {{ .timeout }}
17 changes: 17 additions & 0 deletions templates/docs/charger/alphatec_0.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,20 @@ render:
id: 1
host: 192.0.2.2 # Hostname
port: 502 # Port
advanced: |
type: template
template: alphatec

# RS485 via adapter (Modbus RTU)
modbus: rs485serial
id: 1
device: /dev/ttyUSB0 # USB-RS485 Adapter Adresse
baudrate: 9600 # Prüfe die Geräteeinstellungen, typische Werte sind 9600, 19200, 38400, 57600, 115200
comset: "8N1" # Kommunikationsparameter für den Adapter

# RS485 via TCP/IP (Modbus RTU)
modbus: rs485tcpip
id: 1
host: 192.0.2.2 # Hostname
port: 502 # Port
timeout: 10s # Optional