ThingsAddIncludeViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. _lblTitle.text = [NSString stringWithFormat:@"%@ 추가", _addDevice.prdName];
  63. _imgThings.image = [_addDevice imgaeForAddDel];
  64. _lblDesc.text = [_addDevice manufacturerName];
  65. [self requestIncludeDevice:YES];
  66. }
  67. #pragma mark - Main Logic
  68. - (void)requestIncludeDevice:(BOOL)isStart {
  69. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_DEVICE_COMMAND];
  70. NSDictionary *param = @{@"device_id": [[JDFacade facade].loginUser getHomeHubID],
  71. @"command_type":isStart? CMD_TYPE_PAIRING_START:CMD_TYPE_PAIRING_STOP,
  72. @"cust_id": [[JDFacade facade].loginUser custId],
  73. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  74. [request setRequestMsg:param];
  75. [self sendDataToSocket:request modelClass:[CommandModel class]];
  76. }
  77. - (void)requestExcludeDevice:(BOOL)isStart {
  78. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_DEVICE_COMMAND];
  79. NSDictionary *param = @{@"device_id": [[JDFacade facade].loginUser getHomeHubID],
  80. @"command_type":isStart? CMD_TYPE_UNPAIRING_START:CMD_TYPE_UNPAIRING_STOP,
  81. @"cust_id": [[JDFacade facade].loginUser custId],
  82. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  83. [request setRequestMsg:param];
  84. [self sendDataToSocket:request modelClass:[CommandModel class]];
  85. }
  86. -(void)startLoading:(BOOL)isStart {
  87. _lblElapsedTime.hidden = !isStart;
  88. _imgLoading.hidden = !isStart;
  89. if (isStart) {
  90. _elapsedSeconds = kMaxTimeOut;
  91. NSLog(@"ElapsedSecond : %zd", _elapsedSeconds);
  92. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  93. if (!_timer) {
  94. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  95. }
  96. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  97. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  98. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  99. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  100. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  101. animation.duration = 2.0f;
  102. animation.repeatCount = INFINITY;
  103. [_imgLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  104. }
  105. });
  106. }
  107. else {
  108. _elapsedSeconds = kMaxTimeOut;
  109. if (_timer) {
  110. [_timer invalidate];
  111. _timer = nil;
  112. }
  113. if ([_imgLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  114. [_imgLoading.layer removeAnimationForKey:@"SpinAnimation"];
  115. }
  116. }
  117. }
  118. - (void)updateInclusionStatus {
  119. dispatch_async(dispatch_get_main_queue(), ^(void) {
  120. _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  121. _elapsedSeconds--;
  122. if (_elapsedSeconds == 0) {
  123. [self didFailInclusion];
  124. }
  125. });
  126. }
  127. - (void)didFailExclusion {
  128. [self startLoading:NO];
  129. [self requestIncludeDevice:NO];
  130. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  131. vc.addDevice = _addDevice;
  132. // vc.providesPresentationContextTransitionStyle = YES;
  133. // vc.definesPresentationContext = YES;
  134. //
  135. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  136. //
  137. // UIViewController *pvc = self.presentingViewController;
  138. // [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  139. // [pvc presentViewController:vc animated:YES completion:nil];
  140. // }];
  141. [self showTransparencyModalView:vc];
  142. }
  143. - (void)didFailInclusion {
  144. [self startLoading:NO];
  145. // [self requestIncludeDevice:NO];
  146. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  147. vc.addDevice = _addDevice;
  148. // vc.providesPresentationContextTransitionStyle = YES;
  149. // vc.definesPresentationContext = YES;
  150. //
  151. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  152. //
  153. // UIViewController *pvc = self.presentingViewController;
  154. // [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  155. // [pvc presentViewController:vc animated:YES completion:nil];
  156. // }];
  157. [self showTransparencyModalView:vc];
  158. }
  159. #pragma mark - UI Events
  160. - (IBAction)btnCancelTouched:(id)sender {
  161. [self startLoading:NO];
  162. [UIView animateWithDuration:kfAnimationDur animations:^{
  163. // _maskView.alpha = 0.0;
  164. } completion:^(BOOL finished) {
  165. [[JDFacade facade] dismissModalStack:NO completion:^{
  166. [[JDFacade facade] toast:@"취소되었습니다"];
  167. }];
  168. }];
  169. }
  170. #pragma mark - SocketService
  171. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
  172. {
  173. NSLog(@"Result Info : %@", result);
  174. NSLog(@"Result Info : %@", message);
  175. // if (result.messageType) {
  176. // <#statements#>
  177. // }
  178. NSLog(@"Message Type : %@", result.messageType);
  179. if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_DEVICE_COMMAND]) {
  180. CommandModel *response = (CommandModel *)message;
  181. if (result.isSuccess) {
  182. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  183. _startCommand = response;
  184. }
  185. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  186. _stopCommand = response;
  187. }
  188. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  189. _startCommand = response;
  190. }
  191. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  192. _stopCommand = response;
  193. }
  194. }
  195. else {
  196. [self didFailInclusion];
  197. }
  198. }
  199. }
  200. - (void) socketDidFailWithError:(NSError *)error {
  201. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  202. }
  203. -(void) receiveSocketData:(NSNotification *)notification {
  204. SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
  205. CommandModel *response = [[CommandModel alloc] initWithDictionary:notification.object error:nil];
  206. NSLog(@"Result : %@", result);
  207. NSLog(@"Response : %@", response);
  208. if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_DEVICE_COMMAND_RES]) {
  209. NSLog(@"Response : %@", response);
  210. if (result.isSuccess) {
  211. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  212. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  213. [self startLoading:YES];
  214. }
  215. }
  216. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  217. [self startLoading:NO];
  218. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  219. vc.isDeleteMode = NO;
  220. // vc.providesPresentationContextTransitionStyle = YES;
  221. // vc.definesPresentationContext = YES;
  222. //
  223. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  224. //
  225. // [self presentViewController:vc animated:YES completion:^{
  226. // _maskView.alpha = 0.0f;
  227. // }];
  228. [self showTransparencyModalView:vc];
  229. }
  230. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  231. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  232. [self startLoading:YES];
  233. }
  234. }
  235. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  236. [self startLoading:NO];
  237. // ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  238. // vc.isDeleteMode = NO;
  239. //
  240. // vc.providesPresentationContextTransitionStyle = YES;
  241. // vc.definesPresentationContext = YES;
  242. //
  243. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  244. //
  245. // [self presentViewController:vc animated:YES completion:^{
  246. // _maskView.alpha = 0.0f;
  247. // }];
  248. // TODO : 장치 삭제 완료 페이지로 이동
  249. }
  250. }
  251. else {
  252. [self didFailInclusion];
  253. }
  254. }
  255. else if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_NODE_REGIST_RES]) {
  256. [self requestIncludeDevice:NO];
  257. }
  258. }
  259. #pragma mark - MemoryWarning
  260. - (void)didReceiveMemoryWarning
  261. {
  262. [super didReceiveMemoryWarning];
  263. // Dispose of any resources that can be recreated.
  264. }
  265. @end