HomeHubUpdateStartViewController.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. //
  2. // HomeHubUpdateStartViewController.m
  3. // OneCable
  4. //
  5. // Created by nComz on 2017. 5. 11..
  6. // Copyright © 2017년 ntels. All rights reserved.
  7. //
  8. #import "HomeHubUpdateStartViewController.h"
  9. #import "HomeHubUpdateCompleteViewController.h"
  10. @interface HomeHubUpdateStartViewController () {
  11. BOOL endUpgrade;
  12. NSTimer *_timer;
  13. NSInteger _elapsedSeconds;
  14. }
  15. @end
  16. @implementation HomeHubUpdateStartViewController
  17. - (void)viewDidLoad {
  18. [super viewDidLoad];
  19. endUpgrade = NO;
  20. _imgvLoading.hidden = YES;
  21. _btnConfirm.enabled = NO;
  22. }
  23. - (void)viewWillAppear:(BOOL)animated {
  24. [super viewWillAppear:animated];
  25. [self startLoading:YES];
  26. switch (_updateType) {
  27. case 1: //전체
  28. {
  29. }
  30. break;
  31. case 2: //펌웨어
  32. {
  33. [self requestHubFirmwareUpgrade];
  34. }
  35. break;
  36. case 3: //소프트웨어
  37. {
  38. [self requestHubSoftwareUpgrade];
  39. }
  40. break;
  41. }
  42. }
  43. - (void)didReceiveMemoryWarning {
  44. [super didReceiveMemoryWarning];
  45. }
  46. -(void)startLoading:(BOOL)isStart {
  47. // _lblTimer.hidden = !isStart;
  48. _imgvLoading.hidden = !isStart;
  49. if (isStart) {
  50. _elapsedSeconds = kMaxTimeOut;
  51. // _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  52. if (!_timer) {
  53. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  54. }
  55. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  56. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  57. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  58. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  59. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  60. animation.duration = 2.0f;
  61. animation.repeatCount = INFINITY;
  62. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  63. }
  64. });
  65. }
  66. else {
  67. _elapsedSeconds = kMaxTimeOut;
  68. if (_timer) {
  69. [_timer invalidate];
  70. _timer = nil;
  71. }
  72. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  73. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  74. }
  75. }
  76. }
  77. - (void)updateInclusionStatus {
  78. dispatch_async(dispatch_get_main_queue(), ^(void) {
  79. _elapsedSeconds--;
  80. if (_elapsedSeconds == 0) {
  81. }
  82. });
  83. }
  84. #pragma mark - User Event
  85. - (IBAction)btnConfirmTouched:(id)sender {
  86. }
  87. //#define MSG_TYPE_FIRMWARE_UPGRADE @"firmware.upgrade"
  88. //#define MSG_TYPE_FIRMWARE_UPGRADE_RESULT @"firmware.upgrade.result"
  89. //#define MSG_TYPE_SOFTWARE_UPGRADE @"software.upgrade"
  90. //#define MSG_TYPE_SOFTWARE_UPGRADE_RESULT @"software.upgrade.result"
  91. //#define MSG_TYPE_NODES_RELOAD @"nodes.reload"
  92. #pragma mark - soket
  93. -(void)requestHubFirmwareUpgrade {
  94. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_FIRMWARE_UPGRADE];
  95. NSDictionary *param = @{@"device_id":_hubInfo.deviceId,
  96. @"cust_id": [[JDFacade facade].loginUser custId],
  97. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  98. [request setRequestMsg:param];
  99. [self sendDataToSocket:request modelClass:[SoKDeviceWorkModel class] isLoading:YES];
  100. }
  101. -(void)requestHubSoftwareUpgrade {
  102. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_SOFTWARE_UPGRADE];
  103. NSDictionary *param = @{@"device_id":_hubInfo.deviceId,
  104. @"cust_id": [[JDFacade facade].loginUser custId],
  105. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  106. [request setRequestMsg:param];
  107. [self sendDataToSocket:request modelClass:[SoKDeviceWorkModel class] isLoading:YES];
  108. }
  109. - (void)failSocket:(NSString*)messageType {
  110. [[JDFacade facade] confirmTitle:@"홈허브 업데이트 실패"
  111. message:@"오류가 발생하여 홈허브 업데이트가 완료되지 않았습니다."
  112. btnOKLabel:@"재시도"
  113. btnCancelLabel:@"취소"
  114. completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  115. if (buttonIndex == 0) { //재시도
  116. [self startLoading:YES];
  117. if (EQUALS(messageType, MSG_TYPE_FIRMWARE_UPGRADE_RESULT))
  118. [self requestHubFirmwareUpgrade];
  119. else
  120. [self requestHubFirmwareUpgrade];
  121. }
  122. else {
  123. [self.navigationController popToRootViewControllerAnimated:YES];
  124. }
  125. }];
  126. }
  127. #pragma mark - SocketService
  128. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result {
  129. if (!result.isSuccess) {
  130. [self startLoading:NO];
  131. [self failSocket:result.messageType];
  132. }
  133. }
  134. -(void) receiveSocketData:(NSNotification *)notification {
  135. SoKDeviceWorkModel *result = [[SoKDeviceWorkModel alloc] initWithDictionary:notification.object error:nil];
  136. //NSLog(@"receiveSocketData Result : %@", result);
  137. [self startLoading:NO];
  138. //오류
  139. // 업그레이드 결과(실패:fail, 성공:success, 타임아웃:timeout)
  140. if (EQUALS(result.work_result, @"fail") || EQUALS(result.work_result, @"timeout")) {
  141. [self failSocket:result.messageType];
  142. }
  143. else if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_FIRMWARE_UPGRADE_RESULT]) {
  144. if (_updateType == 1) {
  145. //끝
  146. [self moveUpdateComplete];
  147. }
  148. else {
  149. [self startLoading:YES];
  150. [self requestHubSoftwareUpgrade];
  151. }
  152. }
  153. else if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_SOFTWARE_UPGRADE_RESULT]) {
  154. //끝
  155. [self moveUpdateComplete];
  156. }
  157. }
  158. - (void)moveUpdateComplete {
  159. HomeHubUpdateCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];;
  160. [self.navigationController pushViewController:vc animated:YES];
  161. }
  162. @end