ThingsAddIncludeViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. CommandModel *_startCommand;
  20. CommandModel *_stopCommand;
  21. }
  22. @end
  23. // 가스밸브 타이틀 : 가스 밸브 센서 추가
  24. // 가스밸브 장치초기화 코멘트 : 밸브를 열림 상태에 두고 "위로" 버튼을 비프음이 날 때까지 5초 이상 누르세요.
  25. // 가스밸브 이미지 명 : img_things_product_addimg_01_smartgasvalve_wait
  26. // 도어센서 타이틀 : 도어 센서 추가
  27. // 도어센서 장치초기화 코멘트 : 센서 아래 버튼을 1회 누른 후 LED가 깜빡임을 멈추면 1회 더 누르세요.
  28. // 도어센서 이미지 명 : img_things_product_addimg_02_mutisensor_door_wait
  29. // 스마트플러그 타이틀 : 스마트 플러그 추가
  30. // 스마트플러그 장치초기화 코멘트 : 위에 버튼에 빨간 불이 깜빡일 때까지 5초 이상 누르세요.
  31. // 스마트플러그 이미지 명 : img_things_product_addimg_03_smartplug_wait
  32. #pragma mark - Class Definition
  33. @implementation ThingsAddIncludeViewController
  34. - (void)viewDidLoad {
  35. [super viewDidLoad];
  36. // Do any additional setup after loading the view.
  37. [self initUI];
  38. [self prepareViewDidLoad];
  39. }
  40. - (void)initUI {
  41. [self startLoading:NO];
  42. }
  43. - (void)viewDidAppear:(BOOL)animated {
  44. [super viewDidAppear:animated];
  45. [UIView animateWithDuration:kfAnimationDur animations:^{
  46. _maskView.alpha = 0.7;
  47. } completion:^(BOOL finished) {
  48. _constraintPopViewTop.constant = (IPHONE_HEIGHT - _popView.height) / 2;
  49. [UIView animateWithDuration:kfAnimationDur animations:^{
  50. [self.view layoutIfNeeded];
  51. }];
  52. }];
  53. }
  54. - (void)prepareViewDidLoad {
  55. if (_addableDevice) {
  56. _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _addableDevice[@"deviceName"]];
  57. [self requestIncludeDevice:YES];
  58. } else if (_removableDevice) {
  59. _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _removableDevice.deviceName];
  60. [self requestExcludeDevice:YES];
  61. }
  62. }
  63. #pragma mark - Main Logic
  64. - (void)requestIncludeDevice:(BOOL)isStart {
  65. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_DEVICE_COMMAND];
  66. NSDictionary *param = @{@"device_id": [[JDFacade facade].loginUser getHomeHubID],
  67. @"command_type":isStart? CMD_TYPE_PAIRING_START:CMD_TYPE_PAIRING_STOP,
  68. @"cust_id": [[JDFacade facade].loginUser custId],
  69. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  70. [request setRequestMsg:param];
  71. [self sendDataToSocket:request modelClass:[CommandModel class]];
  72. }
  73. - (void)requestExcludeDevice:(BOOL)isStart {
  74. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_DEVICE_COMMAND];
  75. NSDictionary *param = @{@"device_id": [[JDFacade facade].loginUser getHomeHubID],
  76. @"command_type":isStart? CMD_TYPE_UNPAIRING_START:CMD_TYPE_UNPAIRING_STOP,
  77. @"cust_id": [[JDFacade facade].loginUser custId],
  78. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  79. [request setRequestMsg:param];
  80. [self sendDataToSocket:request modelClass:[CommandModel class]];
  81. }
  82. -(void)startLoading:(BOOL)isStart {
  83. _lblElapsedTime.hidden = !isStart;
  84. _imgLoading.hidden = !isStart;
  85. if (isStart) {
  86. _elapsedSeconds = 60;
  87. NSLog(@"ElapsedSecond : %zd", _elapsedSeconds);
  88. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  89. if (!_timer) {
  90. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  91. }
  92. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  93. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  94. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  95. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  96. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  97. animation.duration = 2.0f;
  98. animation.repeatCount = INFINITY;
  99. [_imgLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  100. }
  101. });
  102. }
  103. else {
  104. _elapsedSeconds = 60;
  105. if (_timer) {
  106. [_timer invalidate];
  107. _timer = nil;
  108. }
  109. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  110. [_imgLoading.layer removeAnimationForKey:@"SpinAnimation"];
  111. }
  112. }
  113. }
  114. - (void)updateInclusionStatus {
  115. dispatch_async(dispatch_get_main_queue(), ^(void) {
  116. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  117. _elapsedSeconds--;
  118. if (_elapsedSeconds == 0) {
  119. [self didFailInclusion];
  120. }
  121. });
  122. }
  123. - (void)didFailExclusion {
  124. [self startLoading:NO];
  125. [self requestIncludeDevice:NO];
  126. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  127. vc.addableDevice = _addableDevice;
  128. vc.providesPresentationContextTransitionStyle = YES;
  129. vc.definesPresentationContext = YES;
  130. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  131. UIViewController *pvc = self.presentingViewController;
  132. [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  133. [pvc presentViewController:vc animated:YES completion:nil];
  134. }];
  135. }
  136. - (void)didFailInclusion {
  137. [self startLoading:NO];
  138. [self requestIncludeDevice:NO];
  139. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  140. vc.addableDevice = _addableDevice;
  141. vc.providesPresentationContextTransitionStyle = YES;
  142. vc.definesPresentationContext = YES;
  143. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  144. UIViewController *pvc = self.presentingViewController;
  145. [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  146. [pvc presentViewController:vc animated:YES completion:nil];
  147. }];
  148. }
  149. #pragma mark - UI Events
  150. - (IBAction)btnCancelTouched:(id)sender {
  151. [self startLoading:NO];
  152. [UIView animateWithDuration:kfAnimationDur animations:^{
  153. _maskView.alpha = 0.0;
  154. } completion:^(BOOL finished) {
  155. [[JDFacade facade] dismissModalStack:NO completion:^{
  156. [[JDFacade facade] toast:@"취소되었습니다"];
  157. }];
  158. }];
  159. }
  160. #pragma mark - SocketService
  161. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
  162. {
  163. NSLog(@"Result Info : %@", result);
  164. NSLog(@"Result Info : %@", message);
  165. // if (result.messageType) {
  166. // <#statements#>
  167. // }
  168. NSLog(@"Message Type : %@", result.messageType);
  169. if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_DEVICE_COMMAND]) {
  170. CommandModel *response = (CommandModel *)message;
  171. if (result.isSuccess) {
  172. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  173. _startCommand = response;
  174. }
  175. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  176. _stopCommand = response;
  177. }
  178. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  179. _startCommand = response;
  180. }
  181. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  182. _stopCommand = response;
  183. }
  184. }
  185. else {
  186. [self didFailInclusion];
  187. }
  188. }
  189. }
  190. - (void) socketDidFailWithError:(NSError *)error {
  191. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  192. }
  193. -(void) receiveSocketData:(NSNotification *)notification {
  194. SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
  195. CommandModel *response = [[CommandModel alloc] initWithDictionary:notification.object error:nil];
  196. NSLog(@"Result : %@", result);
  197. NSLog(@"Response : %@", response);
  198. if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_DEVICE_COMMAND_RES]) {
  199. NSLog(@"Response : %@", response);
  200. if (result.isSuccess) {
  201. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  202. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  203. [self startLoading:YES];
  204. }
  205. }
  206. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  207. [self startLoading:NO];
  208. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  209. vc.isDeleteMode = NO;
  210. vc.providesPresentationContextTransitionStyle = YES;
  211. vc.definesPresentationContext = YES;
  212. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  213. [self presentViewController:vc animated:YES completion:^{
  214. _maskView.alpha = 0.0f;
  215. }];
  216. }
  217. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  218. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  219. [self startLoading:YES];
  220. }
  221. }
  222. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  223. [self startLoading:NO];
  224. // ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  225. // vc.isDeleteMode = NO;
  226. //
  227. // vc.providesPresentationContextTransitionStyle = YES;
  228. // vc.definesPresentationContext = YES;
  229. //
  230. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  231. //
  232. // [self presentViewController:vc animated:YES completion:^{
  233. // _maskView.alpha = 0.0f;
  234. // }];
  235. // TODO : 장치 삭제 완료 페이지로 이동
  236. }
  237. }
  238. else {
  239. [self didFailInclusion];
  240. }
  241. }
  242. else if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_NODE_REGIST_RES]) {
  243. [self requestIncludeDevice:NO];
  244. }
  245. }
  246. #pragma mark - MemoryWarning
  247. - (void)didReceiveMemoryWarning
  248. {
  249. [super didReceiveMemoryWarning];
  250. // Dispose of any resources that can be recreated.
  251. }
  252. @end