ThingsAddInitViewController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. //
  2. // ThingsInitViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 3. 20..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "ThingsAddInitViewController.h"
  9. #import "CustomButton.h"
  10. #import "CustomLabel.h"
  11. #import "CustomImageView.h"
  12. #import "ThingsAddStartViewController.h"
  13. #import "ThingsAddInitCompleteViewController.h"
  14. #import "ThingsAddInitFailViewController.h"
  15. @interface ThingsAddInitViewController (){
  16. NSTimer *_timer;
  17. NSInteger _elapsedSeconds;
  18. CommandModel *_startCommand;
  19. CommandModel *_stopCommand;
  20. }
  21. @end
  22. // 가스밸브 타이틀 : 가스 밸브 센서 초기화
  23. // 가스밸브 장치초기화 코멘트 : 밸브를 열림 상태에 두고 "위로" 버튼을 비프음이 날 때까지 5초 이상 누르세요.
  24. // 가스밸브 이미지 명 : img_things_product_addimg_01_smartgasvalve_wait
  25. // 도어센서 타이틀 : 도어 센서 초기화
  26. // 도어센서 장치초기화 코멘트 : 센서 아래 버튼을 1회 누른 후 LED가 깜빡임을 멈추면 1회 더 누르세요.
  27. // 도어센서 이미지 명 : img_things_product_addimg_02_mutisensor_door_wait
  28. // 스마트플러그 타이틀 : 스마트 플러그 초기화
  29. // 스마트플러그 장치초기화 코멘트 : 위에 버튼에 빨간 불이 깜빡일 때까지 5초 이상 누르세요.
  30. // 스마트플러그 이미지 명 : img_things_product_addimg_03_smartplug_wait
  31. @implementation ThingsAddInitViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. [self initUI];
  35. [self prepareViewDidLoad];
  36. }
  37. - (void)viewDidAppear:(BOOL)animated {
  38. [super viewDidAppear:animated];
  39. [UIView animateWithDuration:kfAnimationDur animations:^{
  40. _maskView.alpha = 0.7;
  41. } completion:^(BOOL finished) {
  42. // _constraintPopViewTop.constant = (IPHONE_HEIGHT - _popView.height) / 2;
  43. [UIView animateWithDuration:kfAnimationDur animations:^{
  44. [self.view layoutIfNeeded];
  45. }];
  46. }];
  47. }
  48. - (void)initUI {
  49. // [self initTableViewAsDefaultStyle:_tableView];
  50. [self startLoading:NO];
  51. }
  52. - (void)prepareViewDidLoad {
  53. _imgThings.image = [_addDevice imgaeForAddDel];
  54. _lblTitle.text = [NSString stringWithFormat:@"%@ 준비", _addDevice.prdName];
  55. _lblComment.text = [_addDevice getAddDelDescription];
  56. [self requestExcludeDevice:YES];
  57. }
  58. #pragma mark - Main Logic
  59. - (void)requestExcludeDevice:(BOOL)isStart {
  60. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_DEVICE_COMMAND];
  61. NSDictionary *param = @{@"device_id": [[JDFacade facade].loginUser isMultiHomeHub] ? _selectHub.deviceId:[[JDFacade facade].loginUser getHomeHubID],
  62. @"command_type":isStart? CMD_TYPE_UNPAIRING_START:CMD_TYPE_UNPAIRING_STOP,
  63. @"cust_id": [[JDFacade facade].loginUser custId],
  64. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  65. [request setRequestMsg:param];
  66. [self sendDataToSocket:request modelClass:[CommandModel class]];
  67. }
  68. -(void)startLoading:(BOOL)isStart {
  69. _lblElapsedTime.hidden = !isStart;
  70. _imgLoading.hidden = !isStart;
  71. if (isStart) {
  72. _elapsedSeconds = kMaxTimeOut;
  73. NSLog(@"ElapsedSecond : %zd", _elapsedSeconds);
  74. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  75. if (!_timer) {
  76. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  77. }
  78. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  79. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  80. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  81. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  82. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  83. animation.duration = 2.0f;
  84. animation.repeatCount = INFINITY;
  85. [_imgLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  86. }
  87. });
  88. }
  89. else {
  90. _elapsedSeconds = kMaxTimeOut;
  91. if (_timer) {
  92. [_timer invalidate];
  93. _timer = nil;
  94. }
  95. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  96. [_imgLoading.layer removeAnimationForKey:@"SpinAnimation"];
  97. }
  98. }
  99. }
  100. - (void)updateInclusionStatus {
  101. dispatch_async(dispatch_get_main_queue(), ^(void) {
  102. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  103. _elapsedSeconds--;
  104. if (_elapsedSeconds == 0) {
  105. [self finishDeviceDel:NO];
  106. }
  107. });
  108. }
  109. - (void)finishDeviceDel:(BOOL)isSuccess {
  110. [self startLoading:NO];
  111. // [self requestExcludeDevice:NO];
  112. // Thi *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsGeneralDelCompleteViewController" storyboardName:@"Things"];
  113. // vc.add = _delDevice;
  114. // vc.isSuccess = isSuccess;
  115. // // vc.addableDevice = _addableDevice;
  116. //
  117. // [self showTransparencyModalView:vc];
  118. if (isSuccess) {
  119. ThingsAddInitCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddInitCompleteViewController" storyboardName:@"Things"];
  120. vc.addDevice = _addDevice;
  121. vc.selectHub = _selectHub;
  122. [self showTransparencyModalView:vc];
  123. } else {
  124. ThingsAddInitFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddInitFailViewController" storyboardName:@"Things"];
  125. vc.addDevice = _addDevice;
  126. vc.selectHub = _selectHub;
  127. [self showTransparencyModalView:vc];
  128. }
  129. }
  130. - (IBAction)btnCancelTouched:(id)sender {
  131. [self startLoading:NO];
  132. [self requestExcludeDevice:NO];
  133. ThingsAddStartViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddStartViewController" storyboardName:@"Things"];
  134. vc.addDevice = _addDevice;
  135. vc.selectHub = _selectHub;
  136. [self showTransparencyModalView:vc];
  137. }
  138. #pragma mark - SocketService
  139. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
  140. {
  141. // if (result.messageType) {
  142. // <#statements#>
  143. // }
  144. NSLog(@"Message Type : %@", result.messageType);
  145. if ([result.messageType isEqualToIgnoreCase:MSG_TYPE_DEVICE_COMMAND]) {
  146. CommandModel *response = (CommandModel *)message;
  147. if (result.isSuccess) {
  148. if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  149. _startCommand = response;
  150. }
  151. else if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  152. _stopCommand = response;
  153. }
  154. }
  155. else {
  156. [self finishDeviceDel:NO];
  157. }
  158. }
  159. }
  160. - (void) socketDidFailWithError:(NSError *)error {
  161. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  162. }
  163. -(void) receiveSocketData:(NSNotification *)notification {
  164. SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
  165. CommandModel *response = [[CommandModel alloc] initWithDictionary:notification.object error:nil];
  166. //NSLog(@"Result : %@", result);
  167. //NSLog(@"Response : %@", response);
  168. if ([result.messageType isEqualToIgnoreCase:MSG_TYPE_DEVICE_COMMAND_RES]) {
  169. //NSLog(@"Response : %@", response);
  170. if (result.isSuccess) {
  171. if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  172. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  173. [self startLoading:YES];
  174. }
  175. }
  176. else if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  177. // [self startLoading:NO];
  178. [self finishDeviceDel:YES];
  179. }
  180. }
  181. else {
  182. [self finishDeviceDel:NO];
  183. }
  184. }
  185. else if ([result.messageType isEqualToIgnoreCase:MSG_TYPE_NODE_DELETE_RES]) {
  186. // [self requestExcludeDevice:NO];
  187. [self finishDeviceDel:YES];
  188. }
  189. }
  190. - (void)didReceiveMemoryWarning {
  191. [super didReceiveMemoryWarning];
  192. }
  193. @end