Documentation
¶
Index ¶
- Variables
- func CreateAM319Codec() string
- func CreateMCFLW13IOCodec() string
- func CreateSDM230Codec() string
- func InjectConversionHelpers(vm *goja.Runtime) error
- func InjectDeviceHelpers(vm *goja.Runtime, device DeviceInterface) error
- func InjectStateHelpers(vm *goja.Runtime, state *State) error
- type Codec
- type CodecLibrary
- func (cl *CodecLibrary) Add(codec *Codec) error
- func (cl *CodecLibrary) Clear()
- func (cl *CodecLibrary) Count() int
- func (cl *CodecLibrary) FromJSON(data []byte) error
- func (cl *CodecLibrary) Get(id int) (*Codec, error)
- func (cl *CodecLibrary) GetNextID() int
- func (cl *CodecLibrary) List() []CodecMetadata
- func (cl *CodecLibrary) LoadDefaults()
- func (cl *CodecLibrary) Remove(id int) error
- func (cl *CodecLibrary) SetNextID(id int)
- func (cl *CodecLibrary) ToJSON() ([]byte, error)
- func (cl *CodecLibrary) Update(id int, name string, script string) error
- type CodecMetadata
- type DeviceInterface
- type Executor
- func (e *Executor) Close()
- func (e *Executor) ExecuteDecode(script string, bytes []byte, fPort uint8, state *State, device DeviceInterface) error
- func (e *Executor) ExecuteEncode(script string, state *State, device DeviceInterface) ([]byte, uint8, error)
- func (e *Executor) GetMetrics() ExecutorMetrics
- func (e *Executor) ResetMetrics()
- type ExecutorConfig
- type ExecutorMetrics
- type Registry
- func (r *Registry) AddCodec(codec *Codec) error
- func (r *Registry) Close()
- func (r *Registry) DecodePayload(codecID int, devEUI string, bytes []byte, fPort uint8, device DeviceInterface) error
- func (r *Registry) EncodePayload(codecID int, devEUI string, device DeviceInterface) ([]byte, uint8, error)
- func (r *Registry) GetCodec(id int) (*Codec, error)
- func (r *Registry) GetCodecCount() int
- func (r *Registry) GetCodecIDByName(name string) int
- func (r *Registry) GetNextID() int
- func (r *Registry) GetOrCreateState(devEUI string) *State
- func (r *Registry) ListCodecs() []CodecMetadata
- func (r *Registry) Load(filepath string) error
- func (r *Registry) LoadDefaults()
- func (r *Registry) LoadStates(filepath string) error
- func (r *Registry) RemoveCodec(id int) error
- func (r *Registry) RemoveState(devEUI string)
- func (r *Registry) Save(filepath string) error
- func (r *Registry) SaveStates(filepath string) error
- func (r *Registry) UpdateCodec(id int, name string, script string) error
- type State
- type VMPool
Constants ¶
This section is empty.
Variables ¶
var ( // ErrCodecNotFound is returned when a codec is not found ErrCodecNotFound = errors.New("codec not found") // ErrInvalidCodecFormat is returned when codec validation fails ErrInvalidCodecFormat = errors.New("invalid codec format") )
var ( // ErrInvalidScript is returned when the JavaScript code is invalid ErrInvalidScript = errors.New("invalid JavaScript code") // ErrOnUplinkNotFound is returned when OnUplink function is not defined ErrOnUplinkNotFound = errors.New("OnUplink function not found") // ErrInvalidReturnType is returned when the codec returns an invalid type ErrInvalidReturnType = errors.New("invalid return type from codec") // ErrExecutionTimeout is returned when codec execution exceeds the configured timeout ErrExecutionTimeout = errors.New("codec execution timeout") )
Functions ¶
func CreateAM319Codec ¶
func CreateAM319Codec() string
CreateAM319Codec returns the Milesight AM319 codec script
func CreateMCFLW13IOCodec ¶
func CreateMCFLW13IOCodec() string
CreateMCFLW13IOCodec returns the Enginko MCF-LW13IO I/O Controller codec script
func CreateSDM230Codec ¶
func CreateSDM230Codec() string
CreateSDM230Codec returns the Eastron SDM230 energy meter codec script
func InjectConversionHelpers ¶
InjectConversionHelpers injects payload conversion helper functions into the JavaScript VM These allow explicit conversion from hex/base64 strings to byte arrays
func InjectDeviceHelpers ¶
func InjectDeviceHelpers(vm *goja.Runtime, device DeviceInterface) error
InjectDeviceHelpers injects device configuration helper functions into the JavaScript VM These allow JavaScript codecs to read and modify device settings
Types ¶
type Codec ¶
type Codec struct {
ID int `json:"id"` // Unique identifier (sequential)
Name string `json:"name"` // Human-readable name
Script string `json:"script"` // JavaScript code
}
Codec represents a JavaScript codec for encoding/decoding device payloads Compatible with ChirpStack codec format
func (*Codec) Metadata ¶
func (c *Codec) Metadata() CodecMetadata
Metadata returns metadata without the script
type CodecLibrary ¶
type CodecLibrary struct {
// contains filtered or unexported fields
}
CodecLibrary manages a collection of codecs
func NewCodecLibrary ¶
func NewCodecLibrary() *CodecLibrary
NewCodecLibrary creates a new codec library
func (*CodecLibrary) Add ¶
func (cl *CodecLibrary) Add(codec *Codec) error
Add adds a codec to the library with the next available ID
func (*CodecLibrary) FromJSON ¶
func (cl *CodecLibrary) FromJSON(data []byte) error
FromJSON deserializes a codec library from JSON
func (*CodecLibrary) Get ¶
func (cl *CodecLibrary) Get(id int) (*Codec, error)
Get retrieves a codec by ID
func (*CodecLibrary) GetNextID ¶
func (cl *CodecLibrary) GetNextID() int
GetNextID returns the next ID that will be assigned
func (*CodecLibrary) List ¶
func (cl *CodecLibrary) List() []CodecMetadata
List returns all codec metadata
func (*CodecLibrary) LoadDefaults ¶
func (cl *CodecLibrary) LoadDefaults()
LoadDefaults loads default example codecs with sequential IDs
func (*CodecLibrary) Remove ¶
func (cl *CodecLibrary) Remove(id int) error
Remove removes a codec from the library
func (*CodecLibrary) SetNextID ¶
func (cl *CodecLibrary) SetNextID(id int)
SetNextID sets the next ID to assign
func (*CodecLibrary) ToJSON ¶
func (cl *CodecLibrary) ToJSON() ([]byte, error)
ToJSON serializes the codec library to JSON
type CodecMetadata ¶
CodecMetadata holds metadata about a codec without the script
type DeviceInterface ¶
type DeviceInterface interface {
GetSendInterval() time.Duration
SetSendInterval(time.Duration)
Print(content string, err error, printType int)
}
DeviceInterface defines the interface for accessing device configuration from JavaScript
type Executor ¶
type Executor struct {
// contains filtered or unexported fields
}
Executor manages JavaScript codec execution with goja
func NewExecutor ¶
func NewExecutor(config *ExecutorConfig) *Executor
NewExecutor creates a new codec executor
func (*Executor) Close ¶
func (e *Executor) Close()
Close closes the executor and releases resources
func (*Executor) ExecuteDecode ¶
func (e *Executor) ExecuteDecode(script string, bytes []byte, fPort uint8, state *State, device DeviceInterface) error
ExecuteDecode executes the OnDownlink function from a JavaScript codec Parameters:
- script: The JavaScript code containing the OnDownlink function
- bytes: The byte array to decode
- fPort: The LoRaWAN fPort
- state: Device state for stateful decoding
- device: Device interface for accessing configuration
OnDownlink is executed for its side effects (log, setState, setSendInterval). Any return value from the JavaScript function is ignored.
func (*Executor) ExecuteEncode ¶
func (e *Executor) ExecuteEncode(script string, state *State, device DeviceInterface) ([]byte, uint8, error)
ExecuteEncode executes the OnUplink function from a JavaScript codec Parameters:
- script: The JavaScript code containing the OnUplink function
- state: Device state for stateful encoding
- device: Device interface for accessing configuration (send interval, etc.)
Returns the encoded byte array, the fPort (from device or codec), and any error
func (*Executor) GetMetrics ¶
func (e *Executor) GetMetrics() ExecutorMetrics
GetMetrics returns current executor metrics
func (*Executor) ResetMetrics ¶
func (e *Executor) ResetMetrics()
ResetMetrics resets all metrics to zero
type ExecutorConfig ¶
ExecutorConfig holds configuration for the Executor
func DefaultExecutorConfig ¶
func DefaultExecutorConfig() *ExecutorConfig
DefaultExecutorConfig returns default configuration
type ExecutorMetrics ¶
type ExecutorMetrics struct {
TotalExecutions uint64
TotalErrors uint64
TotalTimeouts uint64
// contains filtered or unexported fields
}
ExecutorMetrics tracks codec execution statistics
type Registry ¶
type Registry struct {
// contains filtered or unexported fields
}
Registry manages codecs and device states for the entire simulator
func NewRegistry ¶
func NewRegistry(config *ExecutorConfig) *Registry
NewRegistry creates a new codec registry
func (*Registry) Close ¶
func (r *Registry) Close()
Close closes the registry and releases resources
func (*Registry) DecodePayload ¶
func (r *Registry) DecodePayload(codecID int, devEUI string, bytes []byte, fPort uint8, device DeviceInterface) error
DecodePayload executes the OnDownlink function from a codec Parameters:
- codecID: ID of the codec to use
- devEUI: Device EUI for state management
- bytes: Bytes to decode
- fPort: LoRaWAN fPort
- device: Device interface for accessing configuration
OnDownlink is executed for its side effects (log, setState, setSendInterval).
func (*Registry) EncodePayload ¶
func (r *Registry) EncodePayload(codecID int, devEUI string, device DeviceInterface) ([]byte, uint8, error)
EncodePayload encodes a payload using a codec Parameters:
- codecID: ID of the codec to use
- devEUI: Device EUI for state management
- device: Device interface for accessing configuration (send interval, etc.)
Returns the encoded bytes, actual fPort (from codec or device), and any error
func (*Registry) GetCodecCount ¶
GetCodecCount returns the number of codecs in the library
func (*Registry) GetCodecIDByName ¶
GetCodecIDByName returns the ID of a codec by its name, or 0 if not found
func (*Registry) GetOrCreateState ¶
GetOrCreateState gets or creates a state for a device
func (*Registry) ListCodecs ¶
func (r *Registry) ListCodecs() []CodecMetadata
ListCodecs returns all codec metadata
func (*Registry) Load ¶
Load loads the codec library from a file If the file doesn't exist or loading fails, it loads defaults instead
func (*Registry) LoadDefaults ¶
func (r *Registry) LoadDefaults()
LoadDefaults loads default codecs into the library
func (*Registry) LoadStates ¶
LoadStates replaces the in-memory state map with the contents of the given file. Missing file is non-fatal and leaves the map empty.
func (*Registry) RemoveCodec ¶
RemoveCodec removes a codec from the library
func (*Registry) RemoveState ¶
RemoveState removes the persisted state for a device (e.g. on device deletion)
func (*Registry) SaveStates ¶
SaveStates writes per-device codec state to disk as a JSON map keyed by DevEUI.
type State ¶
type State struct {
DevEUI string `json:"devEUI"`
Variables map[string]interface{} `json:"variables"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
// contains filtered or unexported fields
}
State holds the runtime state for a device's codec execution
func (*State) GetVariable ¶
GetVariable returns the value of a variable (nil if not set)
func (*State) SetVariable ¶
SetVariable sets the value of a variable
type VMPool ¶
type VMPool struct {
// contains filtered or unexported fields
}
VMPool manages a pool of goja VMs for reuse
func NewVMPool ¶
NewVMPool creates a new VM pool with the specified size. All VMs are pre-created and callers block until one is available.