|
|
@@ -21,7 +21,8 @@
|
|
|
|
|
|
|
|
|
@interface HomeModeUpdateViewController () {
|
|
|
-
|
|
|
+ NSTimer *_modeChangePollingTimer;
|
|
|
+ NSDate *_requestTime;
|
|
|
}
|
|
|
@end
|
|
|
|
|
|
@@ -92,6 +93,11 @@
|
|
|
[[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[JDJSONModel class] completion:^(id responseObject) {
|
|
|
|
|
|
if ([[JDFacade facade].currentViewController isEqual:self]) {
|
|
|
+
|
|
|
+ //check requet done,
|
|
|
+ _requestTime = [NSDate systemDate];
|
|
|
+ [self requestPollingHomeModeChanges];
|
|
|
+
|
|
|
[self setContentsForComplete];
|
|
|
}
|
|
|
|
|
|
@@ -137,6 +143,77 @@
|
|
|
// Dispose of any resources that can be recreated.
|
|
|
}
|
|
|
|
|
|
+- (NSInteger)elapsedSecondsFromNow {
|
|
|
+ NSInteger seconds = 0;
|
|
|
+
|
|
|
+ if (_requestTime) {
|
|
|
+ NSTimeInterval elapsed = [[NSDate systemDate] timeIntervalSinceDate:_requestTime];
|
|
|
+ seconds = elapsed;
|
|
|
+ }
|
|
|
+ return seconds;
|
|
|
+}
|
|
|
+
|
|
|
+- (void)requestPollingHomeModeChanges {
|
|
|
+
|
|
|
+ //every 3sec?
|
|
|
+ NSString *path = [NSString stringWithFormat:API_GET_MODE_STATUS, _mode.modeId];
|
|
|
+
|
|
|
+ dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
|
|
|
+ ModeModel *response = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:nil
|
|
|
+ modelClass:[ModeModel class] showLoadingView:YES];
|
|
|
+ if (response) {
|
|
|
+ BOOL isRequestDone = [response.status boolValue];
|
|
|
+
|
|
|
+
|
|
|
+ if (isRequestDone) {
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //실행 여부 및 10초 경과 확인
|
|
|
+ BOOL isOverTimeLimit = [self elapsedSecondsFromNow] > 10;
|
|
|
+ }
|
|
|
+
|
|
|
+ _mode.
|
|
|
+ rdevice.isRequesting = [rdevice.contentValue isEqualToString:matchedDevice.contentValue] && !hasChangedStatus && !isOverTimeLimit;
|
|
|
+
|
|
|
+ //TODO - home hub check
|
|
|
+ // rdevice.requestTime = matchedDevice.requestTime;
|
|
|
+ // rdevice.collectTime = matchedDevice.collectTime;
|
|
|
+ rdevice.onlineState = matchedDevice.onlineState;
|
|
|
+
|
|
|
+ if (!rdevice.isRequesting || !rdevice.isOnline || ![JDFacade facade].loginUser.isHomehubOnline) {//정상적으로 변경됨.
|
|
|
+ rdevice.contentValue = matchedDevice.contentValue;
|
|
|
+ [_commandArray removeObject:rdevice];
|
|
|
+
|
|
|
+ isStatusChanged = YES;
|
|
|
+ }
|
|
|
+
|
|
|
+#ifdef DEBUG_MODE
|
|
|
+ NSLogInfo(@"==########== device command status = %@, elapsedTime = %zd ==########==", [JDFacade facade].loginUser.homehubOnlineState, elapsedTime);
|
|
|
+#endif
|
|
|
+ }];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (_commandArray.count) {//커맨드 실행 중인 디바이스가 있을 경우,
|
|
|
+ //schedul timer.
|
|
|
+ if (!_deviceCommandsBackgroundTimer) {
|
|
|
+ _deviceCommandsBackgroundTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(requestPollingCommandStatusOfDeviceInBackground:) userInfo:nil repeats:YES];
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ [_deviceCommandsBackgroundTimer invalidate];
|
|
|
+ _deviceCommandsBackgroundTimer = nil;
|
|
|
+ }
|
|
|
+
|
|
|
+ //변화가 있을 경우, 컬렉션뷰를 리로드
|
|
|
+ if (isStatusChanged) {
|
|
|
+ [_collectionView reloadData];
|
|
|
+
|
|
|
+ ThingsDetailViewController *vc = (ThingsDetailViewController *)[JDFacade facade].currentViewController;
|
|
|
+ if (vc) {
|
|
|
+ [vc.tableView reloadData];
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
|
|
|
|
|
|
@end
|