Documentation
¶
Index ¶
- Constants
- func NewModbusDriver() driver.Driver
- func NewModbusExecutor() driver.Driver
- func NewModbusSimpleDriver() driver.Driver
- type AddressFilter
- type AddressInfo
- type Decoder
- type DeviceProbeResult
- type DeviceState
- type DeviceStateMachine
- type MTUMeasurement
- type MetricsRecorder
- type ModbusDriver
- func (d *ModbusDriver) Connect(ctx context.Context) error
- func (d *ModbusDriver) Disconnect() error
- func (d *ModbusDriver) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, ...)
- func (d *ModbusDriver) GetMetrics() model.ChannelMetrics
- func (d *ModbusDriver) GetProbeResult(slaveID uint8, regType string) *DeviceProbeResult
- func (d *ModbusDriver) Health() driver.HealthStatus
- func (d *ModbusDriver) Init(config model.DriverConfig) error
- func (d *ModbusDriver) ProbeDevice(ctx context.Context, slaveID uint8, regType string, startAddr uint16, ...) *DeviceProbeResult
- func (d *ModbusDriver) ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)
- func (d *ModbusDriver) ReadPointsWithSlaveID(ctx context.Context, slaveID uint8, points []model.Point) (map[string]model.Value, error)
- func (d *ModbusDriver) SetDeviceConfig(config map[string]any) error
- func (d *ModbusDriver) SetSlaveID(slaveID uint8) error
- func (d *ModbusDriver) TriggerReprobe(ctx context.Context, slaveID uint8, regType string, startAddr uint16, ...)
- func (d *ModbusDriver) WritePoint(ctx context.Context, point model.Point, value any) error
- type ModbusExecutor
- func (d *ModbusExecutor) Connect(ctx context.Context) error
- func (d *ModbusExecutor) Disconnect() error
- func (d *ModbusExecutor) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, ...)
- func (d *ModbusExecutor) Health() driver.HealthStatus
- func (d *ModbusExecutor) Init(cfg model.DriverConfig) error
- func (d *ModbusExecutor) ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)
- func (d *ModbusExecutor) SetDeviceConfig(config map[string]any) error
- func (d *ModbusExecutor) SetSlaveID(slaveID uint8) error
- func (d *ModbusExecutor) WritePoint(ctx context.Context, point model.Point, value any) error
- type ModbusSimpleDriver
- func (d *ModbusSimpleDriver) Connect(ctx context.Context) error
- func (d *ModbusSimpleDriver) Disconnect() error
- func (d *ModbusSimpleDriver) GetConnectionController() *core.ConnectionController
- func (d *ModbusSimpleDriver) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, ...)
- func (d *ModbusSimpleDriver) GetMetrics() model.ChannelMetrics
- func (d *ModbusSimpleDriver) Health() driver.HealthStatus
- func (d *ModbusSimpleDriver) Init(config model.DriverConfig) error
- func (d *ModbusSimpleDriver) ReadPoints(ctx context.Context, points []model.Point) (map[string]model.Value, error)
- func (d *ModbusSimpleDriver) SetDeviceConfig(config map[string]any) error
- func (d *ModbusSimpleDriver) SetSlaveID(slaveID uint8) error
- func (d *ModbusSimpleDriver) WritePoint(ctx context.Context, point model.Point, value any) error
- type ModbusTransport
- func (t *ModbusTransport) Connect(ctx context.Context) error
- func (t *ModbusTransport) DetectMTU(ctx context.Context) (uint16, error)
- func (t *ModbusTransport) Disconnect() error
- func (t *ModbusTransport) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, ...)
- func (t *ModbusTransport) IsConnected() bool
- func (t *ModbusTransport) NeedProbeCheck() bool
- func (t *ModbusTransport) ProbeConnection()
- func (t *ModbusTransport) ReadCoil(ctx context.Context, offset uint16) (bool, error)
- func (t *ModbusTransport) ReadCustom(ctx context.Context, funcCode byte, offset uint16, count uint16) ([]byte, error)
- func (t *ModbusTransport) ReadDiscreteInput(ctx context.Context, offset uint16) (bool, error)
- func (t *ModbusTransport) ReadRegisters(ctx context.Context, regType string, offset uint16, count uint16) ([]byte, error)
- func (t *ModbusTransport) RecordFailure(err error)
- func (t *ModbusTransport) RecordSuccess()
- func (t *ModbusTransport) SetMetricsRecorder(recorder MetricsRecorder, channelID string)
- func (t *ModbusTransport) SetUnitID(id uint8)
- func (t *ModbusTransport) WriteCoil(ctx context.Context, offset uint16, value bool) error
- func (t *ModbusTransport) WriteRegister(ctx context.Context, offset uint16, value uint16) error
- func (t *ModbusTransport) WriteRegisters(ctx context.Context, offset uint16, values []uint16) error
- type PointDecoder
- func (d *PointDecoder) Decode(point model.Point, raw []byte) (any, string, error)
- func (d *PointDecoder) EnableDataformatDecoder(enable bool)
- func (d *PointDecoder) Encode(point model.Point, value any) ([]uint16, error)
- func (d *PointDecoder) GetRegisterCount(dataType string) uint16
- func (d *PointDecoder) ParseAddress(addr string) (model.RegisterType, uint16, error)
- type PointGroup
- type PointRuntime
- type PointScheduler
- func (s *PointScheduler) GetDecoder() Decoder
- func (s *PointScheduler) GetSlaveID() uint8
- func (s *PointScheduler) Read(ctx context.Context, points []model.Point) (map[string]model.Value, error)
- func (s *PointScheduler) SetAddressMap(addressMap *ValidAddressMap)
- func (s *PointScheduler) SetGroupThreshold(threshold uint16)
- func (s *PointScheduler) SetMaxPacketSize(m uint16)
- func (s *PointScheduler) SetSlaveID(slaveID uint8)
- func (s *PointScheduler) Write(ctx context.Context, point model.Point, value any) error
- type ProbeConfig
- type ProbeEngine
- func (e *ProbeEngine) GetCachedResult(slaveID uint8, regType string) *DeviceProbeResult
- func (e *ProbeEngine) InvalidateCache(slaveID uint8, regType string)
- func (e *ProbeEngine) ProbeDevice(ctx context.Context, slaveID uint8, regType string, startAddr uint16, ...) *DeviceProbeResult
- func (e *ProbeEngine) TriggerReprobe(ctx context.Context, slaveID uint8, regType string, startAddr uint16, ...)
- type RTTModel
- type Scheduler
- type Transport
- type ValidAddressMap
- func (m *ValidAddressMap) GetOptimalBatchSize(slaveID uint8, regType string) int
- func (m *ValidAddressMap) GetValidBlocks(slaveID uint8, regType string, configuredStart uint16, configuredEnd uint16) []ValidBlock
- func (m *ValidAddressMap) IsAddressValid(slaveID uint8, regType string, addr uint16) bool
- func (m *ValidAddressMap) RecordAddressRange(slaveID uint8, regType string, start uint16, end uint16)
- func (m *ValidAddressMap) TriggerProbeIfNeeded(slaveID uint8, regType string, start uint16, end uint16)
- type ValidBlock
Constants ¶
const ( StateDisconnected driver.ConnState = driver.StateDisconnected StateConnecting driver.ConnState = driver.StateConnecting StateConnected driver.ConnState = driver.StateConnected StateRetrying driver.ConnState = driver.StateRetrying StateDead driver.ConnState = driver.StateDead )
Variables ¶
This section is empty.
Functions ¶
func NewModbusDriver ¶
func NewModbusExecutor ¶
func NewModbusSimpleDriver ¶
Types ¶
type AddressFilter ¶
type AddressInfo ¶
type AddressInfo struct {
Point model.Point
RegType model.RegisterType
Offset uint16
RegisterCount uint16 // 该点位占用的寄存器数
}
AddressInfo 用于存储点位的地址信息
type Decoder ¶
type Decoder interface {
Decode(point model.Point, raw []byte) (any, string, error)
Encode(point model.Point, value any) ([]uint16, error)
ParseAddress(addr string) (model.RegisterType, uint16, error)
GetRegisterCount(dataType string) uint16
}
Decoder 接口定义
type DeviceProbeResult ¶
type DeviceState ¶
type DeviceState string
const ( StateOnline DeviceState = "ONLINE" StateDegraded DeviceState = "DEGRADED" StateOffline DeviceState = "OFFLINE" StateRecovering DeviceState = "RECOVERING" StateProbing DeviceState = "PROBING" )
type DeviceStateMachine ¶
type DeviceStateMachine struct {
// contains filtered or unexported fields
}
func NewDeviceStateMachine ¶
func NewDeviceStateMachine() *DeviceStateMachine
func (*DeviceStateMachine) GetState ¶
func (sm *DeviceStateMachine) GetState() DeviceState
func (*DeviceStateMachine) OnFailure ¶
func (sm *DeviceStateMachine) OnFailure()
func (*DeviceStateMachine) OnSuccess ¶
func (sm *DeviceStateMachine) OnSuccess()
func (*DeviceStateMachine) SetProbing ¶
func (sm *DeviceStateMachine) SetProbing()
func (*DeviceStateMachine) SetRunning ¶
func (sm *DeviceStateMachine) SetRunning()
type MTUMeasurement ¶
type MetricsRecorder ¶
type MetricsRecorder interface {
RecordRequest(channelID string, success bool, duration time.Duration, errorType string)
RecordReconnect(channelID string)
RecordConnectionStart(channelID string)
RecordError(channelID string, errType, code, message string)
RecordPointDebug(channelID, pointID string, raw []byte, parsed any, quality string)
RecordCycle(channelID string, success bool)
}
MetricsRecorder 指标记录器接口
type ModbusDriver ¶
type ModbusDriver struct {
// contains filtered or unexported fields
}
ModbusDriver 旧版驱动实现,保留供测试引用。 生产注册由 ModbusExecutor 负责(modbus_executor.go)。
func (*ModbusDriver) Disconnect ¶
func (d *ModbusDriver) Disconnect() error
func (*ModbusDriver) GetConnectionMetrics ¶
func (*ModbusDriver) GetMetrics ¶
func (d *ModbusDriver) GetMetrics() model.ChannelMetrics
GetMetrics 返回Modbus驱动的详细指标
func (*ModbusDriver) GetProbeResult ¶
func (d *ModbusDriver) GetProbeResult(slaveID uint8, regType string) *DeviceProbeResult
GetProbeResult returns cached probe result for a device
func (*ModbusDriver) Health ¶
func (d *ModbusDriver) Health() driver.HealthStatus
func (*ModbusDriver) Init ¶
func (d *ModbusDriver) Init(config model.DriverConfig) error
func (*ModbusDriver) ProbeDevice ¶
func (d *ModbusDriver) ProbeDevice(ctx context.Context, slaveID uint8, regType string, startAddr uint16, endAddr uint16) *DeviceProbeResult
ProbeDevice performs smart address probing for a specific device (slave ID) This is isolated from normal collection and doesn't affect health scoring
func (*ModbusDriver) ReadPoints ¶
func (*ModbusDriver) ReadPointsWithSlaveID ¶
func (d *ModbusDriver) ReadPointsWithSlaveID(ctx context.Context, slaveID uint8, points []model.Point) (map[string]model.Value, error)
ReadPointsWithSlaveID reads points from a specific slave ID It sets the slave ID, reads points, and keeps the slave ID set for subsequent calls until changed
func (*ModbusDriver) SetDeviceConfig ¶
func (d *ModbusDriver) SetDeviceConfig(config map[string]any) error
SetDeviceConfig updates connection parameters dynamically This is required by the Driver interface but Modbus might not use it heavily if URL is fixed
func (*ModbusDriver) SetSlaveID ¶
func (d *ModbusDriver) SetSlaveID(slaveID uint8) error
SetSlaveID sets the unit ID for subsequent requests This is used for devices that support dynamic slave ID switching or when managing multiple slaves over one connection
func (*ModbusDriver) TriggerReprobe ¶
func (d *ModbusDriver) TriggerReprobe(ctx context.Context, slaveID uint8, regType string, startAddr uint16, endAddr uint16)
TriggerReprobe forces a new probe for a specific device Useful when device behavior changes or after firmware updates
func (*ModbusDriver) WritePoint ¶
WritePoint writes a single point
type ModbusExecutor ¶
type ModbusExecutor struct {
// contains filtered or unexported fields
}
func (*ModbusExecutor) Disconnect ¶
func (d *ModbusExecutor) Disconnect() error
func (*ModbusExecutor) GetConnectionMetrics ¶
func (*ModbusExecutor) Health ¶
func (d *ModbusExecutor) Health() driver.HealthStatus
func (*ModbusExecutor) Init ¶
func (d *ModbusExecutor) Init(cfg model.DriverConfig) error
func (*ModbusExecutor) ReadPoints ¶
func (*ModbusExecutor) SetDeviceConfig ¶
func (d *ModbusExecutor) SetDeviceConfig(config map[string]any) error
func (*ModbusExecutor) SetSlaveID ¶
func (d *ModbusExecutor) SetSlaveID(slaveID uint8) error
func (*ModbusExecutor) WritePoint ¶
type ModbusSimpleDriver ¶
type ModbusSimpleDriver struct {
// contains filtered or unexported fields
}
func (*ModbusSimpleDriver) Connect ¶
func (d *ModbusSimpleDriver) Connect(ctx context.Context) error
func (*ModbusSimpleDriver) Disconnect ¶
func (d *ModbusSimpleDriver) Disconnect() error
func (*ModbusSimpleDriver) GetConnectionController ¶
func (d *ModbusSimpleDriver) GetConnectionController() *core.ConnectionController
func (*ModbusSimpleDriver) GetConnectionMetrics ¶
func (*ModbusSimpleDriver) GetMetrics ¶
func (d *ModbusSimpleDriver) GetMetrics() model.ChannelMetrics
func (*ModbusSimpleDriver) Health ¶
func (d *ModbusSimpleDriver) Health() driver.HealthStatus
func (*ModbusSimpleDriver) Init ¶
func (d *ModbusSimpleDriver) Init(config model.DriverConfig) error
func (*ModbusSimpleDriver) ReadPoints ¶
func (*ModbusSimpleDriver) SetDeviceConfig ¶
func (d *ModbusSimpleDriver) SetDeviceConfig(config map[string]any) error
func (*ModbusSimpleDriver) SetSlaveID ¶
func (d *ModbusSimpleDriver) SetSlaveID(slaveID uint8) error
func (*ModbusSimpleDriver) WritePoint ¶
type ModbusTransport ¶
type ModbusTransport struct {
// contains filtered or unexported fields
}
ModbusTransport 实现 Transport 接口
func NewModbusTransport ¶
func NewModbusTransport(cfg model.DriverConfig) *ModbusTransport
func (*ModbusTransport) DetectMTU ¶
func (t *ModbusTransport) DetectMTU(ctx context.Context) (uint16, error)
DetectMTU performs a simple binary-search-like probe to determine a safely readable register count
func (*ModbusTransport) Disconnect ¶
func (t *ModbusTransport) Disconnect() error
func (*ModbusTransport) GetConnectionMetrics ¶
func (t *ModbusTransport) GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string, lastDisconnectTime time.Time)
GetConnectionMetrics 获取连接指标
func (*ModbusTransport) IsConnected ¶
func (t *ModbusTransport) IsConnected() bool
func (*ModbusTransport) NeedProbeCheck ¶
func (t *ModbusTransport) NeedProbeCheck() bool
func (*ModbusTransport) ProbeConnection ¶
func (t *ModbusTransport) ProbeConnection()
func (*ModbusTransport) ReadCustom ¶
func (t *ModbusTransport) ReadCustom(ctx context.Context, funcCode byte, offset uint16, count uint16) ([]byte, error)
ReadCustom 使用自定义功能码读取数据(暂不支持)
func (*ModbusTransport) ReadDiscreteInput ¶
func (*ModbusTransport) ReadRegisters ¶
func (*ModbusTransport) RecordFailure ¶
func (t *ModbusTransport) RecordFailure(err error)
func (*ModbusTransport) RecordSuccess ¶
func (t *ModbusTransport) RecordSuccess()
func (*ModbusTransport) SetMetricsRecorder ¶
func (t *ModbusTransport) SetMetricsRecorder(recorder MetricsRecorder, channelID string)
SetMetricsRecorder 设置指标收集器
func (*ModbusTransport) SetUnitID ¶
func (t *ModbusTransport) SetUnitID(id uint8)
func (*ModbusTransport) WriteRegister ¶
func (*ModbusTransport) WriteRegisters ¶
type PointDecoder ¶
type PointDecoder struct {
// contains filtered or unexported fields
}
PointDecoder 实现 Decoder 接口
func NewPointDecoder ¶
func NewPointDecoder(byteOrder4 string, startAddress int, addressBase uint16) *PointDecoder
func (*PointDecoder) EnableDataformatDecoder ¶
func (d *PointDecoder) EnableDataformatDecoder(enable bool)
func (*PointDecoder) GetRegisterCount ¶
func (d *PointDecoder) GetRegisterCount(dataType string) uint16
GetRegisterCount 根据数据类型获取占用的寄存器数
func (*PointDecoder) ParseAddress ¶
func (d *PointDecoder) ParseAddress(addr string) (model.RegisterType, uint16, error)
ParseAddress 解析Modbus点位地址 支持两种格式: 1. 标准格式:40001-50000(Holding), 30001-40000(Input), 10001-20000(Discrete), 1-10000(Coil) 2. 裸地址格式:直接使用偏移量,需配合register_type字段 3. 范围格式:如 "0-1" 表示从地址0开始的连续寄存器
type PointGroup ¶
type PointGroup struct {
RegType model.RegisterType // 寄存器类型
StartOffset uint16 // 起始地址
Count uint16 // 数量
Points []model.Point // 该组中的所有点位
CustomFuncCode byte // 自定义功能码(当RegType为RegCustom时使用)
}
PointGroup 表示一组连续的点位及其地址信息
type PointRuntime ¶
type PointRuntime struct {
Point model.Point
FailCount int
LastSuccess time.Time
State string // OK, SKIPPED
CooldownUntil time.Time
}
PointRuntime 点位运行态状态
type PointScheduler ¶
type PointScheduler struct {
// contains filtered or unexported fields
}
PointScheduler 实现 Scheduler 接口
func NewPointScheduler ¶
func (*PointScheduler) GetDecoder ¶
func (s *PointScheduler) GetDecoder() Decoder
func (*PointScheduler) GetSlaveID ¶
func (s *PointScheduler) GetSlaveID() uint8
func (*PointScheduler) SetAddressMap ¶
func (s *PointScheduler) SetAddressMap(addressMap *ValidAddressMap)
func (*PointScheduler) SetGroupThreshold ¶
func (s *PointScheduler) SetGroupThreshold(threshold uint16)
SetGroupThreshold 设置块读合并的最大地址间隙(来自 GapOptimizer)。
func (*PointScheduler) SetMaxPacketSize ¶
func (s *PointScheduler) SetMaxPacketSize(m uint16)
SetMaxPacketSize allows updating the scheduler's maximum packet size (e.g. after MTU probe)
func (*PointScheduler) SetSlaveID ¶
func (s *PointScheduler) SetSlaveID(slaveID uint8)
type ProbeConfig ¶
type ProbeEngine ¶
type ProbeEngine struct {
// contains filtered or unexported fields
}
func NewProbeEngine ¶
func NewProbeEngine(transport Transport, config ProbeConfig) *ProbeEngine
func (*ProbeEngine) GetCachedResult ¶
func (e *ProbeEngine) GetCachedResult(slaveID uint8, regType string) *DeviceProbeResult
func (*ProbeEngine) InvalidateCache ¶
func (e *ProbeEngine) InvalidateCache(slaveID uint8, regType string)
func (*ProbeEngine) ProbeDevice ¶
func (e *ProbeEngine) ProbeDevice(ctx context.Context, slaveID uint8, regType string, startAddr uint16, endAddr uint16) *DeviceProbeResult
func (*ProbeEngine) TriggerReprobe ¶
type Scheduler ¶
type Scheduler interface {
Read(ctx context.Context, points []model.Point) (map[string]model.Value, error)
Write(ctx context.Context, point model.Point, value any) error
GetDecoder() Decoder
}
Scheduler 接口定义
type Transport ¶
type Transport interface {
Connect(ctx context.Context) error
Disconnect() error
IsConnected() bool
ReadRegisters(ctx context.Context, regType string, offset uint16, count uint16) ([]byte, error)
ReadCoil(ctx context.Context, offset uint16) (bool, error)
ReadDiscreteInput(ctx context.Context, offset uint16) (bool, error)
ReadCustom(ctx context.Context, funcCode byte, offset uint16, count uint16) ([]byte, error)
WriteRegister(ctx context.Context, offset uint16, value uint16) error
WriteRegisters(ctx context.Context, offset uint16, values []uint16) error
WriteCoil(ctx context.Context, offset uint16, value bool) error
SetUnitID(id uint8)
GetConnectionMetrics() (connectionSeconds int64, reconnectCount int64, localAddr string, remoteAddr string, lastDisconnectTime time.Time)
}
Transport 接口定义
type ValidAddressMap ¶
type ValidAddressMap struct {
// contains filtered or unexported fields
}
func NewValidAddressMap ¶
func NewValidAddressMap(engine *ProbeEngine) *ValidAddressMap
func (*ValidAddressMap) GetOptimalBatchSize ¶
func (m *ValidAddressMap) GetOptimalBatchSize(slaveID uint8, regType string) int
func (*ValidAddressMap) GetValidBlocks ¶
func (m *ValidAddressMap) GetValidBlocks(slaveID uint8, regType string, configuredStart uint16, configuredEnd uint16) []ValidBlock
func (*ValidAddressMap) IsAddressValid ¶
func (m *ValidAddressMap) IsAddressValid(slaveID uint8, regType string, addr uint16) bool
func (*ValidAddressMap) RecordAddressRange ¶
func (m *ValidAddressMap) RecordAddressRange(slaveID uint8, regType string, start uint16, end uint16)
func (*ValidAddressMap) TriggerProbeIfNeeded ¶
func (m *ValidAddressMap) TriggerProbeIfNeeded(slaveID uint8, regType string, start uint16, end uint16)