ThingsAddIncludeViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. if ([result.messageType isEqualToIgnoreCase:MSG_TYPE_DEVICE_COMMAND]) {
  176. CommandModel *response = (CommandModel *)message;
  177. if (result.isSuccess) {
  178. if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  179. _startCommand = response;
  180. }
  181. else if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  182. _stopCommand = response;
  183. }
  184. else if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  185. _startCommand = response;
  186. }
  187. else if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  188. _stopCommand = response;
  189. }
  190. }
  191. else {
  192. [self didFailInclusion];
  193. }
  194. }
  195. }
  196. - (void) socketDidFailWithError:(NSError *)error {
  197. [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
  198. }
  199. -(void) receiveSocketData:(NSNotification *)notification {
  200. SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
  201. CommandModel *response = [[CommandModel alloc] initWithDictionary:notification.object error:nil];
  202. //NSLog(@"Result : %@", result);
  203. //NSLog(@"Response : %@", response);
  204. if ([result.messageType isEqualToIgnoreCase:MSG_TYPE_DEVICE_COMMAND_RES]) {
  205. if (result.isSuccess) {
  206. if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_PAIRING_START]) {
  207. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  208. [self startLoading:YES];
  209. }
  210. }
  211. else if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_PAIRING_STOP]) {
  212. /*
  213. [self startLoading:NO];
  214. DeviceDetailModel *detailModel = [DeviceDetailModel new];
  215. detailModel.deviceId = _selectHub.deviceId;
  216. detailModel.nodeId = [notification.object objectForKey:@"node_id"];
  217. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  218. vc.deviceDetailModel = detailModel;
  219. vc.isDeleteMode = NO;
  220. */
  221. // [self showTransparencyModalView:vc];
  222. // vc.providesPresentationContextTransitionStyle = YES;
  223. // vc.definesPresentationContext = YES;
  224. //
  225. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  226. //
  227. // [self presentViewController:vc animated:YES completion:^{
  228. // _maskView.alpha = 0.0f;
  229. // }];
  230. // [self showTransparencyModalView:vc];
  231. }
  232. if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_START]) {
  233. if ([response.commandId isEqualToString:_startCommand.commandId]) {
  234. [self startLoading:YES];
  235. }
  236. }
  237. else if ([response.commandType isEqualToIgnoreCase:CMD_TYPE_UNPAIRING_STOP]) {
  238. [self startLoading:NO];
  239. // ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  240. // vc.isDeleteMode = NO;
  241. //
  242. // vc.providesPresentationContextTransitionStyle = YES;
  243. // vc.definesPresentationContext = YES;
  244. //
  245. // [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  246. //
  247. // [self presentViewController:vc animated:YES completion:^{
  248. // _maskView.alpha = 0.0f;
  249. // }];
  250. // TODO : 장치 삭제 완료 페이지로 이동
  251. }
  252. }
  253. else {
  254. [self didFailInclusion];
  255. }
  256. }
  257. else if ([result.messageType isEqualToIgnoreCase:MSG_TYPE_NODE_REGIST_RES]) {
  258. // [self requestIncludeDevice:NO];
  259. [self startLoading:NO];
  260. DeviceDetailModel *detailModel = [DeviceDetailModel new];
  261. detailModel.deviceId = _selectHub != nil ? _selectHub.deviceId : _addDevice.deviceId;
  262. detailModel.nodeId = [notification.object objectForKey:@"node_id"];
  263. detailModel.deviceId = [notification.object objectForKey:@"device_id"];
  264. ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
  265. vc.deviceDetailModel = detailModel;
  266. vc.isDeleteMode = NO;
  267. vc.addDevice = _addDevice;
  268. [self showTransparencyModalView:vc];
  269. }
  270. }
  271. #pragma mark - MemoryWarning
  272. - (void)didReceiveMemoryWarning
  273. {
  274. [super didReceiveMemoryWarning];
  275. // Dispose of any resources that can be recreated.
  276. }
  277. @end