| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569 |
- //
- // ThingsViewController.m
- // kneet
- //
- // Created by Jason Lee on 3/10/15.
- // Copyright (c) 2015 ntels. All rights reserved.
- //
- #import "JDObject.h"
- #import "RequestHandler.h"
- #import "JDJSONModel.h"
- #import "DeviceModel.h"
- #import "UIImageView+WebCache.h"
- #import "CustomLabel.h"
- #import "CustomImageView.h"
- #import "CustomTextField.h"
- #import "CustomButton.h"
- #import "JYRefreshController.h"
- #import "WYPopoverController.h"
- #import "ImageUtil.h"
- #import "CommandClassControlView.h"
- #import "ThingsDetailViewController.h"
- #import "ThingsViewController.h"
- #import "UIButton+WebCache.h"
- #import "ThingsAddViewController.h"
- #import "ThingsAddStartViewController.h"
- #import "CustomTableView.h"
- #define kfThingsTableViewCellHeight 100.0f
- @interface ThingsCollectionViewCell () {
- NSInteger _commandStatusElapsedTime;
- }
- @property (weak, nonatomic) NSIndexPath *indexPath;
- @end
- @implementation ThingsCollectionViewCell
- - (void)awakeFromNib {
- _btnDelete.hidden = YES;
-
- }
- @end
- @implementation ThingsAddCollectionViewCell
- @end
- @implementation ThingsCollectionFooterView
- @end
- @interface ThingsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> {
- NSMutableArray<DeviceModel> *_deviceList;
- NSString *_pagingId, *_pagingType;
- BOOL _isNotFirstLoading, _isDeleteMode;
-
- NSMutableArray<DeviceModel> *_commandArray;
- NSTimer *_deviceCommandsBackgroundTimer;
- }
- @property (strong, nonatomic) JYPullToRefreshController *refreshController;
- @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
- @end
- #pragma mark - Class Definition
- @implementation ThingsViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- [self initUI];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
- [self prepareViewDidLoad];
- }
- - (void)initUI {
-
- //set tableview option
- _collectionView.delegate = self;
- _collectionView.dataSource = self;
- _collectionView.backgroundColor = kUIBgColor01;
-
- if ([JDFacade facade].loginUser.homehubDeviceId && ![[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
- [self setThingsPopoverOptions];
- }
-
- //set refresh controls
- // __weak typeof(self) weakSelf = self;
- // self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
- // self.refreshController.pullToRefreshHandleAction = ^{
- // [weakSelf requestPredefinedRulesRecently];
- // };
- //
- // self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
- // self.loadMoreController.pullToLoadMoreHandleAction = ^{
- // [weakSelf requestPredefinedRulesOlder];
- // };
- }
- - (void)setThingsPopoverOptions {
-
- //set Popover Contents
- __weak typeof(self) weakSelf = self;
- _popooverOptionArray = [[NSMutableArray alloc] init];
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
- @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(refreshDeviceList)]}];
- if ([JDFacade facade].loginUser.level == 90) {//권한
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"추가", @"추가"),
- @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(addNewDevice)]}];
-
- [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
- @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
- @"target": weakSelf,
- @"selector": [NSValue valueWithPointer:@selector(toggleEditMode)]}];
- }
- }
- - (void)prepareViewDidLoad {
- //fetch devices from server
-
- if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
- [_mainView bringSubviewToFront:_addHubContainerView];
- _collectionView.hidden = YES;
-
- } else {
-
- [_mainView bringSubviewToFront:_collectionView];
- _addHubContainerView.hidden = YES;
- _collectionView.hidden = NO;
-
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- }
- - (void)updateHomeHubStatusToDevices {
- for (DeviceModel *device in _deviceList) {
- device.onlineState = [JDFacade facade].loginUser.homehubOnlineState;
- }
-
- [_collectionView reloadData];
- }
- - (void)updateDevice:(DeviceModel *)device {
- // if ([device.deviceId isEqualToString:ldevice.deviceId]) {
- // ldevice.cmdclsValue = device.cmdclsValue;
- // ldevice.contentValue = device.contentValue;
- // break;
- // }
- }
- //장치상태를 조회함.
- - (void)requestPollingCommandStatusOfDeviceInBackground:(DeviceModel *)device {
- if (!_commandArray) {
- _commandArray = (NSMutableArray<DeviceModel> *)[[NSMutableArray alloc] init];
- }
-
- __block BOOL isStatusChanged = NO;
- if (device && [device isKindOfClass:[DeviceModel class]]) {//validate, aleady have,
- if (![_commandArray objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", device.deviceId, device.nodeId]) {//일치하는 디바이스가 있을 경우, 추가하지 않음.
- [_commandArray addObject:device];
-
- isStatusChanged = YES;
- }
- }
-
- //TODO : 추가 커맨드를 받을 것인가?
- if (_commandArray.count) {
-
- NSMutableString *pathParams = [[NSMutableString alloc] init];
- for (DeviceModel *pDevice in _commandArray) {
- NSString *prefix = [pathParams isEmptyString] ? ksEmptyString : @",";
- [pathParams appendFormat:@"%@%@_%@", prefix, pDevice.deviceId, pDevice.nodeId];
- }
-
- //20
- NSString *path = [NSString stringWithFormat:API_GET_DEVICE_NODE_STATUS, pathParams];
-
- dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
- DeviceListModel *fdevices = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:nil
- modelClass:[DeviceListModel class] showLoadingView:YES];
-
- if (fdevices && fdevices.list && fdevices.list.count) {
-
- [_commandArray enumerateObjectsUsingBlock:^(DeviceModel *rdevice, NSUInteger idx, BOOL * _Nonnull stop) {
-
- DeviceModel *matchedDevice = (DeviceModel *)[fdevices.list objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", rdevice.deviceId, rdevice.nodeId];
-
- //실행 여부 및 10초 경과 확인
- BOOL isOverTimeLimit = [self elapsedSecondsFromNow:rdevice] > 10;
-
- //실행 여부 확인
- NSInteger elapsedTime = [self elapsedSecondsFrom:rdevice to:matchedDevice];
- BOOL hasChangedStatus = elapsedTime > 0;
-
- 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];
- }
- }
- });
- }
- }
- - (NSInteger)elapsedSecondsFromNow:(DeviceModel *)runningDevice {
- NSInteger seconds = 0;
- if (runningDevice.requestTime && ![runningDevice.requestTime isEmptyString]) {
- NSDate *rdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:runningDevice.requestTime]];
- NSTimeInterval elapsed = [[NSDate systemDate] timeIntervalSinceDate:rdate];
- seconds = elapsed;
- }
- return seconds;
- }
- - (NSInteger)elapsedSecondsFrom:(DeviceModel *)runningDevice to:(DeviceModel *)fetchedDevice {
- NSInteger seconds = 0;
- if (runningDevice.requestTime && ![runningDevice.requestTime isEmptyString] && fetchedDevice.collectTime && ![fetchedDevice.collectTime isEmptyString]) {
- NSDate *rdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:runningDevice.requestTime]];
- NSDate *fdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:fetchedDevice.collectTime]];
- seconds = [fdate secondsAfterDate:rdate];
- }
- return seconds;
- }
- - (void)addNewDevice {
- UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
- [self presentViewController:vc animated:YES completion:nil];
- }
- - (void)toggleEditMode {
- _isDeleteMode = !_isDeleteMode;
-
- [_collectionView reloadData];
-
- _constraintEditModeRight.constant = _isDeleteMode ? 0 : -_editModeView.width;
-
- [UIView animateWithDuration:kfAnimationDur animations:^{
- [self.view layoutIfNeeded];
- }];
- }
- - (void)refreshDeviceList {
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- #pragma mark - Main Logic
- - (void)requestDeviceListRecently {
- DeviceModel *firstDevice = [_deviceList firstObject];
- _pagingType = ksListPagingTypeUpward;
- _pagingId = firstDevice.createDatetime;
-
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- - (void)requestDeviceListOlder {
- DeviceModel *lastDevice = [_deviceList lastObject];
- _pagingType = ksListPagingTypeDownward;
- _pagingId = lastDevice.createDatetime;
-
- [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
- }
- - (void)requestDeviceList {
- //parameters
- NSDictionary *parameter = @{@"paging_datetime": _pagingId ? _pagingId : ksEmptyString,
- @"paging_type": _pagingType ? _pagingType : ksEmptyString};
- NSString *path = [NSString stringWithFormat:API_GET_DEVICE_LIST];
- [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DeviceListModel class] completion:^(id responseObject) {
- if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
- return;
- }
- DeviceListModel *deviceList = (DeviceListModel *)responseObject;
- if (deviceList && deviceList.list && deviceList.list.count) {
- _deviceList = deviceList.list;
-
- [self updateTitle];
- } else {
- if (!_deviceList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
- // NoContentView *noContentView = [NoContentView viewFromNib];
- // _tableView.tableFooterView = noContentView;
- }
- }
- [self matchDeviceListWithOnCommandsDevices];
- } failure:^(id errorObject) {
- JDErrorModel *error = (JDErrorModel *)errorObject;
- [[JDFacade facade] alert:error.errorMessage];
- }];
- }
- - (void)updateTitle {
-
- if (![JDFacade facade].loginUser.isHomehubOnline) {
- _lblTitle.text = @"홈허브 오프라인";
- [_lblTitle setColor:kUITextColor02 text:_lblTitle.text];
- return;
- }
-
- _lblTitle.text = [NSString stringWithFormat:@"장치 전체 %zd", _deviceList.count];
- [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _deviceList.count]];
- }
- - (void)matchDeviceListWithOnCommandsDevices {
-
- for (DeviceModel *rdevice in _commandArray) {
- DeviceModel *matchedDevice = [_deviceList objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", rdevice.deviceId, rdevice.nodeId]; //일치하는 디바이스가 있을 경우, 추가하지 않음.
- if (matchedDevice) {
-
- matchedDevice.isRequesting = rdevice.isRequesting;
- matchedDevice.requestTime = rdevice.requestTime;
- }
- }
-
- [_collectionView reloadData];
- }
- #pragma mark - UICollectionView Delegate
- - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
-
- NSInteger auth = [JDFacade facade].loginUser.level == 90 && !_isDeleteMode; //마스터 권한일 경우,
- NSInteger count = _deviceList.count % 2 == 0 ? _deviceList.count : _deviceList.count + auth; //홀수일 경우, 멤버 초대 버튼을 추가해줌.
- return count;
- }
- - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
-
- UICollectionViewCell *rcell = nil;
-
- if (indexPath.row < _deviceList.count) {
- ThingsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
-
- DeviceModel *device =_deviceList[indexPath.row];
- cell.indexPath = indexPath;
-
- [cell.btnDevice sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] forState:UIControlStateNormal
- placeholderImage:nil
- options:SDWebImageRefreshCached
- completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
- [cell layoutIfNeeded];
- }];
-
- cell.lblDeviceName.text = device.deviceName;
-
- //커맨드 클래스 뷰를 초기화함.
- [[cell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
- UIView *subview = (UIView *)obj;
- [subview removeFromSuperview];
- }];
-
- cell.btnDelete.hidden = !_isDeleteMode;
- cell.controlContainer.hidden = _isDeleteMode;
- cell.aiv.hidden = !device.isRequesting;
- if (!cell.aiv.hidden) {//show
- [cell.aiv startAnimating];
-
- cell.btnDelete.userInteractionEnabled = NO;
-
- } else {//hide
- [cell.aiv stopAnimating];
-
- cell.btnDelete.userInteractionEnabled = YES;
- }
-
- if (!cell.controlContainer.hidden) {
- //허브 On-Off line check
- cell.lblDeviceStatus.hidden = !([[JDFacade facade].loginUser.homehubOnlineState isEqualToString:@"OFF"] || [device.onlineState isEqualToString:@"OFF"]);
- cell.controlContainer.hidden = !cell.lblDeviceStatus.hidden;
-
- if (cell.controlContainer.hidden) {
- cell.lblDeviceStatus.text = @"OFFLINE";
-
- } else {//커맨드 클래스 타입별 컨트롤 호출
- CommandClassControlView *controlView = [CommandClassControlView viewForCommandClass:device.cmdclsType];
- controlView.device = device;
-
- cell.controlContainer.hidden = !controlView;
- if (!cell.controlContainer.hidden) {
- UIView *superview = cell.controlContainer;
- [superview addSubview:controlView];
-
- [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
- make.size.mas_equalTo(controlView.frame.size);
- make.center.equalTo(superview);
- }];
- }
- }
-
- } else {
- cell.btnDelete.value = device;
- if (![cell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [cell.btnDelete addTarget:self action:@selector(btnDeleteTouched:) forControlEvents:UIControlEventTouchUpInside];
- }
- }
-
- rcell = cell;
-
- } else {//디바이스 추가 옵션
- ThingsAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCollectionCellIdentifier" forIndexPath:indexPath];
-
- if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [cell.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
- }
- rcell = cell;
- }
-
- return rcell;
- }
- - (void)btnDeleteTouched:(id)sender {
- CustomButton *btn = (CustomButton *)sender;
-
- DeviceModel *device = (DeviceModel *)btn.value;
-
- ThingsAddStartViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddStartViewController" storyboardName:@"Things"];
- vc.removableDevice = device;
- if (device.groupName && ![device.groupName isEmptyString]) {
- vc.removableGroups = [_deviceList filteredArrayUsingPredicateFormat:@"groupName == %@", device.groupName]; //group devices
- }
-
- vc.providesPresentationContextTransitionStyle = YES;
- vc.definesPresentationContext = YES;
-
- [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
-
- [self presentViewController:vc animated:NO completion:nil];
- }
- - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
- viewForSupplementaryElementOfKind:(NSString *)kind
- atIndexPath:(NSIndexPath *)indexPath
- {
- ThingsCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
- withReuseIdentifier:@"FooterIdentifier" forIndexPath:indexPath];
- if (![footerView.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
- [footerView.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
- }
- return footerView;
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
-
- //FIXME : 권한 추가
- // if (_memberList.count % 2 == 1 || [JDFacade facade].loginUser.level < 90 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
- if (_deviceList.count % 2 == 1 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
- return CGSizeZero;
- }
-
- return CGSizeMake(IPHONE_WIDTH, 160.0f);
- }
- - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
- if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
- return CGSizeMake(212.0f, 197.0f);
- } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
- return CGSizeMake(187.5, 197.0f);
- }
- return CGSizeMake(160.0f, 197.0f);
- }
- - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
- [collectionView deselectItemAtIndexPath:indexPath animated:YES];
-
- if (indexPath.row < _deviceList.count) {//디바이스인 경우,
- ThingsCollectionViewCell *cell = (ThingsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
- DeviceModel *device = _deviceList[indexPath.row];
-
- ThingsDetailViewController *vc = (ThingsDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsDetailViewController" storyboardName:@"Things"];
- vc.refDevice = device;
- [self presentViewController:vc animated:YES completion:nil];
- }
- }
- #pragma mark - UI Events
- - (void)btnAddHubTouched:(id)sender {
- [[JDFacade facade] gotoHomeHubRegistration];
- }
- - (IBAction)btnOptionTouched:(id)sender {
- [self toggleOptions:sender];
- }
- - (IBAction)btnCloseOnEditModeTouched:(id)sender {
- [self toggleEditMode];
- }
- #pragma mark - MemoryWarning
- - (void)viewWillDisappear:(BOOL)animated {
-
- if (_deviceCommandsBackgroundTimer) {
- [_deviceCommandsBackgroundTimer invalidate];
- _deviceCommandsBackgroundTimer = nil;
- }
- }
- - (void)didReceiveMemoryWarning
- {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
|