HomeHubWifiSearchSuccessViewController.m 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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)viewWillAppear:(BOOL)animated {
  31. [super viewWillAppear:animated];
  32. bleService.delegate = self;
  33. }
  34. - (void)viewWillDisappear:(BOOL)animated {
  35. [super viewWillDisappear:animated];
  36. bleService.delegate = nil;
  37. }
  38. - (void)didReceiveMemoryWarning {
  39. [super didReceiveMemoryWarning];
  40. }
  41. - (void)initUI {
  42. _imgvLoading.hidden = YES;
  43. _btnNext.enabled = NO;
  44. [self initTableViewAsDefaultStyle:_tableView];
  45. }
  46. - (void)prepareViewDidLoad {
  47. //ble
  48. bleService = [BLEServiceHandler sharedManager];
  49. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  50. [_tableView reloadData];
  51. }
  52. -(void)startLoading:(BOOL)isStart {
  53. _imgvLoading.hidden = !isStart;
  54. if (isStart) {
  55. _elapsedSeconds = kMaxTimeOut;
  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. if (_elapsedSeconds == 0) {
  85. [self startLoading:NO];
  86. }
  87. });
  88. }
  89. #pragma mark - UITableView DataSource & Delegate
  90. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  91. return 1;
  92. }
  93. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  94. return _wifiList.count;
  95. }
  96. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  97. return 101.0f;
  98. }
  99. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  100. HomeHubWifiSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WifiSearchCellIdentifier"];
  101. BLEWLanModel *model = [_wifiList objectAtIndex:indexPath.row];
  102. cell.lblHubName.text = model.ssid;
  103. cell.chkBtn.selected = [selectedWLanModel isEqual:model];
  104. cell.imgvWifi.highlighted = !EQUALS(model.security, @"none");
  105. return cell;
  106. }
  107. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  108. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  109. BLEWLanModel *model = [_wifiList objectAtIndex:indexPath.row];
  110. selectedWLanModel = model;
  111. _btnNext.enabled = YES;
  112. [_tableView reloadData];
  113. }
  114. //#pragma mark - CustomCheckBox Delegate {
  115. //- (void)didCheckBoxClicked:(id)sender {
  116. //
  117. // CustomCheckBox *btn = sender;
  118. // btn.selected = !btn.selected;
  119. //
  120. // selectedWLanModel = btn.value;
  121. // _btnNext.enabled = YES;
  122. //
  123. // [_tableView reloadData];
  124. //
  125. //}
  126. #pragma mark - user Event
  127. - (IBAction)btnCancelTouched:(id)sender {
  128. [self.navigationController popToRootViewControllerAnimated:YES];
  129. }
  130. - (IBAction)btnNextTouched:(id)sender {
  131. NSLog(@"selectedWLanModel : %@", selectedWLanModel);
  132. if (selectedWLanModel != nil) {
  133. [bleService setWiFiSSID:selectedWLanModel.ssid];
  134. [bleService readAndNotifyCharacteristicUUID:kBLEChrStSSIDArg
  135. isNotify:NO];
  136. }
  137. }
  138. #pragma mark - ble delegate
  139. - (void)BLEWiFiSSIDUpdate:(NSString*)ssid {
  140. NSLog(@"\nwifi search seccess BLEWiFiSSIDUpdate ssid : %@", selectedWLanModel.ssid);
  141. NSLog(@"ssid : %@", ssid);
  142. if (EQUALS(selectedWLanModel.ssid, ssid)) {
  143. if (EQUALS(selectedWLanModel.security, @"none")) {
  144. [self startLoading:YES];
  145. [bleService setWiFiPwd:@""];
  146. [bleService enableDHCP];
  147. [bleService readAndNotifyCharacteristicUUID:kBLEChrStDHCPArg isNotify:NO];
  148. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  149. }
  150. else {
  151. HomeHubWifiPasswdInputViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubWifiPasswdInputViewController" storyboardName:@"HomeHub"];
  152. vc.selectedWLanModel = selectedWLanModel;
  153. [self.navigationController pushViewController:vc animated:YES];
  154. }
  155. }
  156. else {
  157. //실패처리
  158. [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패"
  159. message:@"인터넷 연결 설정에 실패하였습니다."];
  160. }
  161. }
  162. //enableDHCP
  163. - (void)BLEWiFiDHCPUpdate:(id)data {
  164. NSLog(@"\nwifi search seccess BLEWiFiDHCPUpdate!");
  165. [bleService applyWiFiSettingInfo];
  166. [self performSelector:@selector(afterApplyNotifyConnect) withObject:nil afterDelay:0.5];
  167. }
  168. - (void)afterApplyNotifyConnect {
  169. NSLog(@"\afterApplyNotifyConnect!");
  170. [bleService readAndNotifyCharacteristicUUID:kBLEChrRdConInfo isNotify:YES];
  171. }
  172. //kBLEChrRdConInfo
  173. - (void)BLEWiFiConnectionUpdate:(CBCharacteristic *)info {
  174. NSLog(@"\nwifi search seccess BLEWiFiConnectionUpdate!");
  175. NSString *value = [bleService hexStringValue:info] ;
  176. if (value.length > 0 &&
  177. EQUALS([value substringFromIndex:value.length-1], @"1")) {
  178. [bleService readConnectionWiFiInfo];
  179. }
  180. else if (value.length > 0 &&
  181. EQUALS([value substringFromIndex:value.length-1], @"0")) {
  182. //실패
  183. [self startLoading:NO];
  184. [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패" message:@"인터넷 연결 설정에 실패하였습니다."];
  185. }
  186. }
  187. //readConnectionWiFiInfo
  188. - (void)BLEWiFiConnectionInfoUpdateWithKey:(NSString *)kBLEChr
  189. result:(NSString *)result {
  190. NSLog(@"\nwifi search seccess BLEWiFiConnectionInfoUpdateWithKey") ;
  191. if (EQUALS(kBLEChr, kBLEChrRdSSID)) {
  192. updateSSID = YES;
  193. }
  194. else if(EQUALS(kBLEChr, kBLEChrRdBSSID)) {
  195. updateBSSID = YES;
  196. }
  197. else if(EQUALS(kBLEChr, kBLEChrRnIpSet)) {
  198. updateIPSet = YES;
  199. }
  200. else if(EQUALS(kBLEChr, kBLEChrRnIpAddr)) {
  201. updateIPAddr = YES;
  202. }
  203. // 연결 확인 완료
  204. if (updateSSID && updateBSSID && updateIPSet && updateIPAddr) {
  205. [self startLoading:NO];
  206. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];
  207. [self.navigationController pushViewController:vc animated:YES];
  208. }
  209. }
  210. @end