Documentation
¶
Index ¶
Constants ¶
const ( ReadCoilsFunc = "ReadCoils" // 0x01 ReadDiscreteInputsFunc = "ReadDiscreteInputs" // 0x02 ReadHoldingRegistersFunc = "ReadHoldingRegisters" // 0x03 ReadInputRegistersFunc = "ReadInputRegisters" // 0x04 WriteCoilsFunc = "WriteCoils" // 0x05, 0x0F WriteHoldingRegistersFunc = "WriteHoldingRegisters" // 0x06, 0x10 ByteOrderABCD = "ABCD" // Big-endian (standard network order) ByteOrderDCBA = "DCBA" // Little-endian (x86, ARM in little-endian mode) ByteOrderCDAB = "CDAB" // Middle-endian (PDP-style) ByteOrderBADC = "BADC" // Byte-swapped (less common, but used in some contexts) )
const ( BoolType = "bool" Int16Type = "int16" Int32Type = "int32" Uint16Type = "uint16" Uint32Type = "uint32" Float32Type = "float32" StringType = "string" )
Variables ¶
var ( // ErrInvalidWriteValue indicates the write value does not match the register type. ErrInvalidWriteValue = errors.New("invalid write value") // ErrValueOutOfRange indicates the write value is out of range for the register type. ErrValueOutOfRange = errors.New("value out of range for type") // ErrUnsupportedWriteType indicates the field type cannot be written to a register. ErrUnsupportedWriteType = errors.New("unsupported write type") // ErrAddressOutOfRange indicates a field extends past the last Modbus address. ErrAddressOutOfRange = errors.New("field address out of range") // or returned an invalid response. ErrDeviceUnavailable = errors.New("modbus device unavailable") // ErrDeviceRejected indicates the device returned a Modbus exception response. ErrDeviceRejected = errors.New("modbus device rejected request") )
var ClientOrderFields = map[string]string{
"id": "id",
"name": "LOWER(name)",
}
ClientOrderFields maps API-facing order keys to SQL column expressions for the modbus clients table.
Functions ¶
Types ¶
type ClientRepository ¶
type ClientRepository interface {
// Save persists multiple Modbus clients.
// Clients are saved using a transaction.
// If one client fails, then none will be saved.
// Successful operation is indicated by non-nil error response.
Save(ctx context.Context, dls ...Client) ([]Client, error)
// RetrieveByThing retrieves clients related to
// a certain thing identified by a given ID.
RetrieveByThing(ctx context.Context, thingID string, pm PageMetadata) (ClientsPage, error)
// RetrieveByGroup retrieves Modbus clients related to
// a certain group identified by a given ID.
RetrieveByGroup(ctx context.Context, groupID string, pm PageMetadata) (ClientsPage, error)
// RetrieveByID retrieves a client having the provided ID.
RetrieveByID(ctx context.Context, id string) (Client, error)
// RetrieveAll retrieves all clients.
RetrieveAll(ctx context.Context) ([]Client, error)
// Update performs an update to the existing client.
// A non-nil error is returned to indicate operation failure.
Update(ctx context.Context, w Client) error
// Remove removes Modbus clients having the provided IDs.
Remove(ctx context.Context, ids ...string) error
// RemoveByThing removes Modbus clients related to
// a certain thing identified by a given thing ID.
RemoveByThing(ctx context.Context, thingID string) error
// RemoveByGroup removes Modbus clients related to
// a certain group identified by a given group ID.
RemoveByGroup(ctx context.Context, groupID string) error
}
type ClientsPage ¶
type ClientsPage struct {
PageMetadata
Clients []Client
}
type PageMetadata ¶ added in v0.39.0
type PageMetadata struct {
Total uint64 `json:"total,omitempty"`
Offset uint64 `json:"offset,omitempty"`
Limit uint64 `json:"limit,omitempty"`
Order string `json:"order,omitempty"`
Dir string `json:"dir,omitempty"`
Name string `json:"name,omitempty"`
IPAddress string `json:"ip_address,omitempty"`
Port string `json:"port,omitempty"`
SlaveID int64 `json:"slave_id,omitempty"`
Frequency string `json:"frequency,omitempty"`
}
PageMetadata contains page metadata that helps navigation.
type Publisher ¶ added in v0.42.0
type Publisher interface {
messaging.MessageDispatcher
}
Publisher specifies the minimal publishing capability the modbus service needs.
type ReadRequest ¶ added in v0.44.0
type ReadRequest struct {
ThingID string
Name string
Unit string
IPAddress string
Port string
SlaveID uint8
FunctionCode string
Address uint16
Type string
ByteOrder string
Scale float64
Length uint16
}
ReadRequest describes an immediate Modbus read made on behalf of a thing, with no persisted client. Type, ByteOrder, Scale and Length apply to register function codes only. The value read is published as a message of the thing under Name, or under the address when Name is empty.
type Service ¶
type Service interface {
// CreateClients creates clients for certain thing identified by the thing ID.
CreateClients(ctx context.Context, token, thingID string, Clients ...Client) ([]Client, error)
// CreateClient creates a single client for the thing identified by the thing ID,
// with data fields parsed from an uploaded file.
CreateClient(ctx context.Context, token, thingID string, client Client) (Client, error)
// ListClientsByThing retrieves data about a subset of clients
// related to a certain thing.
ListClientsByThing(ctx context.Context, token, thingID string, pm PageMetadata) (ClientsPage, error)
// ListClientsByGroup retrieves data about a subset of clients
// related to a certain group.
ListClientsByGroup(ctx context.Context, token, groupID string, pm PageMetadata) (ClientsPage, error)
// ViewClient retrieves data about a client identified with the provided ID.
ViewClient(ctx context.Context, token, id string) (Client, error)
// ReadRegister performs an immediate Modbus read on behalf of the thing, publishes the
// decoded value as a message of the thing and returns it. No client is created or looked up.
ReadRegister(ctx context.Context, token string, req ReadRequest) (any, error)
// WriteRegister performs an immediate Modbus write on behalf of the thing, reads the value back
// and publishes it as a message of the thing. No client is created or looked up.
WriteRegister(ctx context.Context, token string, req WriteRequest) error
// UpdateClient updates client identified by the provided ID.
UpdateClient(ctx context.Context, token string, client Client) error
// RemoveClients removes clients identified with the provided IDs.
RemoveClients(ctx context.Context, token string, id ...string) error
// RemoveClientsByThing removes clients related to the specified thing,
// identified by the provided thing ID.
RemoveClientsByThing(ctx context.Context, thingID string) error
// RemoveClientsByGroup removes clients related to the specified group,
// identified by the provided group ID.
RemoveClientsByGroup(ctx context.Context, groupID string) error
// RescheduleTasks reschedules all tasks for things related to the specified profile ID.
RescheduleTasks(ctx context.Context, profileID string, config *domain.ProfileConfig) error
// LoadAndScheduleTasks loads schedulers and starts them to execute requests based on client configuration.
LoadAndScheduleTasks(ctx context.Context) error
}
Service specifies an API that must be fulfilled by the domain service implementation, and all of its decorators (e.g. logging & metrics). All methods that accept a token parameter use it to identify and authorize the user performing the operation.
func New ¶
func New(things domain.ThingsClient, pub Publisher, clients ClientRepository, idp uuid.IDProvider, logger logger.Logger) Service
type WriteRequest ¶ added in v0.44.0
type WriteRequest struct {
ThingID string
Name string
Unit string
IPAddress string
Port string
SlaveID uint8
Address uint16
Type string
ByteOrder string
Scale float64
Value any
}
WriteRequest describes an immediate Modbus write made on behalf of a thing, with no persisted client. The write function code is derived from Type, not from any FunctionCode. The value read back after the write is published as a message of the thing under Name, or under the address when Name is empty.