HomeHubWifiSearchSuccessViewController.m 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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*2;
  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. [self startLoading:NO];
  88. [[JDFacade facade] alert:@"네트워크 연결에 실패하였습니다." completionHander:^{
  89. [self.navigationController popViewControllerAnimated:NO];
  90. }];
  91. }
  92. });
  93. }
  94. #pragma mark - UITableView DataSource & Delegate
  95. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  96. return 1;
  97. }
  98. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  99. return _wifiList.count;
  100. }
  101. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. return 101.0f;
  103. }
  104. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  105. HomeHubWifiSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WifiSearchCellIdentifier"];
  106. BLEWLanModel *model = [_wifiList objectAtIndex:indexPath.row];
  107. cell.lblHubName.text = model.ssid;
  108. cell.chkBtn.selected = [selectedWLanModel isEqual:model];
  109. cell.imgvWifi.highlighted = !EQUALS(model.security, @"none");
  110. return cell;
  111. }
  112. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  113. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  114. BLEWLanModel *model = [_wifiList objectAtIndex:indexPath.row];
  115. selectedWLanModel = model;
  116. _btnNext.enabled = YES;
  117. [_tableView reloadData];
  118. }
  119. //#pragma mark - CustomCheckBox Delegate {
  120. //- (void)didCheckBoxClicked:(id)sender {
  121. //
  122. // CustomCheckBox *btn = sender;
  123. // btn.selected = !btn.selected;
  124. //
  125. // selectedWLanModel = btn.value;
  126. // _btnNext.enabled = YES;
  127. //
  128. // [_tableView reloadData];
  129. //
  130. //}
  131. #pragma mark - user Event
  132. - (IBAction)btnCancelTouched:(id)sender {
  133. [self.navigationController popToRootViewControllerAnimated:YES];
  134. }
  135. - (IBAction)btnNextTouched:(id)sender {
  136. NSLog(@"selectedWLanModel : %@", selectedWLanModel);
  137. if (selectedWLanModel != nil) {
  138. [bleService setWiFiSSID:selectedWLanModel.ssid];
  139. [bleService readAndNotifyCharacteristicUUID:kBLEChrStSSIDArg
  140. isNotify:NO];
  141. }
  142. }
  143. #pragma mark - ble delegate
  144. - (void)BLEWiFiSSIDUpdate:(NSString*)ssid {
  145. NSLog(@"\nwifi search seccess BLEWiFiSSIDUpdate ssid : %@", selectedWLanModel.ssid);
  146. NSLog(@"ssid : %@", ssid);
  147. if (EQUALS(selectedWLanModel.ssid, ssid)) {
  148. if (EQUALS(selectedWLanModel.security, @"none")) {
  149. [self startLoading:YES];
  150. //1. write pass
  151. [bleService setWiFiPwd:@""];
  152. [self performSelector:@selector(BLESendEnableDHCP) withObject:nil afterDelay:1.0];
  153. updateSSID = updateBSSID = updateIPSet = updateIPAddr = NO;
  154. }
  155. else {
  156. HomeHubWifiPasswdInputViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubWifiPasswdInputViewController" storyboardName:@"HomeHub"];
  157. vc.selectedWLanModel = selectedWLanModel;
  158. [self.navigationController pushViewController:vc animated:YES];
  159. }
  160. }
  161. else {
  162. //실패처리
  163. [[JDFacade facade] alertTitle:@"인터넷 연결 설정 실패"
  164. message:@"인터넷 연결 설정에 실패하였습니다."];
  165. }
  166. }
  167. - (void)BLESendEnableDHCP {
  168. //2. set ip set arg chr
  169. [bleService enableDHCP];
  170. [bleService readAndNotifyCharacteristicUUID:kBLEChrStDHCPArg isNotify:NO];
  171. }
  172. //enableDHCP
  173. - (void)BLEWiFiDHCPUpdate:(id)data {
  174. //3. apply chr
  175. [bleService applyWiFiSettingInfo];
  176. }
  177. //kBLEChrRdConInfo
  178. - (void)BLEWiFiConnectionUpdate:(CBCharacteristic *)info {
  179. NSLog(@"\nwifi search seccess BLEWiFiConnectionUpdate!");
  180. NSString *value = [bleService hexStringValue:info] ;
  181. if (value.length > 0 &&
  182. EQUALS([value substringFromIndex:value.length-1], @"1")) {
  183. //5.
  184. [bleService readConnectionWiFiInfo];
  185. }
  186. else if (value.length > 0 &&
  187. EQUALS([value substringFromIndex:value.length-1], @"0")) {
  188. }
  189. }
  190. //readConnectionWiFiInfo
  191. - (void)BLEWiFiConnectionInfoUpdateWithKey:(NSString *)kBLEChr
  192. result:(NSString *)result {
  193. NSLog(@"\nwifi search seccess BLEWiFiConnectionInfoUpdateWithKey") ;
  194. if (EQUALS(kBLEChr, kBLEChrRdSSID)) {
  195. updateSSID = YES;
  196. }
  197. else if(EQUALS(kBLEChr, kBLEChrRdBSSID)) {
  198. updateBSSID = YES;
  199. }
  200. else if(EQUALS(kBLEChr, kBLEChrRnIpSet)) {
  201. updateIPSet = YES;
  202. }
  203. else if(EQUALS(kBLEChr, kBLEChrRnIpAddr)) {
  204. updateIPAddr = YES;
  205. }
  206. // 연결 확인 완료
  207. if (updateSSID && updateBSSID && updateIPSet && updateIPAddr) {
  208. [self startLoading:NO];
  209. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];
  210. [self.navigationController pushViewController:vc animated:YES];
  211. }
  212. }
  213. - (void)BLEDisConnected:(BTLEDeivceModel *)info {
  214. [[JDFacade facade] toast:@"홈허브와 연결할 수 없습니다."];
  215. [self.navigationController popToRootViewControllerAnimated:YES];
  216. }
  217. @end