网关与子设备功能

一.监听下发消息

XIotMqttClient新增addSubDevicesListener与removeSubDevicesListener

/**
 *  添加监听网关与子设备功能下发消息
 * @param callback
 */
void addSubDevicesListener(XIotSubDevicesCallback callback)

/**
 *  移除下发消息监听
 * @param callback
 */
void removeSubDevicesListener(XIotSubDevicesCallback callback)

代码示例

1.实现XIotSubDevicesCallbacki接口

private XIotSubDevicesCallback subDeviceListener = new XIotSubDevicesCallback() {

     /**
      * 网关与子设备的拓扑关系
      * @param message   消息载体
      * @param data  具体操作协议内容
      * @param subDevices    子设备 List
      */
     @Override
     public void onSubDeviceTopo(XIotMessage message, XiotPayload.Data data, List<SubDeviceBean> subDevices) {
         XiotLog.d("[onSubDeviceTopo] ", message.getStringPayload());
         runOnUiThread(new Runnable() {
             @Override
             public void run() {
                 txt_topo_relation.setText("");
                 if (subDevices != null && subDevices.size() > 0) {
                     for (int i = 0; i < subDevices.size(); i++) {
                         SubDeviceBean bean = subDevices.get(i);
                         String msg = "subDevices"+ i+":"+ bean.getdName();
                         XiotLog.d("[onSubDeviceTopo] ", msg);
                         txt_topo_relation.append(msg);
                         txt_topo_relation.append("\n");
                     }
                 }
             }
         });
     }

     /**
      * 子设备注册下发消息
      * @param message   消息载体
      * @param data  具体操作协议内容
      * @param subDevices    子设备 List
      */
     @Override
     public void onSubDeviceRgister(XIotMessage message, XiotPayload.Data data, List<SubDeviceBean> subDevices) {
         XiotLog.d("[onSubDeviceRgister] ", message.getStringPayload());
         if (subDevices != null && subDevices.size() > 0) {
             for (int i = 0; i < subDevices.size(); i++) {
                 SubDeviceBean bean = subDevices.get(i);
                 XiotLog.d("[onSubDeviceRgister] ", "subDevices"+ i+":" + bean.getpKey() + "," + bean.getdName());
             }
         }
     }

     /**
      * 子设备上线下发消息
      * @param message   消息载体
      * @param data  具体操作协议内容
      * @param pKey   子设备productKey
      * @param dName 子设备deviceName
      */
     @Override
     public void onSubDeviceOnline(XIotMessage message, XiotPayload.Data data,String pKey,String dName) {
         XiotLog.d("[onSubDeviceOnline] ", message.getStringPayload());
         if (data.getRet().equals(XiotPayload.OK)){
             XiotLog.d("[onSubDeviceOnline] ", "pKey:"+pKey+",dName:"+dName);
         }
     }

     /**
      * 子设备离线下发消息
      * @param message   消息载体
      * @param data  具体操作协议内容
      * @param pKey   子设备productKey
      * @param dName 子设备deviceName
      */
     @Override
     public void onSubDeviceOffline(XIotMessage message, XiotPayload.Data data,String pKey,String dName) {
         XiotLog.d("[onSubDeviceOffline] ", message.getStringPayload());
         if (data.getRet().equals(XiotPayload.OK)){
             XiotLog.d("[onSubDeviceOffline] ", "pKey:"+pKey+",dName:"+dName);
         }
     }

     /**
      * 子设备删除下发消息
      * @param message   消息载体
      * @param data  具体操作协议内容
      * @param pKey   子设备productKey
      * @param dName 子设备deviceName
      */
     @Override
     public void onSubDeviceDelete(XIotMessage message, XiotPayload.Data data,String pKey,String dName) {
         XiotLog.d("[onSubDeviceDelete] ", message.getStringPayload());
         if (data.getRet().equals(XiotPayload.OK)){
             XiotLog.d("[onSubDeviceDelete] ", "pKey:"+pKey+",dName:"+dName);
         }
     }
 };

2.注册与反注册下发消息监听

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    XIotMqttClient.getInstance().addSubDevicesListener(subDeviceListener);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    if (subDeviceListener != null) {
        XIotMqttClient.getInstance().removeSubDevicesListener(subDeviceListener);
    }
}

二.获取网关与子设备的拓扑关系

/**
 *   XIotMqttClient
 *  获取网关与子设备的拓扑关系
 * @param eventMsg  要发布的消息载体
 */
public void subDeviceGetTopo(XIotMessage eventMsg)

代码示例

XIotMqttClient.getInstance().subDeviceGetTopo(new XIotMessage());

三.子设备注册协议

/**
 *   XIotMqttClient
 *  子设备注册协议
 * @param eventMsg  要发布的消息载体
 * @param subDevices      子设备List
 */
public void subDeviceRegister(XIotMessage eventMsg,List<SubDeviceBean> subDevices)

代码示例

List<SubDeviceBean> subDevices = new ArrayList<>();
subDevices.add(new SubDeviceBean("子设备1productKey","子设备1deviceName"));
subDevices.add(new SubDeviceBean("子设备2productKey","子设备2deviceName"));
try {
    XIotMqttClient.getInstance().subDeviceRegister(new XIotMessage(), subDevices);
} catch (XIotException | JSONException e) {
    e.printStackTrace();
}

四.子设备上线

/**
 * 子设备上线消息
 * @param eventMsg  发布的消息载体
 * @param pKey  子设备productKey
 * @param dName 子设备deviceName
 * @param clientId  子设备clientId
 * @param password  子设备password
 */
public void subDeviceOnline(XIotMessage eventMsg,String pKey,String dName,String clientId,String password)

子设备的clientId与passWord在子设备中使用如下方法获取

String clientId = XIotMqttClient.getInstance().getClientId();
String passWord = XIotMqttClient.getInstance().getPassWord();

代码示例

XIotMqttClient.getInstance().subDeviceOnline(new XIotMessage(),"pKey","dName", "clientId", "password");

五.子设备离线

/**
 *  XIotMqttClient
 * 子设备离线消息
 * @param eventMsg  发布的消息载体
 * @param pKey   子设备ProductKey
 * @param dName  子设备DeviceName
 */
 public void subDeviceOffline(XIotMessage eventMsg,String pKey,String dName)

代码示例

XIotMqttClient.getInstance().subDeviceOffline(new XIotMessage(), "子设备ProductKey", "子设备DeviceName");

六.删除子设备

/**
 *  XIotMqttClient
 * 删除子设备
 * @param eventMsg  发布的消息载体
 * @param pKey  子设备ProductKey
 * @param dName 子设备DeviceName
 */
void subDeviceDelete(XIotMessage eventMsg,String pKey,String dName)

代码示例

XIotMqttClient.getInstance().subDeviceDelete(new XIotMessage(), "子设备ProductKey", "子设备DeviceName");

results matching ""

    No results matching ""