遠程配置可以用于更新設備的配置信息,包括設備的系統參數、網絡參數或者本地策略等等。
主動獲取配置
說明 使用該功能前,需在云端開啟產品的遠程配置功能,相關接口參見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) + "]");
}
});