设备管理 IoTDM(联通用户专用)-Agent Lite SDK使用指南(Java)(联通用户专用):命令接收

时间:2023-11-01 16:20:05

命令接收

当开发者希望设备或网关只接收topic为“/gws/deviceid/signaltrans/v2/categories/”的消息,且对消息中的header进行解析时,可以调用“设备命令接收”接口。

应用服务器可以调用 物联网平台 的应用侧API接口给设备或网关下发命令,所以网关得随时监听命令下发的广播,以便接收到命令时进行相应业务处理。

注册观察者对命令接收进行相应处理。

123456
//注册观察者AgentLiteDataTrans agentLiteDataTrans = AgentLiteDataTrans.getInstance();DataTransService dataTransService = DataTransService.getInstance();dataTransService.registerObserver(agentLiteDataTrans);//命令接收agentLiteDataTrans.getCmdReceive();

被动接收命令的方法getCmdReceive

 1 2 3 4 5 6 7 8 91011121314151617
private void getCmdReceive(IotaMessage iotaMsg) {    System.out.println("=========receive iotCMD ============");    String deviceId = iotaMsg.getString(DataTransService.DATATRANS_IE_DEVICEID);    String requestId = iotaMsg.getString(DataTransService.DATATRANS_IE_REQUSTID);    String serviceId = iotaMsg.getString(DataTransService.DATATRANS_IE_SERVICEID);    String method = iotaMsg.getString(DataTransService.DATATRANS_IE_METHOD);    String cmd = iotaMsg.getString(DataTransService.DATATRANS_IE_CMDCONTENT);    if (method.equals("REMOVE_GATEWAY")) {        //rmvGateway(context);    }    System.out.println ("Receive cmd :"                    + "ndeviceId  = " + deviceId                    + "nrequestId = " + requestId                    + "nserviceId = " + serviceId                    + "nmethod    = " + method                    + "ncmd       = " + cmd);}
support.huaweicloud.com/devg-IoT/iot_02_5118.html