Dynamic Device method injection utilities for the Total Control scripting system.
This module lets you define and attach custom JavaScript functions to the Device and DeviceArray
prototypes at runtime, so they can be called as native methods (e.g., device.myFn() or
devices.myFn() for multiple devices). It supports both synchronous and asynchronous execution
across multiple devices and provides a safe way to remove injected methods.
It is designed to be used from Userlib.js (Total Control 7.0.0+), where custom behaviors are
registered via addDeviceFunction() and, when needed, removed with delDeviceFunction().
Features
- Register user-defined functions as
Device/DeviceArraymethods with a chosen alias - Execute on a single device or broadcast to multiple devices (
multiple: true) - Choose execution mode per function:
- sync: iterate devices one-by-one
- async: parallel per-device execution using Java threads
- Automatic argument injection: the target
Deviceinstance is prepended to your function’s arguments - Safe teardown: remove custom methods from
Array,Device, andDeviceArrayprototypes - Graceful error handling during dynamic binding (
evalguarded with try/catch) - Backward compatibility defaults for options (sync + single-device if not specified)
Usage Example
// 1) Define the implementation (first parameter will be the Device instance)
function test(dev) {
dev.print("hello from " + dev.getSN());
}
// 2) Register as a method on Device (and DeviceArray if multiple=true)
// Note: requires Total Control 7.0.0+ for addDeviceFunction(functionName, fn, option)
addDeviceFunction("testFn", test, { mode: "async", multiple: true });
// 3) Invoke on one device
var { Device } = require("sigma/device");
var d = Device.getMain();
d.testFn(); // calls `test(d)`
// 4) Invoke across many devices in parallel
var all = Device.searchObject(sigmaConst.DevAll);
if (all) all.testFn();
// 5) Remove the injected method if no longer needed
delDeviceFunction("testFn", true); // verbose deletion logs
Integration
- Methods are dynamically attached to
Device.prototypeand, whenmultiple: true, also toDeviceArray.prototype. - In async mode, each device call runs in its own Java thread and the wrapper waits until all complete.
- The injected method receives the target device as its first argument automatically.
Notes
- Custom registrations should be placed in Userlib.js; check Script List to locate/edit it.
- API availability:
addDeviceFunction(functionName, function, option)is supported in Total Control 7.0.0+.
- Deletion (
delDeviceFunction) attempts removal fromArray.prototype,Device.prototype,
andDeviceArray.prototype, returningtrueif the process completes.
Functions
(inner) addDeviceFunction(functionName, function, optionopt)
Customize the device object method to dynamically set the method for Device.
Supports:
addDeviceFunction(functionName, function)
addDeviceFunction(functionName, function, option)
note:
The custom device object method can only be written in the Userlib.js file of Total Control, and the Userlib.js file can be viewed in the "Script List".
The API "addDeviceFunction(functionName, function, option)" is available in Total Control 7.0.0 and later.
Example
// Example: Add a custom method `testFn` to each device that prints a message.
// Define the custom function to be executed
function test() {
print("this is test");
}
// Register the function as a method called `testFn` on device objects
addDeviceFunction('testFn', test, { mode: 'async', multiple: true });
// Get all available devices
var devices = Device.searchObject(sigmaConst.DevAll);
if (devices != null) {
// Call the newly added method on all found devices
devices.testFn();
} else {
print("Failed to get the master device object");
}
// Expected output (if two devices are connected):
// this is test
// this is test
Parameters:
| string | functionName |
The alias name to be added to the device object, which will act as the method name. |
|||||||||||
| function | function |
The actual function logic to be executed. This function must be defined by the user. |
|||||||||||
| Object | option |
<optional> |
Optional configuration object. Properties
|
(inner) delDeviceFunction(name, verboseopt) → {boolean}
Deletes a method or property with the specified name from the prototypes of Array, Device, and DeviceArray.
Typically used to remove custom methods added dynamically, such as via addDeviceFunction.
Example
// Define a custom method `click2` on the Device prototype
function myclick(d, x, y) {
d.click(x * 2, y * 2);
}
Device.prototype.click2 = myclick;
// Use the custom method
var device = Device.getMain();
device.click2(device, 100, 100);
// Delete the custom method using delDeviceFunction
var ret = delDeviceFunction('click2');
if (ret === true) {
print("Method successfully deleted");
} else {
print("Failed");
}
// Running result (if successful):
// Method successfully deleted
Parameters:
| string | name |
The name of the method or property to delete. |
||
| boolean | verbose |
<optional> |
false | If true, logs each deletion action to the console for debugging purposes. |
Returns:
| boolean |
Returns |