远程配置可以用于更新设备的配置信息,包括设备的系统参数、网络参数或者本地策略等等。

主动获取配置

说明 使用该功能前,需在云端开启产品的远程配置功能,相关接口参见IDeviceCOTA
RequestModel<Map> requestModel = new RequestModel<Map>();
requestModel.id = "123";
requestModel.method = "thing.config.get";
requestModel.version = "1.0";
Map<String, String> paramsMap = new HashMap<String, String>();
paramsMap.put("configScope", "product");
paramsMap.put("getType", "file");
requestModel.params = paramsMap;

LinkKit.getInstance().getDeviceCOTA().COTAGet(requestModel, new IConnectSendListener() {
    @Override
    public void onResponse(ARequest aRequest, AResponse aResponse) {
        ALog.d(TAG, "onResponse() called with: aRequest = [" + aRequest + "], aResponse = [" + (aResponse == null ? null : aResponse.data) + "]");
    }

    @Override
    public void onFailure(ARequest aRequest, AError aError) {
        ALog.d(TAG, "onFailure() called with: aRequest = [" + aRequest + "], aError = [" + getError(aError) + "]");
    }
});
        

订阅获取

设备端可以通过订阅获取远程配置信息。

LinkKit.getInstance().getDeviceCOTA().setCOTAChangeListener(new IConnectRrpcListener() {
    @Override
    public void onSubscribeSuccess(ARequest aRequest) {
        ALog.d(TAG, "onSubscribeSuccess() called with: aRequest = [" + aRequest + "]");
    }
    
    @Override
    public void onSubscribeFailed(ARequest aRequest, AError aError) {
        ALog.d(TAG, "onSubscribeFailed() called with: aRequest = [" + aRequest + "], aError = [" + getError(aError) + "]");
    }
    
    @Override
    public void onReceived(ARequest aRequest, IConnectRrpcHandle iConnectRrpcHandle) {
        ALog.d(TAG, "onReceived() called with: aRequest = [" + aRequest + "], iConnectRrpcHandle = [" + iConnectRrpcHandle + "]");
        if (aRequest instanceof MqttRrpcRequest) {
            // 云端下行数据 拿到
            String cotaData = new String((byte[]) ((MqttRrpcRequest) aRequest).payloadObj);
            ALog.d(TAG, "received data=" + cotaData);
    //                     ((MqttRrpcRequest) aRequest).payloadObj;
    //                    ResponseModel<Map<String, String>> responseModel = JSONObject.parseObject(((MqttRrpcRequest) aRequest).payloadObj, new TypeReference<ResponseModel<Map<String, String>>>(){}.getType());
    
        }
        // 返回数据示例
            /*{
                "id": "123",
                "version": "1.0",
                "code": 200,
                "data": {
                "configId": "123dagdah",
                    "configSize": 1234565,
                    "sign": "123214adfadgadg",
                    "signMethod": "Sha256",
                    "url": "https://iotx-config.oss-cn-shanghai.aliyuncs.com/nopoll_0.4.4.tar.gz?Expires=1502955804&OSSAccessKeyId=XXXXXXXXXXXXXXXXXXXX&Signature=XfgJu7P6DWWejstKJgXJEH0qAKU%3D&security-token=CAISuQJ1q6Ft5B2yfSjIpK6MGsyN1Jx5jo6mVnfBglIPTvlvt5D50Tz2IHtIf3NpAusdsv03nWxT7v4flqFyTINVAEvYZJOPKGrGR0DzDbDasumZsJbo4f%2FMQBqEaXPS2MvVfJ%2BzLrf0ceusbFbpjzJ6xaCAGxypQ12iN%2B%2Fr6%2F5gdc9FcQSkL0B8ZrFsKxBltdUROFbIKP%2BpKWSKuGfLC1dysQcO1wEP4K%2BkkMqH8Uic3h%2Boy%2BgJt8H2PpHhd9NhXuV2WMzn2%2FdtJOiTknxR7ARasaBqhelc4zqA%2FPPlWgAKvkXba7aIoo01fV4jN5JXQfAU8KLO8tRjofHWmojNzBJAAPpYSSy3Rvr7m5efQrrybY1lLO6iZy%2BVio2VSZDxshI5Z3McKARWct06MWV9ABA2TTXXOi40BOxuq%2B3JGoABXC54TOlo7%2F1wTLTsCUqzzeIiXVOK8CfNOkfTucMGHkeYeCdFkm%2FkADhXAnrnGf5a4FbmKMQph2cKsr8y8UfWLC6IzvJsClXTnbJBMeuWIqo5zIynS1pm7gf%2F9N3hVc6%2BEeIk0xfl2tycsUpbL2FoaGk6BAF8hWSWYUXsv59d5Uk%3D",
                    "getType": "file"
                }
            }*/
    
    }
    
    @Override
    public void onResponseSuccess(ARequest aRequest) {
        ALog.d(TAG, "onResponseSuccess() called with: aRequest = [" + aRequest + "]");
    }
    
    @Override
    public void onResponseFailed(ARequest aRequest, AError aError) {
        ALog.d(TAG, "onResponseFailed() called with: aRequest = [" + aRequest + "], aError = [" + getError(aError) + "]");
    }
});