task

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:

  1. Absolute path (e.g., E:/scripts/test.js)
  2. Predefined variable scriptPath
  3. System environment variable TCSCRIPTPATH
  4. 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.

(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.

  1. 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.
  2. 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.
  3. 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.

(inner) taskDelete(name) → {boolean|null}

Delete the task based on the task name.

Example
// Example: Create a task, wait 5 seconds, then delete it.

var { taskCreate, taskDelete } = require("sigma/task");
var taskCreate = taskCreate('task001', 'E:\\\\File\\\\getname.js', 1);
if (ret1 === true) {
  delay(5000);
  // Execute this JS API
  var taskDelete = taskDelete('task001');
  if (taskDelete === true) {
    print("Congratulations, this API executes successfully.\nReturn value: " + taskDelete);
  } else {
    print("Sorry! " + lastError() + "\nReturn value: " + taskDelete);
  }
} else {
  print(lastError());
}


// If successful, the output will be:

Congratulations, this API executes successfully.
Return value: true


// If failed:

Sorry! <error message>
Return value: false
Parameters:
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.

(inner) taskInfo(name) → {Object|null}

Get the details of the task.

Example
// Example 1: Query task "task001" and print its info.

var task = taskInfo('task001');
if (task != null) {
  var taskID = task.taskID;
  var taskName = task.taskName;
  var taskStatus = task.status;
  var taskIterCount = task.iterCount;
  var taskOptions = task.options;
  print("Task info:\n"
    + "taskID: " + taskID + "\n"
    + "taskName: " + taskName + "\n"
    + "taskStatus: " + taskStatus + "\n"
    + "taskIterCount: " + taskIterCount + "\n"
    + "taskOptions: " + JSON.stringify(taskOptions));
} else {
  print("Get task info failed");
}


// If successful:

Task info:
taskID: 1535180604361
taskName: task001
taskStatus: -1
taskIterCount: 1
taskOptions: { deviceNames: ['PIONEER K88L','VIVO VIVO X9I'], schedule: '2020-8-15 16:10:00', iteration: 1 }

// If failed:

Get task info failed
Parameters:
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:

  • taskID {string} - Unique task identifier.
  • taskName {string} - The name of the task.
  • status {number} - Status of the task, -1: running, -2: normal stop, -3: abnormal stop, -4: pause, -5: user termination, -6: scheduled task.
  • iterCount {number} - Current iterations.
  • options {Object} - Original configuration (e.g., schedule, deviceNames, iteration).

(inner) taskList() → {Array.<Object>|null}

Get a list of tasks in the current executor.

Example
// Example: Iterate and print all task information

var { taskList } = require("sigma/task");
var list = taskList();
if (list != null) {
  for (var i = 0; i < list.length; i++) {
    var task = list[i];
    var taskID = task.taskID;
    var taskName = task.taskName;
    var taskStatus = task.status;
    var taskIterCount = task.iterCount;
    var taskOptions = task.options;
    print("Task info:\n"
      + "taskID: " + taskID + "\n"
      + "taskName: " + taskName + "\n"
      + "taskStatus: " + taskStatus + "\n"
      + "taskIterCount: " + taskIterCount + "\n"
      + "taskOptions: " + JSON.stringify(taskOptions));
  }
}


// If it executes successfully, the output may be:

Task info:
taskID: 1591462288231
taskName: taskCreate
taskStatus: -2
taskIterCount: 1
taskOptions: {"deviceNames": ["PIONEER K88L", "VIVO VIVO X9I"], "schedule": "2020-8-15 16:10:00", "iteration": 1}

Task info:
taskID: 1595375593638
taskName: task002
taskStatus: -6
taskIterCount: 1
taskOptions: {"deviceName": [], "schedule": "2020-8-18 13:54:46", "iteration": 1}
Returns:
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:

  • taskID {string} - Unique task identifier.
  • taskName {string} - The name of the task.
  • status {number} - Status of the task, -1: running, -2: normal stop, -3: abnormal stop, -4: pause, -5: user termination, -6: scheduled task.
  • iterCount {number} - Current iterations.
  • options {Object} - Original configuration (e.g., schedule, deviceNames, iteration).

(inner) taskPause(name) → {boolean|null}

Pause the task based on the task name.

Example
// Example: Create a task, wait for 5 seconds, then pause it.

var { taskCreate, taskPause } = require("sigma/task");
var task001 = taskCreate('task001', 'E:/File/test.js', 1);
if (task001 === true) {
  delay(5000);
  var ret = taskPause('task001');
  if (ret === true) {
    print("Congratulations, this API executes successfully.\nReturn value: " + ret);
  } else {
    print("Sorry! " + lastError() + "\nReturn value: " + ret);
  }
} else {
  print(lastError());
}


// If successful, the output will be:

Congratulations, this API executes successfully.
Return value: true


// If failed:

Sorry! <error message>
Return value: false
Parameters:
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.

(inner) taskResume(name) → {boolean|null}

Resume the task based on the task name.

Example
// Example: Create a task, pause it, then resume it.

var { taskCreate, taskPause, taskResume} = require("sigma/task");
var taskCreate = taskCreate('task001', 'E:/File/test.js', 1);
if (taskCreate === true) {
  delay(5000);
  var taskPause = taskPause('task001');
  var taskResume = taskResume('task001');
  if (taskPause === true && taskResume === true) {
    print("Congratulations, this API executes successfully.\nReturn value: " + taskResume);
  } else {
    print("Sorry! " + lastError() + "\nReturn value: " + taskResume);
  }
} else {
  print(lastError());
}


// If successful, the output will be:

Congratulations, this API executes successfully.
Return value: true


// If failed:

Sorry! <error message>
Return value: false
Parameters:
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.

(inner) taskSet(name, options) → {boolean|null}

Modify the task parameters according to the task name, you can modify the task parameters that have been successfully created but not yet run, and the completed tasks cannot be modified. After calling this interface successfully, the corresponding parameters will be modified, and the unmodified task parameters remain the same as before.

Example
var { taskSet } = require("sigma/task");
// Example 1: Modify task "testLogin_1" to run at 10:00.

var ret = taskSet('task006', { time: '10:00' });
if (ret == true) {
  print("taskSet succeed");
} else {
  print("taskSet failed: " + lastError());
}


// Example 2: Modify task "task007" to run 10 times, on Tuesday and Thursday.

var { taskSet } = require("sigma/task");
var ret = taskSet('task007', { iteration: 10, repeat: [2, 4] });
if (ret == true) {
  print("taskSet succeed");
} else {
  print("taskSet failed: " + lastError());
}


// If successful, the output will be:

taskSet succeed

// If failed, it will output:

taskSet failed: <specific error message>
Parameters:
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
number iteration <optional>

number of task executions

string time <optional>

New time to execute (e.g., "10:00").

Array.<number> repeat <optional>

Days of the week to repeat (0=Sunday to 6=Saturday). (e.g., [1,3]).

Returns:
boolean | null

Returns true if successful, false if it fails, and the specific error message is obtained by the lastError() function.

(inner) taskStop(name) → {boolean|null}

Stop the task based on the task name.

Example
// Example: Create a task, wait 5 seconds, then stop it.

var { taskCreate, taskStop} = require("sigma/task");
var taskCreate = taskCreate('task001', 'E:/File/test.js', 1);
if (taskCreate === true) {
  delay(5000);
  var taskStop = taskStop('task001');
  if (taskStop === true) {
    print("Congratulations, this API executes successfully.\nReturn value: " + taskStop);
  } else {
    print("Sorry! " + lastError() + "\nReturn value: " + taskStop);
  }
} else {
  print(lastError());
}


// If successful:

Congratulations, this API executes successfully.
Return value: true


// If failed:

Sorry! <error message>
Return value: false
Parameters:
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.