ThingsAddIncludeViewController.m 12 KB

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