Skip to content

Commit

Permalink
Merge pull request #65 from hikhvar/main
Browse files Browse the repository at this point in the history
Add workaround to sleep after connect

Thanks hikvar and sorry for the delay in merging this
  • Loading branch information
bastischubert authored Mar 6, 2024
2 parents e1dec78 + 4ebdc1c commit 7d0d073
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
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

0 comments on commit 7d0d073

Please sign in to comment.