OTA升级
更新时间:2024-12-11 19:02:54
APP端触发设备固件升级
@protocol RTCXDeviceMgrProtocol <NSObject>
/**
* 获取设备当前可用的固件信息
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)otaCheckFirmwareUpdateWithReq:(nonnull RTCXFirmwareUpdateCheckReq *)req onSuccess:(nullable RTCXDeviceMgrOnSuccess)onSuccess onError:(nullable RTCXDeviceMgrOnError)onError;
/**
* 确定设备允许进行升级
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)otaFirmwareUpdateWithReq:(nonnull RTCXFirmwareUpdateReq *)req onSuccess:(nullable RTCXDeviceMgrOnSuccess)onSuccess onError:(nullable RTCXDeviceMgrOnError)onError;
/**
* 获取固件升级进度
*
* @param req 请求参数对象
* @param onSuccess 成功回调
* @param onError 失败回调
*/
- (void)otaFirmwareUpdateProgressWithReq:(nonnull RTCXFirmwareUpdateProgressReq *)req onSuccess:(nullable RTCXDeviceMgrOnSuccess)onSuccess onError:(nullable RTCXDeviceMgrOnError)onError;
@end
使用 RTCXDeviceSDK 单例宏对象调用示例如下:
获取设备升级信息
RTCXFirmwareUpdateCheckReq *req = [[RTCXFirmwareUpdateCheckReq alloc] init];
req.iotId = self.curDevice.iotId;
[RTCXDeviceService(RTCXDeviceMgrProtocol) otaCheckFirmwareUpdateWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
RTCXFirmwareUpdateCheckRsp *rsp = data;
if (rsp.firmwareVersion) {
UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"检测到新固件" message:@"当前固件存在新版本,确定要升级到新版本吗?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
RTCXFirmwareUpdateReq *req = [[RTCXFirmwareUpdateReq alloc] init];
req.devices = @[@{@"iotId":self.curDevice.iotId}];
[RTCXDeviceService(RTCXDeviceMgrProtocol) otaFirmwareUpdateWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[self showToast:@"升级中,请勿离开当前页面"];
self.otaUpdating = YES;
} onError:^(NSError * _Nullable error) {
[self showToast:[NSString stringWithFormat:@"升级失败: %@", error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
[alertVC addAction:cancelAction];
[alertVC addAction:confirmAction];
[self presentViewController:alertVC animated:YES completion:nil];
} else {
[self showToast:@"没有新固件"];
}
} onError:^(NSError * _Nullable error) {
[self showToast:[NSString stringWithFormat:@"检测失败: %@", error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];
触发设备升级
RTCXFirmwareUpdateReq *req = [[RTCXFirmwareUpdateReq alloc] init];
req.devices = @[@{@"iotId":self.curDevice.iotId}];
[RTCXDeviceService(RTCXDeviceMgrProtocol) otaFirmwareUpdateWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
[self showToast:@"升级中,请勿离开当前页面"];
self.otaUpdating = YES;
} onError:^(NSError * _Nullable error) {
[self showToast:[NSString stringWithFormat:@"升级失败: %@", error.userInfo[NSLocalizedFailureReasonErrorKey]]];
}];
获取设备升级状 态
float otaUpdateTimeout = 10*60.0;
// 定时器轮循升级状态
self.commonTimer = [NSTimer xt_scheduledTimerWithTimeInterval:1 repeats:YES block:^{
// 实时更新固件升级进度
if (weakSelf.otaUpdating) {// 轮循获取OTA设备升级进度
if (weakSelf.otaUpdateCounter >= otaUpdateTimeout) {
weakSelf.otaUpdating = NO;
weakSelf.otaUpdateCounter = 0;
[weakSelf showToast:@"升级超时"];
return;
}
if (weakSelf.otaUpdateCounter % 2 == 0) {
RTCXFirmwareUpdateProgressReq *req = [[RTCXFirmwareUpdateProgressReq alloc] init];
req.iotId = weakSelf.curDevice.iotId;
[RTCXDeviceService(RTCXDeviceMgrProtocol) otaFirmwareUpdateProgressWithReq:req onSuccess:^(id _Nullable data, id _Nullable rawData) {
RTCXFirmwareUpdateProgressRsp *rsp = data;
int progress = ((float)weakSelf.otaUpdateCounter/otaUpdateTimeout)*100;
[weakSelf showToast:[NSString stringWithFormat:@"当前升级进度:%d%%",progress]];
if ([rsp.status isEqualToString:@"SUCCEEDED"]) {
weakSelf.otaUpdating = NO;
[weakSelf showToast:[NSString stringWithFormat:@"固件升级成功"]];
} else if ([rsp.status isEqualToString:@"FAILED"]) {
weakSelf.otaUpdating = NO;
[weakSelf showToast:[NSString stringWithFormat:@"固件升级失败"]];
}
} onError:^(NSError * _Nullable error) {
[weakSelf showToast:@"获取升级进度失败"];
}];
}
weakSelf.otaUpdateCounter ++;
}
}];
[self.commonTimer fire];