Unit 4: Simulating the Measurement Point Data and Results¶
After the temperature sensor and air-con device are connected to EnOS Cloud, you can simulate the temperature data of the temperature sensor using EnOS Device SDK, upload the simulated data to EnOS Cloud, and send a command to the air-con device to adjust the temperature. Refer to the steps below.
Import the following.
import com.envisioniot.enos.iot_mqtt_sdk.core.exception.EnvisionException; import com.envisioniot.enos.iot_mqtt_sdk.core.msg.IMessageHandler; import com.envisioniot.enos.iot_mqtt_sdk.message.downstream.tsl.ServiceInvocationCommand; import com.envisioniot.enos.iot_mqtt_sdk.message.downstream.tsl.ServiceInvocationReply; import com.envisioniot.enos.iot_mqtt_sdk.message.upstream.tsl.MeasurepointPostRequest; import com.envisioniot.enos.iot_mqtt_sdk.message.upstream.tsl.MeasurepointPostResponse; import com.google.common.collect.Maps; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit;
Set the handlers for the temperature sensor and air-con device, and post the measurement point to EnOS.
public static void main(String[] args) throws Exception { initWithCallback(); // Set handler to handle measurepoint set commands airConClient.setArrivedMsgHandler(ServiceInvocationCommand.class, new SvcHandler()); // Set handler to handle measurepoint set commands sensorClient.setArrivedMsgHandler(ServiceInvocationCommand.class, new SvcHandler()); // Post measurement point int loop = 100; while ((--loop) >= 0) { postMeasurepoint(); TimeUnit.SECONDS.sleep(3L); } }
Use the
postMeasurepoint
function to post the measurement point from the temperature sensor device. The measurement point will be saved to EnOS via the Checkpoint node which routes the message to the EnOS_default rule, and when the Script Switch node in the routing rule detects the message with atemperature
parameter of more than 30, it will route the message to the Send Command node.static void postMeasurepoint() throws EnvisionException { // device measurement points are defined in the model, which can be viewed in Model Details page in EnOS Console MeasurepointPostRequest request = MeasurepointPostRequest.builder() .addMeasurePoint("temperature", 37) .build(); MeasurepointPostResponse response = sensorClient.publish(request); System.out.println(response); }
Use the
SvcHandler
function to handle measurement points and set commands. When the Send Command node from the routing rule sends a message containing actrlTemperature
parameter, the air-con device will respond with a temperature of 22.static class SvcHandler implements IMessageHandler<ServiceInvocationCommand, ServiceInvocationReply> { @Override public ServiceInvocationReply onMessage(ServiceInvocationCommand arrivedMessage, List<String> argList) throws Exception { System.out.println("svc msg: " + arrivedMessage); if (arrivedMessage.<Map<String, String>>getParams().containsKey("ctrlTemperature")) { Map<String, Object> datas = Maps.newHashMap(); datas.put("newTemperature", 22); return ServiceInvocationReply.builder() .setCode(200) .setOutputDatas(datas) .build(); } else { return ServiceInvocationReply.builder() .setCode(406) .setMessage("unknown sev") .build(); } } }
Results¶
The program will simulate the sensor temperature data according to the what is specified in step 3 above and upload the data to EnOS Cloud. Open the Device Details page of the Temperature Sensor device, click Measurement Points, and view the simulated temperature data of the sensor.
Since the simulated temperature is above 30, a command will be sent to the air-con device, and it will in turn respond with the temperature in step 4 above. Open the Device Details page of the Air Con Device device, click Commands, and view the command and response of the air-con device.