PlayerViewController+RBD.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //
  2. // CloudPhoneViewController+RBD.m
  3. // VclustersGemini
  4. //
  5. // Created by APPLE on 2021/8/16.
  6. // Copyright © 2021 APPLE. All rights reserved.
  7. //
  8. #import "PlayerViewController+RBD.h"
  9. #import "CloudPhoneModel.h"
  10. #import <RCCloudPhoneSDK/RCCommandChannel.h>
  11. #import "AppDelegate.h"
  12. #import "PhoneP2PModel.h"
  13. #import "P2PDataManager.h"
  14. #import "FLSocketManager.h"
  15. #import <objc/runtime.h>
  16. #import "UseAccountManage.h"
  17. #import "SafeForKey.h"
  18. @interface PlayerViewController(RBD)
  19. @end
  20. @implementation PlayerViewController(RBD)
  21. @dynamic commandChannelManager;
  22. @dynamic businessCommandChannelManager;
  23. - (void)setCommandChannelManager:(RCCommandChannelManager *)commandChannelManager {
  24. objc_setAssociatedObject(self, @selector(commandChannelManager), commandChannelManager, OBJC_ASSOCIATION_RETAIN);
  25. }
  26. - (RCCommandChannelManager *)commandChannelManager {
  27. return objc_getAssociatedObject(self, @selector(commandChannelManager));
  28. }
  29. - (void)setBusinessCommandChannelManager:(RCCommandChannelManager *)businessCommandChannelManager {
  30. objc_setAssociatedObject(self, @selector(businessCommandChannelManager), businessCommandChannelManager, OBJC_ASSOCIATION_RETAIN);
  31. }
  32. - (RCCommandChannelManager *)businessCommandChannelManager {
  33. return objc_getAssociatedObject(self, @selector(businessCommandChannelManager));
  34. }
  35. - (void)extensionPhoneGetRBD
  36. {
  37. [self showNewIndicator];
  38. self.deviceModel.startGetRBDTime = [[NSDate date] timeIntervalSince1970];
  39. self.deviceModel.chaofenResult = NO;
  40. [self toCheckChaofenStateForShowWithPhone];
  41. }
  42. - (void)toCheckChaofenStateForShowWithPhone
  43. {
  44. CloudPhoneModel *deviceDcit = self.deviceModel;
  45. self.rbdNotification = 2;
  46. /*超分接口调用*/
  47. [[UseAccountManage shareInstance] extensionCloudPhoneToGetDeviceStatesWithDeviceId:deviceDcit.phoneId coordinatesLat:@"" coordinatesLng:@"" success:^(id _Nonnull responseObject) {
  48. NSDictionary *dict = (NSDictionary *)responseObject;
  49. SuperModel *mod = [[SuperModel alloc] initWithDictionary:dict error:nil];
  50. NSDictionary *deviceDict = nil;
  51. if ((mod.status && mod.status.integerValue == 0) || (mod.status && mod.status.integerValue == 6001)) {
  52. if ([[dict allKeys] containsObject:@"data"])
  53. {
  54. deviceDict= [[NSDictionary alloc] initWithDictionary:[dict objectForKey:@"data"]];
  55. }
  56. }
  57. if (CODE == 0) {
  58. PhoneP2PModel *model = [[PhoneP2PModel alloc] initWithDictionary:deviceDict error:nil];
  59. if (model.phoneId != self.deviceModel.phoneId) {
  60. return;
  61. }
  62. if (model.phoneId != self.deviceModel.phoneId) {
  63. return;
  64. }
  65. model.phoneId = deviceDcit.phoneId;
  66. model.ctime = [[NSDate date] timeIntervalSince1970];
  67. [[P2PDataManager shareInstance] saveOrUpdateRecoderWithModel:model];
  68. HLog(@"\n----超分正常----");
  69. if ([[deviceDict allKeys] containsObject:@"sn"])
  70. {
  71. [deviceDcit setValue:[deviceDict objectForKey:@"sn"] forKey:@"sn"];
  72. }
  73. if ([[deviceDict allKeys] containsObject:@"internetVideoPort"])
  74. {
  75. [deviceDcit setValue:[deviceDict objectForKey:@"internetVideoPort"] forKey:@"internetVideoPort"];
  76. }
  77. if ([[deviceDict allKeys] containsObject:@"internetBusinessPort"])
  78. {
  79. [deviceDcit setValue:[deviceDict objectForKey:@"internetBusinessPort"] forKey:@"internetBusinessPort"];
  80. }
  81. if ([[deviceDict allKeys] containsObject:@"internetIp"])
  82. {
  83. [deviceDcit setValue:[deviceDict objectForKey:@"internetIp"] forKey:@"internetIp"];
  84. }
  85. if ([[deviceDict allKeys] containsObject:@"roomInternetIp"])
  86. {
  87. [deviceDcit setValue:[deviceDict objectForKey:@"roomInternetIp"] forKey:@"roomInternetIp"];
  88. }
  89. if ([[deviceDict allKeys] containsObject:@"vpnStatus"])
  90. {
  91. [deviceDcit setValue:[deviceDict objectForKey:@"vpnStatus"] forKey:@"vpnStatus"];
  92. }
  93. if ([[deviceDict allKeys] containsObject:@"virtualKeyStatus"])
  94. {
  95. [deviceDcit setValue:[deviceDict objectForKey:@"virtualKeyStatus"] forKey:@"virtualKeyStatus"];
  96. }
  97. if ([[deviceDict allKeys] containsObject:@"videoCode"])
  98. {
  99. [deviceDcit setValue:[deviceDict objectForKey:@"videoCode"] forKey:@"videoCode"];
  100. }
  101. if ([[deviceDict allKeys] containsObject:@"usageHabit"])
  102. {
  103. [deviceDcit setValue:[deviceDict objectForKey:@"usageHabit"] forKey:@"usageHabit"];
  104. }
  105. if ([[deviceDict allKeys] containsObject:@"deviceBoard"])
  106. {
  107. [deviceDcit setValue:[deviceDict objectForKey:@"deviceBoard"] forKey:@"deviceBoard"];
  108. }
  109. if ([[deviceDict allKeys] containsObject:@"internetHttps"])
  110. {
  111. [deviceDcit setValue:[deviceDict objectForKey:@"internetHttps"] forKey:@"internetHttps"];
  112. }
  113. if ([[deviceDict allKeys] containsObject:@"internetHttp"])
  114. {
  115. [deviceDcit setValue:[deviceDict objectForKey:@"internetHttp"] forKey:@"internetHttp"];
  116. }
  117. if ([[deviceDict allKeys] containsObject:@"localIp"])
  118. {
  119. [deviceDcit setValue:[deviceDict objectForKey:@"localIp"] forKey:@"localIp"];
  120. }
  121. if ([[deviceDict allKeys] containsObject:@"cardToken"])
  122. {
  123. [deviceDcit setValue:[deviceDict objectForKey:@"cardToken"] forKey:@"cardToken"];
  124. }
  125. if ([[deviceDict allKeys] containsObject:@"pushFlowCode"])
  126. {
  127. [deviceDcit setValue:[deviceDict objectForKey:@"pushFlowCode"] forKey:@"pushFlowCode"];
  128. }
  129. if ([[deviceDict allKeys] containsObject:@"resolvingPower"])
  130. {
  131. [deviceDcit setValue:[deviceDict objectForKey:@"resolvingPower"] forKey:@"resolvingPower"];
  132. }
  133. if ([[deviceDict allKeys] containsObject:@"high"])
  134. {
  135. [deviceDcit setValue:[deviceDict objectForKey:@"high"] forKey:@"high"];
  136. }
  137. if ([[deviceDict allKeys] containsObject:@"width"])
  138. {
  139. [deviceDcit setValue:[deviceDict objectForKey:@"width"] forKey:@"width"];
  140. }
  141. if ([[deviceDict allKeys] containsObject:@"dpi"])
  142. {
  143. [deviceDcit setValue:[deviceDict objectForKey:@"dpi"] forKey:@"dpi"];
  144. }
  145. if ([[deviceDict allKeys] containsObject:@"vpnMode"])
  146. {
  147. [deviceDcit setValue:[deviceDict objectForKey:@"vpnMode"] forKey:@"vpnMode"];
  148. }
  149. HLog(@"__________连接000000__________deviceModel.cardToken = %@ 新",self.deviceModel.cardToken);
  150. [self toConnectDeice];
  151. }else if(CODE == 5201 || CODE == 5200) {
  152. double nowTime = [[NSDate date] timeIntervalSince1970];
  153. if (nowTime - self.deviceModel.startGetRBDTime < 30 - 1)
  154. {
  155. if ([FLSocketManager shareManager].fl_socketStatus == FLSocketStatusReceived || [FLSocketManager shareManager].fl_socketStatus == FLSocketStatusConnected){
  156. self.chaofenDeviceModel = self.deviceModel;
  157. self.chaofenDeviceModel.chaofenPhoneIndex = self.phoneIndex;
  158. double nowTime = [[NSDate date] timeIntervalSince1970];
  159. double delayTime = 30 - (nowTime - self.deviceModel.startGetRBDTime);
  160. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  161. if (self.chaofenDeviceModel.chaofenResult == NO) {
  162. [self removeNewIndicator];
  163. [[iToast makeText:@"云机优化中,请稍后重试"] show];
  164. [self exitPlayerView];
  165. }
  166. });
  167. }else{
  168. /*继续询问服务器*/
  169. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  170. [self toCheckChaofenStateForShowWithPhone];
  171. });
  172. }
  173. }
  174. else
  175. {
  176. [self removeNewIndicator];
  177. [[iToast makeText:@"云机优化中,请稍后重试"] show];
  178. [self exitPlayerView];
  179. }
  180. }
  181. // else if(CODE == 5200) {
  182. // self.chaofenDeviceModel = deviceDcit;
  183. //
  184. // double nowTime = [[NSDate date] timeIntervalSince1970];
  185. // double delayTime = 30 - (nowTime - deviceDcit.startGetRBDTime);
  186. //
  187. // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delayTime * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  188. // if (self.chaofenDeviceModel.chaofenResult == NO) {
  189. // [self removeLoadingWithTipsView];
  190. // [[iToast makeText:@"云机优化中,请稍后重试"] show];
  191. // }
  192. // });
  193. // }
  194. else if (5203 == CODE){/*排队中*/
  195. NSString *msg = MSG.length > 0 ? MSG : @"正在排队中,请稍等";
  196. [[iToast makeText:msg] show];
  197. [self removeNewIndicator];
  198. [self exitPlayerView];
  199. }
  200. else {
  201. [self removeNewIndicator];
  202. [self exitPlayerView];
  203. [[iToast makeText:@"云手机连接超时,请稍后再试!"] show];
  204. }
  205. } faild:^(NSError * _Nonnull error) {
  206. [self removeNewIndicator];
  207. [self exitPlayerView];
  208. [[iToast makeText:@"云手机连接超时,请稍后再试!"] show];
  209. }];
  210. }
  211. - (void)toConnectDeice
  212. {
  213. self.canControl = YES;
  214. //清空WebSocket连接
  215. [self.commandChannelManager rc_close];
  216. [self.businessCommandChannelManager rc_close];
  217. /*传入IP与port*/
  218. self.ip = self.deviceModel.internetIp;
  219. self.internetVideoPort = self.deviceModel.internetVideoPort;
  220. self.vpnStatus = self.deviceModel.vpnStatus;
  221. self.internetHttp = self.deviceModel.internetHttp;
  222. if (self.deviceModel.width > 200 && self.deviceModel.high > 200) {
  223. ksharedAppDelegate.couldPhone_W_PHONE = self.deviceModel.width;
  224. ksharedAppDelegate.couldPhone_H_PHONE = self.deviceModel.high;
  225. }else{
  226. if ([self.deviceModel.resolvingPower isEqualToString:@"720"]) {
  227. ksharedAppDelegate.couldPhone_W_PHONE = 720;
  228. ksharedAppDelegate.couldPhone_H_PHONE = 1280;
  229. }else{
  230. ksharedAppDelegate.couldPhone_W_PHONE = 1080;
  231. ksharedAppDelegate.couldPhone_H_PHONE = 1920;
  232. }
  233. }
  234. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  235. //设置WebSocket
  236. //初始化指令通道
  237. [self opencommandChannelManagerrc_openURL];
  238. //初始化业务通道
  239. [self openbusinessCommandChannelManagerrc_openURL];
  240. });
  241. if(self.deviceModel.videoCode && [self.deviceModel.videoCode isEqualToString:@"H265"])
  242. {
  243. self.isSupportH265Type = YES;
  244. }
  245. else
  246. {
  247. self.isSupportH265Type = NO;
  248. }
  249. if ([self isEqual:self.navigationController.topViewController])
  250. {
  251. globalBlock(^{
  252. // 初始化成功后连接
  253. @synchronized (self) {
  254. if ([self initVideoServer])
  255. {
  256. [self connectVideoServer];
  257. }
  258. }
  259. });
  260. }
  261. }
  262. - (void)extensionRBDGetRbdDeviceNotification:(NSNotification *)notification
  263. {
  264. if (self.rbdNotification != 2) {
  265. return;
  266. }
  267. self.rbdNotification = 0;
  268. double nowTime = [[NSDate date] timeIntervalSince1970];
  269. NSDictionary *dict = [notification userInfo];
  270. if ([[dict allKeys] containsObject:@"data"])
  271. {
  272. NSDictionary *data = [dict objectForKey:@"data"];
  273. CloudPhoneModel *deviceModel = [[UseAccountManage shareInstance].cloudPhoneListModel.diskInfo objectAtIndex:self.chaofenDeviceModel.chaofenPhoneIndex];
  274. NSString *userCardIdStr = [NSString stringWithFormat:@"%@",[data safeStringForKey:@"userCardId"]];
  275. //cardState string 0:成功,1:失败
  276. NSString *cardStateStr = [NSString stringWithFormat:@"%@",[data safeStringForKey:@"cardState"]];
  277. if (userCardIdStr.intValue == self.chaofenDeviceModel.userCardId) { //当sn相同,userCardIdStr相同,表示同一个设备在进行修改
  278. self.chaofenDeviceModel.chaofenResult = YES;
  279. }
  280. //超分成功了,继续走成功的流程(比较sn, userCardId字段相等,且超分成功,时间在30S之内)
  281. if ([cardStateStr isEqualToString:@"0"] && ((nowTime - self.chaofenDeviceModel.startGetRBDTime) <= 30) && (userCardIdStr.intValue == self.chaofenDeviceModel.userCardId)) {
  282. PhoneP2PModel *model = [[PhoneP2PModel alloc] initWithDictionary:data error:nil];
  283. if (deviceModel.phoneId != model.phoneId) {
  284. return;
  285. }
  286. model.phoneId = deviceModel.phoneId;
  287. model.ctime = [[NSDate date] timeIntervalSince1970];
  288. [[P2PDataManager shareInstance] saveOrUpdateRecoderWithModel:model];
  289. // NSDictionary *dictLogin = [[UseAccountManage shareInstance] getLoginInfo];
  290. // if (dictLogin && [[dictLogin allKeys] containsObject:@"userName"])
  291. // {
  292. // /*判断数据库中有没有对应phoneid*/
  293. // NSString* username = [dictLogin objectForKey:@"userName"];
  294. // NSString * bg_tableName = P2PModelList_bg_tableName(username);
  295. // //先显示本地缓存数据
  296. // NSArray* finfAlls = [PhoneP2PListModel bg_findAll:bg_tableName];
  297. // if (finfAlls && finfAlls.count >0) {
  298. // PhoneP2PListModel *listMod = finfAlls[0];
  299. // BOOL haveFind = NO;
  300. // for (PhoneP2PModel *p2pModel in listMod.data) {
  301. // if (p2pModel.phoneId == deviceModel.phoneId){
  302. // PhoneP2PModel *model = [[PhoneP2PModel alloc] initWithDictionary:data error:nil];
  303. // model.ctime = [[NSDate date] timeIntervalSince1970];
  304. // model.phoneId = deviceModel.phoneId;
  305. // [listMod.data removeObject:p2pModel];
  306. // [listMod.data addObject:model];
  307. // [listMod bg_cover];
  308. // haveFind = YES;
  309. // break;
  310. // }
  311. // }
  312. // if (haveFind == NO) {
  313. // PhoneP2PModel *model = [[PhoneP2PModel alloc] initWithDictionary:data error:nil];
  314. // model.ctime = [[NSDate date] timeIntervalSince1970];
  315. // model.phoneId = deviceModel.phoneId;
  316. // [listMod.data addObject:model];
  317. // [listMod bg_cover];
  318. // }
  319. // }else{
  320. // PhoneP2PListModel *listmodel = [[PhoneP2PListModel alloc] init];
  321. // PhoneP2PModel *model = [[PhoneP2PModel alloc] initWithDictionary:data error:nil];
  322. // model.ctime = [[NSDate date] timeIntervalSince1970];
  323. // model.phoneId = deviceModel.phoneId;
  324. // listmodel.data = [[NSMutableArray alloc] initWithObjects:model, nil];
  325. // listmodel.bg_tableName = bg_tableName;
  326. // [listmodel bg_cover];
  327. // }
  328. // }
  329. //继续往下进入超分成功0的流程
  330. if ([[data allKeys] containsObject:@"sn"])
  331. {
  332. [deviceModel setValue:[data objectForKey:@"sn"] forKey:@"sn"];
  333. }
  334. if ([[data allKeys] containsObject:@"internetVideoPort"])
  335. {
  336. [deviceModel setValue:[data objectForKey:@"internetVideoPort"] forKey:@"internetVideoPort"];
  337. }
  338. if ([[data allKeys] containsObject:@"internetBusinessPort"])
  339. {
  340. [deviceModel setValue:[data objectForKey:@"internetBusinessPort"] forKey:@"internetBusinessPort"];
  341. }
  342. if ([[data allKeys] containsObject:@"internetIp"])
  343. {
  344. [deviceModel setValue:[data objectForKey:@"internetIp"] forKey:@"internetIp"];
  345. }
  346. if ([[data allKeys] containsObject:@"roomInternetIp"])
  347. {
  348. [deviceModel setValue:[data objectForKey:@"roomInternetIp"] forKey:@"roomInternetIp"];
  349. }
  350. if ([[data allKeys] containsObject:@"vpnStatus"])
  351. {
  352. [deviceModel setValue:[data objectForKey:@"vpnStatus"] forKey:@"vpnStatus"];
  353. }
  354. if ([[data allKeys] containsObject:@"virtualKeyStatus"])
  355. {
  356. [deviceModel setValue:[data objectForKey:@"virtualKeyStatus"] forKey:@"virtualKeyStatus"];
  357. }
  358. if ([[data allKeys] containsObject:@"videoCode"])
  359. {
  360. [deviceModel setValue:[data objectForKey:@"videoCode"] forKey:@"videoCode"];
  361. }
  362. if ([[data allKeys] containsObject:@"usageHabit"])
  363. {
  364. [deviceModel setValue:[data objectForKey:@"usageHabit"] forKey:@"usageHabit"];
  365. }
  366. if ([[data allKeys] containsObject:@"deviceBoard"])
  367. {
  368. [deviceModel setValue:[data objectForKey:@"deviceBoard"] forKey:@"deviceBoard"];
  369. }
  370. if ([[data allKeys] containsObject:@"internetHttps"])
  371. {
  372. [deviceModel setValue:[data objectForKey:@"internetHttps"] forKey:@"internetHttps"];
  373. }
  374. if ([[data allKeys] containsObject:@"internetHttp"])
  375. {
  376. [deviceModel setValue:[data objectForKey:@"internetHttp"] forKey:@"internetHttp"];
  377. }
  378. if ([[data allKeys] containsObject:@"localIp"])
  379. {
  380. [deviceModel setValue:[data objectForKey:@"localIp"] forKey:@"localIp"];
  381. }
  382. if ([[data allKeys] containsObject:@"cardToken"])
  383. {
  384. [deviceModel setValue:[data objectForKey:@"cardToken"] forKey:@"cardToken"];
  385. }
  386. if ([[data allKeys] containsObject:@"pushFlowCode"])
  387. {
  388. [deviceModel setValue:[data objectForKey:@"pushFlowCode"] forKey:@"pushFlowCode"];
  389. }
  390. if ([[data allKeys] containsObject:@"resolvingPower"])
  391. {
  392. [deviceModel setValue:[data objectForKey:@"resolvingPower"] forKey:@"resolvingPower"];
  393. }
  394. if ([[data allKeys] containsObject:@"high"])
  395. {
  396. [deviceModel setValue:[data objectForKey:@"high"] forKey:@"high"];
  397. }
  398. if ([[data allKeys] containsObject:@"width"])
  399. {
  400. [deviceModel setValue:[data objectForKey:@"width"] forKey:@"width"];
  401. }
  402. if ([[data allKeys] containsObject:@"dpi"])
  403. {
  404. [deviceModel setValue:[data objectForKey:@"dpi"] forKey:@"dpi"];
  405. }
  406. if ([[data allKeys] containsObject:@"vpnMode"])
  407. {
  408. [deviceModel setValue:[data objectForKey:@"vpnMode"] forKey:@"vpnMode"];
  409. }
  410. [self toConnectDeice];
  411. }
  412. else
  413. {
  414. [self removeNewIndicator];
  415. [self exitPlayerView];
  416. [[iToast makeText:@"云手机连接超时,请稍后再试!"] show];
  417. }
  418. }
  419. }
  420. @end