HomeHubWifiPasswdInputViewController.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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;
  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. //[self startLoading:NO];
  80. }
  81. });
  82. }
  83. #pragma mark - User Event
  84. - (IBAction)btnAgainTouched:(id)sender {
  85. BOOL find = NO;
  86. for (UIViewController *vc in self.navigationController.viewControllers) {
  87. if ([vc isKindOfClass:HomeHubConnectWifiViewController.class]) {
  88. find = YES;
  89. [self.navigationController popToViewController:vc animated:YES];
  90. }
  91. }
  92. if (!find) {
  93. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubConnectWifiViewController" storyboardName:@"HomeHub"];
  94. [self.navigationController pushViewController:vc animated:YES];
  95. }
  96. }
  97. - (IBAction)btnConnectTouched:(id)sender {
  98. NSString *pass = _txtInputPasswd.text.trim;
  99. if (pass.length == 0) {
  100. return;
  101. }
  102. [self startLoading:YES];
  103. //1. write pass
  104. [bleService setWiFiPwd:pass];
  105. [self performSelector:@selector(BLESendEnableDHCP) withObject:nil afterDelay:1.0];
  106. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  107. }
  108. - (IBAction)btnCancelTouched:(id)sender {
  109. [self.navigationController popToRootViewControllerAnimated:YES];
  110. }
  111. - (BOOL)textFieldShouldReturn:(UITextField *)textField {
  112. if (textField.text.length > 0) {
  113. [self.view endEditing:YES];
  114. }
  115. return YES;
  116. }
  117. - (void)BLESendEnableDHCP {
  118. //2. set ip set arg chr
  119. [bleService enableDHCP];
  120. [bleService readAndNotifyCharacteristicUUID:kBLEChrStDHCPArg isNotify:NO];
  121. }
  122. #pragma mark - ble delegate
  123. //enableDHCP
  124. - (void)BLEWiFiDHCPUpdate:(id)data {
  125. //3. apply chr
  126. [bleService applyWiFiSettingInfo];
  127. }
  128. //kBLEChrRdConInfo
  129. - (void)BLEWiFiConnectionUpdate:(CBCharacteristic *)info {
  130. NSString *value = [bleService hexStringValue:info] ;
  131. if (value.length > 0 &&
  132. EQUALS([value substringFromIndex:value.length-1], @"1")) {
  133. //5.
  134. [bleService readConnectionWiFiInfo];
  135. }
  136. else if (value.length > 0 &&
  137. EQUALS([value substringFromIndex:value.length-1], @"0")) {
  138. //실패
  139. [self startLoading:NO];
  140. [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패" message:@"인터넷 연결 설정에 실패하였습니다."];
  141. }
  142. }
  143. //readConnectionWiFiInfo
  144. - (void)BLEWiFiConnectionInfoUpdateWithKey:(NSString *)kBLEChr
  145. result:(NSString *)result {
  146. NSLog(@"Connection Info Update!") ;
  147. if (EQUALS(kBLEChr, kBLEChrRdSSID)) {
  148. updateSSID = YES;
  149. }
  150. else if(EQUALS(kBLEChr, kBLEChrRdBSSID)) {
  151. updateBSSID = YES;
  152. }
  153. else if(EQUALS(kBLEChr, kBLEChrRnIpSet)) {
  154. updateIPSet = YES;
  155. }
  156. else if(EQUALS(kBLEChr, kBLEChrRnIpAddr)) {
  157. updateIPAddr = YES;
  158. }
  159. // 연결 확인 완료
  160. if (updateSSID && updateBSSID && updateIPSet && updateIPAddr) {
  161. [self startLoading:NO];
  162. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];
  163. [self.navigationController pushViewController:vc animated:YES];
  164. }
  165. }
  166. - (void)BLEDisConnected:(BTLEDeivceModel *)info {
  167. [[JDFacade facade] toast:@"홈허브와 연결할 수 없습니다."];
  168. [self.navigationController popToRootViewControllerAnimated:YES];
  169. }
  170. @end