HomeHubWifiSearchViewController.m 5.0 KB

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