HomeHubWifiSearchViewController.m 5.0 KB

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