WallpadSubDevicePopupView.m 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // WallpadSubDevicePopupView.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 7/21/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "DeviceModel.h"
  11. #import "CustomLabel.h"
  12. #import "CustomTextField.h"
  13. #import "DeviceModel.h"
  14. #import "WallpadSubDevicePopupView.h"
  15. #import "ValidateUtil.h"
  16. #import "LDProgressView.h"
  17. #define kfWallpadAddSubDeviceTimeMax 60.0f
  18. @interface WallpadSubDevicePopupView () <CustomTextFieldDelegate> {
  19. NSTimer *_progressTimer;
  20. CGFloat _sec;
  21. NSString *_commandId;
  22. BOOL _isRequesting;
  23. }
  24. @end
  25. @implementation WallpadSubDevicePopupView
  26. - (id)initFromNib {
  27. for (UIView *view in [CommonUtil nibViews:@"WallpadSubDevicePopupView"]) {
  28. if ([view isKindOfClass:[WallpadSubDevicePopupView class]]) {
  29. self = (WallpadSubDevicePopupView *)view;
  30. //XIB의 경우, 현재 화면 사이즈로 맞춰줘야 함.
  31. self.frame = [UIScreen mainScreen].bounds;
  32. self.lblTitle.text = NSLocalizedString(@"장치 검색", @"장치 검색");
  33. _progressView.color = kUILineColor;
  34. _progressView.flat = @YES;
  35. _progressView.animate = @YES;
  36. _progressView.showText = @NO;
  37. _progressView.showStroke = @NO;
  38. _progressView.progressInset = @5;
  39. _progressView.showBackground = @NO;
  40. _progressView.outerStrokeWidth = @3;
  41. _progressView.type = LDProgressSolid;
  42. [self.btnConfirm setTitle:NSLocalizedString(@"취소", @"취소") forState:UIControlStateNormal];
  43. }
  44. }
  45. return self;
  46. }
  47. - (void)didMoveToSuperview {
  48. }
  49. - (void)setWallpadId:(NSString *)wallpadId {
  50. _wallpadId = wallpadId;
  51. //타이머를 시작함.
  52. // _sec = 0;
  53. // _progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(progressInvalidate) userInfo:nil repeats:YES];
  54. [self requestAddWallPadSubDevice];
  55. }
  56. - (void)requestAddWallPadSubDevice {
  57. //parameters
  58. NSDictionary *parameter = @{@"request_value": @"ADD",
  59. @"request_value_type": ksEmptyString,
  60. @"command_type": ksEmptyString};
  61. NSString *path = [NSString stringWithFormat:API_POST_DEVICE_COMMAND, _wallpadId];
  62. [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:parameter modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
  63. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  64. return;
  65. }
  66. DeviceModel *commandOfDevice = (DeviceModel *)responseObject;
  67. if (commandOfDevice) {//API 성공,
  68. _commandId = commandOfDevice.commandId;
  69. //타이머를 시작함.
  70. _sec = 0;
  71. _progressTimer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(progressInvalidate) userInfo:nil repeats:YES];
  72. }
  73. } failure:^(id errorObject) {
  74. JDErrorModel *error = (JDErrorModel *)errorObject;
  75. [[JDFacade facade] alert:error.errorMessage];
  76. }];
  77. }
  78. - (void)progressInvalidate {
  79. _sec++;
  80. CGFloat progress = _sec / kfWallpadAddSubDeviceTimeMax;
  81. _progressView.progress = progress;
  82. if (_sec >= kfWallpadAddSubDeviceTimeMax && !_isRequesting) {
  83. [_progressTimer invalidate];
  84. //do next stub.
  85. [self btnConfirmTouched:nil];
  86. [[JDFacade facade] alert:NSLocalizedString(@"새로운 장치가\n발견되지 않았습니다", @"새로운 장치가\n발견되지 않았습니다")];
  87. return;
  88. }
  89. if ((int)_sec % 3 == 1 && !_isRequesting) {//serial queue,
  90. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  91. _isRequesting = YES;
  92. //parameters
  93. NSDictionary *parameter = @{@"command_id": _commandId};
  94. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_COMMAND, _wallpadId];
  95. DeviceModel *commandOfDevice = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:parameter
  96. modelClass:[DeviceModel class] showLoadingView:NO];
  97. if (commandOfDevice) {
  98. if ([commandOfDevice.status isEqualToString:@"INC"]) {//추가
  99. dispatch_sync(dispatch_get_main_queue(), ^{
  100. _foundedDevice = commandOfDevice;
  101. [_progressTimer invalidate];
  102. [self btnConfirmTouched:nil];
  103. });
  104. } else if ([commandOfDevice.status isEqualToString:@"EXC"]) {//삭제
  105. dispatch_sync(dispatch_get_main_queue(), ^{
  106. _foundedDevice = commandOfDevice;
  107. [_progressTimer invalidate];
  108. [self btnConfirmTouched:nil];
  109. });
  110. } else if ([commandOfDevice.status isEqualToString:@"WAT"]) {//대기
  111. //do nothing
  112. }
  113. }
  114. _isRequesting = NO;
  115. });
  116. }
  117. }
  118. #pragma mark - Main Logic
  119. - (void)actionForDiscoveredDevice:(DeviceModel *)device {
  120. }
  121. #pragma mark - UI Events
  122. - (void)btnConfirmTouched:(id)sender {
  123. [_progressTimer invalidate];
  124. _progressTimer = nil;
  125. [super btnConfirmTouched:sender];
  126. }
  127. @end