wifi绑定的逻辑

This commit is contained in:
祝发冬 2023-08-20 18:18:23 +08:00
parent e103d0e5ed
commit 6131f5ad5e
1 changed files with 205 additions and 78 deletions

View File

@ -32,16 +32,24 @@
#import "ConnectHotpotViewController.h"
#import "ConAquarNowWiFiPwdVC.h"
#import "InfoByImageViewController.h"
#import "YooseeNextConnectViewController.h"
#import "HKPieChartView.h"
#import "dataContorl.h"
#import<SystemConfiguration/CaptiveNetwork.h>
#import "IFishHotpotUDPHelper.h"
#import "ESPTouchResult.h"
#import "ESPTouchTask.h"
#import "IfishDataUnity.h"
#import "MyMD5.h"
#import <GWP2P/GWP2P.h>
typedef NS_ENUM(NSInteger,lodingViewdissMissStyle) {
lodingViewdissMissSuccsess=0,// default is 0
lodingViewdissMissfail,
lodingViewdissMissAlreadyBinded
};
@interface ConfigWifiViewController ()<UITextFieldDelegate>
@interface ConfigWifiViewController ()<IFishHotpotUDPHelperDelegate,UITextFieldDelegate>
@property (nonatomic, strong) NSCondition *_condition;
@property (atomic, strong) ESPTouchTask *_esptouchTask;
@property (nonatomic, strong) EspTouchDelegateImpl *_esptouchDelegate;
@ -56,11 +64,27 @@ typedef NS_ENUM(NSInteger,lodingViewdissMissStyle) {
@property(nonatomic,strong) UILabel *maclabel;
@property(nonatomic,strong)IfishConnectingView *connectingView;
@property (nonatomic, assign) BOOL isGoChangeWiFi;
@property (nonatomic, strong) JMAirKissConnection *airKissConnection; ;
Strong CLLocationManager *locationManager;//iOS13,WiFi
Strong NSMutableString *trackString;
Strong MBProgressHUD *HUD;
Strong NSTimer *timer;
Strong UITapGestureRecognizer*tap;
/**
mac
*/
Copy NSString *macAddress;
/**
*/
Assign BOOL isBindingDevice;
Strong NSTimer *bindTimer;
Assign NSInteger retryTimes;
@end
//extern BOOL formLogIn;//
extern BOOL isfromCameraView;
@ -189,6 +213,21 @@ extern BOOL isfromCameraView;
[self.navigationController pushViewController:info animated:YES];
}
//ap
-(void)goSettingClick:(UIButton*)button
{
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url]) {
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}else {
[[UIApplication sharedApplication] openURL:url];
}
self.isGoChangeWiFi = YES;
}
}
-(void)creatConnectStateUI{
@ -199,75 +238,17 @@ extern BOOL isfromCameraView;
-(void)connectNormalView
{
if (!self.connectingView) {
CGFloat yOffset = 0;
if (is_iPhone_X) {
yOffset = 88;
} else {
yOffset = 64;
}
IfishConnectingView *connectingView=[[IfishConnectingView alloc] initWithFrame:self.connectTipLbl.frame];
self.connectingView =connectingView;
[self.connectingView.textChangeView animationWithTexts:[NSArray arrayWithObjects:@"正在拼命连接中...",@"请将水族箱放在wifi信号良好的位置",@"连接成功后过几秒才能看到温度哟",@"连接不上,请点击下方说明书",nil]];
//self.connectingView.backgroundColor = [UIColor redColor];
self.connectingView.alpha = 0;
[self.view addSubview:connectingView];
self.wifiNamelabel.hidden = NO;
self.wifiTextFiled.hidden = NO;
self.connectTipLbl.hidden = NO;
self.connectBtn.hidden = NO;
self.connectTypeBtn.hidden = NO;
}else{
[UIView animateWithDuration:2.0 animations:^{
self.connectingView.alpha = 0;
//self.connectingView.hidden = YES;
} completion:^(BOOL finished) {
[self.connectingView.textChangeView stopAnimation];
self.wifiNamelabel.hidden = NO;
self.wifiTextFiled.hidden = NO;
self.connectTipLbl.hidden = NO;
self.connectBtn.hidden = NO;
self.connectTypeBtn.hidden = NO;
}];
}
self.connectingView.hidden=YES;
self.connectBtn.hidden = NO;
[self.indicator stopAnimating];
}
-(void)connectNotNormalView{
[UIView animateWithDuration:2.0 animations:^{
self.connectingView.alpha = 1;
//self.connectingView.hidden = NO;
} completion:^(BOOL finished) {
[self.connectingView.textChangeView startTextAnimation];
}];
// self.wifiNamelabel.hidden = YES;
// self.wifiTextFiled.hidden = YES;
self.connectTipLbl.hidden = YES;
-(void)connectNotNormalView
{
self.connectingView.hidden=NO;
[self.indicator startAnimating];
self.connectBtn.hidden = YES;
self.connectTypeBtn.hidden = YES;
}
-(void)initMacIdString:(NSString *)ssid{
@ -692,6 +673,127 @@ extern BOOL isfromCameraView;
}
#pragma mark ap
-(void)viewWillEnterForeground:(NSNotification *)noti{
if (self.isGoChangeWiFi) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.35 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
[self checkWiFi];
});
}
}
-(void)checkWiFi{
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
NSString *wifiName = SSID;
//wifiwifiwifi
if ([wifiName isEqualToString:self.wifiNamelabel.text]) {
[self.view makeToast:@"请先连接设备热点"];
return;
}
[self.timer invalidate];
self.timer = nil;
[self sendToDeviceWithSSIDName:self.ssid andSSIDPWD:self.wifiPass];
// // Do any additional setup after loading the view.
self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}];
}
- (void)sendToDeviceWithSSIDName:(NSString *)name andSSIDPWD:(NSString *)pwd {
[IFishHotpotUDPHelper sharedInstance].delegate = self;
[[IFishHotpotUDPHelper sharedInstance] broadCastHotspotConnectCommandWith:name pwd:pwd];
}
#pragma mark - IFishHotpotUDPHelperDelegate
//
- (void)udpHelperCommandExecutedSuccess:(IFishUDPHelperBackMsgModel *)backModel {
self.ssid = backModel.senderMacAddress;
[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]];
self.macAddress = backModel.senderMacAddress;
[IFishHotpotUDPHelper sharedInstance].delegate = nil;
[[IFishHotpotUDPHelper sharedInstance] broadCastRestartCommand];
//
self.bindTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startBinding) userInfo:nil repeats:YES];
}
- (void)startBinding {
[CommonUtils getNetworkTypeComplete:^(NSString *netconnType, NSString *BSSID, NSString *SSID) {
if ([netconnType isEqualToString:@"no network"]||([netconnType isEqualToString:@"Wifi"]&&[SSID.lowercaseString hasPrefix:@"ifish"]))
{
//
}
else
{
if (self.isBindingDevice||self.storeNameView) {
return;
}
if (self.macAddress.length == 0) {
return;
}
self.retryTimes = 0;
if (self.deviceType==DEVICEPETS)
{
[self showStoreNameView];
}
else
{
[self bindDeviceWithSsid:self.macAddress];
}
}
}];
}
- (void)udpHelperMessage:(NSString *)msg {
if (!self.timer)
{
return;
}
[MBProgressHUD hideHUDForView:self.view animated:YES];
self.HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.HUD.mode = MBProgressHUDModeIndeterminate;
self.HUD.labelText = msg;
if ([msg containsString:@"Socket连接失败"])
{
[self.HUD hide:YES afterDelay:3];
if (self.retryTimes<10)
{
self.retryTimes++;
WEAK_SELF;
[[IFishHotpotUDPHelper sharedInstance] closeComplete:^{
[weakSelf sendToDeviceWithSSIDName:weakSelf.ssid andSSIDPWD:weakSelf.wifiPass];
}];
}
else
{
;
}
}
}
- (void)appendTrakContent:(NSString *)msg
{
}
#pragma mark Ap
- (NSArray *) executeForResults
{
[self._condition lock];
@ -768,12 +870,21 @@ extern BOOL isfromCameraView;
#pragma mark
-(void)bindDeviceWithSsid:(NSString*)bssid{
NSLog(@"设备bssid_%@",bssid);
NSLog(@"设备bssid_%@",bssid);
if (self.isBindingDevice) {
return;
}
else
{
self.isBindingDevice = YES;
if (self.bindTimer) {
[self.bindTimer invalidate];
self.bindTimer = nil;
}
}
self.HUD.labelText = @"正在绑定设备";
NSString *userId = [dataContorl dataControlGetUserIdInfo];
[AFHttpTool requestWihtMethod:RequestMethodTypePost url:@"" params:@{} success:^(id response) {
} failure:^(NSError *err) {
}];
NSString*url=[NSString stringWithFormat:@"%@%@",kBindDevice,userId];
NSMutableDictionary*bind=[[NSMutableDictionary alloc]initWithDictionary:@{@"macAddress":bssid}];
NSString* timestamp=[NSString stringWithFormat:@"%lld",[[NSDate date] timeIntervalSince1970]*1000];
@ -790,6 +901,8 @@ extern BOOL isfromCameraView;
[AFHttpTool requestWihtMethod:RequestMethodTypePost url:url params:bind success:^(id response) {
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error:nil];
NSLog(@"data:%@",resultDic[@"data"]);
self.isBindingDevice = NO;
self.macAddress = nil;
if ([resultDic[@"result"] isEqualToString:@"100"]) {
//
@ -888,10 +1001,24 @@ extern BOOL isfromCameraView;
} failure:^(NSError *err) {
//[self showTitle:@"" messsage:@"网络异常"];
[self.view makeToast:@"网络异常"];
self.bakbutton.userInteractionEnabled=YES;
self.isBindingDevice=NO;
if (self.retryTimes < 10) {
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.HUD.labelText = @"自动重连...";
[self bindDeviceWithSsid:self.macAddress];
self.retryTimes += 1;
});
} else {
self.tap.enabled=YES;
self.isBindingDevice = NO;
self.HUD.labelText = err.localizedDescription;
[self.HUD hide:YES afterDelay:2];
[self appendTrakContent:[NSString stringWithFormat:@"绑定网络请求错误:%@", err.localizedDescription]];
self.bakbutton.userInteractionEnabled=YES;
}
}];
}