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

Add workaround to sleep after connect #65

Merged
merged 4 commits into from
Mar 6, 2024
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
22 changes: 14 additions & 8 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package config

import (
"fmt"
"time"

multierror "github.com/hashicorp/go-multierror"
)
Expand Down Expand Up @@ -59,14 +60,19 @@ type ListTargets map[string]*Module

// Module defines the configuration parameters of a modbus module.
type Module struct {
Name string `yaml:"name"`
Protocol ModbusProtocol `yaml:"protocol"`
Timeout int `yaml:"timeout"`
Baudrate int `yaml:"baudrate"`
Databits int `yaml:"databits"`
Stopbits int `yaml:"stopbits"`
Parity string `yaml:"parity"`
Metrics []MetricDef `yaml:"metrics"`
Name string `yaml:"name"`
Protocol ModbusProtocol `yaml:"protocol"`
Timeout int `yaml:"timeout"`
Baudrate int `yaml:"baudrate"`
Databits int `yaml:"databits"`
Stopbits int `yaml:"stopbits"`
Parity string `yaml:"parity"`
Metrics []MetricDef `yaml:"metrics"`
Workarounds Workarounds `yaml:"workarounds"`
}

type Workarounds struct {
SleepAfterConnect time.Duration `yaml:"sleepAfterConnect"`
}

// RegisterAddr specifies the register in the possible output of _digital
Expand Down
4 changes: 4 additions & 0 deletions modbus.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ modules:
# Module name, needs to be passed as parameter by Prometheus.
- name: "fake"
protocol: 'tcp/ip'
# Certain modbus devices need special timing workarounds
workarounds:
# Sleep a certain time after the TCP connection is established
sleepAfterConnect: "1s"
metrics:
# Name of the metric.
- name: "power_consumption_total"
Expand Down
4 changes: 4 additions & 0 deletions modbus/modbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ func (e *Exporter) Scrape(targetAddress string, subTarget byte, moduleName strin
targetAddress, module.Name)
}

if module.Workarounds.SleepAfterConnect > 0 {
time.Sleep(module.Workarounds.SleepAfterConnect)
}

// TODO: Should we reuse this?
c := modbus.NewClient(handler)

Expand Down
Loading