Unit 4: Controlling the Device State


After the battery device is connected to EnOS Cloud, you can send commands to control the state of the device. In this tutorial, use the start_charging service defined in the battery model to control the charging state of the battery device. Refer to the steps below.

  1. Declare the following main functions for simulating the device data.

    public static void main(String[] args) throws Exception {
        initWithCallback();
        handleServiceInvocation();
    }
    


  2. Use the handleServiceInvocation function to process the service invocation.

    public static void handleServiceInvocation() {
            IMessageHandler<ServiceInvocationCommand, ServiceInvocationReply> handler = new IMessageHandler<ServiceInvocationCommand, ServiceInvocationReply>() {
                public ServiceInvocationReply onMessage(ServiceInvocationCommand request, List<String> argList) throws Exception {
                    System.out.println("rcvn async service invocation command" + request + " topic " + argList);
                    return (ServiceInvocationReply)ServiceInvocationReply.builder().addOutputData("point1", 11).build();
                }
            };
            client.setArrivedMsgHandler(ServiceInvocationCommand.class, handler);
        }
    


  3. From the EnOS Console Management menu, click Device Management > Products. Click the Edit icon for the Battery_Product and navigate to the Debugging tab in the Product Details page.

    ../../_images/debugging_device.png


  4. Select start_charging from the Debugging field, input a value for the result parameter, and click Run. The instruction will be sent to the device.

    ../../_images/service_instruction.png


  5. The running program will receive the service invocation message. An example of the message is as per the below.

    rcvn async service invocation commandAnswerableMessageBody{id='2267850613537742848', method='thing.service.start_charging', version='1.0', params={result=1}} topic [OjupXK76, keyofbattery1, start_charging]