// // connectDeviceManager.m // 隐私保护 // // Created by xd h on 2023/10/9. // //第三方 瑞云 的链接服务 #import "connectDeviceManager.h" #import #import "WHPingTester.h" #import "RSATool.h" #import "errorAlertTool.h" @interface connectDeviceManager ()< GCDAsyncSocketDelegate, //RaylinkProxyDelegate, WHPingDelegate > { dispatch_queue_t initProxyQueue; WHPingTester *whPingTester; BOOL didRuiYunInitOkType; BOOL disconnected_cb_type;//瑞云回调链接断开了 BOOL didPingType;//是否ping过 BOOL didRuiyunLinkType;//是否瑞云链接通过 NSInteger RuiyunLinkSecond;//瑞云链接描述 } @property (nonatomic, strong) NSTimer *connectCheckTimer;//这个应该是检查初始化是否完成的 //@property (nonatomic, strong) GCDAsyncSocket *socket; // //@property (nonatomic, strong) GCDAsyncSocket *serverSocket; @property (nonatomic, assign) BOOL sdnConnected; @property (nonatomic, assign) BOOL peerConnected; @property (nonatomic, assign) NSInteger tcpPort; @property (nonatomic, copy) NSString *Pre_sdnId;//上一个sdn好 @property (nonatomic, assign) BOOL isGetThridMsgType; @end @implementation connectDeviceManager static connectDeviceManager *connectDeviceManagerInstance = nil; +(connectDeviceManager *)shareInstance; { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ connectDeviceManagerInstance = [[connectDeviceManager alloc] init]; connectDeviceManagerInstance.curConnectDeviceState = DeviceConnectUnknown; }); return connectDeviceManagerInstance; } #pragma mark 根据扫码的sn获取第三方信息 -(void)getThridMsgBySN:(NSString*)snStr needReconnect:(BOOL)needReconnect didNetEnd:(netWork_DidEndByOK)didNetEndIsOK { if(_isGetThridMsgType){ didNetEndIsOK(-1); return; } if(needReconnect){ _curConnectDeviceState = DeviceConnectGetThridMsging; //更换设备 // NSString *curSdnId = self.DeviceThirdIdMod.data.sdnId; // if(curSdnId){ // _Pre_sdnId = curSdnId; // } } NSMutableDictionary *paraDict = [NSMutableDictionary new]; [paraDict setValue:snStr forKey:@"sn"]; _isGetThridMsgType = YES; KWeakSelf [[netWorkManager shareInstance] CommonGetWithCallBackCode:getThirdIdBySn Parameters:paraDict success:^(id _Nonnull responseObject) { if(!needReconnect){ NSError *parseError; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:responseObject options:NSJSONWritingPrettyPrinted error:&parseError]; if (parseError) { //解析出错 } NSString * str = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; [cachesFileManager writeLogsWithMsg:str]; } self->_isGetThridMsgType = NO; DeviceThirdIdModel *model = [[DeviceThirdIdModel alloc] initWithDictionary:responseObject error:nil]; if(model.status == 0 && model.data) { weakSelf.DeviceThirdIdMod = model; NSString*desPwdstr = [RSATool AES128Decrypt:weakSelf.DeviceThirdIdMod.data.password key:AESCODEKEEYY]; didNetEndIsOK(YES); /*密码保持在本地*/ NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:deviceDict]; [dict setObject:desPwdstr forKey:Const_Have_Add_Device_PWD]; NSString *sdnid = model.data.sdnId; if(sdnid){ [dict setObject:sdnid forKey:Const_Have_Add_Device_sdnid]; } [HWDataManager setObjectWithKey:Const_Have_Add_Device_Privacy_Mode value:[NSNumber numberWithBool:model.data.isPrivacyMode]]; [HWDataManager setObjectWithKey:Const_Have_Add_Device value:dict]; // bool connected = 1;// [RaylinkProxy.sharedManager isSdnConnected]; if(!connected || needReconnect){ self->didPingType = NO; self->didRuiyunLinkType = NO; self->_isPingOk = NO; self->_tcpPortStr = nil; self->_tcpPort = 0; weakSelf.curConnectDeviceState = DeviceConnectGetThridOK; NSString *ipStr = weakSelf.DeviceThirdIdMod.data.ip; [weakSelf startPingDeviceIp:ipStr]; [weakSelf initRuiyunSDKFun]; } } else{ if(model.status == 201||model.status == 202){ didNetEndIsOK(model.status); } else{ didNetEndIsOK(2); } } } failure:^(NSError * _Nonnull error) { HLog("网络报错"); self->_isGetThridMsgType = NO; didNetEndIsOK(NO); }]; } #pragma mark 刷新扫码的sn获取第三方信息 是否需要重连 -(void)RefreshThridMsg { /*先判断本地有无设备 无设备时需要先扫码添加设备*/ NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device]; if (deviceDict && [[deviceDict allKeys] containsObject:Const_Have_Add_Device_SN]){ //有设备了先去做链接准备 // 80bec9c5 NSString *SNStr = deviceDict[@"Const_Have_Add_Device_SN"]; if(SNStr){ [[connectDeviceManager shareInstance] getThridMsgBySN:SNStr needReconnect:NO didNetEnd:^(NSInteger didSuc) { }]; } } } #pragma mark 初始化瑞云的SDK等 - (void)initRuiyunSDKFun{ if(didRuiYunInitOkType){ [self onConnectFun]; return; } HLog(@"initRuiyunSDKFun"); self.sdnConnected = NO; self.peerConnected = NO; //self.sendBtn.enabled = NO; //self.sdnConnectStateLab.text = @"SDN state: Connectting"; //self.sdnIDLab.text = @"My SDN ID:"; //RaylinkProxy.sharedManager.delegate = self; initProxyQueue = dispatch_queue_create("init_proxy", 0); NSURL *logUrl = [[NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject URLByAppendingPathComponent:@"logs"]; if (![NSFileManager.defaultManager fileExistsAtPath:logUrl.path]) { [NSFileManager.defaultManager createDirectoryAtURL:logUrl withIntermediateDirectories:YES attributes:NULL error:NULL]; } KWeakSelf dispatch_async(initProxyQueue, ^{ NSData *sdnInfo = [NSData dataWithContentsOfURL:[[NSBundle.mainBundle bundleURL] URLByAppendingPathComponent:@"planet.1ali_3ry_peer"]]; /// 1. 初始化代理库 //[RaylinkProxy.sharedManager initProxy:logUrl.path rootSdnInfo:sdnInfo]; self->didRuiYunInitOkType = YES; /// 启动定时器监听 SND 连接状态 self.connectCheckTimer = [NSTimer scheduledTimerWithTimeInterval:0.5 repeats:YES block:^(NSTimer * _Nonnull timer) { /// 2. 监听SDN 连接状态 bool connected = 1;// [RaylinkProxy.sharedManager isSdnConnected]; if (self.sdnConnected == connected) { return; } self.sdnConnected = connected; if (self.sdnConnected) { [weakSelf onConnectFun]; [self->_connectCheckTimer invalidate]; //self.sdnConnectStateLab.text = @"SDN state: Connected"; //self.sdnIDLab.text = [@"My SDN ID: " stringByAppendingString:[RaylinkProxy.sharedManager getSdnId]]; } else { //self.sdnConnectStateLab.text = @"SDN state: Connectting"; } }]; [[NSRunLoop currentRunLoop] addTimer:self.connectCheckTimer forMode:NSRunLoopCommonModes]; [[NSRunLoop currentRunLoop] run]; }); /// 作为服务端时,启动端口监听 // self.serverSocket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; // // if ([self.serverSocket acceptOnPort:0 error:NULL]) { // //self.serverPortLab.text = [NSString stringWithFormat: @"server port: %d", self.serverSocket.localPort]; // NSLog(@"accept ok %d",self.serverSocket.localPort); // } } - (void)onConnectFun{ if (self.peerConnected == YES && _Pre_sdnId && ![_Pre_sdnId isEqualToString:self.DeviceThirdIdMod.data.sdnId]) { //[self disconnect]; //return; } RuiyunLinkSecond = 0; _curConnectDeviceState = DeviceConnectDeciceing; NSString *curSdnId = self.DeviceThirdIdMod.data.sdnId; //@"3dfe7c1f"; if(!curSdnId){ NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device]; if(deviceDict && [[deviceDict allKeys] containsObject:Const_Have_Add_Device_sdnid]) { curSdnId = deviceDict[Const_Have_Add_Device_sdnid]; } } /// 3. 根据对端的 SDN ID 创建连接 //[RaylinkProxy.sharedManager createNewConnection:curSdnId]; //self.socket = [[GCDAsyncSocket alloc] initWithDelegate:self delegateQueue:dispatch_get_main_queue()]; //[self.connectBtn setEnabled:NO]; //[self.connectBtn setTitle:@"连接中..." forState:UIControlStateDisabled]; //KWeakSelf dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(30 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if(!self->didRuiyunLinkType){ //弹框瑞云链接失败 [[errorAlertTool shareInstance] showNetErrorAlertFun:11 didClickBut:^(NSInteger tag) { }]; } }); } - (void)disconnect { NSString *curSdnId = self.DeviceThirdIdMod.data.sdnId; if(_Pre_sdnId){ curSdnId = _Pre_sdnId; } if(curSdnId){ // [RaylinkProxy.sharedManager closeConnection:curSdnId]; // [RaylinkProxy.sharedManager closeHttpService:curSdnId]; //[self.connectBtn setTitle:@"连接" forState:UIControlStateNormal]; self.peerConnected = NO; } } - (IBAction)onClickSend:(id)sender { // if (self.messageTF.text.length <= 0) { // return; // } //[self.socket writeData:[@"111" dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0]; } //- (void)socket:(GCDAsyncSocket *)sock didAcceptNewSocket:(GCDAsyncSocket *)newSocket { // // NSLog(@"didAcceptNewSocket"); // // // [newSocket setDelegate:self delegateQueue:dispatch_get_main_queue()]; // // NSThread *revcThread = [[NSThread alloc] initWithBlock:^{ // // while (true) { // [newSocket readDataWithTimeout:-1 tag:0]; // [NSThread sleepForTimeInterval:0.1]; // } // }]; // // [revcThread start]; // //} - (void)on111ProxyConnected:(NSString *)sdnId status:(int)status { } - (void)On22ProxyDisconnected_cb:(NSString *)sdnId { } //瑞云sock 断开了 需要重连 - (void)tryReconnectFun{ [cachesFileManager writeLogsWithMsg:@"tryReconnect"]; [cachesFileManager writeLogsWithMsg:@"click again try Net Work"]; [self getThridMsgBySN:self.DeviceThirdIdMod.data.changeSn needReconnect:NO didNetEnd:^(NSInteger didSuc) { NSString *tip = [[NSString alloc] initWithFormat:@"Net Work state:%ld",didSuc]; [cachesFileManager writeLogsWithMsg:tip]; }]; if(!disconnected_cb_type){ [cachesFileManager writeLogsWithMsg:@"tryReconnect return"]; return; } NSURL *logUrl = [[NSFileManager.defaultManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject URLByAppendingPathComponent:@"logs"]; if (![NSFileManager.defaultManager fileExistsAtPath:logUrl.path]) { [NSFileManager.defaultManager createDirectoryAtURL:logUrl withIntermediateDirectories:YES attributes:NULL error:NULL]; } // dispatch_queue_t reinitProxyQueue = dispatch_queue_create("reinit_proxy", 0); // // // [cachesFileManager writeLogsWithMsg:@"tryReconnect initProxy 0"]; // dispatch_async(reinitProxyQueue, ^{ // NSData *sdnInfo = [NSData dataWithContentsOfURL:[[NSBundle.mainBundle bundleURL] URLByAppendingPathComponent:@"planet.1ali_3ry_peer"]]; // // /// 1. 初始化代理库 // [cachesFileManager writeLogsWithMsg:@"tryReconnect initProxy 1"]; // self->disconnected_cb_type = NO; // self->_isReInitType = YES; // [RaylinkProxy.sharedManager initProxy:logUrl.path rootSdnInfo:sdnInfo]; // self->_isReInitType = NO; // [cachesFileManager writeLogsWithMsg:@"tryReconnect initProxy 2"]; // }); // //KWeakSelf // NSMutableDictionary *paraDict = [NSMutableDictionary new]; // if(self.DeviceThirdIdMod.data.sn){ // [paraDict setValue:self.DeviceThirdIdMod.data.sn forKey:@"sn"]; // } // // [cachesFileManager writeLogsWithMsg:@"tryReconnect CommonGetWithCallBackCode"]; // [[netWorkManager shareInstance] CommonGetWithCallBackCode:getThirdIdBySn Parameters:paraDict success:^(id _Nonnull responseObject) { // // dispatch_async(self->initProxyQueue, ^{ // NSData *sdnInfo = [NSData dataWithContentsOfURL:[[NSBundle.mainBundle bundleURL] URLByAppendingPathComponent:@"planet.1ali_3ry_peer"]]; // // /// 1. 初始化代理库 // [RaylinkProxy.sharedManager initProxy:logUrl.path rootSdnInfo:sdnInfo]; // [cachesFileManager writeLogsWithMsg:@"tryReconnect initProxy"]; // }); // // // } failure:^(NSError * _Nonnull error) { // HLog("网络报错"); // }]; } //音视频 链接返回-5的时候调用 - (void)recreateHttpServiceFun{ // [cachesFileManager writeLogsWithMsg:@"recreateHttpServiceFun"]; // NSString *sdnId = self.DeviceThirdIdMod.data.sdnId; // self.tcpPort = [RaylinkProxy.sharedManager createHttpService:sdnId]; // self.tcpPortStr = [[NSString alloc] initWithFormat:@"%ld",self.tcpPort]; // [[netWorkManager shareInstance] setAFHTTPSessionManagerFunBy:self.tcpPort]; // [RaylinkProxy.sharedManager addSdnId:sdnId ip:ruiyunlinkIp allowPort:9300]; // [RaylinkProxy.sharedManager addSdnId:sdnId ip:ruiyunlinkIp allowPort:9100]; // [RaylinkProxy.sharedManager addSdnId:sdnId ip:ruiyunlinkIp allowPort:9888]; } //- (void)socket:(GCDAsyncSocket *)sock didConnectToHost:(NSString *)host port:(uint16_t)port { // // NSLog(@"didConnectToHost %d", port); // // NSThread *thread = [[NSThread alloc] initWithBlock:^{ // while (true) { // // [self.socket readDataWithTimeout:-1 tag:0]; // [NSThread sleepForTimeInterval:0.1]; // // } // }]; // // [thread start]; // // /// 7. Socket 连接成功后,通过 socket 发送, self.peerPortTF 为对端服务端口 // //[sock writeData:[[NSString stringWithFormat:@"CONNECT 127.0.0.1:%@ HTTP/1.0\r\n\r\n", @"9100"] dataUsingEncoding:NSUTF8StringEncoding] withTimeout:-1 tag:0]; // // NSLog(@"readDataToData"); // // [cachesFileManager writeLogsWithMsg:@"didConnectToHost writeData"]; //} // //- (void)socket:(GCDAsyncSocket *)sock didWriteDataWithTag:(long)tag //{ // NSLog(@"socket:%p didWriteDataWithTag:%ld", sock, tag); //} // //- (void)socket:(GCDAsyncSocket *)sock didReadData:(NSData *)data withTag:(long)tag { // // // if (tag == 0) { // // NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // // /// 8. 收到包含 `HTTP/1.0 200`的字符串,则认为连接成功。可以正常给对端发消息了 // if ([str containsString:@"HTTP/1.0 200"]) { // NSLog(@"Connected"); // self.peerConnected = YES; //// self.connectBtn.enabled = YES; //// [self.connectBtn setTitle:@"断开连接" forState:UIControlStateNormal]; // } // // } // // //self.revcLab.text = [NSString stringWithFormat:@"接收:%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]]; //} #pragma mark 保持卡密码 -(void)updateCardInfoBySN:(NSString*)snStr withPwdStr:(NSString*)pwd didNetEnd:(netWork_DidEndByOK)didNetEndIsOK { NSMutableDictionary *paraDict = [NSMutableDictionary new]; [paraDict setValue:snStr forKey:@"sn"]; NSString* enPwdStr = [RSATool AES128Encrypt:pwd key:AESCODEKEEYY]; if(enPwdStr){ [paraDict setValue:enPwdStr forKey:@"password"]; } //KWeakSelf [[netWorkManager shareInstance] CommonPostCallBackCode:updateCardInfo Parameters:paraDict success:^(id _Nonnull responseObject) { SuperModel*model = [[SuperModel alloc] initWithDictionary:responseObject error:nil]; if(model && model.status == 0) { didNetEndIsOK(YES); ksharedAppDelegate.DeviceThirdIdMod.data.password = enPwdStr; /*密码保持在本地*/ NSDictionary *deviceDict = [HWDataManager getObjectWithKey:Const_Have_Add_Device]; NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithDictionary:deviceDict]; [dict setObject:pwd forKey:Const_Have_Add_Device_PWD]; [HWDataManager setObjectWithKey:Const_Have_Add_Device value:dict]; } else{ didNetEndIsOK(NO); } } failure:^(NSError * _Nonnull error) { HLog("网络报错"); didNetEndIsOK(NO); }]; } #pragma mark ---ping-----站点 判断是否是局域网 - (void)startPingDeviceIp:(NSString *)roomInternetIp { //roomInternetIp = @"www.baidu.com"; if(!roomInternetIp || roomInternetIp.length < 7){ self.isPingOk = NO; return; } if (self->whPingTester) { [self->whPingTester stopPing]; self->whPingTester = nil; } self->whPingTester = [[WHPingTester alloc] initWithHostName:roomInternetIp]; self->whPingTester.delegate = self; //self->whPingTester.countdownTime = 1; //1秒ping一次 [self->whPingTester startPing]; /// 启动定时器监听 10秒后 whPingTester的状态 KWeakSelf dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if(self->whPingTester){ [weakSelf didGetPingStateIsOK:NO]; } }); } #pragma mark ---ping---- 代理回调 - (void) didPingSucccessWithTime:(float)time withError:(NSError*) error { HLog(@"time:%f \n error: %@",time,error) //if(error.code == 111 || time > 1000) //超时或者延时超过100ms,就要记录数据 if(error != nil) { [self didGetPingStateIsOK:NO]; } else if(time > 0){ [self didGetPingStateIsOK:YES]; //test code //[self didGetPingStateIsOK:NO]; } } - (void) didPingfialewithError:(NSError*)error { HLog(@" error error: %@",error) [self didGetPingStateIsOK:NO]; } -(void)didGetPingStateIsOK:(BOOL)isOk { self.isPingOk = isOk; [self->whPingTester stopPing]; self->whPingTester = nil; didPingType = YES; [self beginLinkWebSocketFun]; } - (void)beginLinkWebSocketFun { if(!didPingType){ return; } //局域网不通 瑞云还没连接好 if(!self.isPingOk && !didRuiyunLinkType){ return; } //切换设备 // if([webSocketManager shareInstance].commandChannelManager){ // //// [[webSocketManager shareInstance].commandChannelManager rc_close]; //// [webSocketManager shareInstance].commandChannelManager = nil; // ksharedAppDelegate.isWebSockLinkOKAginType = NO; // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // if([connectDeviceManager shareInstance].isFirstInputPwdDone){ // [[webSocketManager shareInstance] opencommandChannelManagerrc_openURL]; // } // }); // } // else{//第一次启动 // if([connectDeviceManager shareInstance].isFirstInputPwdDone){ // [[webSocketManager shareInstance] opencommandChannelManagerrc_openURL]; // } // } } @end