This topic provides script examples for parsing uplink and downlink property data. You will learn how to write a data parsing script based on a Thing Specification Language (TSL) model for a product that uses the pass-through/custom data format.
Step 1: Edit the script
Create a product in the Link Living platform.
For more information about how to create a product, see Create a product.
NoteData Format to Pass-through/Custom.
Define a TSL model for the product.
For more information about how to define custom properties, see Add a custom feature.
This example defines the following three properties.
Identifier Data type Value range Read/Write type prop_float float -100 to 100 Read/Write prop_int16 int32 -100 to 100 Read/Write prop_bool bool 0: On; 1: Off Read/Write The data parsing script is written based on this TSL model to parse uplink and downlink TSL model data.
- Add a TSL model.
This example defines the following three properties:
Identifier Data type Value range Read/Write type prop_float float -100 to 100 Read/Write prop_int16 int32 -100 to 100 Read/Write prop_bool bool 0: On; 1: Off Read/Write The message parsing script is written based on the TSL model defined here. The script parses data from mobile originated and mobile terminated TSL model messages.
The parameter value lengths for communication in this example are defined as follows:
- Device data reporting request
Field Bytes Frame type 1 byte Request ID 4 bytes Property prop_int16 2 bytes Property prop_bool 1 byte Property prop_float 4 bytes - Device data reporting response
Field Bytes Frame type 1 byte Request ID 4 bytes Result code 1 byte - Set property request
Field Bytes Frame type 1 byte Request ID 4 bytes Property prop_int16 2 bytes Property prop_bool 1 byte Property prop_float 4 bytes - Set property response
Field Bytes Frame type 1 byte Request ID 4 bytes Result code 1 byte
- Device data reporting request
Write the script.
On the Products page, find the product and click View.
On the Product Details page, click the Data Parsing tab.
Select a scripting language. Then, enter the script in the text box under Edit Script.
Two scripting languages are supported: JavaScript (ECMAScript 5) and Python 2.7.
You must define the following two functions in the script to parse uplink and downlink TSL model data.
Supported scripting language Function to transform custom device data format to Alink JSON format (mobile originated communication) Function to transform Alink JSON format to custom device data format (mobile terminated communication) Sample code JavaScript (ECMAScript 5) rawDataToProtocol protocolToRawData JavaScript script sample Python 2.7 raw_data_to_protocol protocol_to_raw_data Python script sample PHP 7.2 rawDataToProtocol protocolToRawData PHP script sample For complete script examples, see JavaScript script examples, Python script examples, and PHP script examples.
Step 2: Test the script online
After you edit the script, in the Input Simulation section, select a Simulation Type and enter simulated data to test the script online.
- Simulate parsing property data reported by a device.
Select Data reported by device as the simulation type, enter the following simulated data, and then click Execute.
Note The following simulated request parameter data is for JavaScript scripts. For more sample content, see JavaScript script sample.For simulated request parameter data for Python and PHP scripts, see Python script sample and PHP script sample.
You can use a string-to-hexadecimal converter to convert the JSON request parameter data to hexadecimal data. For example, if the converted data is
00002233441232013fa00000, enter the following data.0x00002233441232013fa00000The DPI engine transforms the pass-through data into JSON data based on the script rules.
Click Running Result to view the parsing result.
{ "method": "thing.event.property.post", "id": "2241348", "params": { "prop_float": 1.25, "prop_int16": 4658, "prop_bool": 1 }, "version": "1.0" } - Simulate parsing the response data sent by IoT Platform.
Select Device Accepts Data as the impersonation type, enter the following data in JSON format, and then click Execute.
{ "id": "12345", "version": "1.0", "code": 200, "method": "thing.event.property.post", "data": {} }The data parsing engine transforms JSON data into the following:
0x0200003039c8 - Simulate parsing the property setting data sent by IoT Platform.
Select Device Accepts Data as the impersonation type, enter the following data in JSON format, and then click Execute.
{ "method": "thing.service.property.set", "id": "12345", "version": "1.0", "params": { "prop_float": 123.452, "prop_int16": 333, "prop_bool": 1 } }The data parsing engine transforms JSON data into the following:
0x0100003039014d0142f6e76d - Simulate parsing the property setting response data returned by the device.
For Impersonation type, select Device-reported data, enter the following data, and then click Execute.
0x0300223344c8The DPI engine transforms the pass-through data into the following JSON data:
{ "code": "200", "data": {}, "id": "2241348", "version": "1.0" }
Step 3: Submit the script
After you confirm that the script parses data correctly, click Submit. The script is submitted to IoT Platform. IoT Platform calls the script to parse data during mobile originated and mobile terminated communication.
Step 4: Test with a real device
Before you use the script in a production environment, use a real device to communicate with IoT Platform to verify that the script can successfully parse mobile originated and mobile terminated data.
You can use the Link SDK provided by IoT Platform to develop a device and connect it to IoT Platform. For more information, see Link SDK.
- Test reporting property data.
- Use the device to report property data, such as
0x00002233441232013fa00000. - In the IoT Platform console, choose .
- Find the device and click View. On the Device Details page, click the tab and verify that the corresponding property data is displayed.
- Use the device to report property data, such as
- Test sending property data.
- In the IoT Platform console, choose .
- Select the product and device that you want to test. Select Default Module. For Function, select the identifier of the property that you want to test, such as Property (prop_int16). Set Method to Set. Enter the following data and click Send Command.
{ "method": "thing.service.property.set", "id": "12345", "version": "1.0", "params": { "prop_float": 123.452, "prop_int16": 333, "prop_bool": 1 } } - Verify that the device receives the property setting instruction.
- On the Device Details page of the device, click the tab and verify that the device reports the current property data.
References
To view JavaScript (ECMAScript 5) script templates and examples, see JavaScript script examples.
To view Python 2.7 script templates and examples, see Python script examples.
To view PHP 7.2 script templates and examples, see PHP script examples.
For more information about the data parsing process, see Pass-through/Edit Script.、