HomeHubWifiSearchSuccessViewController.m 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. //
  2. // HomeHubWifiSearchSuccessViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 11..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "HomeHubWifiSearchSuccessViewController.h"
  9. #import "HomeHubWifiPasswdInputViewController.h"
  10. @interface HomeHubWifiSearchSuccessViewController () <CustomCheckBoxDelegate> {
  11. BLEServiceHandler *bleService;
  12. BLEWLanModel *selectedWLanModel;
  13. //비밀번호 없는 경우
  14. BOOL updateSSID;
  15. BOOL updateBSSID;
  16. BOOL updateIPSet;
  17. BOOL updateIPAddr;
  18. NSTimer *_timer;
  19. NSInteger _elapsedSeconds;
  20. }
  21. @end
  22. @implementation HomeHubWifiSearchTableViewCell
  23. @end
  24. @implementation HomeHubWifiSearchSuccessViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. [self initUI];
  28. [self prepareViewDidLoad];
  29. }
  30. - (void)didReceiveMemoryWarning {
  31. [super didReceiveMemoryWarning];
  32. }
  33. - (void)initUI {
  34. _imgvLoading.hidden = YES;
  35. _btnNext.enabled = NO;
  36. [self initTableViewAsDefaultStyle:_tableView];
  37. }
  38. - (void)prepareViewDidLoad {
  39. //ble
  40. bleService = [BLEServiceHandler sharedManager];
  41. bleService.delegate = self;
  42. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  43. [_tableView reloadData];
  44. }
  45. -(void)startLoading:(BOOL)isStart {
  46. _imgvLoading.hidden = !isStart;
  47. if (isStart) {
  48. _elapsedSeconds = kMaxTimeOut;
  49. if (!_timer) {
  50. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  51. }
  52. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  53. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  54. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  55. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  56. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  57. animation.duration = 2.0f;
  58. animation.repeatCount = INFINITY;
  59. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  60. }
  61. });
  62. }
  63. else {
  64. _elapsedSeconds = kMaxTimeOut;
  65. if (_timer) {
  66. [_timer invalidate];
  67. _timer = nil;
  68. }
  69. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  70. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  71. }
  72. }
  73. }
  74. - (void)updateInclusionStatus {
  75. dispatch_async(dispatch_get_main_queue(), ^(void) {
  76. _elapsedSeconds--;
  77. if (_elapsedSeconds == 0) {
  78. [self startLoading:NO];
  79. }
  80. });
  81. }
  82. #pragma mark - UITableView DataSource & Delegate
  83. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  84. return 1;
  85. }
  86. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  87. return _wifiList.count;
  88. }
  89. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  90. return 101.0f;
  91. }
  92. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  93. HomeHubWifiSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WifiSearchCellIdentifier"];
  94. BLEWLanModel *model = [_wifiList objectAtIndex:indexPath.row];
  95. cell.lblHubName.text = model.ssid;
  96. cell.chkBtn.selected = [selectedWLanModel isEqual:model];
  97. cell.imgvWifi.highlighted = !EQUALS(model.security, @"none");
  98. return cell;
  99. }
  100. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  101. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  102. BLEWLanModel *model = [_wifiList objectAtIndex:indexPath.row];
  103. // selectedWLanModel = model;
  104. [_tableView reloadData];
  105. }
  106. //#pragma mark - CustomCheckBox Delegate {
  107. //- (void)didCheckBoxClicked:(id)sender {
  108. //
  109. // CustomCheckBox *btn = sender;
  110. // btn.selected = !btn.selected;
  111. //
  112. // selectedWLanModel = btn.value;
  113. // _btnNext.enabled = YES;
  114. //
  115. // [_tableView reloadData];
  116. //
  117. //}
  118. #pragma mark - user Event
  119. - (IBAction)btnCancelTouched:(id)sender {
  120. [self.navigationController popToRootViewControllerAnimated:YES];
  121. }
  122. - (IBAction)btnNextTouched:(id)sender {
  123. if (selectedWLanModel != nil) {
  124. if (EQUALS(selectedWLanModel.security, @"none")) {
  125. [self startLoading:YES];
  126. [bleService enableDHCP];
  127. [bleService readAndNotifyCharacteristicUUID:kBLEChrStDHCPArg isNotify:NO];
  128. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  129. }
  130. else {
  131. [bleService setWiFiSSID:selectedWLanModel.ssid];
  132. [bleService readAndNotifyCharacteristicUUID:kBLEChrStSSIDArg
  133. isNotify:NO];
  134. }
  135. }
  136. }
  137. #pragma mark - ble delegate
  138. - (void)BLEWiFiSSIDUpdate:(NSString*)ssid {
  139. NSLog(@"selectedWLanModel.ssid : %@", selectedWLanModel.ssid);
  140. NSLog(@"ssid : %@", ssid);
  141. if (EQUALS(selectedWLanModel.ssid, ssid)) {
  142. HomeHubWifiPasswdInputViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubWifiPasswdInputViewController" storyboardName:@"HomeHub"];
  143. vc.selectedWLanModel = selectedWLanModel;
  144. [self.navigationController pushViewController:vc animated:YES];
  145. }
  146. else {
  147. //실패처리
  148. [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패"
  149. message:@"인터넷 연결 설정에 실패하였습니다."];
  150. }
  151. }
  152. //enableDHCP
  153. - (void)BLEWiFiDHCPUpdate:(id)data {
  154. [bleService applyWiFiSettingInfo];
  155. [bleService readAndNotifyCharacteristicUUID:kBLEChrRdConInfo isNotify:YES];
  156. }
  157. //kBLEChrRdConInfo
  158. - (void)BLEWiFiConnectionUpdate:(CBCharacteristic *)info {
  159. NSLog(@"Connection Update!");
  160. NSString *value = [bleService hexStringValue:info] ;
  161. if (value.length > 0 &&
  162. EQUALS([value substringFromIndex:value.length-1], @"1")) {
  163. [bleService readConnectionWiFiInfo];
  164. }
  165. }
  166. //readConnectionWiFiInfo
  167. - (void)BLEWiFiConnectionInfoUpdateWithKey:(NSString *)kBLEChr
  168. result:(NSString *)result {
  169. NSLog(@"Connection Info Update!") ;
  170. if (EQUALS(kBLEChr, kBLEChrRdSSID)) {
  171. updateSSID = YES;
  172. }
  173. else if(EQUALS(kBLEChr, kBLEChrRdBSSID)) {
  174. updateBSSID = YES;
  175. }
  176. else if(EQUALS(kBLEChr, kBLEChrRnIpSet)) {
  177. updateIPSet = YES;
  178. }
  179. else if(EQUALS(kBLEChr, kBLEChrRnIpAddr)) {
  180. updateIPAddr = YES;
  181. }
  182. // 연결 확인 완료
  183. if (updateSSID && updateBSSID && updateIPSet && updateIPAddr) {
  184. [self startLoading:NO];
  185. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];
  186. [self.navigationController pushViewController:vc animated:YES];
  187. }
  188. }
  189. @end