ThingsAddIncludeViewController.m 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. //
  2. // ThingsAddIncludeViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 11/13/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "DeviceModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomButton.h"
  13. #import "ThingsAddIncludeViewController.h"
  14. #import "ThingsAddFailViewController.h"
  15. #import "ThingsAddCompleteViewController.h"
  16. @interface ThingsAddIncludeViewController () {
  17. NSTimer *_timer;
  18. NSInteger _elapsedSeconds;
  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. #pragma mark - Class Definition
  31. @implementation ThingsAddIncludeViewController
  32. - (void)viewDidLoad {
  33. [super viewDidLoad];
  34. // Do any additional setup after loading the view.
  35. [self initUI];
  36. [self prepareViewDidLoad];
  37. }
  38. - (void)initUI {
  39. _elapsedSeconds = 60;
  40. }
  41. - (void)viewDidAppear:(BOOL)animated {
  42. [super viewDidAppear:animated];
  43. [UIView animateWithDuration:kfAnimationDur animations:^{
  44. _maskView.alpha = 0.7;
  45. } completion:^(BOOL finished) {
  46. _constraintPopViewTop.constant = (IPHONE_HEIGHT - _popView.height) / 2;
  47. [UIView animateWithDuration:kfAnimationDur animations:^{
  48. [self.view layoutIfNeeded];
  49. }];
  50. }];
  51. }
  52. - (void)prepareViewDidLoad {
  53. if (_addableDevice) {
  54. _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _addableDevice[@"deviceName"]];
  55. [self requestIncludeDevice];
  56. } else if (_removableDevice) {
  57. _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _removableDevice.deviceName];
  58. [self requestExcludeDevice];
  59. }
  60. }
  61. #pragma mark - Main Logic
  62. - (void)requestIncludeDevice {
  63. NSLog(@"웹소켓 날리기");
  64. // NSString *path = [NSString stringWithFormat:API_POST_DEVICE_INCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
  65. //
  66. // [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[DeviceModel class] completion:^(id responseObject) {
  67. // if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  68. // return;
  69. // }
  70. //
  71. // DeviceModel *device = (DeviceModel *)responseObject;
  72. //
  73. // if (device) {//API 성공 ,
  74. // [self requestCheckInclustion:device.commandId];
  75. // }
  76. // } failure:^(id errorObject) {
  77. // JDErrorModel *error = (JDErrorModel *)errorObject;
  78. // [[JDFacade facade] alert:error.errorMessage];
  79. // }];
  80. }
  81. - (void)requestExcludeDevice {
  82. NSString *path = [NSString stringWithFormat:API_DELETE_DEVICE_EXCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
  83. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[DeviceModel class] showLoadingView:YES completion:^(id responseObject) {
  84. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  85. return;
  86. }
  87. DeviceModel *device = (DeviceModel *)responseObject;
  88. if (device) {//API 성공 ,
  89. [self requestCheckInclustion:device.commandId];
  90. }
  91. } failure:^(id errorObject) {
  92. JDErrorModel *error = (JDErrorModel *)errorObject;
  93. [[JDFacade facade] alert:error.errorMessage];
  94. }];
  95. }
  96. - (void)requestCheckInclustion:(NSString *)commandId {
  97. if (!_timer) {
  98. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  99. // _timer = [NSTimer timerWithTimeInterval:1.0f
  100. // target:self
  101. // selector:@selector(updateInclusionStatus)
  102. // userInfo:nil repeats:YES];
  103. // [[NSRunLoop mainRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
  104. }
  105. //parameters
  106. NSDictionary *parameter = @{@"command_id": commandId};
  107. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_COMMAND, [JDFacade facade].loginUser.homehubDeviceId];
  108. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestGET parameters:parameter
  109. modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
  110. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  111. return;
  112. }
  113. DeviceModel *device = (DeviceModel *)responseObject;
  114. if (device) {//API 성공 ,
  115. if ([device.status isEqualToString:KNEET_DEVICE_WAITING]) {
  116. if (_elapsedSeconds > 2) {
  117. [self performSelector:@selector(requestCheckInclustion:) withObject:commandId afterDelay:3.0f];
  118. }
  119. //retry
  120. return;
  121. } else if ([device.status isEqualToString:KNEET_DEVICE_INCLUSION] || [device.status isEqualToString:KNEET_DEVICE_EXCLUSION]) {
  122. //success
  123. if (_timer) {
  124. [_timer invalidate];
  125. _timer = nil;
  126. }
  127. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  128. vc.isDeleteMode = [device.status isEqualToString:KNEET_DEVICE_EXCLUSION];
  129. vc.providesPresentationContextTransitionStyle = YES;
  130. vc.definesPresentationContext = YES;
  131. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  132. [self presentViewController:vc animated:YES completion:nil];
  133. }
  134. }
  135. } failure:^(id errorObject) {
  136. JDErrorModel *error = (JDErrorModel *)errorObject;
  137. [[JDFacade facade] alert:error.errorMessage completionHander:^{
  138. [self didFailInclusion];
  139. }];
  140. }];
  141. }
  142. - (void)didFailInclusion {
  143. if (_timer) {
  144. [_timer invalidate];
  145. _timer = nil;
  146. }
  147. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  148. vc.addableDevice = _addableDevice;
  149. vc.providesPresentationContextTransitionStyle = YES;
  150. vc.definesPresentationContext = YES;
  151. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  152. UIViewController *pvc = self.presentingViewController;
  153. [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  154. [pvc presentViewController:vc animated:YES completion:nil];
  155. }];
  156. }
  157. - (void)updateInclusionStatus {
  158. dispatch_async(dispatch_get_main_queue(), ^(void) {
  159. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  160. _elapsedSeconds--;
  161. if (_elapsedSeconds == 0) {
  162. [self didFailInclusion];
  163. }
  164. });
  165. }
  166. #pragma mark - UI Events
  167. - (IBAction)btnCancelTouched:(id)sender {
  168. if (_timer) {
  169. [_timer invalidate];
  170. _timer = nil;
  171. }
  172. [UIView animateWithDuration:kfAnimationDur animations:^{
  173. _maskView.alpha = 0.0;
  174. } completion:^(BOOL finished) {
  175. [[JDFacade facade] dismissModalStack:NO completion:^{
  176. [[JDFacade facade] toast:@"취소되었습니다"];
  177. }];
  178. }];
  179. }
  180. #pragma mark - MemoryWarning
  181. - (void)didReceiveMemoryWarning
  182. {
  183. [super didReceiveMemoryWarning];
  184. // Dispose of any resources that can be recreated.
  185. }
  186. @end