mock

package
v1.1.10 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Inverter

type Inverter struct {

	// ReadCount tracks total ReadRegisters calls for test assertions.
	ReadCount int
	// WriteCount tracks total write calls for test assertions.
	WriteCount int

	// ReadHook is called on every ReadRegisters call before returning data.
	// Return a non-nil error to simulate a device error for that range.
	ReadHook func(startAddr, count uint16) error
	// WriteHook is called on every write call before committing.
	WriteHook func(addr, value uint16) error
	// contains filtered or unexported fields
}

Inverter is an in-memory MODBUS device for testing. It implements the modbus.Client interface with a configurable register map.

func NewInverter

func NewInverter(initial map[uint16]uint16) *Inverter

NewInverter creates a mock Inverter with the given initial register values.

func (*Inverter) Close

func (m *Inverter) Close() error

func (*Inverter) Connect

func (m *Inverter) Connect() error

func (*Inverter) GetRegister

func (m *Inverter) GetRegister(addr uint16) (uint16, bool)

GetRegister reads a register value directly.

func (*Inverter) ReadRegisters

func (m *Inverter) ReadRegisters(startAddr uint16, count uint16) ([]uint16, error)

func (*Inverter) SetRegister

func (m *Inverter) SetRegister(addr, value uint16)

SetRegister sets a register value directly (bypasses hooks).

func (*Inverter) WriteMultipleRegisters

func (m *Inverter) WriteMultipleRegisters(startAddr uint16, values []uint16) error

func (*Inverter) WriteSingleRegister

func (m *Inverter) WriteSingleRegister(addr uint16, value uint16) error

type Sim

type Sim struct {
	*Inverter
	// contains filtered or unexported fields
}

Sim is a live-simulated inverter that ticks internally, updating registers to mimic a real ASP48100U200-H. Embeds Inverter for the modbus.Client interface.

In addition to the time-based background simulation (Start/Tick), Sim supports deterministic "poke" methods (SetSOC, SetPV, SetLoad, etc.) that immediately update all related registers without jitter. This makes Sim usable both as a live simulator and as a deterministic test fixture.

func NewSim

func NewSim() *Sim

NewSim creates a Sim pre-loaded with realistic register values for an ASP48100U200-H (48V LiFePO4, split-phase 120/240V).

func (*Sim) ClearFaults

func (s *Sim) ClearFaults()

ClearFaults resets all fault registers to zero.

func (*Sim) SetGridVoltage

func (s *Sim) SetGridVoltage(volts float64)

SetGridVoltage sets the grid input voltage (0 = off-grid) and updates registers.

func (*Sim) SetLoad

func (s *Sim) SetLoad(watts float64)

SetLoad sets the current AC load in watts and updates all related registers.

func (*Sim) SetPV

func (s *Sim) SetPV(pv1Watts, pv2Watts float64)

SetPV sets PV1 and PV2 power in watts and updates all related registers. This overrides the time-based PV curve until the next Tick/Start cycle.

func (*Sim) SetParallelMode

func (s *Sim) SetParallelMode(mode uint16)

SetParallelMode sets the parallel mode register value.

func (*Sim) SetSOC

func (s *Sim) SetSOC(pct float64)

SetSOC directly sets the battery state of charge (0-100).

func (*Sim) Start

func (s *Sim) Start(interval time.Duration)

Start begins the background simulation tick at the given interval. Call Stop() to clean up.

func (*Sim) Stop

func (s *Sim) Stop()

Stop halts the background simulation.

func (*Sim) Tick

func (s *Sim) Tick()

Tick advances the simulation by one step manually (for deterministic tests).

func (*Sim) TriggerFault

func (s *Sim) TriggerFault(code uint16)

TriggerFault queues a fault code to be applied on the next tick.

Jump to

Keyboard shortcuts

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