HomeHubWifiSearchViewController.m 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. //
  2. // HomeHubWifiSearchViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 11..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "HomeHubWifiSearchViewController.h"
  9. #import "HomeHubWifiSearchSuccessViewController.h"
  10. @interface HomeHubWifiSearchViewController (){
  11. BLEServiceHandler *bleService;
  12. NSTimer *_timer;
  13. NSInteger _elapsedSeconds;
  14. BOOL startScan;
  15. NSMutableDictionary *wifiData;
  16. }
  17. @end
  18. @implementation HomeHubWifiSearchViewController
  19. - (void)viewDidLoad {
  20. [super viewDidLoad];
  21. [self prepareViewDidLoad];
  22. }
  23. - (void)didReceiveMemoryWarning {
  24. [super didReceiveMemoryWarning];
  25. }
  26. - (void)viewWillAppear:(BOOL)animated{
  27. [super viewWillAppear:animated];
  28. [self initUI];
  29. bleService.delegate = self;
  30. [bleService connect:bleService.conDevice];
  31. // if (!bleService.isConnected) {
  32. //
  33. //
  34. // }
  35. // else {
  36. // [self BLEConnected:nil];
  37. // }
  38. }
  39. - (void)viewWillDisappear:(BOOL)animated {
  40. [super viewWillDisappear:animated];
  41. bleService.delegate = nil;
  42. }
  43. - (void)initUI {
  44. [self startLoading:YES];
  45. }
  46. - (void)prepareViewDidLoad {
  47. wifiData = [NSMutableDictionary new];
  48. //ble
  49. bleService = [BLEServiceHandler sharedManager];
  50. startScan = NO;
  51. }
  52. -(void)startLoading:(BOOL)isStart {
  53. _lblTimer.hidden = !isStart;
  54. _imgvLoading.hidden = !isStart;
  55. if (isStart) {
  56. _elapsedSeconds = kMaxTimeOut;
  57. _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  58. if (!_timer) {
  59. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  60. }
  61. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  62. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  63. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  64. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  65. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  66. animation.duration = 2.0f;
  67. animation.repeatCount = INFINITY;
  68. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  69. }
  70. });
  71. }
  72. else {
  73. _elapsedSeconds = kMaxTimeOut;
  74. if (_timer) {
  75. [_timer invalidate];
  76. _timer = nil;
  77. }
  78. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  79. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  80. }
  81. }
  82. }
  83. - (void)updateInclusionStatus {
  84. dispatch_async(dispatch_get_main_queue(), ^(void) {
  85. _elapsedSeconds--;
  86. _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  87. if (startScan) {
  88. [bleService getWLanList:_elapsedSeconds%3];
  89. }
  90. if (_elapsedSeconds == 0) {
  91. if (wifiData.allKeys.count > 0) {
  92. [self moveHomeHubWifiSearchSuccess];
  93. }
  94. else {
  95. [self moveHomeHubWifiSearchFail];
  96. }
  97. }
  98. });
  99. }
  100. #pragma mark - User Event
  101. - (IBAction)btnCloseTouched:(id)sender {
  102. [self.navigationController popViewControllerAnimated:YES];
  103. }
  104. #pragma mark - ble service delegate
  105. - (void)BLEConnected:(BTLEDeivceModel *)info {
  106. NSLog(@"home hub wifi search view connected!") ;
  107. startScan = YES ;
  108. [bleService scanWiFiList];
  109. }
  110. - (void)BLEDisConnected:(BTLEDeivceModel *)info {
  111. NSLog(@"home hub wifi search view disconnected!") ;
  112. }
  113. - (void)BLEWLanUpdateWithKey:(NSString *)kBLEChr result:(NSString *)result{
  114. BLEWLanListModel *response = [[BLEWLanListModel alloc] initWithString:result error:nil];
  115. if (response == nil) {
  116. return;
  117. }
  118. [wifiData setObject:response
  119. forKey:kBLEChr];
  120. if (wifiData.allKeys.count == 3) {
  121. [self moveHomeHubWifiSearchSuccess];
  122. }
  123. }
  124. - (void)moveHomeHubWifiSearchSuccess {
  125. [self startLoading:NO];
  126. NSArray *allKeys = wifiData.allKeys ;
  127. NSMutableArray *wifiList = [NSMutableArray new] ;
  128. for (NSString *key in allKeys) {
  129. BLEWLanListModel *modelList = [wifiData objectForKey:key] ;
  130. for (BLEWLanModel *model in modelList.wlan) {
  131. [wifiList addObject:model];
  132. }
  133. }
  134. HomeHubWifiSearchSuccessViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubWifiSearchSuccessViewController" storyboardName:@"HomeHub"];
  135. vc.wifiList = wifiList;
  136. [self.navigationController pushViewController:vc animated:YES];
  137. }
  138. - (void)moveHomeHubWifiSearchFail {
  139. [self startLoading:NO];
  140. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubWifiSearchFailViewController" storyboardName:@"HomeHub"];
  141. [self.navigationController pushViewController:vc animated:YES];
  142. }
  143. @end