2.12 JavaScript Script Editing
Function Overview
The IoT gateway supports JavaScript script editing, allowing users to implement flexible logic control and data processing through custom scripts. Users can write scripts based on business requirements and configure scripts to run at startup or on a schedule, meeting customization needs in complex scenarios. The built-in JavaScript editor provides a rich set of functions to help users develop scripts quickly.Function Configuration
The procedure is as follows:- Click the Add button.
- in add a unique JS name in the popup edit box, there are three execution modes: run at startup, loop and scheduled.
- run at startup: programstartwhenrun JS script;
- loop: according to Configuration execution interval(ms)loopJS script; (removed, please use Run at Startup with while(true))
- scheduled: execute the JS script at the specified daily time(when: minute: seconds)JS script;
- Clickselectneed to run function;
- Click the "Add" button, in popupinselectoneneed to reador Settings point;
- Click the "Insert" button, create functionaddjsedit box cursorafter;
- can3,4,5 stepsperformmultiplefunction add;
- Click the "OK" buttoncompleteJS script edit.

Function Description
JS script editorbuilt-in Function Descriptionas follows:read/write functions
ReadBit Function reads a Boolean value from the specified Tag point, the input parameter is the Tag point name string, if the point does not exist or the quality stamp is not Good, returns undefined.
Example: var tagValueBit = ReadBit('Device1.tag0001');ReadStatus Function reads the status from the specified Tag point, the input parameter is the Tag point name string, the return value is Boolean, True indicates acquisition succeeded, False indicates acquisition failed, if the point does not exist or the quality stamp is not Good, returns undefined.
Example: var tagStatus = ReadStatus('Device1.tag0001');ReadInt Function reads an integer from the specified Tag point, the input parameter is the Tag point name string, if the point does not exist or the quality stamp is not Good, returns undefined.
Example: var tagValueInt = ReadInt('Device1.tag0001');ReadFloat Function reads a floating-point number from the specified Tag point, if the point does not exist or the quality stamp is not Good, returns undefined.
Example: var tagValueFloat = ReadFloat('Device1.tag0001');WriteBit Function writes a Boolean value to the specified Tag point, the parameters in sequence are: Tag point Name(type: string), need to write Tag point value(type: Boolean), with no return value.
Example: var value = true;WriteBit('Device1.tag0001', value);
WriteInt Function writes an integer to the specified Tag point, the parameters in sequence are: Tag point Name(type: string), need to write Tag point value(type: integer), with no return value.
Example: var value = 10;WriteInt('Device1.tag0001', value);
WriteFloat Function writes a floating-point value to the specified Tag point, the parameters in sequence are: Tag point Name(type: string), need to write Tag point value(Typeisfloating-point), with no return value.
Example: var value = 1.23;WriteFloat('Device1.tag0001', value);
WriteString Function tospecified Tag pointwritestring, the parameters in sequence are: Tag point Name(type: string), need to write Tag point value(type: string), with no return value.
Example: var value = "String";WriteString('Device1.tag0001', value);
communication functions
SendMsgToPort Function tospecifiedserial portsendmessage, andreturnreceive message, the parameters in sequence are: serial port number, baud rate, data bits, stop bits, parity( 0:N, 1:O, 2:E), timeout(unitmilliseconds: 1000 milliseconds=1 seconds), send message, waitreceive number of bytes. returnvalueisstringtype.
Example:while(true){ // 1endless loop
var A = [0x17, 0x03, 0x00, 0x00, 0x00, 0x04, 0x46, 0xff]; // messagecontent
var rs = SendMsgToPort('com1',9600,8,1,0,2000,A,0); // toserial portsendmessage
// console.log(rs); //printreturnvalue
if(rs){ // determine whetherreturnvalueisis
var str=rs.slice(7,8); // according tomessage Descriptionextract
// console.log(str); //printstr content
reint = parseInt(str,16); // convert/writestringconvert toint 16
WriteFloat('user.tag0001',reint); // convert/writevaluewritespecified user points
}
Sleep(1000); // wait 1 secondsafteragainloop
}
SendMsgToTCP Function tospecified Ethernet portsendspecifiedmessage, andreturnreceive message. the parameters in sequence are: IP address, Port, timeout(unitmilliseconds: 1000 milliseconds=1 seconds), send message. returnvalueisstringtype.
Example:while(true){ // 1endless loop
var A = [0x17, 0x03, 0x00, 0x00, 0x00, 0x04, 0x46, 0xff]; // messagecontent
var rs = SendMsgToTCP('192.168.1.88',81,2000,A); // to Ethernet portsendmessage
// console.log(rs); //printreturnvalue
if(rs){ // determine whetherreturnvalueisis
var str = rs.slice(7,8); // according tomessage Descriptionextract
// console.log(str); //printstr content
reint = parseInt(str,16); // convert/writestringconvert toint 16
WriteFloat('user.tag0001',reint); // convert/writevaluewritespecified user points
}
Sleep(1000); // wait 1 secondsafteragainloop
}
system functions
Sleep Function delayed execution, input Parametervalueismilliseconds, with no return value.
Example:Sleep(1000);// delay and wait 1000 m S
Usage Example
run at startupExamplewhile (true) {
var status;
status = ReadStatus('system.TIME_SECOND'); // ReadStatus: readdata pointquality stamp
console.log('system.TIME_SECOND status is ', status);
if(status){ // ifthe point exists and the quality stamp is Good
var curVal;
curVal = ReadInt('system.TIME_SECOND'); // Read Int: fromspecifieddata pointreadinteger
console.log('system.TIME_SECOND value is ', status);
WriteInt('user.test',curVal); // Write Int: tospecifieddata pointwriteinteger
}
Sleep(1000); // sleep for 1 second
}
var status;
status = ReadStatus('system.TIME_SECOND'); // ReadStatus: readdata pointquality stamp
console.log('system.TIME_SECOND status is ', status);
if(status){
var curVal;
curVal = ReadInt('system.TIME_SECOND'); // Read Int: fromspecifieddata pointreadinteger
console.log('system.TIME_SECOND value is ', status);
WriteInt('user.test',curVal); // Write Int: tospecifieddata pointwriteinteger
}
write JS scriptimplementgatewayscheduledrestart
Sleep(1000); // when 1000 millisecondsstartloop
var i = 0;
while(true){
if(i==72) {
Reboot();
}
i++;
Sleep(1000*60*60); // loopwhen 1 when
}