| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219 |
- //
- // HomeModeUpdateViewController.m
- //
- //
- // Created by Jason Lee on 10/30/15.
- //
- //
- #import "JDObject.h"
- #import "CustomButton.h"
- #import "CustomLabel.h"
- #import "CustomImageView.h"
- #import "RequestHandler.h"
- #import "JDJSONModel.h"
- #import "HomeModeUpdateViewController.h"
- #import "ModeModel.h"
- #import "DeviceSelectPopupView.h"
- #import "HomeModeSettingsViewController.h"
- @interface HomeModeUpdateViewController () {
- NSTimer *_modeChangePollingTimer;
- NSDate *_requestTime;
- }
- @end
- #pragma mark - Class Definition
- @implementation HomeModeUpdateViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
-
- NSString *message = nil, *highlightMessage = nil;
- NSString *btnTitle = nil;
- if ([_mode.basicModeCode isEqualToString:HOME_MODE_HOME]) {
- btnTitle = @"귀가";
- highlightMessage = @"잘 돌아왔어요!";
- message = @"쾌적한 귀가모드를 \r실행할까요?";
-
- } else if ([_mode.basicModeCode isEqualToString:HOME_MODE_AWAY]) {
- btnTitle = @"외출";
- highlightMessage = @"안녕히 다녀오세요!";
- message = @"안전한 외출모드를 \r실행할까요?";
-
- } else if ([_mode.basicModeCode isEqualToString:HOME_MODE_MORNING]) {
- btnTitle = @"기상";
- highlightMessage = @"좋은 아침!";
- message = @"상쾌한 기상모드를\r실행할까요?";
-
- } else if ([_mode.basicModeCode isEqualToString:HOME_MODE_NIGHT]) {
- btnTitle = @"취침";
- highlightMessage = @"잘 자요!";
- message = @"편안한 취침모드를 \r실행할까요?";
-
- }
-
- [_btnExecute setTitle:btnTitle forState:UIControlStateNormal];
- _lblMessage.text = [NSString stringWithFormat:@"%@\n\n%@", highlightMessage, message];
- [_lblMessage setColor:kUITextColor02 text:highlightMessage];
-
- _btnSetting.hidden = [JDFacade facade].loginUser.level < 90;
-
- //TODO : set icon;
- }
- - (void)prepareViewDidLoad {
-
- }
- #pragma mark - Main Logic
- - (void)requestChangeHomeMode {
- NSString *modeTitle = [NSString stringWithFormat:@"%@ 모드", _mode.modeName];
- _lblMessage.text = [NSString stringWithFormat:@"%@\r\r실행 중입니다.", modeTitle];
- [_lblMessage setColor:kUITextColor02 text:modeTitle];
-
- NSString *path = [NSString stringWithFormat:API_POST_DASHBOARD_MODE_CHANGE, _mode.modeId];
-
- [[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];
- }
-
- [[JDFacade facade] toast:[NSString stringWithFormat:@"%@ 상태가 되었습니다", _mode.modeName]];
-
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)setContentsForComplete {
-
- NSString *highlightMessage = nil;
- highlightMessage = [NSString stringWithFormat:@"%@ 상태가 되었습니다.", _mode.modeName];
- //TODO : chagne button image
- }
- #pragma mark - UI Events
- - (IBAction)btnExecuteTouched:(id)sender {
- [self requestChangeHomeMode];
- }
- - (IBAction)btnSettingTouched:(id)sender {
- HomeModeSettingsViewController *vc = (HomeModeSettingsViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"HomeModeSettingsViewController" storyboardName:@"Main"];
- vc.mode = _mode;
-
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (IBAction)btnCloseTouched:(id)sender {
- [self dismissViewControllerAnimated:YES completion:nil];
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // 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
|