PlayerViewController+otherDelegate.mm 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  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. if (!hostImgVer || hostImgVer.length < 3) {
  124. //[self showImageViewRenewTipViewFun];
  125. return;
  126. }
  127. NSArray *versionArr = [hostImgVer componentsSeparatedByString:@"."];
  128. if(versionArr.count >= 2)
  129. {
  130. NSString * oneStr = versionArr[0];
  131. NSString * twoStr = versionArr[1];
  132. if(oneStr.integerValue >=1 && twoStr.integerValue >=2){
  133. return;
  134. }
  135. }
  136. [self showImageViewRenewTipViewFun];
  137. }
  138. - (void)getCouldPhoneTvStatusResponseFun:(NSDictionary *)dataDict
  139. {
  140. TvStatusModel *model = [[TvStatusModel alloc] initWithDictionary:dataDict error:nil];
  141. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneTvStatusNotification object:model];
  142. if(![model.msg containsString:@"PushStreamBActivity"])
  143. {
  144. return;
  145. }
  146. UIViewController*topVc = self.navigationController.viewControllers.lastObject;
  147. if([topVc isKindOfClass:[PlayerViewController class]]){
  148. [[iToast makeText:NSLocalizedString(@"tv_p2p_ing",nil)] show];
  149. }
  150. }
  151. - (void)showImageViewRenewTipViewFun
  152. {
  153. imageVersionRenewTipView * RenewTipView = [[imageVersionRenewTipView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H)];
  154. [self.view addSubview:RenewTipView];
  155. [self.view bringSubviewToFront:RenewTipView];
  156. }
  157. #pragma mark 监听文件开始的的通知
  158. - (void)beginGotoUploadFileFun:(NSNotification *)notification
  159. {
  160. self.isSuspendUploadType = NO;
  161. uploadFileDataModel *uploadFileDataMod = [notification object];
  162. //HLog(@"%@",uploadFileDataMod);
  163. // if(self.curUploadFileDataMod && uploadFileDataMod.bg_id.integerValue == self.curUploadFileDataMod.bg_id.integerValue){
  164. // return;
  165. // }
  166. self.curUploadFileDataMod = uploadFileDataMod;
  167. if(![uploadFileDataMod isKindOfClass:[uploadFileDataMod class]]){
  168. HLog(@"上传的文件类型不对");
  169. return;
  170. }
  171. [self ApplyUploadFileFun];
  172. }
  173. - (void)applyUploadFileServiceResponseFun:(NSDictionary *)dataDict
  174. {
  175. if([dataDict isKindOfClass:[NSDictionary class]]){
  176. if ([[dataDict allKeys] containsObject:@"data"]) {
  177. NSDictionary *data = dataDict[@"data"];
  178. if ([[data allKeys] containsObject:@"status"]) {
  179. NSString *status = [data objectForKey:@"status"];
  180. if(![status isKindOfClass:[NSString class]]){
  181. status = [[NSString alloc] initWithFormat:@"%@",status];
  182. }
  183. //if(1)
  184. if([status isEqualToString:@"1"])
  185. {
  186. //[self tryAgainApplyUploadFun];
  187. [self clearUploadFileDataFun];
  188. [[uploadFileManager shareInstance] uploadFileFailFun];
  189. }else if ([status isEqualToString:@"0"]){
  190. if ([[data allKeys] containsObject:@"taskUid"]) {
  191. NSString *taskUid = data[@"taskUid"];
  192. if(![status isKindOfClass:[NSString class]]){
  193. taskUid = [[NSString alloc] initWithFormat:@"%@",taskUid];
  194. }
  195. if(taskUid && [taskUid isEqualToString:self.taskUid]){
  196. NSNumber * fileLengthNum = @0;
  197. if ([[data allKeys] containsObject:@"fileLength"])
  198. {
  199. fileLengthNum = data[@"fileLength"];
  200. }
  201. // [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:[fileLengthNum integerValue] withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  202. //
  203. // }];
  204. if([fileLengthNum integerValue] == self.curUploadFileDataMod.totalBytes){
  205. self.taskUid = nil;
  206. NSMutableArray *delArr = [NSMutableArray new];
  207. [delArr addObject:self.curUploadFileDataMod];
  208. [[uploadFileManager shareInstance] deleteUploadFileRecordBy:delArr withDelCache:NO complete:^(BOOL isSuccess) {
  209. if (isSuccess) {
  210. [[NSNotificationCenter defaultCenter] postNotificationName:uploadFileDeteleNotification object:self.curUploadFileDataMod];
  211. }
  212. }];
  213. // mainBlock(^{
  214. // [[iToast makeText:NSLocalizedString(@"File_upload_file_already_exists",nil)] show];
  215. // });
  216. HLog(@"hxd already 上传任务异常终止")
  217. return;
  218. }
  219. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeImage){
  220. [self beginGotoUploadImageFun:fileLengthNum];
  221. }
  222. else{
  223. [self beginGotoUploadVidelFun:fileLengthNum];
  224. }
  225. }
  226. }
  227. }
  228. }
  229. }
  230. else{
  231. //异常
  232. [self tryAgainApplyUploadFun];
  233. }
  234. }
  235. else{
  236. //异常
  237. [self tryAgainApplyUploadFun];
  238. }
  239. }
  240. #pragma mark 图片上传
  241. -(void)beginGotoUploadImageFun:(NSNumber*)fileLengthNum{
  242. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  243. if(!self.cutFileDataArr){
  244. [self clearUploadFileDataFun];
  245. [[uploadFileManager shareInstance] uploadFileFailFun];
  246. self.taskUid = nil;
  247. mainBlock(^{
  248. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  249. });
  250. HLog(@"hxd fail 上传任务异常终止")
  251. return;
  252. }
  253. //答复的taskUid 跟申请的一致 开发发送内容
  254. [self upLoadFileFun];
  255. }
  256. #pragma mark 视频上传
  257. //每次从文件重新切一片 传一片
  258. -(void)beginGotoUploadVidelFun:(NSNumber*)fileLengthNum{
  259. NSData *data = [self cutVideoFileFunAtIndex:fileLengthNum.longLongValue];
  260. HLog(@"data leng:%ld",[data length]);
  261. self.AllFileData = data;
  262. self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:0];
  263. self.indexOfUploadFlie = 0;
  264. HLog(@"data leng:%ld",[data length]);
  265. //return;
  266. // NSString*videoPathStr = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo];
  267. // self.AllFileData = [NSData dataWithContentsOfFile:videoPathStr];
  268. // self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData withDidUpLoadSize:[fileLengthNum integerValue]];
  269. if(!self.cutFileDataArr){
  270. [self clearUploadFileDataFun];
  271. [[uploadFileManager shareInstance] uploadFileFailFun];
  272. self.taskUid = nil;
  273. mainBlock(^{
  274. [[iToast makeText:NSLocalizedString(@"File_upload_fail",nil)] show];
  275. });
  276. HLog(@"hxd fail 上传任务异常终止")
  277. return;
  278. }
  279. //答复的taskUid 跟申请的一致 开发发送内容
  280. [self upLoadFileFun];
  281. }
  282. #pragma mark 分段读视频文件
  283. -(NSData*)cutVideoFileFunAtIndex:(NSUInteger)dataIndex{
  284. NSString *filePath = [cachesFileManager getFilePathWithName:self.curUploadFileDataMod.filename type:uploadFileTypeVideo]; // 文件路径
  285. NSFileManager *manager0 = [NSFileManager defaultManager];
  286. if(![manager0 fileExistsAtPath:filePath]) {
  287. return [NSData new];
  288. }
  289. NSFileHandle *fileHandle = [NSFileHandle fileHandleForReadingAtPath:filePath]; // 创建文件句柄
  290. // 设置分段读取的大小,这里以每次读取1KB为例
  291. //const NSUInteger chunkSize = 50 * 1024 *1024;
  292. const NSUInteger chunkSize = 5 * 1024 *1024;
  293. NSMutableData *data = [NSMutableData data];
  294. if (fileHandle) {
  295. long long endOfFile = [fileHandle seekToEndOfFile];
  296. if (endOfFile >= chunkSize) {
  297. // 读取文件的分段数据到某个位置
  298. [fileHandle seekToFileOffset:dataIndex];
  299. // 读取文件的分段数据
  300. NSData* chunk = [fileHandle readDataOfLength:chunkSize];
  301. if (chunk) {
  302. [data appendData:chunk];
  303. }
  304. }
  305. else{
  306. // 读取文件的分段数据到某个位置
  307. [fileHandle seekToFileOffset:dataIndex];
  308. [data appendData:[fileHandle readDataToEndOfFile]];
  309. }
  310. // 在这里可以对文件内容进行处理
  311. // ...
  312. // 关闭文件句柄
  313. [fileHandle closeFile];
  314. }
  315. return data;
  316. }
  317. - (void)upLoadFileFunServiceResponseFun:(NSDictionary *)dataDict
  318. {
  319. //{"type":"uploadFileRandomRet","data":{"progress":"9\/10","filePath":"/sdcard/Download/IMG_0036.JPG","status":"0","msg":"success"}}
  320. if([dataDict isKindOfClass:[NSDictionary class]]){
  321. if ([[dataDict allKeys] containsObject:@"data"]) {
  322. NSDictionary *data = dataDict[@"data"];
  323. if ([[data allKeys] containsObject:@"status"]) {
  324. NSString *status = [data objectForKey:@"status"];
  325. if(![status isKindOfClass:[NSString class]]){
  326. status = [[NSString alloc] initWithFormat:@"%@",status];
  327. }
  328. if ([status isEqualToString:@"1"]) {
  329. //[self tryAgainApplyUploadFun];
  330. [self clearUploadFileDataFun];
  331. [[uploadFileManager shareInstance] uploadFileFailFun];
  332. }else if ([status isEqualToString:@"0"]){
  333. NSNumber * fileLengthNum = @0;
  334. if ([[data allKeys] containsObject:@"fileLenth"])
  335. {
  336. fileLengthNum = data[@"fileLenth"];
  337. }
  338. [self handleUploadingFun:fileLengthNum];
  339. }
  340. }
  341. }
  342. else{
  343. //异常
  344. [self tryAgainApplyUploadFun];
  345. }
  346. }
  347. else{
  348. //异常
  349. [self tryAgainApplyUploadFun];
  350. }
  351. }
  352. #pragma mark 申请上传文件
  353. - (void)ApplyUploadFileFun
  354. {
  355. //if(!self.taskUid){
  356. self.taskUid = [iTools getTaskUidStr];
  357. self.fileName = self.curUploadFileDataMod.filename;
  358. if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo){
  359. //self.AllFileData = self.curUploadFileDataMod.videoData;
  360. }
  361. else{
  362. self.AllFileData = self.curUploadFileDataMod.imageData;
  363. }
  364. self.indexOfUploadFlie = 0;
  365. //HLog(@"%@ %@",self.taskUid,self.fileName);
  366. //self.cutFileDataArr = [self fileCutPartsBy:self.AllFileData];
  367. //}
  368. NSString * commandStr = [RCCommandHelp applyForUploadFileBy:self.taskUid filePath:self.fileName];
  369. [self.commandChannelManager rc_sendData:commandStr];
  370. }
  371. #pragma mark 遇到异常时 重新上传
  372. - (void)tryAgainApplyUploadFun
  373. {
  374. self.taskUid = nil;
  375. [self ApplyUploadFileFun];
  376. }
  377. #pragma mark 处理上传过程
  378. - (void)handleUploadingFun:(NSNumber*)fileLengthNum
  379. {
  380. if(self.isSuspendUploadType){
  381. //暂停
  382. self.taskUid = nil;
  383. //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  384. NSInteger didUploadSize = fileLengthNum.longLongValue;
  385. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateSuspend withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  386. }];
  387. return;
  388. }
  389. if(self.indexOfUploadFlie < self.cutFileDataArr.count -1) {
  390. //NSInteger didUploadSize = (self.indexOfUploadFlie+1) * (self.cutFileEachPieceSzie);
  391. NSInteger didUploadSize = fileLengthNum.longLongValue;
  392. [[uploadFileManager shareInstance] changeUploadFileState:uploadStateUploading withDidUploadBytes:didUploadSize withModel:self.curUploadFileDataMod complete:^(BOOL isSuccess) {
  393. }];
  394. self.indexOfUploadFlie ++;
  395. [self upLoadFileFun];
  396. }
  397. else if(self.curUploadFileDataMod.curUploadFileType == uploadFileTypeVideo && fileLengthNum && fileLengthNum.longLongValue < self.curUploadFileDataMod.totalBytes){
  398. //视频做了二次切割 不一定上传完了
  399. HLog(@"视频上传传完一片 再来一片");
  400. [self beginGotoUploadVidelFun:fileLengthNum];
  401. }
  402. else{
  403. HLog(@"上传完成");
  404. [self clearUploadFileDataFun];
  405. [[uploadFileManager shareInstance] uploadFileDoneFun];
  406. }
  407. }
  408. -(void)clearUploadFileDataFun{
  409. //上传数据清空
  410. self.taskUid = nil;
  411. self.fileName = nil;
  412. self.AllFileData = nil;
  413. self.indexOfUploadFlie = 0;
  414. self.cutFileDataArr = nil;
  415. self.curUploadFileDataMod = nil;
  416. self.isSuspendUploadType = NO;
  417. }
  418. - (Byte)getBCC:(NSData *)data {
  419. Byte bcc = 0;
  420. unsigned char *bytes = (unsigned char *)[data bytes];
  421. for (int i = 0; i < [data length]; i++) {
  422. bcc = (Byte)(bcc ^ bytes[i]);
  423. }
  424. return bcc;
  425. }
  426. #pragma mark 文件切片
  427. -(NSMutableArray *)fileCutPartsBy:(NSData*)curData withDidUpLoadSize:(NSInteger)didLoadSize{
  428. NSData * data = curData;
  429. NSUInteger allLength = data.length;
  430. NSUInteger subs = 1*1024*1024;//4096;//要切片的大小,我这里设置的是4096字节
  431. //NSUInteger subs = 31*1024;//4096;//要切片的大小,内存没有暴增
  432. self.cutFileEachPieceSzie = subs;
  433. NSInteger index = 0;//起始位置
  434. if(didLoadSize != 0){
  435. if (didLoadSize % subs != 0){
  436. //出错了
  437. return nil;
  438. }
  439. else{
  440. // index = didLoadSize/subs;
  441. // allLength = allLength - didLoadSize;
  442. self.indexOfUploadFlie = didLoadSize/subs;
  443. }
  444. }
  445. NSMutableArray *dataArray =[NSMutableArray new];
  446. do {
  447. //@autoreleasepool {
  448. if (allLength>subs) {
  449. NSRange range =NSMakeRange(index*subs, subs);
  450. index++;
  451. //NSLog(@"%@",NSStringFromRange(range));
  452. [dataArray addObject:[data subdataWithRange:range]];
  453. allLength = allLength - subs;
  454. }else{
  455. NSRange range = NSMakeRange(index*subs, allLength);
  456. //NSLog(@"%@",NSStringFromRange(range));
  457. [dataArray addObject:[data subdataWithRange:range]];
  458. allLength = 0;
  459. }
  460. //}
  461. } while (allLength>0);
  462. HLog(@"dataArray leng:%ld",dataArray.count);//最后得到切片的结果,数组里面是NSData对象
  463. return dataArray;
  464. }
  465. // int转NSData
  466. - (NSData *)int2Data:(int)i{
  467. Byte b0 = i & 0xff;
  468. Byte b1 = i >> 8 & 0xff;
  469. Byte b2 = i >> 16 & 0xff;
  470. Byte b3 = i >> 24 & 0xff;
  471. // 有大小端模式问题?
  472. //Byte result[] = {b0, b1, b2, b3};
  473. Byte result[] = {b3, b2, b1, b0};
  474. return [NSData dataWithBytes:result length:sizeof(result)];
  475. }
  476. // short转NSData
  477. - (NSData *)short2Data:(short)i{
  478. Byte b0 = i & 0xff;
  479. Byte b1 = i >> 8 & 0xff;
  480. // 有大小端模式问题?
  481. //Byte result[] = {b0, b1, b2, b3};
  482. Byte result[] = { b1, b0};
  483. return [NSData dataWithBytes:result length:sizeof(result)];
  484. }
  485. #pragma mark 可以发送文件了
  486. /*
  487. 文件数据字节流byte[]
  488. 格式1:下载日志文件压缩包
  489. |head|msgType|taskId| datalen | curNum | totalNum| data |bcc校验|end|
  490. 长度: 1 1 6 (curNum+totalNum+data) 4 4 datalen- 8 1 1
  491. 0x68 0x0e 0x16
  492. bcc校验从第1位 也就是从msgType起 到 校验位前(不包含校验位)数据作bcc校验
  493. */
  494. - (void)upLoadFileFun
  495. {
  496. //HLog(@"上传文件%ld/%ld",self.indexOfUploadFlie +1,self.cutFileDataArr.count);
  497. if(self.cutFileDataArr.count <= self.indexOfUploadFlie)
  498. {
  499. return;
  500. }
  501. //data
  502. NSData *curData = self.cutFileDataArr[self.indexOfUploadFlie];
  503. //HLog(@"headData:%@",curData);
  504. //head
  505. unsigned char headNum = 104;
  506. NSData *headData = [NSMutableData dataWithBytes:&headNum length:sizeof(headNum)];
  507. //HLog(@"headData:%@",headData);
  508. //msgType
  509. //unsigned char msgType = 12;
  510. unsigned char msgType = 15;
  511. NSData *msgTypeData = [NSMutableData dataWithBytes:&msgType length:sizeof(msgType)];
  512. //HLog(@"msgTypeData:%@",msgTypeData);
  513. //taskId
  514. NSData *taskIdData = [self.taskUid dataUsingEncoding:NSUTF8StringEncoding];
  515. //HLog(@"taskIdData:%@",taskIdData);
  516. //datalen
  517. int datalenNum = int([curData length] +(4+4));
  518. //NSData *datalenData = [NSData dataWithBytes:&datalenNum length:sizeof(datalenNum)];
  519. NSData *datalenData = [self int2Data:datalenNum];
  520. //HLog(@"datalenData %@",datalenData);
  521. //curNum
  522. int curNum = (int)self.indexOfUploadFlie;
  523. //NSData *curNumData = [NSData dataWithBytes:&curNum length:sizeof(curNum)];
  524. NSData * curNumData = [self int2Data:curNum];
  525. //HLog(@"curNumData:%@",curNumData);
  526. //totalNum
  527. int totalNum = (int)self.cutFileDataArr.count;
  528. //NSData *totalNumData = [NSData dataWithBytes:&totalNum length:sizeof(totalNum)];
  529. NSData * totalNumData = [self int2Data:totalNum];
  530. //HLog(@"totalNumData:%@",totalNumData);
  531. NSMutableData *checkBCCData = [NSMutableData new];
  532. [checkBCCData appendData:msgTypeData];
  533. [checkBCCData appendData:taskIdData];
  534. [checkBCCData appendData:datalenData];
  535. [checkBCCData appendData:curNumData];
  536. [checkBCCData appendData:totalNumData];
  537. [checkBCCData appendData:curData];
  538. long length = [curData length];
  539. //HLog(@"curData length %d",length);
  540. Byte bccCheck = [self getBCC:checkBCCData];
  541. //HLog(@"bcc 000 %d",bccCheck);
  542. //bcc校验
  543. char bccNum = (char)bccCheck;
  544. NSData *bccData = [NSMutableData dataWithBytes:&bccNum length:sizeof(bccNum)];
  545. //HLog(@"%@",bccData);
  546. //end
  547. unsigned char endNum = 22;
  548. NSData *endData = [NSMutableData dataWithBytes:&endNum length:sizeof(endNum)];
  549. //HLog(@"%@",endData);
  550. NSMutableData *sendData = [NSMutableData new];
  551. [sendData appendData:headData];
  552. [sendData appendData:checkBCCData];
  553. [sendData appendData:bccData];
  554. [sendData appendData:endData];
  555. //HLog(@"sendData length %ld",[sendData length]);
  556. [self.commandChannelManager rc_sendData:sendData];
  557. //HLog(@"文件上传sendData %@",sendData);
  558. }
  559. - (void)suspendUploadFileFun:(NSNotification *)notification
  560. {
  561. self.isSuspendUploadType = YES;
  562. }
  563. @end