HomeHubUpdateStartViewController.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. #import "RequestHandler.h"
  11. @interface HomeHubUpdateStartViewController () {
  12. BOOL endUpgrade;
  13. NSTimer *_timer;
  14. NSInteger _elapsedSeconds;
  15. }
  16. @end
  17. @implementation HomeHubUpdateStartViewController
  18. - (void)viewDidLoad {
  19. [super viewDidLoad];
  20. endUpgrade = NO;
  21. _imgvLoading.hidden = YES;
  22. _btnConfirm.enabled = NO;
  23. }
  24. - (void)viewWillAppear:(BOOL)animated {
  25. [super viewWillAppear:animated];
  26. [self startLoading:YES];
  27. switch (_updateType) {
  28. case 1: //전체
  29. {
  30. [self requestHubFirmwareUpgrade];
  31. }
  32. break;
  33. case 2: //펌웨어
  34. {
  35. [self requestHubFirmwareUpgrade];
  36. }
  37. break;
  38. case 3: //소프트웨어
  39. {
  40. [self requestHubSoftwareUpgrade];
  41. }
  42. break;
  43. }
  44. }
  45. - (void)didReceiveMemoryWarning {
  46. [super didReceiveMemoryWarning];
  47. }
  48. -(void)startLoading:(BOOL)isStart {
  49. // _lblTimer.hidden = !isStart;
  50. _imgvLoading.hidden = !isStart;
  51. if (isStart) {
  52. _elapsedSeconds = kMaxTimeOut;
  53. // _lblTimer.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
  54. if (!_timer) {
  55. _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
  56. }
  57. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  58. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] == nil) {
  59. CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
  60. animation.fromValue = [NSNumber numberWithFloat:0.0f];
  61. animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
  62. animation.duration = 2.0f;
  63. animation.repeatCount = INFINITY;
  64. [_imgvLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
  65. }
  66. });
  67. }
  68. else {
  69. _elapsedSeconds = kMaxTimeOut;
  70. if (_timer) {
  71. [_timer invalidate];
  72. _timer = nil;
  73. }
  74. if ([_imgvLoading.layer animationForKey:@"SpinAnimation"] != nil) {
  75. [_imgvLoading.layer removeAnimationForKey:@"SpinAnimation"];
  76. }
  77. }
  78. }
  79. - (void)updateInclusionStatus {
  80. dispatch_async(dispatch_get_main_queue(), ^(void) {
  81. _elapsedSeconds--;
  82. if (_elapsedSeconds == 0) {
  83. }
  84. });
  85. }
  86. #pragma mark - User Event
  87. - (IBAction)btnConfirmTouched:(id)sender {
  88. }
  89. //#define MSG_TYPE_FIRMWARE_UPGRADE @"firmware.upgrade"
  90. //#define MSG_TYPE_FIRMWARE_UPGRADE_RESULT @"firmware.upgrade.result"
  91. //#define MSG_TYPE_SOFTWARE_UPGRADE @"software.upgrade"
  92. //#define MSG_TYPE_SOFTWARE_UPGRADE_RESULT @"software.upgrade.result"
  93. //#define MSG_TYPE_NODES_RELOAD @"nodes.reload"
  94. #pragma mark - SocketService
  95. //자동로그인 요청
  96. - (void)requestAutoLogin {;
  97. LoginModel *tmpInfo = [[LoginModel alloc] init];
  98. tmpInfo.memberId = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_ID];
  99. tmpInfo.authToken = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_AUTOTOKEN];
  100. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_AUTO_LOGIN];
  101. NSDictionary *param = @{@"Authorization": tmpInfo.authorization,
  102. @"device_token": [JDFacade facade].APNSToken ? [JDFacade facade].APNSToken : ksEmptyString,
  103. @"os_type": MOBILE_DEVICE_TYPE};
  104. [request setRequestMsg:param];
  105. [self sendDataToSocket:request modelClass:[LoginModel class] isLoading:YES];
  106. }
  107. -(void)requestHubFirmwareUpgrade {
  108. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_FIRMWARE_UPGRADE];
  109. NSDictionary *param = @{@"device_id":_hubInfo.deviceId,
  110. @"cust_id": [[JDFacade facade].loginUser custId],
  111. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  112. [request setRequestMsg:param];
  113. [self sendDataToSocket:request modelClass:[SoKDeviceWorkModel class] isLoading:YES];
  114. }
  115. -(void)requestHubSoftwareUpgrade {
  116. SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_SOFTWARE_UPGRADE];
  117. NSDictionary *param = @{@"device_id":_hubInfo.deviceId,
  118. @"cust_id": [[JDFacade facade].loginUser custId],
  119. @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
  120. [request setRequestMsg:param];
  121. [self sendDataToSocket:request modelClass:[SoKDeviceWorkModel class] isLoading:YES];
  122. }
  123. - (void)failSocket:(NSString*)messageType {
  124. [[JDFacade facade] confirmTitle:@"홈허브 업데이트 실패"
  125. message:@"오류가 발생하여 홈허브 업데이트가 완료되지 않았습니다."
  126. btnOKLabel:@"재시도"
  127. btnCancelLabel:@"취소"
  128. completion:^(CustomAlertView *alertView, NSInteger buttonIndex) {
  129. if (buttonIndex == 0) { //재시도
  130. [self startLoading:YES];
  131. if (EQUALS(messageType, MSG_TYPE_FIRMWARE_UPGRADE_RESULT))
  132. [self requestHubFirmwareUpgrade];
  133. else
  134. [self requestHubFirmwareUpgrade];
  135. }
  136. else {
  137. [self.navigationController popToRootViewControllerAnimated:YES];
  138. }
  139. }];
  140. }
  141. #pragma mark - SocketService
  142. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result {
  143. SWITCH(result.messageType)
  144. {
  145. CASE(MSG_TYPE_AUTO_LOGIN)
  146. {
  147. if (result.isSuccess) {
  148. LoginModel *loginInfo = (LoginModel *)message;
  149. loginInfo.memberId = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_LOGIN_ID];
  150. loginInfo.authToken = [[JDFacade facade] objectForKeyFromKeychain:USDEF_SESSION_AUTOTOKEN];
  151. [self actionForLoginSucceed:loginInfo];
  152. }
  153. else {
  154. [[JDFacade facade] alertTitle:@"알림" message:result.resultMsg];
  155. }
  156. break;
  157. }
  158. CASE(MSG_TYPE_FIRMWARE_UPGRADE)
  159. CASE(MSG_TYPE_SOFTWARE_UPGRADE)
  160. {
  161. if (!result.isSuccess) {
  162. [self startLoading:NO];
  163. [self failSocket:result.messageType];
  164. }
  165. }
  166. DEFAULT
  167. {
  168. break;
  169. }
  170. }
  171. }
  172. -(void) receiveSocketData:(NSNotification *)notification {
  173. SoKDeviceWorkModel *result = [[SoKDeviceWorkModel alloc] initWithDictionary:notification.object error:nil];
  174. //NSLog(@"receiveSocketData Result : %@", result);
  175. [self startLoading:NO];
  176. //오류
  177. // 업그레이드 결과(실패:fail, 성공:success, 타임아웃:timeout)
  178. if (EQUALS(result.work_result, @"fail") || EQUALS(result.work_result, @"timeout")) {
  179. [self failSocket:result.messageType];
  180. }
  181. else if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_FIRMWARE_UPGRADE_RESULT]) {
  182. if (_updateType == 1) {
  183. //끝
  184. [self requestAutoLogin];
  185. //[self moveUpdateComplete];
  186. }
  187. else {
  188. [self startLoading:YES];
  189. [self requestHubSoftwareUpgrade];
  190. }
  191. }
  192. else if ([result.messageType isEquestToIgnoreCase:MSG_TYPE_SOFTWARE_UPGRADE_RESULT]) {
  193. //유저 정보 업데이트
  194. [self requestAutoLogin];
  195. }
  196. }
  197. //로그인 이후 처리.
  198. - (void)actionForLoginSucceed:(LoginModel *)loginInfo {
  199. if (loginInfo) {//API 성공함
  200. [JDFacade facade].loginUser = loginInfo;
  201. NSLog(@"loginUser : %@", [JDFacade facade].loginUser);
  202. if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].tmpEmailId]) {
  203. [JDFacade facade].tmpEmailId = ksEmptyString;
  204. }
  205. if ([[JDFacade facade].loginUser.emailId isEqualToString:[JDFacade facade].loginUser.newEmailId]) {//이메일 변경 요청 중
  206. [JDFacade facade].loginUser.newEmailId = ksEmptyString;
  207. }
  208. [RequestHandler handler].authorization = loginInfo.authorization;
  209. //다음 이동
  210. [self moveUpdateComplete];
  211. }
  212. }
  213. - (void)moveUpdateComplete {
  214. HomeHubUpdateCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateCompleteViewController" storyboardName:@"HomeHub"];;
  215. vc.isUpdate = YES;
  216. [self.navigationController pushViewController:vc animated:YES];
  217. }
  218. @end