PlayerViewController+otherDelegate.mm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. //
  2. // PlayerViewController+otherDelegate.m
  3. // 隐私保护
  4. //
  5. // Created by xd h on 2023/10/13.
  6. //
  7. #import "PlayerViewController+otherDelegate.h"
  8. #import "RCCommandHelp.h"
  9. #import "couldPhoneBaseInfoModel.h"
  10. #import "cachesFileManager.h"
  11. #import "couldphoneSysInfoModel.h"
  12. #import "TvStatusModel.h"
  13. #import "HWVersionModel.h"
  14. #import <MJExtension.h>
  15. #import "HaveNewVersionView.h"
  16. #import "connectDeviceManager.h"
  17. @implementation PlayerViewController (otherDelegate)
  18. #pragma mark ComontAlretViewControllerDelegate
  19. - (void)CommonAlertokBtnClickPressed{
  20. //HLog("11111");
  21. self.isLoginAgainType = YES;
  22. [[NSNotificationCenter defaultCenter] postNotificationName:lockBypwdNotification object:nil];
  23. }
  24. - (void)CommonAlertCancelBtnClickPressed{
  25. HLog("强制退出app");
  26. exit(0);/*强制退出app*/
  27. }
  28. - (void)checkVersionFun
  29. {
  30. [self getVersion];
  31. }
  32. #pragma mark- 网络请求
  33. - (void)getVersion {
  34. NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
  35. [paraDict setValue:@"ios" forKey:@"type"];
  36. [paraDict setValue:@"skyworth" forKey:@"source"];
  37. [[netWorkManager shareInstance] CommonGetWithCallBackCode:upgradeNewVersion Parameters:paraDict success:^(id _Nonnull responseObject) {
  38. //HLog(@"%@", responseObject);
  39. SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  40. if (model.status == 0) {
  41. HWVersionModel *model = [HWVersionModel mj_objectWithKeyValues:responseObject[@"data"]];
  42. [self checkVersion:model];
  43. }
  44. else
  45. {
  46. //[[iToast makeText:model.msg] show];
  47. }
  48. } failure:^(NSError * _Nonnull error) {
  49. HLog(@"%@", error);
  50. }];
  51. }
  52. - (void)checkVersion:(HWVersionModel*)versionModel {
  53. // NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleVersion"];
  54. NSString *currentVersion = [NSBundle mainBundle].infoDictionary[@"CFBundleShortVersionString"];
  55. NSString *newVersion = [versionModel.versionNumber stringByReplacingOccurrencesOfString:@"v" withString:@""]; //去掉v
  56. // HLog(@"%@---%@", newVersion, currentVersion);
  57. if ([currentVersion compare:newVersion options:NSNumericSearch] == NSOrderedSame) { // 本地版本号 == 服务器版本号
  58. }else if ([currentVersion compare:newVersion options:NSNumericSearch] == NSOrderedDescending) { // 本地版本号 > 服务器版本号 (异常情况)
  59. [HWDataManager setBoolWithKey:Const_haveVersion value:NO];
  60. }else { // 本地版本号 < 服务器版本号 (有新版本)
  61. [self showNewVersion:versionModel];
  62. [HWDataManager setBoolWithKey:Const_haveVersion value:YES];
  63. };
  64. }
  65. #pragma mark 需要强制重启
  66. - (void)updateForceStartFun{
  67. NSString* curSn = [connectDeviceManager shareInstance].DeviceThirdIdMod.data.sn;
  68. NSMutableDictionary *paraDict = [NSMutableDictionary dictionary];
  69. [paraDict setValue:curSn forKey:@"sn"];
  70. [paraDict setValue:[NSNumber numberWithBool:YES] forKey:@"isForceStart"];
  71. [[netWorkManager shareInstance] CommonPostCallBackCode:updateForceStart Parameters:paraDict success:^(id _Nonnull responseObject) {
  72. SuperModel *model = [[SuperModel alloc] initWithDictionary:responseObject error:nil];
  73. if (model.status == 0) {
  74. }
  75. else
  76. {
  77. }
  78. } failure:^(NSError * _Nonnull error) {
  79. HLog(@"%@", error);
  80. }];
  81. }
  82. - (void)showNewVersion:(HWVersionModel*)versionModel {
  83. HaveNewVersionView *haveNewVersionView = [HaveNewVersionView shardInstance];
  84. if (haveNewVersionView.isShow) {
  85. return;
  86. }
  87. haveNewVersionView.versionModel = versionModel;
  88. haveNewVersionView.isContinueCheckAlert = NO; // 是否继续走下一步弹窗检测流程
  89. [haveNewVersionView setBackgroundColor:RGBACOLOR(0, 0, 0, 0.5)];
  90. [[iTools getKeyWindow] addSubview:haveNewVersionView];
  91. haveNewVersionView.isShow = YES;
  92. if(!versionModel.necessary){
  93. haveNewVersionView.deleteBtn.hidden = NO;
  94. }
  95. [haveNewVersionView mas_makeConstraints:^(MASConstraintMaker *make) {
  96. make.top.mas_equalTo(0);
  97. make.left.mas_equalTo(0);
  98. make.right.mas_equalTo(0);
  99. make.bottom.mas_equalTo(0);
  100. }];
  101. haveNewVersionView.alpha = 0.0;
  102. [UIView animateWithDuration:0.3 delay:0.0 options:UIViewAnimationOptionCurveEaseIn animations:^ {
  103. haveNewVersionView.alpha = 1.0;
  104. KyoLog(@"in animate start");
  105. } completion:^(BOOL finished) {
  106. KyoLog(@"in animate completion");
  107. }];
  108. }
  109. - (void)getCouldPhoneBaseInfoResponseFun:(NSDictionary *)dataDict
  110. {
  111. couldPhoneBaseInfoModel *model = [[couldPhoneBaseInfoModel alloc] initWithDictionary:dataDict error:nil];
  112. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneBaseInfoNotification object:model];
  113. }
  114. - (void)getCouldPhoneSysInfoResponseFun:(NSDictionary *)dataDict{
  115. couldphoneSysInfoModel *model = [[couldphoneSysInfoModel alloc] initWithDictionary:dataDict error:nil];
  116. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneSysInfoNotification object:model];
  117. NSString * predatestr = [HWDataManager getObjectWithKey:Const_did_show_image_version_date];
  118. NSString *datestr = [iTools getNowDateString];
  119. if(predatestr && [predatestr isEqualToString:datestr]){
  120. return;
  121. }
  122. NSString *hostImgVer = model.data.data.hostImgVer;
  123. NSString *newVersion = model.data.data.MyNewVersion;
  124. if (!hostImgVer || hostImgVer.length < 3) {
  125. return;
  126. }
  127. if (!newVersion || newVersion.length < 3) {
  128. return;
  129. }
  130. NSArray *hostImgVerArr = [hostImgVer componentsSeparatedByString:@"."];
  131. NSArray *newVersionArr = [newVersion componentsSeparatedByString:@"."];
  132. if(!hostImgVerArr || !newVersionArr){
  133. return;
  134. }
  135. NSInteger curMaxCount = hostImgVerArr.count > newVersionArr.count ? newVersionArr.count : hostImgVerArr.count;
  136. BOOL isNeedShow = NO;
  137. for(int i=0;i<curMaxCount;i++){
  138. NSString * verNum = newVersionArr[i];
  139. NSString * hostNum = hostImgVerArr[i];
  140. if(verNum.integerValue > hostNum.integerValue){
  141. isNeedShow = YES;
  142. break;
  143. }
  144. }
  145. if(isNeedShow){
  146. [self showImageViewRenewTipViewFun];
  147. }
  148. }
  149. - (void)getCouldPhoneTvStatusResponseFun:(NSDictionary *)dataDict
  150. {
  151. TvStatusModel *model = [[TvStatusModel alloc] initWithDictionary:dataDict error:nil];
  152. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneTvStatusNotification object:model];
  153. if(![model.msg containsString:@"PushStreamBActivity"])
  154. {
  155. return;
  156. }
  157. UIViewController*topVc = self.navigationController.viewControllers.lastObject;
  158. if([topVc isKindOfClass:[PlayerViewController class]]){
  159. [[iToast makeText:NSLocalizedString(@"tv_p2p_ing",nil)] show];
  160. }
  161. }
  162. - (void)showImageViewRenewTipViewFun
  163. {
  164. imageVersionRenewTipView * RenewTipView = [[imageVersionRenewTipView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
  165. [self.view addSubview:RenewTipView];
  166. [self.view bringSubviewToFront:RenewTipView];
  167. }
  168. #pragma mark 监听文件开始的的通知
  169. - (void)beginGotoUploadFileFun:(NSNotification *)notification
  170. {
  171. self.isSuspendUploadType = NO;
  172. uploadFileDataModel *uploadFileDataMod = [notification object];
  173. //HLog(@"%@",uploadFileDataMod);
  174. // if(self.curUploadFileDataMod && uploadFileDataMod.bg_id.integerValue == self.curUploadFileDataMod.bg_id.integerValue){
  175. // return;
  176. // }
  177. self.curUploadFileDataMod = uploadFileDataMod;
  178. if(![uploadFileDataMod isKindOfClass:[uploadFileDataMod class]]){
  179. HLog(@"上传的文件类型不对");
  180. return;
  181. }
  182. [self ApplyUploadFileFun];
  183. }
  184. - (void)applyUploadFileServiceResponseFun:(NSDictionary *)dataDict
  185. {
  186. if([dataDict isKindOfClass:[NSDictionary class]]){
  187. if ([[dataDict allKeys] containsObject:@"data"]) {
  188. NSDictionary *data = dataDict[@"data"];
  189. if ([[data allKeys] containsObject:@"status"]) {
  190. NSString *status = [data objectForKey:@"status"];
  191. if(![status isKindOfClass:[NSString class]]){
  192. status = [[NSString alloc] initWithFormat:@"%@",status];
  193. }
  194. //if(1)
  195. if([status isEqualToString:@"1"])
  196. {
  197. //[self tryAgainApplyUploadFun];
  198. [self clearUploadFileDataFun];
  199. [[uploadFileManager shareInstance] uploadFileFailFun];
  200. }else if ([status isEqualToString:@"0"]){
  201. if ([[data allKeys] containsObject:@"taskUid"]) {
  202. NSString *taskUid = data[@"taskUid"];
  203. if(![status isKindOfClass:[NSString class]]){
  204. taskUid = [[NSString alloc] initWithFormat:@"%@",taskUid];
  205. }
  206. if(taskUid && [taskUid isEqualToString:self.taskUid]){
  207. NSNumber * fileLengthNum = @0;
  208. if ([[data allKeys] containsObject:@"fileLength"])
  209. {
  210. fileLengthNum = data[@"fileLength"];
  211. }
  212. // [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:[fileLengthNum integerValue] withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  213. //
  214. // }];
  215. if([fileLengthNum integerValue] == self.curUploadFileDataMod.totalBytes){
  216. self.taskUid = nil;
  217. NSMutableArray *delArr = [NSMutableArray new];
  218. [delArr addObject:self.curUploadFileDataMod];
  219. [[uploadFileManager shareInstance] deleteUploadFileRecordBy:delArr withDelCache:NO complete:^(BOOL isSuccess) {
  220. if (isSuccess) {
  221. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileDeteleNotification object:self.curUploadFileDataMod];
  222. }
  223. }];
  224. // mainBlock(^{
  225. // [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
  226. // });
  227. HLog(@"hxd already 上传任务异常终止")
  228. return;
  229. }
  230. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeImage){
  231. [self beginGotoUploadImageFun:fileLengthNum];
  232. }
  233. else{
  234. [self beginGotoUploadVidelFun:fileLengthNum];
  235. }
  236. }
  237. }
  238. }
  239. }
  240. }
  241. else{
  242. //异常
  243. [self tryAgainApplyUploadFun];
  244. }
  245. }
  246. else{
  247. //异常
  248. [self tryAgainApplyUploadFun];
  249. }
  250. }
  251. #pragma mark 图片上传
  252. -(void)beginGotoUploadImageFun:(NSNumber*)fileLengthNum{
  253. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  254. if(!self.cutFileDataArr){
  255. [self clearUploadFileDataFun];
  256. [[uploadFileManager shareInstance] uploadFileFailFun];
  257. self.taskUid = nil;
  258. mainBlock(^{
  259. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  260. });
  261. HLog(@"hxd fail 上传任务异常终止")
  262. return;
  263. }
  264. //答复的taskUid 跟申请的一致 开发发送内容
  265. [self upLoadFileFun];
  266. }
  267. #pragma mark 视频上传
  268. //每次从文件重新切一片 传一片
  269. -(void)beginGotoUploadVidelFun:(NSNumber*)fileLengthNum{
  270. NSData *data = [self cutVideoFileFunAtIndex:fileLengthNum.longLongValue];
  271. HLog(@"data leng:%ld",[data length]);
  272. self.AllFileData = data;
  273. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:0];
  274. self.indexOfUploadFlie = 0;
  275. HLog(@"data leng:%ld",[data length]);
  276. //return;
  277. // NSString*videoPathStr = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo];
  278. // self.AllFileData = [NSData dataWithContentsOfFile:videoPathStr];
  279. // self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  280. if(!self.cutFileDataArr){
  281. [self clearUploadFileDataFun];
  282. [[uploadFileManager shareInstance] uploadFileFailFun];
  283. self.taskUid = nil;
  284. mainBlock(^{
  285. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  286. });
  287. HLog(@"hxd fail 上传任务异常终止")
  288. return;
  289. }
  290. //答复的taskUid 跟申请的一致 开发发送内容
  291. [self upLoadFileFun];
  292. }
  293. #pragma mark 分段读视频文件
  294. -(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex{
  295. NSString *filePath = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo]; // 文件路径
  296. NSFileManager *manager0 = [NSFileManager defaultManager];
  297. if(![manager0 fileExistsAtPath:filePath]) {
  298. return [NSData new];
  299. }
  300. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
  301. // 设置分段读取的大小,这里以每次读取1KB为例
  302. //const NSUInteger chunkSize = 50 * 1024 *1024;
  303. const NSUInteger chunkSize = 5 * 1024 *1024;
  304. NSMutableData *data = [NSMutableData data];
  305. if (fileHandle) {
  306. long long endOfFile = [fileHandle seekToEndOfFile];
  307. if (endOfFile >= chunkSize) {
  308. // 读取文件的分段数据到某个位置
  309. [fileHandle seekToFileOffset:dataIndex];
  310. // 读取文件的分段数据
  311. NSData* chunk = [fileHandle readDataOfLength:chunkSize];
  312. if (chunk) {
  313. [data appendData:chunk];
  314. }
  315. }
  316. else{
  317. // 读取文件的分段数据到某个位置
  318. [fileHandle seekToFileOffset:dataIndex];
  319. [data appendData:[fileHandle readDataToEndOfFile]];
  320. }
  321. // 在这里可以对文件内容进行处理
  322. // ...
  323. // 关闭文件句柄
  324. [fileHandle closeFile];
  325. }
  326. return data;
  327. }
  328. - (void)upLoadFileFunServiceResponseFun:(NSDictionary *)dataDict
  329. {
  330. //{"type":"uploadFileRandomRet","data":{"progress":"9\/10","filePath":"/sdcard/Download/IMG_0036.JPG","status":"0","msg":"success"}}
  331. if([dataDict isKindOfClass:[NSDictionary class]]){
  332. if ([[dataDict allKeys] containsObject:@"data"]) {
  333. NSDictionary *data = dataDict[@"data"];
  334. if ([[data allKeys] containsObject:@"status"]) {
  335. NSString *status = [data objectForKey:@"status"];
  336. if(![status isKindOfClass:[NSString class]]){
  337. status = [[NSString alloc] initWithFormat:@"%@",status];
  338. }
  339. if ([status isEqualToString:@"1"]) {
  340. //[self tryAgainApplyUploadFun];
  341. [self clearUploadFileDataFun];
  342. [[uploadFileManager shareInstance] uploadFileFailFun];
  343. }else if ([status isEqualToString:@"0"]){
  344. NSNumber * fileLengthNum = @0;
  345. if ([[data allKeys] containsObject:@"fileLenth"])
  346. {
  347. fileLengthNum = data[@"fileLenth"];
  348. }
  349. [self handleUploadingFun:fileLengthNum];
  350. }
  351. }
  352. }
  353. else{
  354. //异常
  355. [self tryAgainApplyUploadFun];
  356. }
  357. }
  358. else{
  359. //异常
  360. [self tryAgainApplyUploadFun];
  361. }
  362. }
  363. #pragma mark 申请上传文件
  364. - (void)ApplyUploadFileFun
  365. {
  366. //if(!self.taskUid){
  367. self.taskUid = [iTools getTaskUidStr];
  368. self.fileName = self.curUploadFileDataMod.filename;
  369. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  370. //self.AllFileData = self.curUploadFileDataMod.videoData;
  371. }
  372. else{
  373. self.AllFileData = self.curUploadFileDataMod.imageData;
  374. }
  375. self.indexOfUploadFlie = 0;
  376. //HLog(@"%@ %@",self.taskUid,self.fileName);
  377. //self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData];
  378. //}
  379. NSString * commandStr = [RCCommandHelp applyForUploadFileBy:self.taskUid filePath:self.fileName];
  380. [self.commandChannelManager rc_sendData:commandStr];
  381. }
  382. #pragma mark 遇到异常时 重新上传
  383. - (void)tryAgainApplyUploadFun
  384. {
  385. self.taskUid = nil;
  386. [self ApplyUploadFileFun];
  387. }
  388. #pragma mark 处理上传过程
  389. - (void)handleUploadingFun:(NSNumber*)fileLengthNum
  390. {
  391. if(self.isSuspendUploadType){
  392. //暂停
  393. self.taskUid = nil;
  394. //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  395. NSInteger didUploadSize = fileLengthNum.longLongValue;
  396. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateSuspend withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  397. }];
  398. return;
  399. }
  400. if(self.indexOfUploadFlie < self.cutFileDataArr.count -1) {
  401. //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  402. NSInteger didUploadSize = fileLengthNum.longLongValue;
  403. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  404. }];
  405. self.indexOfUploadFlie ++;
  406. [self upLoadFileFun];
  407. }
  408. else if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo && fileLengthNum && fileLengthNum.longLongValue < self.curUploadFileDataMod.totalBytes){
  409. //视频做了二次切割 不一定上传完了
  410. HLog(@"视频上传传完一片 再来一片");
  411. [self beginGotoUploadVidelFun:fileLengthNum];
  412. }
  413. else{
  414. HLog(@"上传完成");
  415. [self clearUploadFileDataFun];
  416. [[uploadFileManager shareInstance] uploadFileDoneFun];
  417. }
  418. }
  419. -(void)clearUploadFileDataFun{
  420. //上传数据清空
  421. self.taskUid = nil;
  422. self.fileName = nil;
  423. self.AllFileData = nil;
  424. self.indexOfUploadFlie = 0;
  425. self.cutFileDataArr = nil;
  426. self.curUploadFileDataMod = nil;
  427. self.isSuspendUploadType = NO;
  428. }
  429. - (Byte)getBCC:(NSData *)data {
  430. Byte bcc = 0;
  431. unsigned char *bytes = (unsigned char *)[data bytes];
  432. for (int i = 0; i < [data length]; i++) {
  433. bcc = (Byte)(bcc ^ bytes[i]);
  434. }
  435. return bcc;
  436. }
  437. #pragma mark 文件切片
  438. -(NSMutableArray *)fileCutPartsBy:(NSData*)curData withDidUpLoadSize:(NSInteger)didLoadSize{
  439. NSData * data = curData;
  440. NSUInteger allLength = data.length;
  441. NSUInteger subs = 1*1024*1024;//4096;//要切片的大小,我这里设置的是4096字节
  442. //NSUInteger subs = 31*1024;//4096;//要切片的大小,内存没有暴增
  443. self.cutFileEachPieceSzie = subs;
  444. NSInteger index = 0;//起始位置
  445. if(didLoadSize != 0){
  446. if (didLoadSize % subs != 0){
  447. //出错了
  448. return nil;
  449. }
  450. else{
  451. // index = didLoadSize/subs;
  452. // allLength = allLength - didLoadSize;
  453. self.indexOfUploadFlie = didLoadSize/subs;
  454. }
  455. }
  456. NSMutableArray *dataArray =[NSMutableArray new];
  457. do {
  458. //@autoreleasepool {
  459. if (allLength>subs) {
  460. NSRange range =NSMakeRange(index*subs, subs);
  461. index++;
  462. //NSLog(@"%@",NSStringFromRange(range));
  463. [dataArray addObject:[data subdataWithRange:range]];
  464. allLength = allLength - subs;
  465. }else{
  466. NSRange range = NSMakeRange(index*subs, allLength);
  467. //NSLog(@"%@",NSStringFromRange(range));
  468. [dataArray addObject:[data subdataWithRange:range]];
  469. allLength = 0;
  470. }
  471. //}
  472. } while (allLength>0);
  473. HLog(@"dataArray leng:%ld",dataArray.count);//最后得到切片的结果,数组里面是NSData对象
  474. return dataArray;
  475. }
  476. // int转NSData
  477. - (NSData *)int2Data:(int)i{
  478. Byte b0 = i & 0xff;
  479. Byte b1 = i >> 8 & 0xff;
  480. Byte b2 = i >> 16 & 0xff;
  481. Byte b3 = i >> 24 & 0xff;
  482. // 有大小端模式问题?
  483. //Byte result[] = {b0, b1, b2, b3};
  484. Byte result[] = {b3, b2, b1, b0};
  485. return [NSData dataWithBytes:result length:sizeof(result)];
  486. }
  487. // short转NSData
  488. - (NSData *)short2Data:(short)i{
  489. Byte b0 = i & 0xff;
  490. Byte b1 = i >> 8 & 0xff;
  491. // 有大小端模式问题?
  492. //Byte result[] = {b0, b1, b2, b3};
  493. Byte result[] = { b1, b0};
  494. return [NSData dataWithBytes:result length:sizeof(result)];
  495. }
  496. #pragma mark 可以发送文件了
  497. /*
  498. 文件数据字节流byte[]
  499. 格式1:下载日志文件压缩包
  500. |head|msgType|taskId| datalen | curNum | totalNum| data |bcc校验|end|
  501. 长度: 1 1 6 (curNum+totalNum+data) 4 4 datalen- 8 1 1
  502. 0x68 0x0e 0x16
  503. bcc校验从第1位 也就是从msgType起 到 校验位前(不包含校验位)数据作bcc校验
  504. */
  505. - (void)upLoadFileFun
  506. {
  507. //HLog(@"上传文件%ld/%ld",self.indexOfUploadFlie +1,self.cutFileDataArr.count);
  508. if(self.cutFileDataArr.count <= self.indexOfUploadFlie)
  509. {
  510. return;
  511. }
  512. //data
  513. NSData *curData = self.cutFileDataArr[self.indexOfUploadFlie];
  514. //HLog(@"headData:%@",curData);
  515. //head
  516. unsigned char headNum = 104;
  517. NSData *headData = [NSMutableData dataWithBytes:&headNum length:sizeof(headNum)];
  518. //HLog(@"headData:%@",headData);
  519. //msgType
  520. //unsigned char msgType = 12;
  521. unsigned char msgType = 15;
  522. NSData *msgTypeData = [NSMutableData dataWithBytes:&msgType length:sizeof(msgType)];
  523. //HLog(@"msgTypeData:%@",msgTypeData);
  524. //taskId
  525. NSData *taskIdData = [self.taskUid dataUsingEncoding:NSUTF8StringEncoding];
  526. //HLog(@"taskIdData:%@",taskIdData);
  527. //datalen
  528. int datalenNum = int([curData length] +(4+4));
  529. //NSData *datalenData = [NSData dataWithBytes:&datalenNum length:sizeof(datalenNum)];
  530. NSData *datalenData = [self int2Data:datalenNum];
  531. //HLog(@"datalenData %@",datalenData);
  532. //curNum
  533. int curNum = (int)self.indexOfUploadFlie;
  534. //NSData *curNumData = [NSData dataWithBytes:&curNum length:sizeof(curNum)];
  535. NSData * curNumData = [self int2Data:curNum];
  536. //HLog(@"curNumData:%@",curNumData);
  537. //totalNum
  538. int totalNum = (int)self.cutFileDataArr.count;
  539. //NSData *totalNumData = [NSData dataWithBytes:&totalNum length:sizeof(totalNum)];
  540. NSData * totalNumData = [self int2Data:totalNum];
  541. //HLog(@"totalNumData:%@",totalNumData);
  542. NSMutableData *checkBCCData = [NSMutableData new];
  543. [checkBCCData appendData:msgTypeData];
  544. [checkBCCData appendData:taskIdData];
  545. [checkBCCData appendData:datalenData];
  546. [checkBCCData appendData:curNumData];
  547. [checkBCCData appendData:totalNumData];
  548. [checkBCCData appendData:curData];
  549. long length = [curData length];
  550. //HLog(@"curData length %d",length);
  551. Byte bccCheck = [self getBCC:checkBCCData];
  552. //HLog(@"bcc 000 %d",bccCheck);
  553. //bcc校验
  554. char bccNum = (char)bccCheck;
  555. NSData *bccData = [NSMutableData dataWithBytes:&bccNum length:sizeof(bccNum)];
  556. //HLog(@"%@",bccData);
  557. //end
  558. unsigned char endNum = 22;
  559. NSData *endData = [NSMutableData dataWithBytes:&endNum length:sizeof(endNum)];
  560. //HLog(@"%@",endData);
  561. NSMutableData *sendData = [NSMutableData new];
  562. [sendData appendData:headData];
  563. [sendData appendData:checkBCCData];
  564. [sendData appendData:bccData];
  565. [sendData appendData:endData];
  566. //HLog(@"sendData length %ld",[sendData length]);
  567. [self.commandChannelManager rc_sendData:sendData];
  568. //HLog(@"文件上传sendData %@",sendData);
  569. }
  570. - (void)suspendUploadFileFun:(NSNotification *)notification
  571. {
  572. self.isSuspendUploadType = YES;
  573. }
  574. @end