| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332 |
- //
- // ThingsAddIncludeViewController.m
- // kneet2
- //
- // Created by Jason Lee on 11/13/15.
- // Copyright © 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "DeviceModel.h"
- #import "CustomLabel.h"
- #import "CustomButton.h"
- #import "ThingsAddIncludeViewController.h"
- #import "ThingsAddFailViewController.h"
- #import "ThingsAddCompleteViewController.h"
- @interface ThingsAddIncludeViewController () {
- NSTimer *_timer;
- NSInteger _elapsedSeconds;
- }
- @end
- // 가스밸브 타이틀 : 가스 밸브 센서 추가
- // 가스밸브 장치초기화 코멘트 : 밸브를 열림 상태에 두고 "위로" 버튼을 비프음이 날 때까지 5초 이상 누르세요.
- // 가스밸브 이미지 명 : img_things_product_addimg_01_smartgasvalve_wait
- // 도어센서 타이틀 : 도어 센서 추가
- // 도어센서 장치초기화 코멘트 : 센서 아래 버튼을 1회 누른 후 LED가 깜빡임을 멈추면 1회 더 누르세요.
- // 도어센서 이미지 명 : img_things_product_addimg_02_mutisensor_door_wait
- // 스마트플러그 타이틀 : 스마트 플러그 추가
- // 스마트플러그 장치초기화 코멘트 : 위에 버튼에 빨간 불이 깜빡일 때까지 5초 이상 누르세요.
- // 스마트플러그 이미지 명 : img_things_product_addimg_03_smartplug_wait
- #pragma mark - Class Definition
- @implementation ThingsAddIncludeViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
-
- [self initUI];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
- [self startLoading:NO];
- }
- - (void)viewDidAppear:(BOOL)animated {
- [super viewDidAppear:animated];
-
- [UIView animateWithDuration:kfAnimationDur animations:^{
- _maskView.alpha = 0.7;
- } completion:^(BOOL finished) {
-
- _constraintPopViewTop.constant = (IPHONE_HEIGHT - _popView.height) / 2;
-
- [UIView animateWithDuration:kfAnimationDur animations:^{
- [self.view layoutIfNeeded];
- }];
- }];
- }
- - (void)prepareViewDidLoad {
-
- if (_addableDevice) {
- _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _addableDevice[@"deviceName"]];
- [self requestIncludeDevice];
- } else if (_removableDevice) {
- _lblDesc.text = [NSString stringWithFormat:@"%@의\n00 버튼을\n00 누르세요", _removableDevice.deviceName];
- [self requestExcludeDevice];
- }
- }
- #pragma mark - Main Logic
- - (void)requestIncludeDevice {
- NSLog(@"웹소켓 날리기");
-
- SocketRequestModel *request = [[SocketRequestModel alloc] initWithMsgType:MSG_TYPE_DEVICE_COMMAND];
- NSLog(@"Device ID : %@", [[JDFacade facade].loginUser getHomeHubID]);
- NSDictionary *param = @{@"device_id": [[JDFacade facade].loginUser getHomeHubID],
- @"command_type": CMD_TYPE_PAIRING_START,
- @"cust_id": [[JDFacade facade].loginUser custId],
- @"ctrt_grp_id": [[JDFacade facade].loginUser ctrtGrpId]};
- [request setRequestMsg:param];
- [self sendDataToSocket:request modelClass:nil];
-
- [self startLoading:YES];
- // NSString *path = [NSString stringWithFormat:API_POST_DEVICE_INCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
- //
- // [[RequestHandler handler] sendAsyncPostRequestAPIPath:path parameters:nil modelClass:[DeviceModel class] completion:^(id responseObject) {
- // if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- // return;
- // }
- //
- // DeviceModel *device = (DeviceModel *)responseObject;
- //
- // if (device) {//API 성공 ,
- // [self requestCheckInclustion:device.commandId];
- // }
- // } failure:^(id errorObject) {
- // JDErrorModel *error = (JDErrorModel *)errorObject;
- // [[JDFacade facade] alert:error.errorMessage];
- // }];
- }
- -(void)startLoading:(BOOL)isStart {
- _lblElapsedTime.hidden = !isStart;
- _imgLoading.hidden = !isStart;
- if (isStart) {
- _elapsedSeconds = 60;
- NSLog(@"ElapsedSecond : %zd", _elapsedSeconds);
- _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
- if (!_timer) {
- _timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(updateInclusionStatus) userInfo:nil repeats:YES];
- }
-
- dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
- if ([_imgLoading.layer animationForKey:@"SpinAnimation"] == nil) {
- CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
- animation.fromValue = [NSNumber numberWithFloat:0.0f];
- animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
- animation.duration = 2.0f;
- animation.repeatCount = INFINITY;
- [_imgLoading.layer addAnimation:animation forKey:@"SpinAnimation"];
- }
- });
- }
- else {
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
- if ([_imgLoading.layer animationForKey:@"SpinAnimation"] != nil) {
- [_imgLoading.layer removeAnimationForKey:@"SpinAnimation"];
- }
-
- }
- }
- - (void)updateInclusionStatus {
-
- dispatch_async(dispatch_get_main_queue(), ^(void) {
- _lblElapsedTime.text = [NSString stringWithFormat:@"%zd", _elapsedSeconds];
- _elapsedSeconds--;
-
- if (_elapsedSeconds == 0) {
- [self didFailInclusion];
- }
- });
- }
- - (void)requestExcludeDevice {
- NSString *path = [NSString stringWithFormat:API_DELETE_DEVICE_EXCLUSION, [JDFacade facade].loginUser.homehubDeviceId];
-
- [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestDELETE parameters:nil modelClass:[DeviceModel class] showLoadingView:YES completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- DeviceModel *device = (DeviceModel *)responseObject;
-
- if (device) {//API 성공 ,
- [self requestCheckInclustion:device.commandId];
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)requestCheckInclustion:(NSString *)commandId {
-
-
-
- //parameters
- NSDictionary *parameter = @{@"command_id": commandId};
- NSString *path = [NSString stringWithFormat:API_GET_DEVICE_COMMAND, [JDFacade facade].loginUser.homehubDeviceId];
-
- [[RequestHandler handler] sendAsyncRequestAPIPath:path method:ksHTTPRequestGET parameters:parameter
- modelClass:[DeviceModel class] showLoadingView:NO completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
-
- DeviceModel *device = (DeviceModel *)responseObject;
-
- if (device) {//API 성공 ,
-
- if ([device.status isEqualToString:KNEET_DEVICE_WAITING]) {
- if (_elapsedSeconds > 2) {
- [self performSelector:@selector(requestCheckInclustion:) withObject:commandId afterDelay:3.0f];
- }
- //retry
- return;
-
- } else if ([device.status isEqualToString:KNEET_DEVICE_INCLUSION] || [device.status isEqualToString:KNEET_DEVICE_EXCLUSION]) {
- //success
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
- ThingsAddCompleteViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddCompleteViewController" storyboardName:@"Things"];
- vc.isDeleteMode = [device.status isEqualToString:KNEET_DEVICE_EXCLUSION];
-
- vc.providesPresentationContextTransitionStyle = YES;
- vc.definesPresentationContext = YES;
-
- [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
-
- [self presentViewController:vc animated:YES completion:^{
- _maskView.alpha = 0.0f;
- }];
- }
- }
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage completionHander:^{
- [self didFailInclusion];
- }];
- }];
- }
- - (void)didFailInclusion {
- [self startLoading:NO];
-
- ThingsAddFailViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddFailViewController" storyboardName:@"Things"];
-
- vc.addableDevice = _addableDevice;
-
- vc.providesPresentationContextTransitionStyle = YES;
- vc.definesPresentationContext = YES;
-
- [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
-
- UIViewController *pvc = self.presentingViewController;
- [self dismissViewControllerAnimated:NO completion:^{//TODO : UI Confirm with Mr.Mo
- [pvc presentViewController:vc animated:YES completion:nil];
- }];
- }
- #pragma mark - UI Events
- - (IBAction)btnCancelTouched:(id)sender {
- if (_timer) {
- [_timer invalidate];
- _timer = nil;
- }
-
- [UIView animateWithDuration:kfAnimationDur animations:^{
- _maskView.alpha = 0.0;
- } completion:^(BOOL finished) {
- [[JDFacade facade] dismissModalStack:NO completion:^{
- [[JDFacade facade] toast:@"취소되었습니다"];
- }];
- }];
- }
- #pragma mark - SocketService
- - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result
- {
- NSLog(@"Result Info : %@", result);
- NSLog(@"Result Info : %@", message);
- SWITCH(result.messageType)
- {
-
- CASE (MSG_TYPE_DEVICE_COMMAND)
- {
- [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
-
- if (result.isSuccess) {
-
- }
- else
- {
-
- }
-
- break;
- }
- CASE (@"auth.auto")
- {
- [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
- if (result.isSuccess) {
-
-
- }
- else
- {
- [[JDFacade facade] toast:result.resultMsg];
- }
-
- break;
- } DEFAULT
- {
- break;
- }
- }
- }
- - (void) socketDidFailWithError:(NSError *)error {
- [[JDFacade facade] loadIndicator:NO allowUserInteraction:YES];
- }
- #pragma mark - MemoryWarning
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|