HomeHubWifiPasswdInputViewController.m 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. //
  2. // HomeHubWifiPasswdInputViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 11..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "HomeHubWifiPasswdInputViewController.h"
  9. #import "HomeHubConnectWifiViewController.h"
  10. @interface HomeHubWifiPasswdInputViewController ()<UITextFieldDelegate> {
  11. NSTimer *_timer;
  12. NSInteger _elapsedSeconds;
  13. BLEServiceHandler *bleService;
  14. BOOL updateSSID;
  15. BOOL updateBSSID;
  16. BOOL updateIPSet;
  17. BOOL updateIPAddr;
  18. }
  19. @end
  20. @implementation HomeHubWifiPasswdInputViewController
  21. - (void)viewDidLoad {
  22. [super viewDidLoad];
  23. [self initUI];
  24. [self prepareViewDidLoad];
  25. }
  26. - (void)viewWillAppear:(BOOL)animated {
  27. [super viewWillAppear:animated];
  28. bleService.delegate = self;
  29. }
  30. - (void)viewWillDisappear:(BOOL)animated {
  31. [super viewWillDisappear:animated];
  32. bleService.delegate = nil;
  33. }
  34. - (void)initUI {
  35. _lblSSID.text = [NSString stringWithFormat:@"SSID : %@", _selectedWLanModel.ssid];
  36. _imgvLoading.hidden = YES;
  37. }
  38. - (void)prepareViewDidLoad {
  39. //ble
  40. bleService = [BLEServiceHandler sharedManager];
  41. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  42. }
  43. - (void)didReceiveMemoryWarning {
  44. [super didReceiveMemoryWarning];
  45. }
  46. -(void)startLoading:(BOOL)isStart {
  47. _imgvLoading.hidden = !isStart;
  48. if (isStart) {
  49. _elapsedSeconds = kMaxTimeOut*2;
  50. if (!_timer) {
  51. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  52. }
  53. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  54. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  55. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  56. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  57. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  58. animation.duration = 2.0f;
  59. animation.repeatCount = INFINITY;
  60. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  61. }
  62. });
  63. }
  64. else {
  65. _elapsedSeconds = kMaxTimeOut;
  66. if (_timer) {
  67. [_timer invalidate];
  68. _timer = nil;
  69. }
  70. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  71. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  72. }
  73. }
  74. }
  75. - (void)updateInclusionStatus {
  76. dispatch_async(dispatch_get_main_queue(), ^(void) {
  77. _elapsedSeconds--;
  78. if (_elapsedSeconds == 0) {
  79. //이떄 성공하지 않았으면 실패로 처리
  80. [self startLoading:NO];
  81. [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패" message:@"인터넷 연결 설정에 실패하였습니다."];
  82. }
  83. });
  84. }
  85. #pragma mark - User Event
  86. - (IBAction)btnAgainTouched:(id)sender {
  87. BOOL find = NO;
  88. for (UIViewController *vc in self.navigationController.viewControllers) {
  89. if ([vc isKindOfClass:HomeHubConnectWifiViewController.class]) {
  90. find = YES;
  91. [self.navigationController popToViewController:vc animated:YES];
  92. }
  93. }
  94. if (!find) {
  95. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubConnectWifiViewController" storyboardName:@"HomeHub"];
  96. [self.navigationController pushViewController:vc animated:YES];
  97. }
  98. }
  99. - (IBAction)btnConnectTouched:(id)sender {
  100. NSString *pass = _txtInputPasswd.text.trim;
  101. if (pass.length == 0) {
  102. return;
  103. }
  104. [self startLoading:YES];
  105. //1. write pass
  106. [bleService setWiFiPwd:pass];
  107. [self performSelector:@selector(BLESendEnableDHCP) withObject:nil afterDelay:1.0];
  108. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  109. }
  110. - (IBAction)btnCancelTouched:(id)sender {
  111. [self.navigationController popToRootViewControllerAnimated:YES];
  112. }
  113. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  114. if (textField.text.length > 0) {
  115. [self.view endEditing:YES];
  116. }
  117. return YES;
  118. }
  119. - (void)BLESendEnableDHCP {
  120. //2. set ip set arg chr
  121. [bleService enableDHCP];
  122. [bleService readAndNotifyCharacteristicUUID:kBLEChrStDHCPArg isNotify:NO];
  123. }
  124. #pragma mark - ble delegate
  125. //enableDHCP
  126. - (void)BLEWiFiDHCPUpdate:(id)data {
  127. //3. apply chr
  128. [bleService applyWiFiSettingInfo];
  129. }
  130. //kBLEChrRdConInfo
  131. - (void)BLEWiFiConnectionUpdate:(CBCharacteristic *)info {
  132. NSString *value = [bleService hexStringValue:info] ;
  133. if (value.length > 0 &&
  134. EQUALS([value substringFromIndex:value.length-1], @"1")) {
  135. //5.
  136. [bleService readConnectionWiFiInfo];
  137. }
  138. else if (value.length > 0 &&
  139. EQUALS([value substringFromIndex:value.length-1], @"0")) {
  140. //실패
  141. // [self startLoading:NO];
  142. // [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패" message:@"인터넷 연결 설정에 실패하였습니다."];
  143. }
  144. }
  145. //readConnectionWiFiInfo
  146. - (void)BLEWiFiConnectionInfoUpdateWithKey:(NSString *)kBLEChr
  147. result:(NSString *)result {
  148. NSLog(@"Connection Info Update!") ;
  149. if (EQUALS(kBLEChr, kBLEChrRdSSID)) {
  150. updateSSID = YES;
  151. }
  152. else if(EQUALS(kBLEChr, kBLEChrRdBSSID)) {
  153. updateBSSID = YES;
  154. }
  155. else if(EQUALS(kBLEChr, kBLEChrRnIpSet)) {
  156. updateIPSet = YES;
  157. }
  158. else if(EQUALS(kBLEChr, kBLEChrRnIpAddr)) {
  159. updateIPAddr = YES;
  160. }
  161. // 연결 확인 완료
  162. if (updateSSID && updateBSSID && updateIPSet && updateIPAddr) {
  163. [self startLoading:NO];
  164. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];
  165. [self.navigationController pushViewController:vc animated:YES];
  166. }
  167. }
  168. - (void)BLEDisConnected:(BTLEDeivceModel *)info {
  169. [[JDFacade facade] toast:@"홈허브와 연결할 수 없습니다."];
  170. [self.navigationController popToRootViewControllerAnimated:YES];
  171. }
  172. @end