ThingsAddIncludeViewController.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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 isMultiHomeHub] ? _selectHub.deviceId:[[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 isMultiHomeHub] ? _selectHub.deviceId:[[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.selectHub = _selectHub;
  133. // vc.providesPresentationContextTransitionStyle = YES;
  134. // vc.definesPresentationContext = YES;
  135. //
  136. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  137. //
  138. // UIViewController *pvc = self.presentingViewController;
  139. // [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  140. // [pvc presentViewController:vc animated:YES completion:nil];
  141. // }];
  142. [self showTransparencyModalView:vc];
  143. }
  144. - (void)didFailInclusion {
  145. [self startLoading:NO];
  146. // [self requestIncludeDevice:NO];
  147. ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
  148. vc.addDevice = _addDevice;
  149. vc.selectHub = _selectHub;
  150. // vc.providesPresentationContextTransitionStyle = YES;
  151. // vc.definesPresentationContext = YES;
  152. //
  153. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  154. //
  155. // UIViewController *pvc = self.presentingViewController;
  156. // [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
  157. // [pvc presentViewController:vc animated:YES completion:nil];
  158. // }];
  159. [self showTransparencyModalView:vc];
  160. }
  161. #pragma mark - UI Events
  162. - (IBAction)btnCancelTouched:(id)sender {
  163. [self startLoading:NO];
  164. [UIView animateWithDuration:kfAnimationDur animations:^{
  165. // _maskView.alpha = 0.0;
  166. } completion:^(BOOL finished) {
  167. [[JDFacade facade] dismissModalStack:NO completion:^{
  168. [[JDFacade facade] toast:@"취소되었습니다"];
  169. }];
  170. }];
  171. }
  172. #pragma mark - SocketService
  173. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
  174. {
  175. NSLog(@"Result Info : %@", result);
  176. NSLog(@"Result Info : %@", message);
  177. // if (result.messageType) {
  178. // <#statements#>
  179. // }
  180. NSLog(@"Message Type : %@", result.messageType);
  181. if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_DEVICE_COMMAND]) {
  182. CommandModel *response = (CommandModel *)message;
  183. if (result.isSuccess) {
  184. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  185. _startCommand = response;
  186. }
  187. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  188. _stopCommand = response;
  189. }
  190. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  191. _startCommand = response;
  192. }
  193. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  194. _stopCommand = response;
  195. }
  196. }
  197. else {
  198. [self didFailInclusion];
  199. }
  200. }
  201. }
  202. - (void) socketDidFailWithError:(NSError *)error {
  203. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  204. }
  205. -(void) receiveSocketData:(NSNotification *)notification {
  206. SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
  207. CommandModel *response = [[CommandModel alloc] initWithDictionary:notification.object error:nil];
  208. NSLog(@"Result : %@", result);
  209. NSLog(@"Response : %@", response);
  210. if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_DEVICE_COMMAND_RES]) {
  211. if (result.isSuccess) {
  212. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  213. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  214. [self startLoading:YES];
  215. }
  216. }
  217. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  218. [self startLoading:NO];
  219. DeviceDetailModel *detailModel = [DeviceDetailModel new];
  220. detailModel.deviceId = _selectHub.deviceId;
  221. detailModel.nodeId = [notification.object objectForKey:@"node_id"];
  222. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  223. vc.deviceDetailModel = detailModel;
  224. vc.isDeleteMode = NO;
  225. // vc.providesPresentationContextTransitionStyle = YES;
  226. // vc.definesPresentationContext = YES;
  227. //
  228. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  229. //
  230. // [self presentViewController:vc animated:YES completion:^{
  231. // _maskView.alpha = 0.0f;
  232. // }];
  233. [self showTransparencyModalView:vc];
  234. }
  235. if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  236. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  237. [self startLoading:YES];
  238. }
  239. }
  240. else if ([response.commandType isEquestToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  241. [self startLoading:NO];
  242. // ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  243. // vc.isDeleteMode = NO;
  244. //
  245. // vc.providesPresentationContextTransitionStyle = YES;
  246. // vc.definesPresentationContext = YES;
  247. //
  248. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  249. //
  250. // [self presentViewController:vc animated:YES completion:^{
  251. // _maskView.alpha = 0.0f;
  252. // }];
  253. // TODO : 장치 삭제 완료 페이지로 이동
  254. }
  255. }
  256. else {
  257. [self didFailInclusion];
  258. }
  259. }
  260. else if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_NODE_REGIST_RES]) {
  261. [self requestIncludeDevice:NO];
  262. }
  263. }
  264. #pragma mark - MemoryWarning
  265. - (void)didReceiveMemoryWarning
  266. {
  267. [super didReceiveMemoryWarning];
  268. // Dispose of any resources that can be recreated.
  269. }
  270. @end