单元 4:控制设备状态


电池设备连接到 EnOS 云端后,你可以发送命令以使用定义的服务来控制设备状态。本单元使用 battery 模型中定义的 start_charging 服务来控制电池设备的充电状态。详细步骤如下。

  1. 声明以下用于模拟设备数据的主要函数:

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


  2. 使用 handleServiceInvocation 函数处理服务调用。

    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. 从菜单中点击 设备管理 > 产品管理,点击 Battery_Product查看 图标去到 产品详情 页面,再点击 在线调试 选项卡。


    ../../_images/debugging_device.png


  4. 调试设备 选择设备,再从 调试功能 选择 start_charging 服务,为 result 参数输入一个值,然后单击 发送指令


    ../../_images/service_instruction.png


  5. 正在运行的程序将收到服务调用消息,参见以下示例:

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