Use the JavaScript Processor Function
You can use the JavaScript processor function for customizing and manipulating data from input tags using JavaScript programming language. This processor is helpful for scenarios where you need to add, remove, or modify fields in incoming messages.
You can rename fields, modify timestamps, or aggregate data based on your processing needs.
Review the following scenarios where the JavaScript processor function is used to manipulate incoming data for real-world applications. Then, you will simulate PLC data to customize input data in manufacturing settings effectively.
In a manufacturing plant, temperature sensors send data to a monitoring system. The sensor readings include several fields, but you only need specific ones to analyze furnace pressure. Using the JavaScript processor, you can filter out unnecessary fields and rename required fields for clarity.
Code Example:
In an assembly line, multiple devices report operational data, including pressure, speed, and temperature. To calculate the total pressure from all devices, you aggregate the value fields of each input message using the JavaScript processor.
Code Example:
Follow the steps to Connect a Device and configure the following parameters:
- Device Type: Simulator
- Driver Name: Generator
- Enable Alias Topics: Select the checkbox.
After connecting the device, add the following tags. See Add Tags to learn more. Each tag will represent one of the user scenarios noted above.
- Name: Select S - Random value generator.
- Value Type: Select float64.
- Polling Interval: Enter 5.
- Tag Name: Enter Tag1.
- Min Value: Enter 1.
- Max Value: Enter 10.
You can now create the analytics flows using data from the device and tag you previously created. Follow the appropriate step out of 3a or 3b based on the two user scenarios for the JavaScript processor function you want to use.
- Navigate to Analytics.
On the analytics canvas, click Add processor. The Create a processor dialog box displays.
- Select DataHub Subscribe.
In the Topic field, click the Search icon, select the device you previously created, and then select the alias topic for the Tag1 tag.
- Click Save.
- Click Add processor again and select JavaScript processor. The following information defines this function:
- Text Editor: Write the JavaScript code you want to execute. Learn more here. For this example, paste the code from User Scenario 1 above.
- var ids = inputProcessors(); function transformMessage(message) { // Rename 'value' to 'furnacePressure' if (message.hasOwnProperty('value')) { message.furnacePressure = message.value; delete message.value; } // Keep only selected fields return { success: message.success, deviceId: message.deviceId, furnacePressure: message.furnacePressure }; } if (ids.length > 0) { if (values && values[ids[0]]) { result = transformMessage(values[ids[0]]); } }
Click Save.
- Connect the DataHub Subscribe processor (tag: Tag1) to the JavaScript processor with a wire and enter the definition of A for the value-type connection.
- On the analytics canvas, click Save.
The configured analytics flows should look like the following:
- Navigate to Analytics.
On the analytics canvas, click Add processor. The Create a processor dialog box displays.
- Select DataHub Subscribe.
In the Topic field, click the Search icon, select the device you previously created, and then select the alias topic for the Tag1 tag.
- Repeat steps 2-4 in Step 3b to add another DataHub Subscribe node. Select the alias topic for the Tag1 tag again.
- Click Save.
- Click Add processor again and select JavaScript processor. The following information defines this function:
- Text Editor: Write the JavaScript code you want to execute. Learn more here. For this example, paste the code from User Scenario 2 above.
- var ids = inputProcessors(); var totalPressure = 0; if (ids.length > 0) { result["ids"] = ids; // Store input identifiers for (var i = 0; i < ids.length; i++) { var message = values[ids[i]]; if (message && message.value !== undefined) { totalPressure += message.value; // Aggregate 'value' fields } } result["total_pressure"] = totalPressure; // Output the total pressure }
Click Save.
- Connect the DataHub Subscribe processor (tag: Tag1) to the JavaScript processor with a wire and enter the definition of A for the value-type connection.
- Connect another DataHub Subscribe processor (tag: Tag1) to the JavaScript processor with a wire and enter the definition of B for the value-type connection.
- On the analytics canvas, click Save.
The configured analytics flows should look like the following:
Click the View icon in the JavaScript processor to view the output values.
The JavaScript function filters the furnace pressure from the defined tag values.
The JavaScript function calculates the sum from the defined tag values and gives the output Total_pressure.