ThingsGeneralDelStartViewController.m 7.9 KB

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