// // HomeHubWifiSearchViewController.m // OneCable // // Created by nComz on 2017. 5. 11.. // Copyright © 2017년 ntels. All rights reserved. // #import "HomeHubWifiSearchViewController.h" #import "HomeHubWifiSearchSuccessViewController.h" @interface HomeHubWifiSearchViewController (){ BLEServiceHandler *bleService; NSTimer *_timer; NSInteger _elapsedSeconds; BOOL startScan; NSMutableDictionary *wifiData; } @end @implementation HomeHubWifiSearchViewController - (void)viewDidLoad { [super viewDidLoad]; [self initUI]; [self prepareViewDidLoad]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; } - (void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; } - (void)initUI { [self startLoading:YES]; } - (void)prepareViewDidLoad { wifiData = [NSMutableDictionary new]; //ble bleService = [BLEServiceHandler sharedManager]; bleService.delegate = self; startScan = NO; [bleService connect:bleService.conDevice]; } -(void)startLoading:(BOOL)isStart { _lblTimer.hidden = !isStart; _imgvLoading.hidden = !isStart; if (isStart) { _elapsedSeconds = kMaxTimeOut; NSLog(@"ElapsedSecond : %zd", _elapsedSeconds); _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds]; if (!_timer) { _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES]; } dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) { CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"]; animation.fromValue = [NSNumber numberWithFloat:0.0f]; animation.toValue = [NSNumber numberWithFloat: 2*M_PI]; animation.duration = 2.0f; animation.repeatCount = INFINITY; [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"]; } }); } else { _elapsedSeconds = kMaxTimeOut; if (_timer) { [_timer invalidate]; _timer = nil; } if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) { [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"]; } } } - (void)updateInclusionStatus { dispatch_async(dispatch_get_main_queue(), ^(void) { _elapsedSeconds--; _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds]; if (startScan) { NSString *result = [bleService getWLanList:_elapsedSeconds%3]; if (!(EQUALS(result, @"") || EQUALS(result, @"-"))) { BLEWLanListModel *response = [[BLEWLanListModel alloc] initWithString:result error:nil]; [wifiData setObject:response forKey:[NSString stringWithFormat:@"%ld", _elapsedSeconds%3]]; if (wifiData.allKeys.count == 3) { [self moveHomeHubWifiSearchSuccess]; } } } if (_elapsedSeconds == 0) { if (wifiData.allKeys.count > 0) { [self moveHomeHubWifiSearchSuccess]; } else { [self moveHomeHubWifiSearchFail]; } } }); } - (void)getJsonModel:(NSString*)string{ NSError *jsonError; NSData *objectData = [string dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *json = [NSJSONSerialization JSONObjectWithData:objectData options:NSJSONReadingMutableContainers error:&jsonError]; NSLog(@"json : %@", json) ; NSArray *wlans = [json objectForKey:@"wlan"] ; } #pragma mark - User Event - (IBAction)btnCloseTouched:(id)sender { [self dismissViewControllerAnimated:YES completion:nil]; } #pragma mark - ble service delegate - (void)BLEConnected:(BTLEDeivceModel *)info { NSLog(@"home hub wifi search view connected!") ; startScan = YES ; [bleService scanWiFiList]; } - (void)BLEDisConnected:(BTLEDeivceModel *)info { NSLog(@"home hub wifi search view disconnected!") ; } - (void)moveHomeHubWifiSearchSuccess { [self startLoading:NO]; NSArray *allKeys = wifiData.allKeys ; NSMutableArray *wifiList = [NSMutableArray new] ; for (NSString *key in allKeys) { BLEWLanListModel *modelList = [wifiData objectForKey:key] ; for (BLEWLanModel *model in modelList.wlan) { [wifiList addObject:model]; } } HomeHubWifiSearchSuccessViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubWifiSearchSuccessViewController" storyboardName:@"HomeHub"]; vc.wifiList = wifiList; [self presentViewController:vc animated:YES completion:nil]; } - (void)moveHomeHubWifiSearchFail { [self startLoading:NO]; UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubWifiSearchFailViewController" storyboardName:@"HomeHub"]; [self presentViewController:vc animated:YES completion:nil]; } @end