优化连接过程
This commit is contained in:
parent
ff26073344
commit
d0e290bea6
|
|
@ -37,6 +37,28 @@
|
|||
}
|
||||
[IFishHotpotUDPHelper sharedInstance].delegate = self;
|
||||
[[IFishHotpotUDPHelper sharedInstance] broadCastRestCommand];
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
BOOL networkOk = NO;
|
||||
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
|
||||
if (![[strongSelf currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
networkOk = YES;
|
||||
}
|
||||
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
|
||||
networkOk = YES;
|
||||
}
|
||||
if (!networkOk) {
|
||||
return;
|
||||
}
|
||||
if (strongSelf.isDeleteingDevice) {
|
||||
return;
|
||||
}
|
||||
strongSelf.isDeleteingDevice = YES;
|
||||
[strongSelf deletDevice];
|
||||
}];
|
||||
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
|
||||
}
|
||||
- (IBAction)tipBtnClicked:(id)sender {
|
||||
ConnectHotspotTipViewController *vc = [[ConnectHotspotTipViewController alloc] initWithNibName:nil bundle:nil];
|
||||
|
|
@ -46,39 +68,14 @@
|
|||
#pragma mark - IFishHotpotUDPHelperDelegate
|
||||
|
||||
- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel {
|
||||
self.HUD.labelText = @"设备即将重置";
|
||||
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
|
||||
NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
|
||||
[deviceArry enumerateObjectsUsingBlock:^(DeviceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
||||
if ([obj.macAddress isEqualToString:backModel.senderMacAddress]) {
|
||||
self.currentDevice = obj;
|
||||
self.HUD.labelText = @"请切当前Ifish-xxxx到其他网络,以进行后续操作";
|
||||
*stop = YES;
|
||||
}
|
||||
}];
|
||||
|
||||
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
||||
BOOL networkOk = NO;
|
||||
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
|
||||
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
networkOk = YES;
|
||||
}
|
||||
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
|
||||
networkOk = YES;
|
||||
}
|
||||
if (!networkOk) {
|
||||
return;
|
||||
}
|
||||
if (self.isDeleteingDevice) {
|
||||
return;
|
||||
}
|
||||
self.isDeleteingDevice = YES;
|
||||
[self deletDevice];
|
||||
}];
|
||||
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
|
||||
});
|
||||
|
||||
|
||||
self.HUD.labelText = @"设备正在恢复出厂设置";
|
||||
NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
|
||||
[deviceArry enumerateObjectsUsingBlock:^(DeviceModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
|
||||
if ([obj.macAddress isEqualToString:backModel.senderMacAddress]) {
|
||||
self.currentDevice = obj;
|
||||
*stop = YES;
|
||||
}
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)udpHelperMessage:(NSString *)msg {
|
||||
|
|
@ -103,7 +100,7 @@
|
|||
}
|
||||
|
||||
-(void)deletDevice{
|
||||
self.HUD.labelText = @"开始删除设备";
|
||||
self.HUD.labelText = @"正在删除设备";
|
||||
//先判断此设备是否有关联 摄像头
|
||||
//DeviceModel*deviceModel=[_deviceArr objectAtIndex:indexpath.row];
|
||||
//要删除的设设备id
|
||||
|
|
@ -132,7 +129,7 @@
|
|||
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
|
||||
NSLog(@"result:%@",resultDic[@"result"]);
|
||||
if ([resultDic[@"result"] isEqualToString:@"100"]) {
|
||||
self.HUD.labelText = @"删除成功";
|
||||
self.HUD.labelText = @"设备删除成功";
|
||||
//重置数据库
|
||||
NSArray*deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
|
||||
NSMutableArray *newArr = [NSMutableArray arrayWithArray:deviceArry];
|
||||
|
|
@ -143,8 +140,9 @@
|
|||
}
|
||||
}
|
||||
[[DataCenter defaultDtacenter] setValue: newArr forKey:@"deviceInfo"];
|
||||
[self.navigationController popViewControllerAnimated:YES];
|
||||
}else if ([resultDic[@"result"] isEqualToString:@"101"]){
|
||||
self.HUD.labelText = @"删除失败";
|
||||
self.HUD.labelText = @"设备删除失败";
|
||||
}else if ([resultDic[@"result"] isEqualToString:@"301"]){
|
||||
self.HUD.labelText = @"请求验证失败,请重新登陆";
|
||||
}else if ([resultDic[@"result"] isEqualToString:@"302"]){
|
||||
|
|
|
|||
|
|
@ -158,20 +158,20 @@
|
|||
|
||||
- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel {
|
||||
self.ssid = backModel.senderMacAddress;
|
||||
|
||||
self.HUD.labelText = @"请切当前Ifish-xxxx到其他网络,以进行后续操作";
|
||||
[self.view resignFirstResponder];
|
||||
self.HUD.labelText = @"请稍后";
|
||||
self.trackString = [NSMutableString string];
|
||||
[self appendTrakContent:@"=================收到设备答复==================="];
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"答复状态码:%@", backModel.result]];
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"答复Mac地址:%@", backModel.senderMacAddress]];
|
||||
[IFishHotpotUDPHelper sharedInstance].delegate = nil;
|
||||
[[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand];
|
||||
self.macAddress = backModel.senderMacAddress;
|
||||
|
||||
__weak typeof(self) weakSelf = self;
|
||||
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
||||
__strong typeof(weakSelf) strongSelf = weakSelf;
|
||||
BOOL networkOk = NO;
|
||||
if (status == AFNetworkReachabilityStatusReachableViaWiFi) {
|
||||
if (![[self currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
if (![[strongSelf currentWifiSSID].lowercaseString hasPrefix:@"ifish"]) {
|
||||
networkOk = YES;
|
||||
}
|
||||
} else if (status == AFNetworkReachabilityStatusReachableViaWWAN) {
|
||||
|
|
@ -180,16 +180,19 @@
|
|||
if (!networkOk) {
|
||||
return;
|
||||
}
|
||||
if (self.isBindingDevice) {
|
||||
if (strongSelf.isBindingDevice) {
|
||||
return;
|
||||
}
|
||||
if (self.macAddress.length == 0) {
|
||||
if (strongSelf.macAddress.length == 0) {
|
||||
return;
|
||||
}
|
||||
self.isBindingDevice = YES;
|
||||
[self bindDeviceWithSsid:self.macAddress];
|
||||
strongSelf.isBindingDevice = YES;
|
||||
[strongSelf bindDeviceWithSsid:self.macAddress];
|
||||
}];
|
||||
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
|
||||
|
||||
[IFishHotpotUDPHelper sharedInstance].delegate = nil;
|
||||
[[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand];
|
||||
}
|
||||
|
||||
- (void)udpHelperMessage:(NSString *)msg {
|
||||
|
|
@ -203,7 +206,7 @@
|
|||
NSLog(@"设备bssid_%@",bssid);
|
||||
[self appendTrakContent:@"=================进入绑定函数================="];
|
||||
NSString *userId = [dataContorl dataControlGetUserIdInfo];
|
||||
self.HUD.labelText = @"开始绑定设备";
|
||||
self.HUD.labelText = @"正在绑定设备";
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"绑定参数 userID:%@", userId]];
|
||||
[self appendTrakContent:[NSString stringWithFormat:@"绑定参数 macaddress:%@", bssid]];
|
||||
[AFHttpTool bindDeviceWithUserId:userId macAddress:bssid success:^(id response) {
|
||||
|
|
|
|||
|
|
@ -21,8 +21,6 @@
|
|||
NSString *_pwd;
|
||||
}
|
||||
|
||||
@property (nonatomic, assign) BOOL backMessageReceived;
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
|
@ -35,60 +33,55 @@
|
|||
dispatch_once(&onceToken, ^{
|
||||
sharedInstace = [[self alloc] init];
|
||||
});
|
||||
|
||||
return sharedInstace;
|
||||
}
|
||||
|
||||
- (instancetype)init {
|
||||
if (self = [super init]) {
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
return self;
|
||||
}
|
||||
|
||||
- (void)broadCastHotspotConnectCommandWith:(NSString *)ssidName pwd:(NSString *)pwd {
|
||||
_ssidName = ssidName;
|
||||
_pwd = pwd;
|
||||
[self postmsg:@"命令已发出"];
|
||||
self.backMessageReceived = NO;
|
||||
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendConnectHotspotSoketdata) userInfo:nil repeats:YES];
|
||||
[self sendConnectHotspotSoketdata];
|
||||
}
|
||||
|
||||
- (void)broadCastRestCommand {
|
||||
[self postmsg:@"命令已发出"];
|
||||
self.backMessageReceived = NO;
|
||||
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendRestSocketData) userInfo:nil repeats:YES];
|
||||
[self sendRestSocketData];
|
||||
}
|
||||
|
||||
- (void)sendRestSocketData {
|
||||
NSString* hostS = @"192.168.4.1";
|
||||
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
//120.55.190.56 服务器IP 指令中用
|
||||
ResetDeviceModel *model = [ResetDeviceModel new];
|
||||
NSString *request=[NSString stringWithFormat:@"%@", model.description];
|
||||
|
||||
NSData *data=[dataContorl stringToHexData:request];
|
||||
|
||||
UInt16 port = 333;
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
|
||||
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
|
||||
- (void)broadCastRestartCommand {
|
||||
[self postmsg:@"命令已发出"];
|
||||
self.backMessageReceived = NO;
|
||||
self.loopTimer = [NSTimer scheduledTimerWithTimeInterval:K_ReSendTimeInterval target:self selector:@selector(sendRestartSocketData) userInfo:nil repeats:YES];
|
||||
|
||||
[self sendRestSocketData];
|
||||
}
|
||||
|
||||
- (void)sendRestartSocketData {
|
||||
NSString* hostS = @"192.168.4.1";
|
||||
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
//120.55.190.56 服务器IP 指令中用
|
||||
RestartDeviceModel *model = [RestartDeviceModel new];
|
||||
NSString *request=[NSString stringWithFormat:@"%@", model.description];
|
||||
|
|
@ -96,24 +89,13 @@
|
|||
NSData *data=[dataContorl stringToHexData:request];
|
||||
|
||||
UInt16 port = 333;
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
|
||||
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
|
||||
-(void)sendConnectHotspotSoketdata{
|
||||
NSString* hostS = @"192.168.4.1";
|
||||
|
||||
self.clientSocket = [[AsyncUdpSocket alloc] initWithDelegate:self];
|
||||
//120.55.190.56 服务器IP 指令中用
|
||||
ConnectHotspotModel *model = [ConnectHotspotModel new];
|
||||
model.ssidName = _ssidName;
|
||||
|
|
@ -123,27 +105,14 @@
|
|||
NSData *data=[dataContorl stringToHexData:request];
|
||||
|
||||
UInt16 port = 333;
|
||||
UInt16 bindPort = 9954;
|
||||
|
||||
NSError *error;
|
||||
|
||||
[self.clientSocket enableBroadcast:YES error:&error];
|
||||
|
||||
[self.clientSocket sendData:data toHost:hostS port:port withTimeout:-1 tag:1];
|
||||
|
||||
//本地接收端口
|
||||
[self.clientSocket bindToPort:bindPort error:&error];
|
||||
|
||||
[self.clientSocket receiveWithTimeout:-1 tag:0];
|
||||
}
|
||||
|
||||
|
||||
-(BOOL)onUdpSocket:(AsyncUdpSocket *)sock didReceiveData:(NSData *)data withTag:(long)tag fromHost:(NSString *)host port:(UInt16)port
|
||||
{
|
||||
if (self.backMessageReceived) {
|
||||
return YES;
|
||||
}
|
||||
self.backMessageReceived = YES;
|
||||
[self.loopTimer invalidate];
|
||||
|
||||
NSString* result = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
|
||||
IFishUDPHelperBackMsgModel *model = [[IFishUDPHelperBackMsgModel alloc] initWithData:data];
|
||||
if ([model.result isEqualToString:@"01"]) {
|
||||
|
|
@ -154,15 +123,12 @@
|
|||
AppDelegate *app =(AppDelegate*)[UIApplication sharedApplication].delegate;
|
||||
[app.window makeToast:[NSString stringWithFormat:@"收到答复:%@", result]];
|
||||
}
|
||||
[self.clientSocket close];
|
||||
[self.loopTimer invalidate];
|
||||
return YES;
|
||||
|
||||
}
|
||||
|
||||
-(void)onUdpSocketDidClose:(AsyncUdpSocket *)sock{
|
||||
|
||||
[self.clientSocket close];
|
||||
[self.loopTimer invalidate];
|
||||
NSLog(@"关闭");
|
||||
}
|
||||
|
|
@ -179,7 +145,6 @@
|
|||
NSLog(@"dueToError%@",error);
|
||||
[self postmsg:error.localizedDescription];
|
||||
|
||||
[self.clientSocket close];
|
||||
[self.loopTimer invalidate];
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue