This module provides scheduled and immediate task execution functionality within the Total Control environment.
It enables users to create, manage, and control script-based tasks that can run once or repeatedly based on flexible time schedules.
Key Features:
- Create script tasks with absolute or relative paths.
- Support for one-time execution, scheduled execution (date/time), and weekly recurring cycles.
- Configure execution parameters such as iteration count, start time, target days, and devices.
- Pause, resume, stop, or delete tasks at runtime.
- Query task details and list all active or completed tasks.
Task Creation Options:
taskCreate(name, scriptPath)taskCreate(name, scriptPath, iteration)taskCreate(name, scriptPath, options)taskCreate(name, scriptPath, iteration, time)taskCreate(name, scriptPath, iteration, time, repeat)
Script Path Resolution Order:
- Absolute path (e.g.,
E:/scripts/test.js) - Predefined variable
scriptPath - System environment variable
TCSCRIPTPATH - Default internal path
/Scripts
Basic Usage:
var { taskCreate, taskPause, taskResume, taskDelete } = require("sigma/task");
// Create a scheduled task every Wednesday and Friday at 14:30
taskCreate("task001", "script.js", {
iteration: 1,
time: "14:30",
repeat: [3, 5]
});
// Pause and resume it later
taskPause("task001");
taskResume("task001");
Task Status Codes:
-1: Running-2: Normal stop-3: Abnormal stop-4: Paused-5: Terminated by user-6: Waiting for scheduled trigger
This module is suitable for scheduled automation, repetitive testing, and background task control in Total Control scripts.
Functions
(inner) taskCreate(name, scriptPath, iteration or optionsopt, timeopt, repeatopt) → {boolean|null}
Create a task that is executed immediately or periodically. when creating a task, provide a cycle time Monday to Sunday (7 days), the user can set the time to execute the script cyclically, such as every Wednesday at 5 pm or every weekend at 7 am, if Total Control is active and the script will be executed repeatedly at the set time. If Total Control is off, the script will not be executed.
Supports:
taskCreate(name, scriptFile)
taskCreate(name, scriptFile, iteration)
taskCreate(name, scriptFile, options)
taskCreate(name, scriptFile, iteration, time)
taskCreate(name, scriptFile, iteration, time, repeat)
Description of script path:
Absolute path: specify the absolute path of scriptFile,which is the full path to the file.
Relative path: There are two kinds of relative paths, one is the user predefined path, and the other is the default path of the Total Control script.
- User predefined path: If the script has the following definition: Define('scriptPath', 'c:\...'); It will look for the script file under the path specified by scriptPath and then execute the script.
- Environment constiable path: If the user does not define scriptPath in the script, it will be searched by the environment constiable TCSCRIPTPATH. If there is a corresponding JS or SCP file in the path, if it is, execute it.
- The default path of the Total Control script ("/Scripts"): If the scriptPath is not defined in the user's script and the corresponding file is not found in the path specified by the environment constiable TCSCRIPTPATH, it will be searched under the default path of the Total Control script to see if there is a corresponding JS or SCP file. If yes, execute it, if not, exit execution;
Note: Execute the script to find the path of the script in the following order: If it is an absolute path, execute the specified script and report an error if it does not exist. If it is a relative path, first find the script file under the user's predefined path, then find the script file under the path of the Total Control environment constiable, and finally find the script under the default path of the Total Control script. If the script is still not found in the default path of the Total Control script. , return an error message.
Example
Create a script task, supports absolute and relative script paths.
//------------------------------------------
// Example: Create a task with an absolute path
//------------------------------------------
// Example script content of E:/File/test.js:
// Get the current master device object
var device = Device.getMain();
if (device != null) {
// Execute this JS API
var ret = device.click(123, 254, tcConst.STATE_DOWN);
if (ret == 0) {
print("Congratulations, this API executes successfully.\nReturn value: " + ret);
} else {
print("Sorry! " + lastError() + "\nReturn value: " + ret);
}
} else {
print("Failed to get the master device object");
}
// Example 1: Create a one-time task executed at a specific datetime.
var { taskCreate } = require("sigma/task");
var ret = taskCreate('task001', 'E:/File/test.js', 1, '2019-11-28 12:12');
if (ret == true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
// Example 2: Create a weekly scheduled task on Wednesday and Friday at 14:30.
var { taskCreate } = require("sigma/task");
var ret = taskCreate('task002', 'E:/File/test.js', 1, '14:30', [3, 5]);
if (ret == true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
// Example 3: Create an immediate task that executes once as soon as possible.
var { taskCreate } = require("sigma/task");
var ret = taskCreate('task003', 'E:/File/test.js');
if (ret == true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
// Example 4: Create an immediate task that runs 2 times
var { taskCreate } = require("sigma/task");
var ret = taskCreate("task004", "E:/File/test.js", { iteration: 2 });
if (ret === true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
// Example 5: Create a one-time scheduled task at specific datetime
var { taskCreate } = require("sigma/task");
var ret = taskCreate("task005", "E:/File/test.js", { iteration: 1, time: "2020-06-30 14:30" });
if (ret === true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
// Example 6: Weekly recurring task on Wednesday and Friday at 2:30 PM
var { taskCreate } = require("sigma/task");
var ret = taskCreate("task006", "E:/File/test.js", { iteration: 1, time: "14:30", repeat: [3, 5] });
if (ret === true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
// Example 7: Recurring task with device filter
var { taskCreate } = require("sigma/task");
var ret = taskCreate("task007", "E:/File/test.js", {
iteration: 1,
time: "14:30",
repeat: [3, 5],
deviceName: "PIONEER K88L"
});
if (ret === true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
//------------------------------------------
// Example: Create a task with a relative path
//------------------------------------------
//Example 1: Define a relative base path using `define()` and create a task.
var { taskCreate } = require("sigma/task");
define('scriptPath', 'E:/File');
var ret = taskCreate('task008', 'test.js');
if (ret == true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
//Example 2: Use the system environment variable `TCSCRIPTPATH` to resolve the script path. Add TCSCRIPTPATH to the system environment variable: E:/File .
var { taskCreate } = require("sigma/task");
var ret = taskCreate('task009', 'test.js', 2);
if (ret == true) {
print("create task succeed");
} else {
print("create task failed: " + lastError());
}
//------------------------------------------
// Operation Result
//------------------------------------------
// If the task is created successfully, output:
create task succeed
// If it fails, output:
create task failed: <error message>
Parameters:
| string | name |
Task name. note: cannot be duplicated with the task name in the script executor. Otherwise, the task creation will fail. |
|
| string | scriptPath |
Script file, can be a relative path or an absolute path. |
|
| number | Object | iteration or options |
<optional> |
Optional parameter. An integer number, number of execution tasks, must be greater than or equal to 1; options: Json object, optional parameters, for example: taskCreate('test', 'test.js', {iteration: 2, time: '02:00', repeat: [1, 3, 5]}), including keywords "iteration", "time", "repeat", "deviceName", "deviceNames". |
| number | options.iteration |
<optional> |
Optional parameter, Int. Number of execution tasks, must be greater than or equal to 1. |
| string | options.time |
<optional> |
Optional parameter, specifies the task execution time, for example: '2018-09-28 12:12'. Note: When there is a parameter "repeat", the time parameter can only contain hours and minutes, for example: '02:00'. |
| Array.<number> | options.repeat |
<optional> |
Optional parameter, task execution period, with a value of "0-6", 0 means Sunday, 1 means Monday, 2 means Tuesday, ..., 6 means Saturday. For example: repeat to execute the script every Monday and Wednesday, and the value is: [1, 3]. |
| string | options.deviceName |
<optional> |
Optional parameter, Device name. parameter value, for example "VIVO VIVO X9I". |
| Array.<string> | options.deviceNames |
<optional> |
Optional parameter, Device name. parameter value, for example ["VIVO VIVO X9I", "XIAOMI-MI 8UD"]. |
| string | time |
<optional> |
Optional parameter, specifies the task execution time, for example: '2025-05-01 12:12'. Note: When there is a parameter "repeat", the time parameter can only contain hours and minutes, for example: ‘02:00’. |
| Array | repeat |
<optional> |
Optional parameter, task execution period, with a value of "0-6", 0 means Sunday, 1 means Monday, 2 means Tuesday, ..., 6 means Saturday. For example: repeat to execute the script every Monday and Wednesday, and the value is: [1, 3]. |
Returns:
| boolean | null |
Returns true if successful, false if it fails, and the specific error message is obtained by the lastError() function. |
| String | name |
The name of the task. |
Returns:
| boolean | null |
Returns true if successful, false if it fails, and the specific error message is obtained by the lastError() function. |
| String | name |
The name of the task. |
Returns:
| Object | null |
If successful, the Task object is returned. If it fails, it returns null, and the specific error message is obtained by the lastError() function. The returned task object includes:
|
| Array.<Object> | null |
If successful, return to the task list, the list contains Task object. If it fails, it returns null, and the specific error message is obtained by the lastError() function. Each task object includes the following fields:
|
| String | name |
The name of the task |
Returns:
| boolean | null |
Returns true if successful, false if it fails, and the specific error message is obtained by the lastError() function. |
| String | name |
The name of the task. |
Returns:
| boolean | null |
Returns true if successful, false if it fails, and the specific error message is obtained by the lastError() function. |
| String | name |
The name of the task |
||||||||||||
| Object | options |
Optional parameters in taskCreate(), name and scriptfile are not optional.The optional parameters are "iteration": number of task executions, "time": task execution time, "repeat": task execution cycle. Properties
|
Returns:
| boolean | null |
Returns true if successful, false if it fails, and the specific error message is obtained by the lastError() function. |
| String | name |
The name of the task. |
Returns:
| boolean | null |
Returns true if successful, false if it fails, and the specific error message is obtained by the lastError() function. |