Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
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 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
// Defaults to DefaultConnectClient
ConnectFunc func(ctx context.Context, batchProtocol modbus.ProtocolType, address string) (Client, error)
// 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.
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.
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