设备标签

一.添加/修改/删除(全量修改)标签

XIotMqttClient新增标签上传方法

    /**
     * XIotMqttClient
     * 上传标签信息的事件
     * @param eventMsg 要发布的消息载体
     * @param tags     标签键值对(标签名:标签值) 全量修改 
     */
    public String publishTagEvent(XIotMessage eventMsg, HashMap<String, String> tags)

代码示例

    // 标签键值对HashMap
    HashMap<String, String> tags = new HashMap<String, String>();
    //添加3个标签
    for (int i = 0; i < 3; i++) {
        tags.put("标签名"+i,"标签值"+i);
    }
    XIotMqttClient.getInstance().publishTagEvent(new XIotMessage(),tags);

二.接收标签更新信息

XIotTopicCallback接口新增onDeviceNotice

    /**
     * //得到下发标签上传结果
     * @param message   消息载体
     */
    void onDeviceNotice(XIotMessage message);

代码示例

XIotMqttClient.getInstance().registerMsgListener(new TestTopicListener());

public class TestTopicListener implements XIotTopicCallback {

..........................

    @Override
    public void onDeviceNotice(XIotMessage message) {
       final String TAG = "[onDeviceNotice] ";
       Log.d(TAG, message.getStringPayload());
        XiotPayload payload = new XiotPayload();
        try {
            payload.comparseFormJson(new JSONObject(message.getStringPayload()));
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }
        XiotPayload.Data data = payload.getData();

        JSONObject json = null;
        try {
            json = new JSONObject(payload.getData().getParams());
        } catch (JSONException e) {
            e.printStackTrace();
            return;
        }

        HashParam param = HashParam.comparseFromJson(json);

        //获取标签信息
        String tagsStr = param.getString(HashParam.TAGS);
        Log.d(TAG, "tags: " + tagsStr);  //标签 [{\"key\":\"xx\",\"value\":\"xxx\"}]

        //解析下发标签字符串
        HashMap<String, String> tags = XIoTUtil.parseTags(tagsStr);

        if (tags != null) {
            if (tags.size() > 0) {     //修改或者添加了标签
                for (Map.Entry<String, String> entry : tags.entrySet()) {
                    Log.d(TAG, "tags:" + entry.getKey() + "," + entry.getValue());
                }
                //TODO  把获取到的标签存储到本地

            } else {  //标签被删除了
                Log.d(TAG, "tags: 标签被删除了");
                //TODO  清空本地标签

            }
        }
    }
}

results matching ""

    No results matching ""