HomeHubSearchSuccessViewController.m 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. //
  2. // HomeHubSearchSuccessViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 11..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "HomeHubSearchSuccessViewController.h"
  9. #import "CustomTableView.h"
  10. #import "CustomCheckBox.h"
  11. #import "HomeHubConnectWifiViewController.h"
  12. @interface HomeHubSearchSuccessViewController ()<CustomCheckBoxDelegate> {
  13. BLEServiceHandler *bleService;
  14. NSMutableArray *devices;
  15. NSArray *_tableZombieValues;
  16. BTLEDeivceModel *selectedModel;
  17. NSTimer *_timer;
  18. NSInteger _elapsedSeconds;
  19. }
  20. @end
  21. @implementation HomeHubSearchTableViewCell
  22. @end
  23. @implementation HomeHubSearchSuccessViewController
  24. - (void)viewDidLoad {
  25. [super viewDidLoad];
  26. [self initUI];
  27. [self prepareViewDidLoad];
  28. }
  29. - (void)didReceiveMemoryWarning {
  30. [super didReceiveMemoryWarning];
  31. }
  32. - (void)viewWillAppear:(BOOL)animated{
  33. [super viewWillAppear:animated];
  34. bleService.delegate = self;
  35. }
  36. - (void)viewWillDisappear:(BOOL)animated {
  37. [super viewWillDisappear:animated];
  38. bleService.delegate = nil;
  39. }
  40. - (void)initUI {
  41. _btnNext.enabled = NO;
  42. _imgvLoading.hidden = YES;
  43. [self initTableViewAsDefaultStyle:_tableView];
  44. }
  45. - (void)prepareViewDidLoad {
  46. //ble
  47. bleService = [BLEServiceHandler sharedManager];
  48. devices = [bleService getDeviceList];
  49. if (devices != nil && devices.count > 0) {
  50. selectedModel = [devices objectAtIndex:0];
  51. _btnNext.enabled = YES;
  52. }
  53. [_tableView reloadData];
  54. }
  55. -(void)startLoading:(BOOL)isStart {
  56. _lblTimer.hidden = !isStart;
  57. _imgvLoading.hidden = !isStart;
  58. if (isStart) {
  59. _elapsedSeconds = kMaxTimeOut;
  60. _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  61. if (!_timer) {
  62. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  63. }
  64. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  65. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  66. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  67. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  68. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  69. animation.duration = 2.0f;
  70. animation.repeatCount = INFINITY;
  71. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  72. }
  73. });
  74. }
  75. else {
  76. _elapsedSeconds = kMaxTimeOut;
  77. if (_timer) {
  78. [_timer invalidate];
  79. _timer = nil;
  80. }
  81. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  82. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  83. }
  84. }
  85. }
  86. - (void)updateInclusionStatus {
  87. dispatch_async(dispatch_get_main_queue(), ^(void) {
  88. _elapsedSeconds--;
  89. if (_elapsedSeconds == 0) {
  90. }
  91. });
  92. }
  93. #pragma mark - UITableView DataSource & Delegate
  94. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  95. return 1;
  96. }
  97. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  98. return devices.count;
  99. }
  100. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  101. return 107.0f;
  102. }
  103. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  104. HomeHubSearchTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HubSearchCellIdentifier"];
  105. BTLEDeivceModel *bleModel = [devices objectAtIndex:indexPath.row];
  106. cell.lblHubName.text = bleModel.peripheralRef.name;
  107. cell.lblSerialNum.text = [NSString stringWithFormat:@"S/N : %@",bleModel.peripheralRef.identifier];
  108. cell.chkBtn.selected = [selectedModel isEqual:bleModel];
  109. return cell;
  110. }
  111. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  112. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  113. selectedModel = [devices objectAtIndex:indexPath.row];
  114. _btnNext.enabled = YES;
  115. [tableView reloadData];
  116. }
  117. #pragma mark - User Event
  118. - (IBAction)btnNextTouched:(id)sender {
  119. NSLog(@"selectedModel : %@", selectedModel);
  120. if (selectedModel != nil) {
  121. //[bleService setConDevice:selectedModel];
  122. [bleService connect:selectedModel];
  123. [self startLoading:YES];
  124. }
  125. }
  126. - (IBAction)btnCloseTouched:(id)sender {
  127. [self.navigationController popToRootViewControllerAnimated:YES];
  128. }
  129. #pragma mark - ble service delegate
  130. - (void)BLEConnected:(BTLEDeivceModel *)info {
  131. NSLog(@"home hub wifi search view connected!") ;
  132. [self startLoading:NO];
  133. HomeHubConnectWifiViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubConnectWifiViewController" storyboardName:@"HomeHub"];
  134. vc.selectedModel = selectedModel;
  135. [self.navigationController pushViewController:vc animated:YES];
  136. }
  137. - (void)BLEDisConnected:(BTLEDeivceModel *)info {
  138. [self startLoading:NO];
  139. [[JDFacade facade] toast:@"홈허브와 연결할 수 없습니다."];
  140. [self.navigationController popToRootViewControllerAnimated:YES];
  141. }
  142. @end