webRtcManager.m 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  1. //
  2. // webRtcManager.m
  3. // 双子星云手机
  4. //
  5. // Created by xd h on 2024/9/5.
  6. //
  7. #import "webRtcManager.h"
  8. #import "webRtcManager+StatisticsReport.h"
  9. //#import "RcGameWQKeyChain.h"
  10. #import "errorAlertTool.h"
  11. #import "webRtcManager+downloadNasFile.h"
  12. #import "nasMixUploadManager.h"
  13. #import "nasUploadFileManager.h"
  14. #import "nasDownloadFileManager.h"
  15. @interface webRtcManager ()<MediaStreamClientEventsDelegate>
  16. {
  17. NSMutableArray *commandSendCheckArr;//需要检测任务是否发出的的指令
  18. NSTimer *linkCheckSecondTimer; // 检测链接状态
  19. NSInteger tryRelinkNum;//重连次数
  20. }
  21. //第一次链接设备 要发送指令信息 实现单点登录
  22. @property (nonatomic, assign)BOOL didSendfristMsg;
  23. @end
  24. @implementation webRtcManager
  25. + (instancetype)shareManager {
  26. static webRtcManager *_instance;
  27. static dispatch_once_t onceToken;
  28. dispatch_once(&onceToken, ^{
  29. _instance = [[self alloc] init];
  30. });
  31. return _instance;
  32. }
  33. - (instancetype)init {
  34. if (self = [super init]) {
  35. //[self registeNotification];
  36. tryRelinkNum = 0;
  37. //2.客户端开始写日志
  38. [ksharedAppDelegate.WebRtcLogger start];
  39. HLog(@"webrtc :%@",ksharedAppDelegate.WebRtcLogger)
  40. _webRtcChannelSessionId = [iTools getNowTimeStampString];
  41. //_mediaStream = [[RTC_OBJC_TYPE(AMediaStream) alloc] initWithFrame:CGRectZero];
  42. _mediaStream =[[AMediaStream alloc] init];
  43. [_mediaStream setEventDelegate:self];
  44. linkCheckSecondTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:@selector(timerChange) userInfo:nil repeats:YES];
  45. [[NSRunLoop currentRunLoop] addTimer:linkCheckSecondTimer forMode:NSRunLoopCommonModes];
  46. }
  47. return self;
  48. }
  49. #pragma mark 十秒一次的timer检测
  50. - (void)timerChange{
  51. [self checkChannelLinkStateFun];
  52. [self checkAllTaskFun];
  53. }
  54. #pragma mark 检测链接是否OK
  55. - (void)checkChannelLinkStateFun
  56. {
  57. if(_channelState != RTCDataChannelStateOpen
  58. ||(_ConnectionState != RTCIceConnectionStateConnected
  59. && _ConnectionState != RTCIceConnectionStateCompleted)
  60. ){
  61. if(!_didReportWebRtcFailType){//还没上报过通道链接情况
  62. tryRelinkNum ++;
  63. if(tryRelinkNum == 2){
  64. _didReportWebRtcFailType = YES;
  65. // KWeakSelf
  66. // globalBlock(^{
  67. // [[addLogObject shareInstance] gotoAddLogFunBySuccess:^(NSString * _Nonnull key) {
  68. // [weakSelf reportWebRtcRePoportTypeIsChannel:YES withStats:nil withSessionId:self->_webRtcChannelSessionId withLogKey:key];
  69. // }];
  70. // });
  71. //检测盒子心跳
  72. [self checkBoxHeartbeatFun];
  73. }
  74. }
  75. [self relinkWebRtcFun];
  76. }
  77. }
  78. #pragma mark 关闭链接
  79. - (void)closeLinkWebRtcFun
  80. {
  81. if((_ConnectionState == RTCIceConnectionStateConnected
  82. && _ConnectionState == RTCIceConnectionStateCompleted)
  83. || _channelState == RTCDataChannelStateOpen)
  84. {
  85. KWeakSelf
  86. mainBlock(^{
  87. [weakSelf.mediaStream disconnect];
  88. });
  89. }
  90. }
  91. #pragma mark 开始链接
  92. - (void)beginToLinkWebRtcFun
  93. {
  94. KWeakSelf
  95. mainBlock(^{
  96. [weakSelf secondBeginToLinkWebRtcFun];
  97. });
  98. }
  99. - (void)secondBeginToLinkWebRtcFun
  100. {
  101. if(ksharedAppDelegate.isWebSockLinkOKAginType
  102. || !ksharedAppDelegate.DeviceWebRtcMsgMod){
  103. return;
  104. }
  105. webRtcMsgModel * _webRtcMsgMod = ksharedAppDelegate.DeviceWebRtcMsgMod;
  106. webrtcServerModel * webrtcServerMod = ksharedAppDelegate.bestWebrtcServerModel;
  107. //链接用
  108. NSString *signallingUrl = [[NSString alloc] initWithFormat:@"%@:%@",webrtcServerMod.signallingIp,webrtcServerMod.signallingPort];
  109. NSURL *url = [NSURL URLWithString:signallingUrl];
  110. //ice用
  111. NSString *iceUrl = [[NSString alloc] initWithFormat:@"%@:%@",webrtcServerMod.turnIp,webrtcServerMod.turnPort];
  112. // NSMutableDictionary *ice = [NSMutableDictionary new];
  113. // if(iceUrl){
  114. // [ice setValue:iceUrl forKey:@"CHINANET"];
  115. // [ice setValue:iceUrl forKey:@"CMNET"];
  116. // [ice setValue:iceUrl forKey:@"UNICOM"];
  117. // }
  118. NSString *roomName = _webRtcMsgMod.data.uniqueIdentifier;
  119. [cachesFileManager writeLogsWithMsg:[[NSString alloc] initWithFormat:@"webrtc channel startUploadChannel--%@",signallingUrl]];
  120. //1.盒子开始写20秒日志
  121. [_mediaStream setCardLogToFile:@"/sdcard/webrtc_box.log" captureTime:@"20"];
  122. //2.客户端开始写日志
  123. [ksharedAppDelegate.WebRtcLogger start];
  124. HLog(@"webrtc :%@ --- channl start:%@",ksharedAppDelegate.WebRtcLogger,url)
  125. //vclusters 信令不鉴权 写不写都OK
  126. NSInteger result = [_mediaStream startUploadChannel:url ice:iceUrl sn:roomName token:@"vclusters"];
  127. HLog(@"webrtc 发起连接 result:%ld",result)
  128. [_mediaStream setShouldGetStats:YES];
  129. }
  130. - (void)relinkWebRtcFun{
  131. ksharedAppDelegate.isWebSockLinkOKAginType = NO;
  132. if(!_isChangeBoxType){
  133. //客户端停止写日志
  134. [ksharedAppDelegate.WebRtcLogger stop];
  135. [cachesFileManager writeLogsWithMsg:@"webrtc channel relinkWebRtcFun"];
  136. [self beginToLinkWebRtcFun];
  137. }
  138. }
  139. #pragma mark webrtc P2P通道发送消息
  140. - (void)send_data:(NSString *)dataStr
  141. {
  142. [_mediaStream sendData:dataStr];
  143. HLog(@"客户端发出命令:%@",dataStr);
  144. }
  145. #pragma mark 盒子链接成功后需要处理的各种各样事情
  146. - (void)handlAllMsgAfterDidLinkFun
  147. {
  148. _isRebootIngType = NO;
  149. _isResetingType = NO;
  150. _isChangeBoxType = NO;
  151. tryRelinkNum = 0;
  152. ksharedAppDelegate.isWebSockLinkOKAginType = YES;
  153. //获取云机尺寸 兼容 720*1080 &1080*1920 不同分辨率的展示和触控
  154. NSString *getPhoneSizeStr = [RCCommandHelp getPhoneSizecommand];
  155. [self send_data:getPhoneSizeStr];
  156. //判断是否为需要改机
  157. BOOL isNeedRandomChangeParams = [HWDataManager getBoolWithKey:Const_need_random_Change_Params];
  158. if(isNeedRandomChangeParams)
  159. {
  160. NSString *commondStr = @"{\"type\":\"randomChangeParams\"}";
  161. [self send_data:commondStr];
  162. [HWDataManager setBoolWithKey:Const_need_random_Change_Params value:NO];
  163. }
  164. [self fristConnectNeedGiveAMsgFun];
  165. [self updateCopydata];
  166. // 报链接失败 后面又连接上了
  167. [[errorAlertTool shareInstance] dismissErrorAlertFun];
  168. //处理相册备份
  169. [[nasBackupsManager shareInstance] AutohandlePhotosBackupsFun];
  170. //获取磁盘外挂
  171. [self getExtraFilesListFun];
  172. [self getBaseInfoFun];
  173. [self getTvStatusFun];
  174. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  175. [self closeBoxHeartbeatReStartViewFun];
  176. });
  177. if(ksharedAppDelegate.needToShowReStratSucType){
  178. ksharedAppDelegate.needToShowReStratSucType = NO;
  179. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  180. [self showReStartSucFun:1];
  181. });
  182. }
  183. if(self.isReRestartIngBoxType){
  184. self.isReRestartIngBoxType = NO;
  185. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  186. [self showReStartSucFun:2];
  187. });
  188. }
  189. }
  190. #pragma mark 盒子第一次连接成功 给ws发送信息 单点登录 把其他账号挤下去
  191. - (void)fristConnectNeedGiveAMsgFun{
  192. if(!_didSendfristMsg )
  193. {
  194. [self getSysInfoFun];
  195. // NSString *curOaidStr = [RcGameWQKeyChain getOaidStringFun];
  196. // if(!curOaidStr){
  197. // curOaidStr = @"";
  198. // }
  199. // //未调通挤下线
  200. // NSString *commondStr = [[NSString alloc] initWithFormat:@"{\"type\":\"login\",\"value\":\"%@\"}",curOaidStr];
  201. // [self send_data:commondStr];
  202. // [self addCommandSendTaskFunWithType:@"offline_notification" WithCommandStr:commondStr];
  203. _didSendfristMsg = YES;
  204. [self getPreferredLanguage];
  205. NSString *commondStr2 = @"{\"type\":\"TvStatus\"}";
  206. [self send_data:commondStr2];
  207. }
  208. }
  209. #pragma mark 语言和市区 同步云机
  210. -(void)getPreferredLanguage
  211. {//{"data":{"language":"zh-CN","timeZone":"Etc/GMT+8"},"type":"setLanguages"}
  212. // iOS 获取设备当前语言的代码
  213. NSString *preferredLanguage = [[[NSBundle mainBundle] preferredLocalizations] firstObject];
  214. HLog(@"当前语言:%@", preferredLanguage);
  215. //en-US 英文 ja-JP 日文
  216. NSArray *arLanguages = [[NSUserDefaults standardUserDefaults] objectForKey:@"AppleLanguages"];
  217. NSLog(@"arLanguages:%@",arLanguages);
  218. ///获取设备当前地区的代码和APP语言环境
  219. NSString *languageCode = [NSLocale preferredLanguages][0];
  220. //目前支持 中文(简体 繁体) 英文 日语
  221. if([languageCode rangeOfString:@"zh-Hans"].location != NSNotFound)
  222. {
  223. preferredLanguage = @"zh-CN";
  224. }
  225. else if([languageCode rangeOfString:@"zh-Hant"].location != NSNotFound)
  226. {
  227. preferredLanguage = @"zh-HK";
  228. }
  229. else if([languageCode rangeOfString:@"ja-"].location != NSNotFound)
  230. {
  231. preferredLanguage = @"ja-JP";
  232. }
  233. else{
  234. preferredLanguage = @"en-US";
  235. }
  236. // if([languageCode rangeOfString:preferredLanguage].location != NSNotFound){
  237. // preferredLanguage = languageCode;
  238. // }
  239. // else{
  240. // preferredLanguage = @"en-US";
  241. // }
  242. NSString*gmtStr = [self UTCOffset];
  243. HLog(@"%@",gmtStr);
  244. // //获取名字,如“GMT+08:00
  245. NSString *commondStr = [NSString stringWithFormat:@"{\"type\":\"setLanguages\",\"data\":{\"language\":\"%@\",\"timeZone\":\"%@\"}}",preferredLanguage,gmtStr];
  246. [self send_data:commondStr];
  247. }
  248. #pragma mark 获取云机系统镜像等信息
  249. - (void)getSysInfoFun
  250. {
  251. NSString *commondStr = @"{\"type\":\"getSysInfo\"}";
  252. [self send_data:commondStr];
  253. }
  254. -(NSString *)UTCOffset {
  255. NSTimeZone *localTZ = [NSTimeZone localTimeZone];
  256. float offset = localTZ.secondsFromGMT/3600.0;
  257. if(offset > 0){
  258. return [NSString stringWithFormat:@"Etc/GMT+%g",offset];
  259. }
  260. return [NSString stringWithFormat:@"Etc/GMT%g",offset];
  261. }
  262. #pragma mark 收到系统信息消息回调处理
  263. - (void)getCouldPhoneSysInfoResponseFun:(NSDictionary *)dataDict{
  264. couldphoneSysInfoModel *model = [[couldphoneSysInfoModel alloc] initWithDictionary:dataDict error:nil];
  265. //[[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneSysInfoNotification object:model];
  266. // NSString *hostImgVer = model.data.data.hostImgVer;
  267. // NSString *MyNewVersion = model.data.data.MyNewVersion;
  268. NSString *hostImgVer = model.data.hostImgVer;
  269. NSString *MyNewVersion = model.data.MyNewVersion;
  270. //test code
  271. // hostImgVer = @"1.2.3";
  272. // MyNewVersion = @"1.2.0";
  273. //判断当前版本号 待更新版本
  274. if (!hostImgVer || hostImgVer.length < 3
  275. ||!MyNewVersion || MyNewVersion.length < 3) {
  276. return;
  277. }
  278. NSArray *versionArr = [hostImgVer componentsSeparatedByString:@"."];
  279. NSArray *MyNewVersionArr = [MyNewVersion componentsSeparatedByString:@"."];
  280. NSInteger versionArrCount = versionArr.count;
  281. NSInteger MyNewVersionArrCount = MyNewVersionArr.count;
  282. NSInteger maxCount = versionArrCount < MyNewVersionArrCount ? versionArrCount:MyNewVersionArrCount;
  283. BOOL isNeedShowType = NO;
  284. //判断是否要更新镜像框
  285. if(maxCount > 0){
  286. for (int i=0; i<maxCount; i++) {
  287. NSString * numberStr1 = versionArr[i];
  288. NSString * numberStr2 = MyNewVersionArr[i];
  289. if(numberStr2.intValue > numberStr1.intValue){
  290. isNeedShowType =YES;
  291. break;
  292. }
  293. }
  294. }
  295. if(isNeedShowType){
  296. ksharedAppDelegate.isNeedShowImageNewType = YES;
  297. }
  298. //是否禁用文件传输 (1.3以及以上的镜像可以使用文件传输)
  299. if(versionArr.count >= 3)
  300. {
  301. NSString * oneStr = versionArr[0];
  302. NSString * twoStr = versionArr[1];
  303. NSString * threeStr = versionArr[2];
  304. if(oneStr.integerValue <=1 && twoStr.integerValue <=3 && threeStr.integerValue <=0){//禁用
  305. ksharedAppDelegate.DisabledFileTransferType = YES;
  306. if(MyNewVersionArr.count >= 3)
  307. {
  308. NSString * newOneStr = MyNewVersionArr[0];
  309. NSString * newTwoStr = MyNewVersionArr[1];
  310. NSString * newThreeStr = MyNewVersionArr[2];
  311. if(newOneStr.integerValue >=1 && newTwoStr.integerValue >=3 && newThreeStr.integerValue >= 1){
  312. ksharedAppDelegate.isImageNewFor130 = YES;
  313. }
  314. else{
  315. ksharedAppDelegate.isImageNewFor130 = NO;
  316. }
  317. }
  318. }
  319. else{
  320. ksharedAppDelegate.DisabledFileTransferType = NO;
  321. }
  322. //判断镜像是否为1.4.4以后
  323. if(oneStr.integerValue >=2){
  324. ksharedAppDelegate.isImageFor144Orlater = YES;
  325. [HWDataManager setBoolWithKey:stringKeyAddSn(Const_image_version_for_new_token) value:YES];
  326. }
  327. else if(twoStr.integerValue > 4){
  328. ksharedAppDelegate.isImageFor144Orlater = YES;
  329. [HWDataManager setBoolWithKey:stringKeyAddSn(Const_image_version_for_new_token) value:YES];
  330. }
  331. else if(threeStr.integerValue >= 4){
  332. ksharedAppDelegate.isImageFor144Orlater = YES;
  333. [HWDataManager setBoolWithKey:stringKeyAddSn(Const_image_version_for_new_token) value:YES];
  334. }
  335. else{
  336. ksharedAppDelegate.isImageFor144Orlater = NO;
  337. }
  338. }
  339. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneSysInfoNotification object:model];
  340. }
  341. #pragma mark 添加消息重复机制
  342. -(void)addCommandSendTaskFunWithType:(NSString*)type WithCommandStr:(NSString*)commandStr
  343. {
  344. if(!commandSendCheckArr){
  345. commandSendCheckArr = [NSMutableArray new];
  346. }
  347. BOOL didAddType = NO;
  348. for (commandSendCheckModel *model in commandSendCheckArr) {
  349. if([model.type isEqualToString:type]){
  350. didAddType = YES;
  351. model.reSendNum = 0;
  352. model.sendTimerStamp = [iTools getNowTimeStamp];
  353. break;
  354. }
  355. }
  356. if(!didAddType){
  357. commandSendCheckModel *model = [commandSendCheckModel new];
  358. model.commandStr = commandStr;
  359. model.type = type;
  360. model.reSendNum = 0;
  361. model.sendTimerStamp = [iTools getNowTimeStamp];
  362. [commandSendCheckArr addObject:model];
  363. }
  364. }
  365. #pragma mark 删除代理确认收到的消息
  366. -(void)deleteCommandSendTaskFunWith:(NSString*)type
  367. {
  368. if(commandSendCheckArr && commandSendCheckArr.count >0){
  369. NSArray *taskArr = [NSArray arrayWithArray:commandSendCheckArr];
  370. for (commandSendCheckModel *model in taskArr) {
  371. if([type isEqualToString:model.type]){
  372. [commandSendCheckArr removeObject:model];
  373. }
  374. }
  375. }
  376. }
  377. #pragma mark 复制手机消息到云机
  378. - (void)updateCopydata{
  379. UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  380. NSString *str = [pasteboard string];
  381. HLog(@"__________%s______%@____",__func__,str);
  382. if ([str rangeOfString:@"CVLUSTERS_NOUSE_"].location != NSNotFound)
  383. {
  384. str = nil;
  385. }
  386. // if (str && str.length >0)
  387. // {
  388. // //HLog(@"hxd111 cutting %@",str);
  389. // /*发送数据*/
  390. // NSString *dataStr = [RCCommandHelp commandCuttingWithContent:str];
  391. // [self send_data:dataStr];
  392. // //pasteboard.string = @"";
  393. // }
  394. NSString* curSn = ksharedAppDelegate.DeviceThirdIdMod.data.changeSn;
  395. NSMutableArray *pasteDataAry = [HWDataManager getObjectWithKey:curSn];
  396. if(pasteDataAry && [pasteDataAry isKindOfClass:[NSMutableArray class]] /*&& [pasteDataAry isKindOfClass:[NSArray class]]*/)
  397. {
  398. pasteDataAry = [[NSMutableArray alloc] initWithArray:pasteDataAry];
  399. }
  400. if (str){/*当前剪切板中有内容*/
  401. if (pasteDataAry){
  402. if (![pasteDataAry containsObject:str]){
  403. if ([pasteDataAry count] == 10){/*数组长度为10的时候删除最后一个*/
  404. [pasteDataAry removeObjectAtIndex:9];
  405. }
  406. [pasteDataAry insertObject:str atIndex:0];/*新增内容放到第一位*/
  407. /*发送数据到云机*/
  408. NSString *dataStr = [RCCommandHelp commandCuttingWithContent:str];
  409. [self send_data:dataStr];
  410. }
  411. }else{/*缓存数组为空 新建数组*/
  412. pasteDataAry = [[NSMutableArray alloc] init];
  413. [pasteDataAry addObject:str];
  414. /*发送数据*/
  415. NSString *dataStr = [RCCommandHelp commandCuttingWithContent:str];
  416. [self send_data:dataStr];
  417. }
  418. /*更新后的粘贴数组缓存到本地*/
  419. [HWDataManager setObjectWithKey:curSn value:pasteDataAry];;
  420. }
  421. }
  422. #pragma mark 获取云机以及外挂磁盘
  423. - (void)getExtraFilesListFun
  424. {
  425. // NSString *ExtraCommondStr = [RCCommandHelp getExtraFilesList];
  426. // [self send_data:ExtraCommondStr];
  427. //改走http方案
  428. [[NSNotificationCenter defaultCenter] postNotificationName:getExtraFilesDoneNotification object:nil];/*发送通知*/
  429. }
  430. #pragma mark 获取到云机以及外挂磁盘信息
  431. - (void)getExtraFilesResponseFun:(NSDictionary *)dataDict
  432. {
  433. cloudPhoneExtraFileListModel *model = [[cloudPhoneExtraFileListModel alloc] initWithDictionary:dataDict error:nil];
  434. ksharedAppDelegate.cloudPhoneExtraFileListMod = model;
  435. [[NSNotificationCenter defaultCenter] postNotificationName:getExtraFilesDoneNotification object:dataDict];/*发送通知*/
  436. }
  437. #pragma mark 获取云机基本信息
  438. - (void)getBaseInfoFun
  439. {
  440. NSString *commondStr = @"{\"type\":\"getBaseInfo\"}";
  441. [self send_data:commondStr];
  442. }
  443. #pragma mark 获取到云机基本信息
  444. - (void)getCouldPhoneBaseInfoResponseFun:(NSDictionary *)dataDict
  445. {
  446. couldPhoneBaseInfoModel *model = [[couldPhoneBaseInfoModel alloc] initWithDictionary:dataDict error:nil];
  447. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneBaseInfoNotification object:model];
  448. }
  449. #pragma mark 获取到TV投屏状态
  450. - (void)getCouldPhoneTvStatusResponseFun:(NSDictionary *)dataDict
  451. {
  452. TvStatusModel *model = [[TvStatusModel alloc] initWithDictionary:dataDict error:nil];
  453. ksharedAppDelegate.TvStatusMod = model;
  454. [[NSNotificationCenter defaultCenter] postNotificationName:getCouldPhoneTvStatusNotification object:model];
  455. // if(![model.msg containsString:@"PushStreamBActivity"])
  456. // {
  457. // return;
  458. // }
  459. //
  460. // UIViewController*topVc = self.navigationController.viewControllers.lastObject;
  461. // if([topVc isKindOfClass:[PlayerViewController class]]){
  462. // [[iToast makeText:NSLocalizedString(@"tv_p2p_ing",nil)] show];
  463. // }
  464. }
  465. #pragma mark 重启云机
  466. - (void)needToRebootFun
  467. {
  468. NSString *commondStr = @"{\"type\":\"reboot\"}";
  469. [self send_data:commondStr];
  470. //添加到任务监听
  471. [self addCommandSendTaskFunWithType:@"reboot" WithCommandStr:commondStr];
  472. //数据埋点
  473. [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Cloud_restart"];
  474. _isRebootIngType = YES;
  475. //1、重启、恢复出厂设备、首次设置密码 3个情况下8分钟之内不弹
  476. long seconds = [iTools getNowTimeStamp];
  477. NSNumber *secondsNum = [NSNumber numberWithLong:seconds];
  478. if(secondsNum){
  479. [HWDataManager setNumberWithKey:stringKeyAddSn(@"disenable_type2_BoxHeartbeat") value:secondsNum];
  480. }
  481. }
  482. #pragma mark 恢复出厂设置
  483. - (void)needToResetFun
  484. {
  485. NSString *commondStr = @"{\"type\":\"reset\"}";
  486. [self send_data:commondStr];
  487. //添加到任务监听
  488. [self addCommandSendTaskFunWithType:@"reset" WithCommandStr:commondStr];
  489. //数据埋点
  490. [[netWorkManager shareInstance] DataEmbeddingPointBy:3 withEventValue:@"Cloud_restore_factory"];
  491. _isResetingType = YES;
  492. //1、重启、恢复出厂设备、首次设置密码 3个情况下8分钟之内不弹
  493. long seconds = [iTools getNowTimeStamp];
  494. NSNumber *secondsNum = [NSNumber numberWithLong:seconds];
  495. if(secondsNum){
  496. [HWDataManager setNumberWithKey:stringKeyAddSn(@"disenable_type2_BoxHeartbeat") value:secondsNum];
  497. }
  498. }
  499. #pragma mark 云机截图保存到云机图库
  500. - (void)screenshotInCloudPhoneFun{
  501. NSString*taskUid = [iTools getTaskUidStr];
  502. NSString *commondStr = [RCCommandHelp commandCloudPhoneScreenshotWithTaskUid:taskUid];
  503. [self send_data:commondStr];
  504. }
  505. #pragma mark 获取TV投屏状态
  506. - (void)getTvStatusFun
  507. {
  508. NSString *commondStr = @"{\"type\":\"TvStatus\"}";
  509. [self send_data:commondStr];
  510. }
  511. #pragma mark 关闭TV投屏状态
  512. - (void)offTvFun
  513. {
  514. NSString *commondStr = @"{\"type\":\"TvOff\"}";
  515. [self send_data:commondStr];
  516. }
  517. #pragma mark 开启TV投屏状态
  518. - (void)onTvFun
  519. {
  520. NSString *commondStr = @"{\"type\":\"wakeupTV\"}";
  521. [self send_data:commondStr];
  522. }
  523. #pragma mark 创建备份文件夹
  524. - (void)createBackupsFolderBy:(NSString*)backupsDefaultPath
  525. {
  526. NSString *folderName = backupsDefaultPath;
  527. if(folderName && folderName.length >0){
  528. NSString * commandStr = [RCCommandHelp applyForCreateFolderwithFolderName:folderName];
  529. [self send_data:commandStr];
  530. }
  531. }
  532. #pragma mark 获取备份文件夹列表
  533. - (void)getBackupFolderListFun
  534. {
  535. NSString * commandStr = [RCCommandHelp getCreateFolderList];
  536. [self send_data:commandStr];
  537. }
  538. #pragma mark 创建文件夹回调
  539. - (void)createFolderResponseFun:(NSDictionary *)dataDict
  540. {
  541. couldPhoneCommonModel *model = [[couldPhoneCommonModel alloc] initWithDictionary:dataDict error:nil];
  542. if(model){
  543. NSNumber *curNum = [NSNumber numberWithInteger:model.status];
  544. [[NSNotificationCenter defaultCenter] postNotificationName:createFolderDoneNotification object:curNum];/*发送通知*/
  545. }
  546. }
  547. - (void)getFolderListResponseFun:(NSDictionary *)dataDict
  548. {
  549. [[NSNotificationCenter defaultCenter] postNotificationName:getFolderListDoneNotification object:dataDict];/*发送通知*/
  550. }
  551. - (void)searchFileListResponseFun:(NSDictionary *)dataDict
  552. {
  553. [[NSNotificationCenter defaultCenter] postNotificationName:searchFileListDoneNotification object:dataDict];/*发送通知*/
  554. }
  555. #pragma mark U盘插入相关
  556. - (void)getExtraMediaEventResponseFun:(NSDictionary *)dataDict
  557. {
  558. mainBlock((^{
  559. extraMediaEventModel *model = [[extraMediaEventModel alloc] initWithDictionary:dataDict error:nil];
  560. NSString *tip = nil;
  561. if(model.data.event == 0){
  562. tip = NSLocalizedString(@"disk_insertion_tip",nil);
  563. [self showInsertPopViewFun:model.data.name];
  564. }
  565. else if(model.data.event == 1){
  566. tip = NSLocalizedString(@"disk_extract_tip",nil);
  567. }
  568. else if(model.data.event == 2){
  569. tip = NSLocalizedString(@"disk_save_extract_tip",nil);
  570. }
  571. NSString *totalTips = [[NSString alloc] initWithFormat:@"%@%@",model.data.name,tip];
  572. [[iToast makeText:totalTips] show];
  573. }));
  574. }
  575. #pragma mark 显示插入UI弹框
  576. - (void)showInsertPopViewFun:(NSString*)name
  577. {
  578. if(self->curUSBInsertPopV){
  579. [self->curUSBInsertPopV removeFromSuperview];
  580. self->curUSBInsertPopV = nil;
  581. }
  582. self->curUSBInsertPopV = [[USBInsertPopView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, SCREEN_H) withName:name];
  583. [[iTools getKeyWindow] addSubview:self->curUSBInsertPopV];
  584. }
  585. #pragma mark 检测nas任务情况
  586. - (void)checkAllTaskFun
  587. {
  588. HLog(@"checkAllTaskFun");
  589. //处理相册备份
  590. [[nasBackupsManager shareInstance] checkReBackupsFileFun];
  591. //处理上传失败的重新上传
  592. //[[nasUploadFileManager shareInstance] reUploadFileFunByNetWork];
  593. //[[nasDownloadFileManager shareInstance] reDownloadloadFileFunByNetWork];
  594. //if([AudioSessionObject shareManager].isBackgroundType)
  595. {
  596. [self checkFileTransfeTaskFun];
  597. }
  598. if([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusUnknown){
  599. [ksharedAppDelegate MonitorNetworkChangesFun];
  600. }
  601. }
  602. #pragma mark 检测是否正在进行的文件传输任务 设置后台状态
  603. - (void)checkFileTransfeTaskFun
  604. {
  605. BOOL isBackupsingType = [[nasBackupsManager shareInstance] checkBackupsingFun];
  606. BOOL isUploadingType = [[nasMixUploadManager shareManager] checkUploadTaskDoingFun];
  607. BOOL isDownloadingType = [[nasDownloadManager shareManager] isDownLoadIngType];
  608. BOOL isNasDownloadingType = [customDownloadManager shareManager].isDownLoadIngType;
  609. BOOL isBackground = [HWDataManager getBoolWithKey:stringKeyAddSn(Const_file_Transfe_working_background)];
  610. if ((isBackupsingType || isUploadingType || isDownloadingType ||isNasDownloadingType) && isBackground) {
  611. HLog(@"后台保活中");
  612. //[cachesFileManager writeLogsWithMsg:@"Background working"];
  613. }
  614. else{
  615. HLog(@"停止后台保活");
  616. //[cachesFileManager writeLogsWithMsg:@"stop Background working"];
  617. //[[AudioSessionObject shareManager] stopBackgroundActiveFun];
  618. }
  619. }
  620. #pragma mark 收到的webrtc消息处理
  621. - (void)handleWebRtcMsgResponseBy:(NSData*)message
  622. {
  623. if([message isKindOfClass:[NSMutableString class]] || [message isKindOfClass:[NSString class]])
  624. {
  625. message = [(NSString *)message dataUsingEncoding:(NSUTF8StringEncoding)];
  626. }
  627. NSError *error = nil;
  628. NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:&error];
  629. HLog(@"webRtc P2P 通道接收消息:------------------%@",dataDict);
  630. // if([message isKindOfClass:[NSData class]] && [message length] == 0){
  631. // if(type == RCSocketCloudPhoneReceiveTypeForPong)
  632. // {
  633. // [weakSelf keepWebSocketOKFun];
  634. // }
  635. // return;
  636. // }
  637. //
  638. // HLog(@"webSocket指令通道接收消息:------------------\n%@",message);
  639. //
  640. // if([message isKindOfClass:[NSMutableString class]] || [message isKindOfClass:[NSString class]])
  641. // {
  642. // message = [(NSString *)message dataUsingEncoding:(NSUTF8StringEncoding)];
  643. // }
  644. //
  645. // NSError *error = nil;
  646. // NSDictionary *dataDict = [NSJSONSerialization JSONObjectWithData:message options:NSJSONReadingMutableContainers error:&error];
  647. if(!dataDict){
  648. //[weakSelf handleDownloadResponseFunBy:message];
  649. return;
  650. }
  651. if(![dataDict isKindOfClass:[NSDictionary class]]){
  652. //[__NSCFString allKeys] unrecognized selector sent to ins
  653. return;
  654. }
  655. if(![[dataDict allKeys] containsObject:@"type"]){
  656. return;
  657. }
  658. NSString *messageType = dataDict[@"type"];
  659. if ([messageType isEqualToString:@"cutting"]) {
  660. //[[iToast makeText:@"复制成功"] show];
  661. }
  662. // else if ([messageType isEqualToString:@"forwardMsgRep"]){/*转发的回复*/
  663. //
  664. // cloudPhoneCommonModel *model = [[cloudPhoneCommonModel alloc] initWithDictionary:dataDict error:nil];
  665. // if(!model){
  666. // return;
  667. // }
  668. //
  669. // if([model.data.msg isEqualToString:@"only one socket"]||
  670. // [model.data.msg isEqualToString:@"only one socket2"]){
  671. // [weakSelf deleteCommandSendTaskFunWith:@"offline_notification"];
  672. // }
  673. // }
  674. // else if ([messageType isEqualToString:@"forwardMsg"]){/*转发*/
  675. // /*获取指令类型*/
  676. // NSString *code = nil;
  677. // if ([[dataDict allKeys] containsObject:@"data"]) {
  678. // NSDictionary *data = dataDict[@"data"];
  679. //
  680. // if([data isKindOfClass:[NSString class]]){
  681. // NSString * dataStr = (NSString*)data;
  682. // if([dataStr isEqualToString:@"offline_notification"]){
  683. // HLog(@"被别人挤下线了");
  684. // [weakSelf LogoutByOtherFun];
  685. // }
  686. //
  687. // return;
  688. // }
  689. // else if ([data isKindOfClass:[NSDictionary class]] && [[data allKeys] containsObject:@"code"]) {
  690. // code = [data objectForKey:@"code"];
  691. //
  692. // if (![code isKindOfClass:[NSString class]]) {
  693. // code = [NSString stringWithFormat:@"%ld",[code integerValue]];
  694. // }
  695. // }
  696. // }
  697. //
  698. // /*获取用户名*/
  699. // NSString *useName = nil;
  700. // if ([[dataDict allKeys] containsObject:@"data"]) {
  701. // NSDictionary *data = dataDict[@"data"];
  702. // if ([[data allKeys] containsObject:@"userName"]) {
  703. // //useName = [data objectForKey:@"userName"];
  704. // }
  705. // }
  706. //
  707. //
  708. // if ([code isEqualToString:@"phoneSizeChange"]){/*分辨率改变*/
  709. // if ([[dataDict allKeys] containsObject:@"data"]) {
  710. // NSDictionary *data = dataDict[@"data"];
  711. // if ([[data allKeys] containsObject:@"width"]) {
  712. // ksharedAppDelegate.couldPhone_W_PHONE = [[data objectForKey:@"width"] integerValue];
  713. // }
  714. // if ([[data allKeys] containsObject:@"height"]) {
  715. // ksharedAppDelegate.couldPhone_H_PHONE = [[data objectForKey:@"height"] integerValue];
  716. // }
  717. //
  718. // if (ksharedAppDelegate.couldPhone_W_PHONE > ksharedAppDelegate.couldPhone_H_PHONE) {
  719. // CGFloat temp = ksharedAppDelegate.couldPhone_W_PHONE;
  720. // ksharedAppDelegate.couldPhone_W_PHONE = ksharedAppDelegate.couldPhone_H_PHONE;
  721. // ksharedAppDelegate.couldPhone_H_PHONE = temp;
  722. // }
  723. // }
  724. // }
  725. // }
  726. else if ([messageType isEqualToString:@"getPhoneSize"] || [messageType isEqualToString:@"setPhoneSize"]){
  727. //NSString *sn = nil;
  728. if([messageType isEqualToString:@"setPhoneSize"]){
  729. self.isDiDChangePhoneSizeType = YES;
  730. }
  731. if ([[dataDict allKeys] containsObject:@"data"]) {
  732. NSDictionary *data = dataDict[@"data"];
  733. if ([[data allKeys] containsObject:@"status"]) {
  734. NSInteger status = [[data objectForKey:@"status"] integerValue];
  735. if (status == 0) {/*不是当前设备直接返回*/
  736. if ([[data allKeys] containsObject:@"width"]) {
  737. ksharedAppDelegate.couldPhone_W_PHONE = [[data objectForKey:@"width"] integerValue];
  738. }
  739. if ([[data allKeys] containsObject:@"height"]) {
  740. ksharedAppDelegate.couldPhone_H_PHONE = [[data objectForKey:@"height"] integerValue];
  741. }
  742. if (ksharedAppDelegate.couldPhone_W_PHONE > ksharedAppDelegate.couldPhone_H_PHONE) {
  743. CGFloat temp = ksharedAppDelegate.couldPhone_W_PHONE;
  744. ksharedAppDelegate.couldPhone_W_PHONE = ksharedAppDelegate.couldPhone_H_PHONE;
  745. ksharedAppDelegate.couldPhone_H_PHONE = temp;
  746. }
  747. }
  748. }
  749. }
  750. }
  751. // else if ([messageType isEqualToString:@"sync_wifi"]){
  752. // [weakSelf sync_wifiBackHandleFun];
  753. // }
  754. // else if ([messageType isEqualToString:@"reProduceText"]){
  755. // if ([[dataDict allKeys] containsObject:@"data"]) {
  756. // NSDictionary *data = dataDict[@"data"];
  757. //
  758. // if ([[data allKeys] containsObject:@"text"]) {
  759. // NSString *pasteboardStr = [data objectForKey:@"text"];
  760. // UIPasteboard* pasteboard = [UIPasteboard generalPasteboard];
  761. // pasteboard.string = pasteboardStr;
  762. // }
  763. // }
  764. // }
  765. // else if ([messageType isEqualToString:@"downAdnInstallRep"]){
  766. // if ([[dataDict allKeys] containsObject:@"data"]) {
  767. // NSDictionary *data = dataDict[@"data"];
  768. //
  769. // if ([[data allKeys] containsObject:@"status"]) {
  770. // NSString *status = [data objectForKey:@"status"];
  771. // if ([status isEqualToString:@"1"]) {
  772. // mainBlock(^{
  773. // // [[iToast makeText:@"App下载完成"] show];
  774. // });
  775. // }else if ([status isEqualToString:@"0"]){
  776. // mainBlock(^{
  777. // //[[iToast makeText:@"App下载中"] show];
  778. // });
  779. // }
  780. // }
  781. // }
  782. // }
  783. // else if ([messageType isEqualToString:@"shakeit"]){
  784. // HLog(@"\n-----摇一摇成功------");
  785. // }else if ([messageType isEqualToString:@"keyboardFeedbackBean"]){/*调起键盘*/
  786. // HLog(@"\n-----待处理 调起键盘------");
  787. // // [weakSelf keyboardFeedbackBeanFun];
  788. // }else if ([messageType isEqualToString:@"FileRandomReady"]
  789. // ||[messageType isEqualToString:@"FilePartReady"]
  790. // ){/*申请文件上传得到答复*/
  791. // [weakSelf applyUploadFileServiceResponseFun:dataDict];
  792. // }
  793. // else if ([messageType isEqualToString:@"uploadFileRandomRet"]
  794. // ||[messageType isEqualToString:@"uploadFilePartRet"]
  795. // ){/*文件上传得到答复*/
  796. // [weakSelf upLoadFileFunServiceResponseFun:dataDict];
  797. // }
  798. // else if ([messageType isEqualToString:@"backUpFileRandomReady"]
  799. // ||[messageType isEqualToString:@"backUpPartReady"]
  800. // ){/*文件备份得到答复*/
  801. // [weakSelf applyBackupsFileServiceResponseFun:dataDict];
  802. // }
  803. // else if ([messageType isEqualToString:@"backUpFileRandomRet"]
  804. // ||[messageType isEqualToString:@"backUpFilePartRet"]){/*文件备份得到答复*/
  805. // [weakSelf backupsFileFunServiceResponseFun:dataDict];
  806. // }
  807. else if ([messageType isEqualToString:@"getBaseInfo"]){/*获取云机的基本信息*/
  808. [self getCouldPhoneBaseInfoResponseFun:dataDict];
  809. }
  810. else if ([messageType isEqualToString:@"getSysInfo"]){/*获取云机的系统信息*/
  811. [self getCouldPhoneSysInfoResponseFun:dataDict];
  812. }
  813. else if ([messageType isEqualToString:@"TvStatus"]){/*获取TV投屏信息*/
  814. [self getCouldPhoneTvStatusResponseFun:dataDict];
  815. }
  816. else if ([messageType isEqualToString:@"TvOff"]){/*关闭TV投屏*/
  817. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  818. [self getTvStatusFun];
  819. });
  820. }
  821. else if ([messageType isEqualToString:@"wakeupTV"]){/*开启TV投屏*/
  822. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  823. [self getTvStatusFun];
  824. });
  825. }
  826. else if ([messageType isEqualToString:@"reboot"]){/*重启*/
  827. //[weakSelf stopForceStartTimerFun];
  828. }
  829. else if ([messageType isEqualToString:@"mkdir"]){/*创建文件夹*/
  830. [self createFolderResponseFun:dataDict];
  831. }
  832. else if ([messageType isEqualToString:@"getBackupPath"]){/*创建文件夹*/
  833. [self getFolderListResponseFun:dataDict];
  834. }
  835. else if ([messageType isEqualToString:@"search"]){/*创建文件夹*/
  836. [self searchFileListResponseFun:dataDict];
  837. }
  838. else if ([messageType isEqualToString:@"getExtraFiles"]){/*获取云机产品信息*/
  839. [self getExtraFilesResponseFun:dataDict];
  840. }
  841. else if ([messageType isEqualToString:@"extraMediaEvent"]){/*磁盘插拔*/
  842. [self getExtraMediaEventResponseFun:dataDict];
  843. [self getExtraFilesListFun];
  844. }
  845. else if ([messageType isEqualToString:@"reset"]){/**/
  846. [self deleteCommandSendTaskFunWith:@"reset"];
  847. }
  848. else if ([messageType isEqualToString:@"shortcatRep"]){/*截图*/
  849. [[iToast makeText:NSLocalizedString(@"shortcatRep_tip",nil)] show];
  850. }
  851. }
  852. #pragma mark WebRTC 回调 MediaStreamClientEventsDelegate
  853. #pragma mark 不能再这里函数判断 这个是推拉流的
  854. -(void)onChangeConnectionStateFromPeerName:(NSString*)peerName didChangeIceConnectionState:(RTCIceConnectionState)state
  855. {
  856. HLog(@"channel P2P onChangeConnectionStateFromPeerName: state:%ld",state)
  857. self.ConnectionState = state;
  858. [cachesFileManager writeLogsWithMsg:[[NSString alloc] initWithFormat:@"webrtc channel onChangeConnectionStateFromPeerName:%ld",state]];
  859. switch (state) {
  860. case RTCIceConnectionStateConnected:{
  861. //链接成功
  862. [[NSNotificationCenter defaultCenter] postNotificationName:webRtcLinkStateChangeNotification object:nil];/*发送通知*/
  863. }
  864. break;
  865. case RTCIceConnectionStateCompleted:
  866. //链接完成
  867. break;
  868. case RTCIceConnectionStateFailed:
  869. case RTCIceConnectionStateDisconnected:
  870. case RTCIceConnectionStateClosed:{
  871. [[NSNotificationCenter defaultCenter] postNotificationName:webRtcLinkStateChangeNotification object:nil];/*发送通知*/
  872. if(!_didReportWebRtcFailType && state == RTCIceConnectionStateFailed){//还没上报过通道链接情况
  873. _didReportWebRtcFailType = YES;
  874. // KWeakSelf
  875. // globalBlock(^{
  876. // [[addLogObject shareInstance] gotoAddLogFunBySuccess:^(NSString * _Nonnull key) {
  877. // [weakSelf reportWebRtcRePoportTypeIsChannel:YES withStats:nil withSessionId:self->_webRtcChannelSessionId withLogKey:key];
  878. // }];
  879. // });
  880. }
  881. //链接关闭
  882. [self relinkWebRtcFun];
  883. }
  884. break;
  885. default:
  886. break;
  887. }
  888. }
  889. #pragma mark 通道连接状态变化监听
  890. - (void)dataChannelDidChangeFromPeerName:(NSString*)peerName State:(RTCDataChannelState)state
  891. {
  892. HLog(@"webRtc P2P dataChannelDidChangeFromPeerName: state:%ld",state)
  893. [cachesFileManager writeLogsWithMsg:[[NSString alloc] initWithFormat:@"webrtc channel dataChannelDidChangeFromPeerName:%ld",state]];
  894. self.channelState = state;
  895. switch (state) {
  896. case RTCDataChannelStateConnecting:
  897. {
  898. }
  899. break;
  900. case RTCDataChannelStateOpen:
  901. {
  902. [[NSNotificationCenter defaultCenter] postNotificationName:webRtcLinkStateChangeNotification object:nil];/*发送通知*/
  903. //链接成功
  904. [self handlAllMsgAfterDidLinkFun];
  905. //客户端停止写日志
  906. [ksharedAppDelegate.WebRtcLogger stop];
  907. }
  908. break;
  909. case RTCDataChannelStateClosing:
  910. {
  911. }
  912. break;
  913. case RTCDataChannelStateClosed:
  914. {
  915. //链接断开
  916. [self relinkWebRtcFun];
  917. }
  918. break;
  919. default:
  920. break;
  921. }
  922. }
  923. -(void)onChannelDataFromPeerName:(NSString*)peerName buffer:(RTC_OBJC_TYPE(RTCDataBuffer) *)buffer
  924. {
  925. //HLog(@"onIceConnectedFromPeerName:%@",buffer.data);
  926. if(buffer && buffer.data){
  927. KWeakSelf
  928. mainBlock(^{
  929. [weakSelf handleWebRtcMsgResponseBy:buffer.data];
  930. });
  931. }
  932. }
  933. -(void)didGetStats:(NSString*)peerName stats:(RTC_OBJC_TYPE(RTCStatisticsReport) *)stats
  934. {
  935. //HLog(@"didGetStats:%@",stats)
  936. if(!_didReportWebRtcOKType
  937. && (self.ConnectionState == RTCIceConnectionStateConnected ||self.ConnectionState == RTCIceConnectionStateCompleted)){
  938. [self reportWebRtcRePoportTypeIsChannel:YES withStats:stats withSessionId:_webRtcChannelSessionId withLogKey:@""];
  939. _didReportWebRtcOKType = YES;
  940. _didReportWebRtcFailType = NO;//链接成功后 失败要重新上报
  941. [_mediaStream setShouldGetStats:NO];
  942. }
  943. }
  944. -(void)onAuthResultFromPeerName:(NSString*)peerName code:(int)code descriptions:(NSString*)descriptions
  945. {
  946. //HLog(@"webRtc P2P onAuthResultFromPeerName")
  947. }
  948. - (void)connectionChange:(NSString*)peerName
  949. didChangeLocalCandidate:(RTC_OBJC_TYPE(RTCIceCandidate) *)local
  950. remoteCandidate:(RTC_OBJC_TYPE(RTCIceCandidate) *)remote
  951. lastReceivedMs:(int)lastDataReceivedMs
  952. changeReason:(NSString *)reason
  953. {
  954. //HLog(@"webRtc P2P didChangeLocalCandidate")
  955. [self reportWebRtcRePoportTypeIsChannel:YES withLocal:local remoteCandidate:remote withSessionId:self.webRtcChannelSessionId withLogKey:@""];
  956. }
  957. #pragma mark 检测盒子的心跳情况
  958. - (void)checkBoxHeartbeatFun
  959. {
  960. //2、点击系统升级弹框确定按钮后30分钟内不弹;
  961. NSNumber *preSecondNum = [HWDataManager getNumberWithKey:stringKeyAddSn(@"disenable_type1_BoxHeartbeat")];
  962. if(preSecondNum){
  963. long seconds = [iTools getNowTimeStamp];
  964. if(seconds - preSecondNum.longValue <= 60*30){
  965. return;
  966. }
  967. }
  968. //3、重启、恢复出厂设备、首次设置密码 3个情况下8分钟之内不弹
  969. NSNumber *preSecondNum2 = [HWDataManager getNumberWithKey:stringKeyAddSn(@"disenable_type2_BoxHeartbeat")];
  970. if(preSecondNum2){
  971. long seconds = [iTools getNowTimeStamp];
  972. if(seconds - preSecondNum2.longValue <= 60*8){
  973. return;
  974. }
  975. }
  976. if(ksharedAppDelegate.didShowBoxHeartbeatAlertType
  977. ||[webRtcManager shareManager].isResetingType
  978. ||[webRtcManager shareManager].isRebootIngType
  979. || [ksharedAppDelegate didShowImageRenewViewFun]){
  980. return;
  981. }
  982. //1.隐私模式密码界面 3秒检测一次是否输入完成密码了
  983. if(ksharedAppDelegate.isDidShowPwdType){
  984. KWeakSelf
  985. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  986. [weakSelf checkBoxHeartbeatFun];
  987. });
  988. return;
  989. }
  990. // [[BoxHeartbeatAlertTool shareInstance] showBoxHeartbeatAlertFun:1 didClickBut:^(NSInteger tag) {
  991. //
  992. // }];
  993. if(ksharedAppDelegate.isWebSockLinkOKAginType && ksharedAppDelegate.cloudPhoneExtraFileListMod){
  994. return;
  995. }
  996. NSMutableDictionary *paraDict = [NSMutableDictionary new];
  997. KWeakSelf
  998. [[netWorkManager shareInstance] CommonGetWithCallBackCode:queryHeartbeat Parameters:paraDict success:^(id _Nonnull responseObject){
  999. queryHeartbeatModel *heartbeatMod = [[queryHeartbeatModel alloc] initWithDictionary:responseObject error:nil];
  1000. if(heartbeatMod && heartbeatMod.data){
  1001. [weakSelf checkBoxHeartbeatToShowPopViewFunBy:heartbeatMod];
  1002. }
  1003. } failure:^(NSError * _Nonnull error) {
  1004. }];
  1005. }
  1006. #pragma mark 根据盒子的心跳情况 显示异常谈了
  1007. - (void)checkBoxHeartbeatToShowPopViewFunBy:(queryHeartbeatModel*)heartbeatMod
  1008. {
  1009. if(!heartbeatMod || !heartbeatMod.data){
  1010. return;
  1011. }
  1012. if (//!heartbeatMod.data.frpStatus&&
  1013. !heartbeatMod.data.hostAgentStatus
  1014. &&!heartbeatMod.data.containerAgentStatus) {//全部异常
  1015. [[BoxHeartbeatAlertTool shareInstance] showBoxHeartbeatAlertFun:1 didClickBut:^(NSInteger tag) {
  1016. }];
  1017. }
  1018. else if (heartbeatMod.data.hostAgentStatus
  1019. //&& !heartbeatMod.data.frpStatus
  1020. && !heartbeatMod.data.containerAgentStatus) {//frp异常
  1021. [[BoxHeartbeatAlertTool shareInstance] showBoxHeartbeatAlertFun:2 didClickBut:^(NSInteger tag) {
  1022. }];
  1023. }
  1024. }
  1025. #pragma mark 显示重启成功 type:1 云手机 2 盒子
  1026. - (void)showReStartSucFun:(NSInteger)type
  1027. {
  1028. //1.隐私模式密码界面 3秒检测一次是否输入完成密码了
  1029. if(ksharedAppDelegate.isDidShowPwdType){
  1030. KWeakSelf
  1031. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  1032. [weakSelf showReStartSucFun:type];
  1033. });
  1034. return;
  1035. }
  1036. [self closeBoxHeartbeatReStartViewFun];
  1037. BoxHeartbeatReStartSucView *view = [[BoxHeartbeatReStartSucView alloc] initWithType:type];
  1038. [ksharedAppDelegate.window addSubview:view];
  1039. [view mas_makeConstraints:^(MASConstraintMaker *make) {
  1040. make.left.mas_equalTo(0.f);
  1041. make.bottom.mas_equalTo(0.f);
  1042. make.right.mas_equalTo(0.f);
  1043. make.top.mas_equalTo(0.f);
  1044. }];
  1045. }
  1046. #pragma mark 关闭心跳弹框
  1047. - (void)closeBoxHeartbeatReStartViewFun
  1048. {
  1049. NSArray * subViews = ksharedAppDelegate.window.subviews;
  1050. for (BoxHeartbeatReStartView*view in subViews) {
  1051. if([view isKindOfClass:[BoxHeartbeatReStartView class]]
  1052. || [view isKindOfClass:[customRestartingBoxView class]]){
  1053. [view colseFun];
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. @end