单元 4:控制设备状态¶
电池设备连接到 EnOS 云端后,你可以发送命令以使用定义的服务来控制设备状态。本单元使用 battery 模型中定义的 start_charging 命令来控制电池设备的充电状态。详细步骤如下。
声明以下用于模拟设备数据的主要函数:
public static void main(String[] args) throws Exception { initWithCallback(); handleServiceInvocation(); }
使用
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); }
从开发者控制台中点击 运行监控 > 在线调试,在筛选器中选择产品 Battery_Product 和设备 battery1。
在 调试真实设备 标签页中,选择 服务调用。在 调试功能 中选择 start_charging,为 result 参数输入一个值,然后点击 发送指令。
正在运行的程序将收到服务调用消息,参见以下示例:
rcvn async service invocation commandAnswerableMessageBody{id='2267850613537742848', method='thing.service.start_charging', version='1.0', params={result=1}} topic [OjupXK76, keyofbattery1, start_charging]