509 lines
19 KiB
Objective-C
509 lines
19 KiB
Objective-C
//
|
|
// ConnectWifiViewController.m
|
|
// Ifish
|
|
//
|
|
// Created by imac on 15/9/28.
|
|
// Copyright © 2015年 imac. All rights reserved.
|
|
//
|
|
|
|
#import "ConnectWifiViewController.h"
|
|
#import "LeftViewController.h"
|
|
#import "CenterViewController.h"
|
|
#import "ICSDrawerController.h"
|
|
#import <SystemConfiguration/CaptiveNetwork.h>
|
|
#import "AFNetworking.h"
|
|
#import "MyMD5.h"
|
|
#import "DeviceModel.h"
|
|
#import "AsyncSocket.h"
|
|
#import "baseModel.h"
|
|
#import "DeviceOnlineModel.h"
|
|
#import "SaySomethingViewController.h"
|
|
#import "ConnectErrorViewController.h"
|
|
#import "AppDelegate.h"
|
|
#import "ESP_NetUtil.h"
|
|
#import "ESPTouchTask.h"
|
|
#import "ESPTouchResult.h"
|
|
#import "ESPTouchDelegate.h"
|
|
|
|
#import "ChatroomModel.h"
|
|
#import "MMProgressHUD.h"
|
|
#import "MMProgressHUDOverlayView.h"
|
|
#define ALERTVIEW_TAG 11001
|
|
//需求已改 暂不用
|
|
//@interface EspTouchDelegateImpl : NSObject<ESPTouchDelegate>
|
|
//
|
|
//@end
|
|
//
|
|
//@implementation EspTouchDelegateImpl
|
|
//
|
|
//-(void) dismissAlert:(UIAlertView *)alertView
|
|
//{
|
|
// [alertView dismissWithClickedButtonIndex:[alertView cancelButtonIndex] animated:YES];
|
|
//}
|
|
//
|
|
//-(void) showAlertWithResult: (ESPTouchResult *) result
|
|
//{
|
|
// NSString *title = nil;
|
|
// NSString *message = [NSString stringWithFormat:@"%@ is connected to the wifi" , result.bssid];
|
|
// NSTimeInterval dismissSeconds = 3.5;
|
|
// UIAlertView *alertView = [[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
|
|
// [alertView show];
|
|
// [self performSelector:@selector(dismissAlert:) withObject:alertView afterDelay:dismissSeconds];
|
|
//}
|
|
//
|
|
//-(void) onEsptouchResultAddedWithResult: (ESPTouchResult *) result
|
|
//{
|
|
// NSLog(@"EspTouchDelegateImpl onEsptouchResultAddedWithResult bssid: %@", result.bssid);
|
|
//
|
|
// dispatch_async(dispatch_get_main_queue(), ^{
|
|
// // [self showAlertWithResult:result];
|
|
// });
|
|
//}
|
|
//@end
|
|
|
|
typedef NS_ENUM(NSInteger,lodingViewdissMissStyle) {
|
|
lodingViewdissMissSuccsess=0,// default is 0
|
|
lodingViewdissMissfail,
|
|
lodingViewdissMissAlreadyBinded
|
|
};
|
|
@interface ConnectWifiViewController ()<UIAlertViewDelegate>
|
|
|
|
@property (weak, nonatomic) IBOutlet UIButton *notConnectWifiButton;
|
|
|
|
@property (nonatomic, strong) NSCondition *_condition;
|
|
@property (atomic, strong) ESPTouchTask *_esptouchTask;
|
|
//@property (nonatomic, strong) EspTouchDelegateImpl *_esptouchDelegate;
|
|
|
|
@property(nonatomic,strong)NSMutableArray*deviceArry;
|
|
@property(nonatomic,strong)NSMutableArray*chatroomArray;
|
|
@property(nonatomic,strong)NSMutableArray*venderArray;
|
|
|
|
@property(nonatomic,strong)ICSDrawerController*ics;
|
|
@property(nonatomic)BOOL autodismiss;
|
|
@property(nonatomic,assign)lodingViewdissMissStyle lodviewMissStyle;
|
|
@end
|
|
extern BOOL formLogIn;//连接页面是否来自登录界面
|
|
|
|
@implementation ConnectWifiViewController
|
|
|
|
- (void)viewDidLoad {
|
|
[super viewDidLoad];
|
|
self._condition = [[NSCondition alloc]init];
|
|
//self._esptouchDelegate = [[EspTouchDelegateImpl alloc]init];
|
|
|
|
[self addTitleViewWithTitle:@"爱鱼奇"];
|
|
self.notConnectWifiButton.tintColor=COLOR_LABEL_TITLE;
|
|
[self initWifiImage];
|
|
self.view.backgroundColor=[UIColor colorWithRed:36.0/256.0 green:72.0/256.0 blue:107/256.0 alpha:1];
|
|
// [self setText];
|
|
if (formLogIn==YES) {// 判断是否来自登录界面 隐藏返回按钮
|
|
formLogIn=NO;
|
|
self.notConnectWifiButton.hidden=NO;
|
|
|
|
self.bakbutton.hidden=YES;
|
|
}else if (formLogIn==NO){
|
|
|
|
self.notConnectWifiButton.hidden=YES;
|
|
self.bakbutton.hidden=NO;
|
|
|
|
}
|
|
_deviceArry =[[NSMutableArray alloc]init];
|
|
_chatroomArray=[[NSMutableArray alloc]init];
|
|
_venderArray=[[NSMutableArray alloc]init];
|
|
if (kScreenSize.height>568) {
|
|
self.shuomingView.image=[UIImage imageNamed:@"shuoming_text"];
|
|
|
|
|
|
}else if (kScreenSize.height==568){
|
|
|
|
self.shuomingView.image=[UIImage imageNamed:@"shuoming_text2"];
|
|
|
|
|
|
}else if (kScreenSize.height==480){
|
|
self.shuomingView.image=[UIImage imageNamed:@"shuoming_text3"];
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
-(void)initMMProgressHUD{
|
|
NSArray *images = @[[UIImage imageNamed:@"octopus_1.png"],
|
|
[UIImage imageNamed:@"octopus_2.png"],
|
|
[UIImage imageNamed:@"octopus_3.png"],
|
|
[UIImage imageNamed:@"octopus_4.png"],
|
|
[UIImage imageNamed:@"octopus_5.png"],
|
|
[UIImage imageNamed:@"octopus_6.png"],
|
|
[UIImage imageNamed:@"octopus_7.png"],
|
|
[UIImage imageNamed:@"octopus_8.png"]];
|
|
|
|
_autodismiss = YES;//MMProgressHUDWindowOverlayModeLinear
|
|
[[MMProgressHUD sharedHUD] setOverlayMode:MMProgressHUDWindowOverlayModeNone];
|
|
|
|
/**
|
|
* status: Custom Animated Image
|
|
*/
|
|
[MMProgressHUD showWithTitle:@"" status:@"" images:images];
|
|
|
|
|
|
}
|
|
-(void)mmPogressHUDdismiss{
|
|
// if (_autodismiss == YES) {
|
|
// double delayInSeconds = 2.0;
|
|
// dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
|
|
// dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
|
|
[self setMMProgressHUDMissStayle:self.lodviewMissStyle];
|
|
|
|
// });
|
|
// }
|
|
}
|
|
-(void)setMMProgressHUDMissStayle:(lodingViewdissMissStyle)missStyle{
|
|
switch (missStyle) {
|
|
case lodingViewdissMissSuccsess:
|
|
{
|
|
[MMProgressHUD dismissWithSuccess:@""];
|
|
}
|
|
break;
|
|
case lodingViewdissMissfail:
|
|
{
|
|
[MMProgressHUD dismissWithError:@""];
|
|
[self showTitle:@"" messsage:@"再试一下!"];
|
|
}
|
|
break;
|
|
case lodingViewdissMissAlreadyBinded:
|
|
{
|
|
|
|
[MMProgressHUD dismissWithSuccess:@""];
|
|
[self showTitle:@"" messsage:@"已经绑定过了!"];
|
|
}
|
|
default:
|
|
break;
|
|
}
|
|
|
|
}
|
|
- (void)didReceiveMemoryWarning {
|
|
[super didReceiveMemoryWarning];
|
|
}
|
|
|
|
// 暂时不绑定
|
|
- (IBAction)notconnectwifi:(id)sender {
|
|
|
|
LeftViewController*left=[[LeftViewController alloc]init];
|
|
CenterViewController * center = [[CenterViewController alloc]init];
|
|
|
|
|
|
_ics=[[ICSDrawerController alloc]initWithLeftViewController:left centerViewController:center];
|
|
|
|
|
|
|
|
// [self.navigationController pushViewController:_ics animated:YES];
|
|
|
|
UIApplication*app=[UIApplication sharedApplication];
|
|
AppDelegate*app1=(AppDelegate*)app.delegate;
|
|
UINavigationController*nav=[[UINavigationController alloc]initWithRootViewController:_ics];
|
|
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
|
|
app1.window.rootViewController=nav;
|
|
|
|
|
|
}
|
|
-(void)showTitle:(NSString*)title messsage:(NSString*)message{
|
|
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:title message:message delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
|
|
NSTimeInterval dismissSeconds=1.0;
|
|
[alert show];
|
|
[self performSelector:@selector(dismissAlert:) withObject:alert afterDelay:dismissSeconds];
|
|
|
|
}
|
|
-(void)dismissAlert:(UIAlertView*)alertView{
|
|
|
|
[alertView dismissWithClickedButtonIndex:[alertView cancelButtonIndex] animated:YES];
|
|
}
|
|
-(void)initWifiImage{
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
|
|
self.wifiView.animationImages=[NSArray arrayWithObjects:[UIImage imageNamed:@"fish_png_0"],[UIImage imageNamed:@"fish_png_1"],[UIImage imageNamed:@"fish_png_2"],[UIImage imageNamed:@"fish_png_3"], nil];
|
|
self.wifiView.animationDuration=2;
|
|
self.wifiView.animationRepeatCount=0;
|
|
[self.wifiView startAnimating];
|
|
});
|
|
}
|
|
#pragma mark 连接设备
|
|
- (IBAction)connect:(id)sender {
|
|
|
|
[[AFNetworkReachabilityManager sharedManager] startMonitoring];
|
|
[[AFNetworkReachabilityManager sharedManager] setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
|
|
NSLog(@"%ld", (long)status);
|
|
|
|
if (status==AFNetworkReachabilityStatusReachableViaWiFi) {
|
|
|
|
[self connectMyWIFI];
|
|
|
|
}else{
|
|
|
|
[self showTitle:@"" messsage:@"请切换到WI-FI环境下绑定"];
|
|
}
|
|
|
|
}];
|
|
}
|
|
-(void)connectMyWIFI{
|
|
|
|
NSUserDefaults*userDefsult=[NSUserDefaults standardUserDefaults];
|
|
NSString*pass=[userDefsult objectForKey:@"wifiPass"];
|
|
NSDictionary *netInfo = [self fetchNetInfo];
|
|
_ssid = [netInfo objectForKey:@"SSID"];
|
|
_bssid = [netInfo objectForKey:@"BSSID"];
|
|
NSString *msg = [NSString stringWithFormat:@"请输入%@密码",_ssid];
|
|
UIAlertView *alerV = [[UIAlertView alloc]initWithTitle:msg
|
|
message:nil
|
|
delegate:self
|
|
cancelButtonTitle:@"取消"
|
|
otherButtonTitles:@"确定", nil];
|
|
alerV.alertViewStyle = UIAlertViewStylePlainTextInput;
|
|
_wifiFiled=[alerV textFieldAtIndex:0];
|
|
_wifiFiled.secureTextEntry=NO;
|
|
_wifiFiled.text=pass;
|
|
_wifiFiled.clearButtonMode=UITextFieldViewModeAlways;
|
|
alerV.tag=ALERTVIEW_TAG;
|
|
|
|
[alerV show];
|
|
|
|
// [self bindDeviceWithSsid:@"5CCF7F0065CA"];
|
|
// 5CCF7F00669C
|
|
// 5CCF7F006668
|
|
// 5CCF7F0065CA
|
|
|
|
}
|
|
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
|
|
if (buttonIndex==1) {
|
|
[self savePassw];
|
|
[self tapConfirmForResults];
|
|
//[self creatHUD];
|
|
[self initMMProgressHUD];
|
|
[alertView removeFromSuperview];
|
|
alertView.delegate=nil;
|
|
alertView.tag=ALERTVIEW_TAG;
|
|
alertView =nil;//提前释放内存 防止 切换wifi环境弹窗
|
|
[_wifiFiled resignFirstResponder];
|
|
}
|
|
|
|
}
|
|
- (void) tapConfirmForResults{
|
|
|
|
NSLog(@"ViewController do confirm action...");
|
|
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
|
|
dispatch_async(queue, ^{
|
|
NSLog(@"ViewController do the execute work...");
|
|
// execute the task
|
|
NSArray *esptouchResultArray = [self executeForResults];
|
|
// show the result to the user in UI Main Thread
|
|
dispatch_async(dispatch_get_main_queue(), ^{
|
|
// [self._spinner stopAnimating];
|
|
// [self enableConfirmBtn];
|
|
|
|
ESPTouchResult *firstResult = [esptouchResultArray objectAtIndex:0];
|
|
// check whether the task is cancelled and no results received
|
|
if (!firstResult.isCancelled)
|
|
{
|
|
NSMutableString *mutableStr = [[NSMutableString alloc]init];
|
|
NSUInteger count = 0;
|
|
// max results to be displayed, if it is more than maxDisplayCount,
|
|
// just show the count of redundant ones
|
|
const int maxDisplayCount = 5;
|
|
if ([firstResult isSuc])
|
|
{
|
|
|
|
_deviceBssid=firstResult.bssid;
|
|
|
|
NSArray*arry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
|
|
DeviceModel*model=[[DeviceModel alloc]init];
|
|
|
|
NSMutableArray*macdressArr=[[NSMutableArray alloc]init];
|
|
for (model in arry) {
|
|
|
|
[macdressArr addObject:model.macAddress];
|
|
|
|
}
|
|
BOOL exst=[macdressArr containsObject:_deviceBssid];
|
|
if (exst) {
|
|
self.lodviewMissStyle=lodingViewdissMissAlreadyBinded;
|
|
[self mmPogressHUDdismiss];
|
|
self.bakbutton.userInteractionEnabled=YES;
|
|
[self.navigationController popViewControllerAnimated:YES];
|
|
}else{
|
|
|
|
[self bindDeviceWithSsid:_deviceBssid];
|
|
}
|
|
|
|
|
|
for (int i = 0; i < [esptouchResultArray count]; ++i)
|
|
{
|
|
ESPTouchResult *resultInArray = [esptouchResultArray objectAtIndex:i];
|
|
[mutableStr appendString:[resultInArray description]];
|
|
[mutableStr appendString:@"\n"];
|
|
count++;
|
|
if (count >= maxDisplayCount)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (count < [esptouchResultArray count])
|
|
{
|
|
[mutableStr appendString:[NSString stringWithFormat:@"\nthere's %lu more result(s) without showing\n",(unsigned long)([esptouchResultArray count] - count)]];
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
{
|
|
|
|
// [_HUD hide:YES];
|
|
self.lodviewMissStyle=lodingViewdissMissfail;
|
|
[self mmPogressHUDdismiss];
|
|
self.bakbutton.userInteractionEnabled=YES;
|
|
}
|
|
}
|
|
|
|
});
|
|
});
|
|
|
|
}
|
|
- (NSArray *) executeForResults
|
|
{
|
|
[self._condition lock];
|
|
NSString *apSsid = self.ssid;
|
|
NSString *apPwd = self.wifiFiled.text;
|
|
|
|
NSString *apBssid = self.bssid;
|
|
BOOL isSsidHidden = YES;
|
|
// int taskCount = [self._taskResultCountTextView.text intValue];
|
|
int taskCount=1;
|
|
self._esptouchTask =
|
|
[[ESPTouchTask alloc]initWithApSsid:apSsid andApBssid:apBssid andApPwd:apPwd andIsSsidHiden:isSsidHidden];
|
|
// set delegate
|
|
// [self._esptouchTask setEsptouchDelegate:self._esptouchDelegate];
|
|
[self._condition unlock];
|
|
NSArray * esptouchResults = [self._esptouchTask executeForResults:taskCount];
|
|
NSLog(@"ViewController executeForResult() result is: %@",esptouchResults);
|
|
return esptouchResults;
|
|
}
|
|
//保存wifi密码
|
|
-(void)savePassw{
|
|
NSUserDefaults*userdefult=[NSUserDefaults standardUserDefaults];
|
|
|
|
[userdefult setObject:self.wifiFiled.text forKey:@"wifiPass"];
|
|
[userdefult synchronize];
|
|
}
|
|
|
|
- (NSString *)fetchSsid
|
|
{
|
|
NSDictionary *ssidInfo = [self fetchNetInfo];
|
|
|
|
return [ssidInfo objectForKey:@"SSID"];
|
|
}
|
|
|
|
- (NSString *)fetchBssid
|
|
{
|
|
NSDictionary *bssidInfo = [self fetchNetInfo];
|
|
|
|
return [bssidInfo objectForKey:@"BSSID"];
|
|
}
|
|
|
|
// refer to http://stackoverflow.com/questions/5198716/iphone-get-ssid-without-private-library
|
|
- (NSDictionary *)fetchNetInfo
|
|
{
|
|
NSArray *interfaceNames = CFBridgingRelease(CNCopySupportedInterfaces());
|
|
// NSLog(@"%s: Supported interfaces: %@", __func__, interfaceNames);
|
|
|
|
NSDictionary *SSIDInfo;
|
|
for (NSString *interfaceName in interfaceNames) {
|
|
SSIDInfo = CFBridgingRelease(
|
|
CNCopyCurrentNetworkInfo((__bridge CFStringRef)interfaceName));
|
|
// NSLog(@"%s: %@ => %@", __func__, interfaceName, SSIDInfo);
|
|
|
|
BOOL isNotEmpty = (SSIDInfo.count > 0);
|
|
if (isNotEmpty) {
|
|
break;
|
|
}
|
|
}
|
|
return SSIDInfo;
|
|
}
|
|
#pragma mark 绑定设备
|
|
-(void)bindDeviceWithSsid:(NSString*)bssid{
|
|
|
|
NSLog(@"设备bssid_%@",bssid);
|
|
|
|
|
|
AFHTTPRequestOperationManager*mannager=[AFHTTPRequestOperationManager manager];
|
|
mannager.responseSerializer=[AFHTTPResponseSerializer serializer];
|
|
UserModel*model=[[DataCenter defaultDtacenter]valueForKey:@"UserLogIn"];
|
|
NSMutableDictionary * para = [NSMutableDictionary dictionary];
|
|
[para setValue:model.userId forKey:@"userId"];
|
|
[para setValue:bssid forKey:@"macAddress"];// macId
|
|
|
|
[mannager POST:kBindDevice parameters:para success:^(AFHTTPRequestOperation *operation, id responseObject) {
|
|
NSDictionary*resultDic=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
|
|
NSLog(@"result:%@",resultDic[@"result"]);
|
|
NSLog(@"data:%@",resultDic[@"data"]);
|
|
if ([resultDic[@"result"] isEqualToString:@"100"]) {
|
|
//[_HUD hide:YES];
|
|
self.lodviewMissStyle=lodingViewdissMissSuccsess;
|
|
[self mmPogressHUDdismiss];
|
|
|
|
// 保存设备信息
|
|
NSDictionary*dataDic=resultDic[@"data"];
|
|
DeviceModel*devicemodel=[[DeviceModel alloc] initWithDict:dataDic];
|
|
//厂家
|
|
|
|
_deviceArry=[[DataCenter defaultDtacenter]valueForKey:@"deviceInfo"];
|
|
|
|
[_deviceArry insertObject:devicemodel atIndex:0];
|
|
|
|
[[DataCenter defaultDtacenter]setValue:_deviceArry forKey:@"deviceInfo"];
|
|
|
|
|
|
LeftViewController*left=[[LeftViewController alloc]init];
|
|
CenterViewController * center = [[CenterViewController alloc]init];
|
|
_ics=[[ICSDrawerController alloc]initWithLeftViewController:left centerViewController:center];
|
|
UIApplication*app=[UIApplication sharedApplication];
|
|
AppDelegate*app1=app.delegate;
|
|
UINavigationController*nav=[[UINavigationController alloc]initWithRootViewController:_ics];
|
|
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"bar.png"] forBarMetrics:UIBarMetricsDefault];
|
|
app1.window.rootViewController=nav;
|
|
|
|
|
|
}else if ([resultDic[@"result"] isEqualToString:@"101"]){
|
|
[self showTitle:@"" messsage:@"绑定失败"];
|
|
self.bakbutton.userInteractionEnabled=YES;
|
|
}else if ([resultDic[@"result"] isEqualToString:@"301"]){
|
|
[self showTitle:@"" messsage:@"请求验证失败,请重新登录"];
|
|
self.bakbutton.userInteractionEnabled=YES;
|
|
}else if ([resultDic[@"result"] isEqualToString:@"302"]){
|
|
// 请求被舍弃未执行
|
|
self.bakbutton.userInteractionEnabled=YES;
|
|
}
|
|
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
|
|
[self showTitle:@"" messsage:@"请检查网络"];
|
|
self.bakbutton.userInteractionEnabled=YES;
|
|
}];
|
|
|
|
}
|
|
|
|
- (IBAction)fankuiBtn:(id)sender {
|
|
|
|
SaySomethingViewController*say=[[SaySomethingViewController alloc]init];
|
|
[self.navigationController pushViewController:say animated:YES];
|
|
self.navigationController.navigationBarHidden=NO;
|
|
|
|
}
|
|
|
|
- (IBAction)connectError:(id)sender {
|
|
ConnectErrorViewController*error=[[ConnectErrorViewController alloc]init];
|
|
[self.navigationController pushViewController:error animated:YES];
|
|
self.navigationController.navigationBarHidden=NO;
|
|
}
|
|
-(void)viewWillDisappear:(BOOL)animated{
|
|
|
|
|
|
}
|
|
@end
|