HomeHubWifiSearchViewController.m 5.3 KB

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