HomeHubWifiPasswdInputViewController.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. [bleService setWiFiPwd:pass];
  104. [bleService enableDHCP];
  105. [bleService readAndNotifyCharacteristicUUID:kBLEChrStDHCPArg isNotify:NO];
  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. #pragma mark - ble delegate
  118. //enableDHCP
  119. - (void)BLEWiFiDHCPUpdate:(id)data {
  120. [bleService applyWiFiSettingInfo];
  121. [bleService readAndNotifyCharacteristicUUID:kBLEChrRdConInfo isNotify:YES];
  122. }
  123. //kBLEChrRdConInfo
  124. - (void)BLEWiFiConnectionUpdate:(CBCharacteristic *)info {
  125. NSLog(@"Connection Update!");
  126. NSString *value = [bleService hexStringValue:info] ;
  127. NSLog(@"value : %@", value) ;
  128. NSLog(@"value : %@", [value substringFromIndex:value.length-1]) ;
  129. if (value.length > 0 &&
  130. EQUALS([value substringFromIndex:value.length-1], @"1")) {
  131. [bleService readConnectionWiFiInfo];
  132. }
  133. else if (value.length > 0 &&
  134. EQUALS([value substringFromIndex:value.length-1], @"0")) {
  135. //실패
  136. [self startLoading:NO];
  137. [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패" message:@"인터넷 연결 설정에 실패하였습니다."];
  138. }
  139. }
  140. //readConnectionWiFiInfo
  141. - (void)BLEWiFiConnectionInfoUpdateWithKey:(NSString *)kBLEChr
  142. result:(NSString *)result {
  143. NSLog(@"Connection Info Update!") ;
  144. if (EQUALS(kBLEChr, kBLEChrRdSSID)) {
  145. updateSSID = YES;
  146. }
  147. else if(EQUALS(kBLEChr, kBLEChrRdBSSID)) {
  148. updateBSSID = YES;
  149. }
  150. else if(EQUALS(kBLEChr, kBLEChrRnIpSet)) {
  151. updateIPSet = YES;
  152. }
  153. else if(EQUALS(kBLEChr, kBLEChrRnIpAddr)) {
  154. updateIPAddr = YES;
  155. }
  156. // 연결 확인 완료
  157. if (updateSSID && updateBSSID && updateIPSet && updateIPAddr) {
  158. [self startLoading:NO];
  159. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];
  160. [self.navigationController pushViewController:vc animated:YES];
  161. }
  162. }
  163. @end