Register a function, this function will be invoked when UDSClass is initialized.
Non-async or async function
Perform actions following UDS initialization using a normal function.
Util.Init(()=>{
console.log('Hello UDS!')
})
Perform actions following UDS initialization using an async function.
Util.Init(async ()=>{
const file=await fs.readFile(path.join(process.env.PROJECT_ROOT,'file.bin'))
let length=file.length
console.log('Hello UDS file! file length is', length)
})
The last registered function will override the previous ones.
// The following code will be ignored
Util.Init(async ()=>{
console.log('1')
})
// The following code will take effect
Util.Init(async ()=>{
console.log('2')
})
Unsubscribes from CAN messages.
The identifier of the CAN message to unsubscribe from. If true
, unsubscribes from all CAN messages.
The callback function to remove from the event listeners.
Unsubscribes from an event listener for a specific key.
The key to unsubscribe from. Only the first character of the key is used, * is a wildcard.
The callback function to remove from the event listeners.
Unsubscribes from LIN messages.
The identifier of the LIN message to unsubscribe from. If true
, unsubscribes from all LIN messages.
The callback function to remove from the event listeners.
Removes an event listener for signal updates from CAN/LIN databases. The specified callback function will no longer be invoked for signal updates.
The signal name to stop listening for (format: "database.signalName")
The exact callback function to remove (must be the same reference)
// Define a callback function
const rpmCallback = ({ rawValue, physValue }) => {
console.log(`RPM: ${physValue}`)
}
// Register the listener
OnSignal("Engine.EngineRPM", rpmCallback)
// Later, remove the listener
OffSignal("Engine.EngineRPM", rpmCallback)
// Anonymous functions cannot be removed easily, so use named functions:
// ❌ This won't work for removal:
// OnSignal("Engine.RPM", ({ physValue }) => console.log(physValue))
// ✅ This will work for removal:
const callback = ({ physValue }) => console.log(physValue)
OnSignal("Engine.RPM", callback)
OffSignal("Engine.RPM", callback)
Unsubscribes from SOMEIP messages.
The SOMEIP message identifier to unsubscribe from. If true
, unsubscribes from all SOMEIP messages.
The callback function to remove from the event listeners.
Unsubscribes from an event listener for a variable update.
The name of the variable to unsubscribe from, * is a wildcard.
The callback function to remove from the event listeners.
Subscribe to an event, invoking the registered function when the event is emitted.
The
UDS
is a UDSClass type and has already been created by Service.
send functions
Util.On('Can.testService.send', async (req) => {
// The req is a `DiagRequest`
console.log(req.getServiceName(), ': send');
});
recv function
Util.On('Can.testService.recv', async (req) => {
// The req is a `DiagResponse`
console.log(req.getServiceName(), ':recv');
});
Registers an event listener for CAN messages.
The CAN message ID to listen for. If true
, listens for all CAN messages.
The callback function to be invoked when a CAN message is received.
Registers an event listener for CAN messages that will be invoked once.
The CAN message ID to listen for. If true
, listens for all CAN messages.
The callback function to be invoked when a CAN message is received.
Registers an event listener for a specific key.
The key to listen for. Only the first character of the key is used, * is a wildcard.
The callback function to be executed when the event is triggered. This can be a synchronous function or a function returning a Promise.
Registers an event listener for a specific key that will be invoked once.
The key to listen for. Only the first character of the key is used, * is a wildcard.
The callback function to be executed when the event is triggered. This can be a synchronous function or a function returning a Promise.
Registers an event listener for LIN messages.
The LIN message ID or ${databaseName}.${frameName} to listen for. If true
, listens for all LIN messages.
The callback function to be invoked when a LIN message is received.
Registers an event listener for LIN messages that will be invoked once.
The LIN message ID or ${databaseName}.${frameName} to listen for. If true
, listens for all LIN messages.
The callback function to be invoked when a LIN message is received.
Registers an event listener for signal updates from CAN/LIN databases. The signal will be emitted whenever the specified signal value changes.
The signal name to listen for (format: "database.signalName")
The callback function invoked when the signal is updated
// Listen for engine RPM signal updates
OnSignal("Engine.EngineRPM", ({ rawValue, physValue }) => {
console.log(`Engine RPM: ${physValue} (raw: ${rawValue})`)
})
// Listen for gear position signal with enum values
OnSignal("Transmission.GearPosition", ({ rawValue, physValue }) => {
console.log(`Gear: ${physValue}`) // Could be "Park", "Drive", "Reverse", etc.
})
// Async callback example
OnSignal("Battery.Voltage", async ({ rawValue, physValue }) => {
if (physValue < 12.0) {
await sendWarning("Low battery voltage detected!")
}
})
Registers a one-time event listener for signal updates from CAN/LIN databases. The listener will be automatically removed after the first signal update.
The signal name to listen for (format: "database.signalName")
The callback function invoked when the signal is updated (only once)
// Wait for the first engine start signal
OnSignalOnce("Engine.EngineStatus", ({ rawValue, physValue }) => {
if (physValue === "Running") {
console.log("Engine started successfully!")
}
})
// Wait for initialization complete signal
OnSignalOnce("System.InitStatus", async ({ rawValue, physValue }) => {
if (physValue === "Complete") {
await startDiagnosticSequence()
}
})
Registers an event listener for SOMEIP messages.
The SOMEIP message identifier in format "service.instance.method" or "service.instance.*". If true
, listens for all SOMEIP messages.
The callback function to be invoked when a SOMEIP message is received.
// Listen for all SOMEIP messages
Util.OnSomeipMessage(true, (msg) => {
console.log('Received SOMEIP message:', msg);
});
// Listen for specific service/instance/method
Util.OnSomeipMessage('0034.5678.90ab', (msg) => {
console.log('Received specific SOMEIP message:', msg);
});
// Listen for specific service/wildcard
Util.OnSomeipMessage('0034.*.*', (msg) => {
console.log('Received specific SOMEIP message:', msg);
});
Registers a one-time event listener for SOMEIP messages. The listener will be automatically removed after being invoked once.
The SOMEIP message identifier in format "service.instance.method" or "service.instance.*". If true
, listens for all SOMEIP messages.
The callback function to be invoked once when a SOMEIP message is received.
// Listen once for any SOMEIP message
Util.OnSomeipMessageOnce(true, (msg) => {
console.log('Received one SOMEIP message:', msg);
});
// Listen once for specific service/instance/method
Util.OnSomeipMessageOnce('1234.5678.90ab', (msg) => {
console.log('Received one specific SOMEIP message:', msg);
});
Registers an event listener for a variable update.
The name of the variable to listen for, * is a wildcard.
The callback function to be executed when the variable is updated. This can be a synchronous function or a function returning a Promise. The callback receives an object with name and value properties.
Registers an event listener for a variable update that will be invoked once.
The name of the variable to listen for, * is a wildcard.
The callback function to be executed when the variable is updated. This can be a synchronous function or a function returning a Promise. The callback receives an object with name and value properties.