poller

package
v0.0.0-...-8ad4bdb Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 30, 2025 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ParseNetworkAddress

func ParseNetworkAddress(addressURL string) (address string, config modbus.ClientConfig, err error)

ParseNetworkAddress parses given address to modbus.ClientConfig suitable for use with TCP/UDP clients Returned address is formatted as valid URL "scheme://host:port", where scheme default to `tcp` and port to `502`.

Types

type BatchStatistics

type BatchStatistics struct {
	BatchIndex int

	FunctionCode  uint8
	Protocol      modbus.ProtocolType
	ServerAddress string

	// IsPolling shows if that batch job currently in polling or waiting for retry
	IsPolling bool

	// StartCount is count how many times the poll job has (re)started
	StartCount uint64

	// RequestOKCount is count how many modbus request have succeeded for that job
	RequestOKCount uint64

	// RequestErrCount is total count how many request have failed for that job
	// this count does not distinguish modbus errors from network errors
	RequestErrCount uint64

	// RequestModbusErrCount is count how many request have failed with modbus error code for that job
	RequestModbusErrCount uint64

	// SendSkipCount is count how many ResultChan sends were skipped due blocked Result channel
	SendSkipCount uint64
}

BatchStatistics holds statistics about specific Poller batch internal state. Batch is identified by BatchIndex.

type Client

type Client interface {
	Do(ctx context.Context, req packet.Request) (packet.Response, error)
	Close() error
}

Client is interface that Modbus client needs to implement for Poller to be able to request data from server.

func DefaultConnectClient

func DefaultConnectClient(ctx context.Context, protocol modbus.ProtocolType, addressURL string) (Client, error)

DefaultConnectClient is default implementation to create and connect to Modbus server

type ClientConnectFunc

type ClientConnectFunc func(ctx context.Context, protocol modbus.ProtocolType, addressURL string) (Client, error)

ClientConnectFunc is used by poller jobs to open connection to modbus server and request data from it

func NewSingleConnectionPerAddressClientFunc

func NewSingleConnectionPerAddressClientFunc(newClientFunc ClientConnectFunc) ClientConnectFunc

NewSingleConnectionPerAddressClientFunc creates clients that limit themselves to single instance per server address and use mutexes to guard against parallel Client.Do invocations. Use-case for this is connections to serial devices where you can not run multiple requests in parallel.

type Config

type Config struct {
	// Logger is logger instance used by poller to log.
	// Defaults to slog.Default
	Logger *slog.Logger

	// ConnectFunc is used by poller jobs to open connection to modbus server and request data from it
	// If you need single connection per server (for example Serial connections) use built in NewSingleConnectionPerAddressClientFunc.
	// Defaults to DefaultConnectClient
	ConnectFunc ClientConnectFunc

	// OnClientDoErrorFunc is called when Client.Do returns with an error.
	// User can decide do suppress certain errors by not returning from this function. In that
	// case these errors will not be included in statistics.
	//
	// Use-case for this callback:
	// Some engine controllers will return packet.ErrIllegalDataValue error code when the engine
	// is not turned on, and you might not want to pollute logs with modbus errors like that.
	OnClientDoErrorFunc func(err error, batchIndex int) error

	// TimeNow allows mocking Result.Time value in tests
	// Defaults to time.Now
	TimeNow func() time.Time
}

Config is configuration for Poller

type Poller

type Poller struct {
	ResultChan chan Result
	// contains filtered or unexported fields
}

Poller is service for sending modbus requests with interval to servers and emitting extracted values from request to result channel.

Poller creates goroutines for each given modbus.BuilderRequest and runs them in parallel. If your modbus server does not handle parallel requests well (modbus over serial connection) you need to provide network client that limits connections to servers. See Config.ConnectFunc

func NewPoller

func NewPoller(batches []modbus.BuilderRequest) *Poller

NewPoller creates new instance of Poller with default configuration

func NewPollerWithConfig

func NewPollerWithConfig(batches []modbus.BuilderRequest, conf Config) *Poller

NewPollerWithConfig creates new instance of Poller with given configuration

func (*Poller) BatchStatistics

func (p *Poller) BatchStatistics() []BatchStatistics

BatchStatistics returns statistics of all Poller batches.

func (*Poller) Poll

func (p *Poller) Poll(ctx context.Context) error

Poll starts polling until context is cancelled

type Result

type Result struct {
	// BatchIndex is index of modbus.BuilderRequest that Poller was created and produced these results
	BatchIndex int
	// Time contains request start time
	Time time.Time
	// Values contains extracted values from response
	Values []modbus.FieldValue
}

Result contains extracted values from response with request start time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL