Develop a temperature data service
These services get temperature data from devices, calculate the highest hourly temperature, and filter the highest hourly temperatures within a specified time range.
Prerequisites
You have completed the following operations:
You can develop devices in different ways. You can choose the method that best suits your scenario. This tutorial uses a virtual device as an example.
Procedure
-
In the Development Tools section, click Business Logic.
-
On the Business Logic Development page, click the Blank Template card.
-
Create a device-triggered service.
This service uses a temperature monitor device as the trigger node. It receives temperature data reported by the device and stores it in an ApsaraDB RDS for MySQL data table.
-
Enter the basic information for the business logic service and click Confirm.
Parameter
Description
Service Name
A unique identifier for the service within the project. For example: StoreTemperatureData
The name can contain Chinese characters, letters, digits, underscores (_), hyphens (-), and parentheses (()). It must start with a Chinese character, a letter, or a digit, and be up to 30 characters long. Each Chinese character counts as one character.
Project
The project to which the service belongs. In this tutorial, select the project you created, for example, TeaGardenMonitoring.
Description
A description of the service's purpose, up to 100 characters long. Each Chinese character counts as one character.
-
From the Nodes tab on the left side of the service editor, drag a Device Trigger node onto the canvas. Configure it to use the properties reported by the temperature monitor as input data.
In the configuration pane on the right, for Trigger Condition, select Properties: All Properties.
-
Configure an ApsaraDB RDS for MySQL node to store the temperature data from the temperature monitor in the
sensor_datatable. For detailed instructions on node configuration, see Node configuration.Set Operation Type to Insert and Port Number to 3306. Then, enter the username, password, connection endpoint, and database name for your database.
The following example shows the input parameters.
{ "table": "sensor_data", "rows": [ { "id":"{{payload.deviceContext.gmtCreate}}", "deviceName" : "{{payload.deviceContext.deviceName}}", "dateTime" : "{{payload.props.temperature.time}}", "temperature" : "{{payload.props.temperature.value}}" } ] } -
Click the Deploy and Debug button
to deploy and start the service.NoteIn this tutorial, the virtual device is already configured to report data, so you do not need to click Deploy and Debug again. For more information, see Develop a virtual device.
-
-
Create a periodically triggered service.
This service runs on the hour to calculate the highest temperature from the previous hour and stores the result in an ApsaraDB RDS for MySQL data table.
-
Go to the Service List page and click the add icon
to the right of the Service List. -
On the Business Logic Development page, create and configure the periodically triggered service.
-
Configure a periodic trigger node to run the service on the hour.
Set Trigger Mode to Periodic Trigger. For Period, select Hour. Set Interval (hours) to 1, and select the Permanent checkbox.
-
Configure an ApsaraDB RDS for MySQL node to query data from the
sensor_datatable, which stores the reported temperature data.In the node configuration pane, enter the Username, Password, Connection Endpoint, and Database Name. Set the Port Number to
3306and select Query for the Operation Type.The following example shows the input parameters for the query.
{ "table": "sensor_data", "condition": { "where": {"deviceName": "bZxNh9qi6PyqYvFcDvqK"}, "columns": [ "id", "deviceName", "temperature", "dateTime" ], "orders": ["temperature"] } } -
Configure a Node.js script node to extract the highest temperature reported by the device within each hour.
Set the runtime environment to Node.js (Node v6.10), select the Blank Template, and enter the code in the script editor.
The following example shows the script.
/** * @param {Object} payload The output from the previous node. * @param {Object} node The output from a specified node. * @param {Object} query The output from the first node in the service flow. * @param {Object} context { appKey, appSecret } */ module.exports = async function(payload, node, query, context) { console.log("payload: ", payload); let result = {}; result.index = payload.length - 1; result.maxTemperature = payload[result.index].temperature; result.id = payload[result.index].id; result.deviceName = payload[result.index].deviceName; result.dateTime = payload[result.index].dateTime; return result; } -
Configure another ApsaraDB RDS for MySQL node to insert the data processed by the Node.js script into the
maxtemperaturedata table.In the configuration pane on the right, enter the Username, Password, Connection Endpoint, and Database Name. Set the Port Number to
3306and select Insert for the Operation Type.The following example shows the input parameters.
{ "table": "maxtemperature", "rows": [{ "id": "{{payload.id}}", "deviceName": "{{payload.deviceName}}", "dateTime": "{{payload.dateTime}}", "maxTemperature": "{{payload.maxTemperature}}" }] } -
Click the Deploy and Debug button
to deploy and start the service. -
Click the Deploy and Debug button
, enter a simulated time, and debug the service.
-
-
Create an HTTP API to filter data within a specified time range.
-
Create a new service by following the procedure in substeps 1 and 2 of Step 5. For example, name it GetTemperatureData.
-
Configure an HTTP Request node. Set the Action name and request parameters for the API that filters temperature data.
Set the Action to
SelectData.Parameter
Type
Required
Description
DeviceName
String
No
When you call the API, you can pass a device name to query data for a specific device. If you do not pass a device name, data for all devices is returned.
startTime
Int
Yes
When you call the API, you must specify the start time for the query.
endTime
Int
Yes
When you call the API, you must specify the end time for the query.
-
Configure an ApsaraDB RDS for MySQL node to retrieve the maximum hourly temperature from the
maxtemperaturetable.In the node configuration pane, enter the Username, Password, Connection Endpoint, and Database Name. Set the Port Number to
3306and select Query for the Operation Type.The following example shows the input parameters.
{ "table": "maxtemperature", "condition": { "where": { "deviceName": "{{payload.DeviceName}}" }, "columns": [ "maxTemperature", "dateTime" ], "orders": [ "dateTime" ] } } -
Configure a Node.js script node to filter temperature data within the specified time range.
In the service editor, select a Node.js script node. Select the Blank Template and the Node.js (Node v6.10) runtime environment, then enter the following code in the script editor.
The following example shows the script.
/** * @param {Object} payload The output from the previous node. * @param {Object} node The output from a specified node. * @param {Object} query The output from the first node in the service flow. * @param {Object} context { appKey, appSecret } */ module.exports = async function(payload, node, query, context) { console.log("payload: ", payload); let length = 0; for (let i = 0; i < payload.length; i++) { if(parseInt(payload[i].dateTime) >= query.startTime*1000 && parseInt(payload[i].dateTime) <= query.endTime*1000) { length = length + 1; } } console.log("length", length); var result = new Array(length); for(let i = 0; i < length ; i++){ result[i] = new Array(2); result[i][0] = ""; result[i][1] = 0; } let valid = 0; for(let j = 0 ;j < payload.length ; j++ ){ if(parseInt(payload[j].dateTime) >= query.startTime*1000 && parseInt(payload[j].dateTime) <= query.endTime*1000) { result[valid][0] = payload[j].dateTime; result[valid][1] = payload[j].maxTemperature; valid = valid + 1; } } return result; } -
Configure an HTTP Response node to output the data processed by the Node.js node.
In the Response section, for Output, select the
payloadfrom the previous node (the Node.js script node) as the response body. -
Click the Deploy and Debug button
to deploy and start the service. -
Click the Deploy and Debug button
, enter the request parameters, and debug the service.
-
-
After successfully debugging all three services, click the Publish button
in the upper-right corner of the page to publish the services.
in the upper-right corner of the page to publish the services.