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.


  1. 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;
    


  2. 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);
         }
    }
    


  3. 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 a temperature 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);
    }
    


  4. Use the SvcHandler function to handle measurement points and set commands. When the Send Command node from the routing rule sends a message containing a ctrlTemperature 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

  1. 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.


    ../../_images/s1_view_data.png


  2. 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.


    ../../_images/s1_view_command.png