ThingsAddIncludeViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. [self startLoading:NO];
  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. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_DEVICE_COMMAND];
  65. NSLog(@"Device ID : %@", [[JDFacade facade].loginUser getHomeHubID]);
  66. NSDictionary *param = @{@"device_id": [[JDFacade facade].loginUser getHomeHubID],
  67. @"command_type": CMD_TYPE_PAIRING_START,
  68. @"cust_id": [[JDFacade facade].loginUser custId],
  69. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  70. [request setRequestMsg:param];
  71. [self sendDataToSocket:request modelClass:nil];
  72. [self startLoading:YES];
  73. // NSString *path = [NSString stringWithFormat:API_POST_DEVICE_INCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
  74. //
  75. // [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[DeviceModel class] completion:^(id responseObject) {
  76. // if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  77. // return;
  78. // }
  79. //
  80. // DeviceModel *device = (DeviceModel *)responseObject;
  81. //
  82. // if (device) {//API 성공 ,
  83. // [self requestCheckInclustion:device.commandId];
  84. // }
  85. // } failure:^(id errorObject) {
  86. // JDErrorModel *error = (JDErrorModel *)errorObject;
  87. // [[JDFacade facade] alert:error.errorMessage];
  88. // }];
  89. }
  90. -(void)startLoading:(BOOL)isStart {
  91. _lblElapsedTime.hidden = !isStart;
  92. _imgLoading.hidden = !isStart;
  93. if (isStart) {
  94. _elapsedSeconds = 60;
  95. NSLog(@"ElapsedSecond : %zd", _elapsedSeconds);
  96. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  97. if (!_timer) {
  98. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  99. }
  100. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  101. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  102. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  103. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  104. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  105. animation.duration = 2.0f;
  106. animation.repeatCount = INFINITY;
  107. [_imgLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  108. }
  109. });
  110. }
  111. else {
  112. if (_timer) {
  113. [_timer invalidate];
  114. _timer = nil;
  115. }
  116. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  117. [_imgLoading.layer removeAnimationForKey:@"SpinAnimation"];
  118. }
  119. }
  120. }
  121. - (void)updateInclusionStatus {
  122. dispatch_async(dispatch_get_main_queue(), ^(void) {
  123. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  124. _elapsedSeconds--;
  125. if (_elapsedSeconds == 0) {
  126. [self didFailInclusion];
  127. }
  128. });
  129. }
  130. - (void)requestExcludeDevice {
  131. NSString *path = [NSString stringWithFormat:API_DELETE_DEVICE_EXCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
  132. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[DeviceModel class] showLoadingView:YES completion:^(id responseObject) {
  133. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  134. return;
  135. }
  136. DeviceModel *device = (DeviceModel *)responseObject;
  137. if (device) {//API 성공 ,
  138. [self requestCheckInclustion:device.commandId];
  139. }
  140. } failure:^(id errorObject) {
  141. JDErrorModel *error = (JDErrorModel *)errorObject;
  142. [[JDFacade facade] alert:error.errorMessage];
  143. }];
  144. }
  145. - (void)requestCheckInclustion:(NSString *)commandId {
  146. //parameters
  147. NSDictionary *parameter = @{@"command_id": commandId};
  148. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_COMMAND, [JDFacade facade].loginUser.homehubDeviceId];
  149. [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestGET parameters:parameter
  150. modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
  151. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  152. return;
  153. }
  154. DeviceModel *device = (DeviceModel *)responseObject;
  155. if (device) {//API 성공 ,
  156. if ([device.status isEqualToString:KNEET_DEVICE_WAITING]) {
  157. if (_elapsedSeconds > 2) {
  158. [self performSelector:@selector(requestCheckInclustion:) withObject:commandId afterDelay:3.0f];
  159. }
  160. //retry
  161. return;
  162. } else if ([device.status isEqualToString:KNEET_DEVICE_INCLUSION] || [device.status isEqualToString:KNEET_DEVICE_EXCLUSION]) {
  163. //success
  164. if (_timer) {
  165. [_timer invalidate];
  166. _timer = nil;
  167. }
  168. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  169. vc.isDeleteMode = [device.status isEqualToString:KNEET_DEVICE_EXCLUSION];
  170. vc.providesPresentationContextTransitionStyle = YES;
  171. vc.definesPresentationContext = YES;
  172. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  173. [self presentViewController:vc animated:YES completion:^{
  174. _maskView.alpha = 0.0f;
  175. }];
  176. }
  177. }
  178. } failure:^(id errorObject) {
  179. JDErrorModel *error = (JDErrorModel *)errorObject;
  180. [[JDFacade facade] alert:error.errorMessage completionHander:^{
  181. [self didFailInclusion];
  182. }];
  183. }];
  184. }
  185. - (void)didFailInclusion {
  186. [self startLoading:NO];
  187. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  188. vc.addableDevice = _addableDevice;
  189. vc.providesPresentationContextTransitionStyle = YES;
  190. vc.definesPresentationContext = YES;
  191. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  192. UIViewController *pvc = self.presentingViewController;
  193. [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  194. [pvc presentViewController:vc animated:YES completion:nil];
  195. }];
  196. }
  197. #pragma mark - UI Events
  198. - (IBAction)btnCancelTouched:(id)sender {
  199. if (_timer) {
  200. [_timer invalidate];
  201. _timer = nil;
  202. }
  203. [UIView animateWithDuration:kfAnimationDur animations:^{
  204. _maskView.alpha = 0.0;
  205. } completion:^(BOOL finished) {
  206. [[JDFacade facade] dismissModalStack:NO completion:^{
  207. [[JDFacade facade] toast:@"취소되었습니다"];
  208. }];
  209. }];
  210. }
  211. #pragma mark - SocketService
  212. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
  213. {
  214. NSLog(@"Result Info : %@", result);
  215. NSLog(@"Result Info : %@", message);
  216. SWITCH(result.messageType)
  217. {
  218. CASE (MSG_TYPE_DEVICE_COMMAND)
  219. {
  220. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  221. if (result.isSuccess) {
  222. }
  223. else
  224. {
  225. }
  226. break;
  227. }
  228. CASE (@"auth.auto")
  229. {
  230. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  231. if (result.isSuccess) {
  232. }
  233. else
  234. {
  235. [[JDFacade facade] toast:result.resultMsg];
  236. }
  237. break;
  238. } DEFAULT
  239. {
  240. break;
  241. }
  242. }
  243. }
  244. - (void) socketDidFailWithError:(NSError *)error {
  245. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  246. }
  247. #pragma mark - MemoryWarning
  248. - (void)didReceiveMemoryWarning
  249. {
  250. [super didReceiveMemoryWarning];
  251. // Dispose of any resources that can be recreated.
  252. }
  253. @end