ThingsViewController.m 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //
  2. // ThingsViewController.m
  3. // kneet
  4. //
  5. // Created by Jason Lee on 3/10/15.
  6. // Copyright (c) 2015 ntels. All rights reserved.
  7. //
  8. #import "JDObject.h"
  9. #import "RequestHandler.h"
  10. #import "JDJSONModel.h"
  11. #import "DeviceModel.h"
  12. #import "UIImageView+WebCache.h"
  13. #import "CustomLabel.h"
  14. #import "CustomImageView.h"
  15. #import "CustomTextField.h"
  16. #import "CustomButton.h"
  17. #import "JYRefreshController.h"
  18. #import "WYPopoverController.h"
  19. #import "ImageUtil.h"
  20. #import "CommandClassControlView.h"
  21. #import "ThingsDetailViewController.h"
  22. #import "ThingsViewController.h"
  23. #import "UIButton+WebCache.h"
  24. #import "ThingsAddViewController.h"
  25. #import "ThingsAddStartViewController.h"
  26. #import "CustomTableView.h"
  27. #define kfThingsTableViewCellHeight 100.0f
  28. @interface ThingsCollectionViewCell () {
  29. NSInteger _commandStatusElapsedTime;
  30. }
  31. @property (weak, nonatomic) NSIndexPath *indexPath;
  32. @end
  33. @implementation ThingsCollectionViewCell
  34. - (void)awakeFromNib {
  35. _btnDelete.hidden = YES;
  36. }
  37. @end
  38. @implementation ThingsAddCollectionViewCell
  39. @end
  40. @implementation ThingsCollectionFooterView
  41. @end
  42. @interface ThingsViewController () <UICollectionViewDataSource, UICollectionViewDelegate> {
  43. NSMutableArray<DeviceModel> *_deviceList;
  44. NSString *_pagingId, *_pagingType;
  45. BOOL _isNotFirstLoading, _isDeleteMode;
  46. NSMutableArray<DeviceModel> *_commandArray;
  47. NSTimer *_deviceCommandsBackgroundTimer;
  48. }
  49. @property (strong, nonatomic) JYPullToRefreshController *refreshController;
  50. @property (strong, nonatomic) JYPullToLoadMoreController *loadMoreController;
  51. @end
  52. #pragma mark - Class Definition
  53. @implementation ThingsViewController
  54. - (void)viewDidLoad {
  55. [super viewDidLoad];
  56. [self initUI];
  57. }
  58. - (void)viewWillAppear:(BOOL)animated {
  59. [super viewWillAppear:animated];
  60. [self prepareViewDidLoad];
  61. }
  62. - (void)initUI {
  63. //set tableview option
  64. _collectionView.delegate = self;
  65. _collectionView.dataSource = self;
  66. _collectionView.backgroundColor = kUIBgColor01;
  67. if ([JDFacade facade].loginUser.homehubDeviceId && ![[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
  68. [self setThingsPopoverOptions];
  69. }
  70. //set refresh controls
  71. // __weak typeof(self) weakSelf = self;
  72. // self.refreshController = [[JYPullToRefreshController alloc] initWithScrollView:self.tableView];
  73. // self.refreshController.pullToRefreshHandleAction = ^{
  74. // [weakSelf requestPredefinedRulesRecently];
  75. // };
  76. //
  77. // self.loadMoreController = [[JYPullToLoadMoreController alloc] initWithScrollView:self.tableView];
  78. // self.loadMoreController.pullToLoadMoreHandleAction = ^{
  79. // [weakSelf requestPredefinedRulesOlder];
  80. // };
  81. }
  82. - (void)setThingsPopoverOptions {
  83. //set Popover Contents
  84. __weak typeof(self) weakSelf = self;
  85. _popooverOptionArray = [[NSMutableArray alloc] init];
  86. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"새로 고침",nil),
  87. @"iconName": @"tp_01_img_bg_morepopup_icon_refresh",
  88. @"target": weakSelf,
  89. @"selector": [NSValue valueWithPointer:@selector(refreshDeviceList)]}];
  90. if ([JDFacade facade].loginUser.level == 90) {//권한
  91. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"추가", @"추가"),
  92. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  93. @"target": weakSelf,
  94. @"selector": [NSValue valueWithPointer:@selector(addNewDevice)]}];
  95. [_popooverOptionArray addObject:@{@"menuName" : NSLocalizedString(@"삭제", @"삭제"),
  96. @"iconName": @"tp_01_img_bg_morepopup_icon_group_deviceadd",
  97. @"target": weakSelf,
  98. @"selector": [NSValue valueWithPointer:@selector(toggleEditMode)]}];
  99. }
  100. }
  101. - (void)prepareViewDidLoad {
  102. //fetch devices from server
  103. if (![JDFacade facade].loginUser.homehubDeviceId || [[JDFacade facade].loginUser.homehubDeviceId isEmptyString]) {
  104. [_mainView bringSubviewToFront:_addHubContainerView];
  105. _collectionView.hidden = YES;
  106. } else {
  107. [_mainView bringSubviewToFront:_collectionView];
  108. _addHubContainerView.hidden = YES;
  109. _collectionView.hidden = NO;
  110. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  111. }
  112. }
  113. - (void)updateHomeHubStatusToDevices {
  114. for (DeviceModel *device in _deviceList) {
  115. device.onlineState = [JDFacade facade].loginUser.homehubOnlineState;
  116. }
  117. [_collectionView reloadData];
  118. }
  119. - (void)updateDevice:(DeviceModel *)device {
  120. // if ([device.deviceId isEqualToString:ldevice.deviceId]) {
  121. // ldevice.cmdclsValue = device.cmdclsValue;
  122. // ldevice.contentValue = device.contentValue;
  123. // break;
  124. // }
  125. }
  126. //장치상태를 조회함.
  127. - (void)requestPollingCommandStatusOfDeviceInBackground:(DeviceModel *)device {
  128. if (!_commandArray) {
  129. _commandArray = (NSMutableArray<DeviceModel> *)[[NSMutableArray alloc] init];
  130. }
  131. __block BOOL isStatusChanged = NO;
  132. if (device && [device isKindOfClass:[DeviceModel class]]) {//validate, aleady have,
  133. if (![_commandArray objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", device.deviceId, device.nodeId]) {//일치하는 디바이스가 있을 경우, 추가하지 않음.
  134. [_commandArray addObject:device];
  135. isStatusChanged = YES;
  136. }
  137. }
  138. //TODO : 추가 커맨드를 받을 것인가?
  139. if (_commandArray.count) {
  140. NSMutableString *pathParams = [[NSMutableString alloc] init];
  141. for (DeviceModel *pDevice in _commandArray) {
  142. NSString *prefix = [pathParams isEmptyString] ? ksEmptyString : @",";
  143. [pathParams appendFormat:@"%@%@_%@", prefix, pDevice.deviceId, pDevice.nodeId];
  144. }
  145. //20
  146. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_NODE_STATUS, pathParams];
  147. dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{//RUN to background thread
  148. DeviceListModel *fdevices = [[RequestHandler handler] sendSyncGetRequestAPIPath:path parameters:nil
  149. modelClass:[DeviceListModel class] showLoadingView:YES];
  150. if (fdevices && fdevices.list && fdevices.list.count) {
  151. [_commandArray enumerateObjectsUsingBlock:^(DeviceModel *rdevice, NSUInteger idx, BOOL * _Nonnull stop) {
  152. DeviceModel *matchedDevice = (DeviceModel *)[fdevices.list objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", rdevice.deviceId, rdevice.nodeId];
  153. //실행 여부 및 10초 경과 확인
  154. BOOL isOverTimeLimit = [self elapsedSecondsFromNow:rdevice] > 10;
  155. //실행 여부 확인
  156. NSInteger elapsedTime = [self elapsedSecondsFrom:rdevice to:matchedDevice];
  157. BOOL hasChangedStatus = elapsedTime > 0;
  158. rdevice.isRequesting = [rdevice.contentValue isEqualToString:matchedDevice.contentValue] && !hasChangedStatus && !isOverTimeLimit;
  159. //TODO - home hub check
  160. // rdevice.requestTime = matchedDevice.requestTime;
  161. // rdevice.collectTime = matchedDevice.collectTime;
  162. rdevice.onlineState = matchedDevice.onlineState;
  163. if (!rdevice.isRequesting || !rdevice.isOnline || ![JDFacade facade].loginUser.isHomehubOnline) {//정상적으로 변경됨.
  164. rdevice.contentValue = matchedDevice.contentValue;
  165. [_commandArray removeObject:rdevice];
  166. isStatusChanged = YES;
  167. }
  168. #ifdef DEBUG_MODE
  169. NSLogInfo(@"==########== device command status = %@, elapsedTime = %zd ==########==", [JDFacade facade].loginUser.homehubOnlineState, elapsedTime);
  170. #endif
  171. }];
  172. }
  173. if (_commandArray.count) {//커맨드 실행 중인 디바이스가 있을 경우,
  174. //schedul timer.
  175. if (!_deviceCommandsBackgroundTimer) {
  176. _deviceCommandsBackgroundTimer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(requestPollingCommandStatusOfDeviceInBackground:) userInfo:nil repeats:YES];
  177. }
  178. } else {
  179. [_deviceCommandsBackgroundTimer invalidate];
  180. _deviceCommandsBackgroundTimer = nil;
  181. }
  182. //변화가 있을 경우, 컬렉션뷰를 리로드
  183. if (isStatusChanged) {
  184. [_collectionView reloadData];
  185. ThingsDetailViewController *vc = (ThingsDetailViewController *)[JDFacade facade].currentViewController;
  186. if (vc) {
  187. [vc.tableView reloadData];
  188. }
  189. }
  190. });
  191. }
  192. }
  193. - (NSInteger)elapsedSecondsFromNow:(DeviceModel *)runningDevice {
  194. NSInteger seconds = 0;
  195. if (runningDevice.requestTime && ![runningDevice.requestTime isEmptyString]) {
  196. NSDate *rdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:runningDevice.requestTime]];
  197. NSTimeInterval elapsed = [[NSDate systemDate] timeIntervalSinceDate:rdate];
  198. seconds = elapsed;
  199. }
  200. return seconds;
  201. }
  202. - (NSInteger)elapsedSecondsFrom:(DeviceModel *)runningDevice to:(DeviceModel *)fetchedDevice {
  203. NSInteger seconds = 0;
  204. if (runningDevice.requestTime && ![runningDevice.requestTime isEmptyString] && fetchedDevice.collectTime && ![fetchedDevice.collectTime isEmptyString]) {
  205. NSDate *rdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:runningDevice.requestTime]];
  206. NSDate *fdate = [CommonUtil dateFromDateString:[CommonUtil localDateFromUTC:fetchedDevice.collectTime]];
  207. seconds = [fdate secondsAfterDate:rdate];
  208. }
  209. return seconds;
  210. }
  211. - (void)addNewDevice {
  212. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddViewController" storyboardName:@"Things"];
  213. [self presentViewController:vc animated:YES completion:nil];
  214. }
  215. - (void)toggleEditMode {
  216. _isDeleteMode = !_isDeleteMode;
  217. [_collectionView reloadData];
  218. _constraintEditModeRight.constant = _isDeleteMode ? 0 : -_editModeView.width;
  219. [UIView animateWithDuration:kfAnimationDur animations:^{
  220. [self.view layoutIfNeeded];
  221. }];
  222. }
  223. - (void)refreshDeviceList {
  224. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  225. }
  226. #pragma mark - Main Logic
  227. - (void)requestDeviceListRecently {
  228. DeviceModel *firstDevice = [_deviceList firstObject];
  229. _pagingType = ksListPagingTypeUpward;
  230. _pagingId = firstDevice.createDatetime;
  231. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  232. }
  233. - (void)requestDeviceListOlder {
  234. DeviceModel *lastDevice = [_deviceList lastObject];
  235. _pagingType = ksListPagingTypeDownward;
  236. _pagingId = lastDevice.createDatetime;
  237. [self performSelector:@selector(requestDeviceList) withObject:nil afterDelay:0.0f];
  238. }
  239. - (void)requestDeviceList {
  240. //parameters
  241. NSDictionary *parameter = @{@"paging_datetime": _pagingId ? _pagingId : ksEmptyString,
  242. @"paging_type": _pagingType ? _pagingType : ksEmptyString};
  243. NSString *path = [NSString stringWithFormat:API_GET_DEVICE_LIST];
  244. [[RequestHandler handler] sendAsyncGetRequestAPIPath:path parameters:parameter modelClass:[DeviceListModel class] completion:^(id responseObject) {
  245. if (!responseObject) {//응답결과가 잘못되었거나 없을 경우,
  246. return;
  247. }
  248. DeviceListModel *deviceList = (DeviceListModel *)responseObject;
  249. if (deviceList && deviceList.list && deviceList.list.count) {
  250. _deviceList = deviceList.list;
  251. [self updateTitle];
  252. } else {
  253. if (!_deviceList.count) {//이미 로드된 데이터가 있을 경우는 출력하지 않음.
  254. // NoContentView *noContentView = [NoContentView viewFromNib];
  255. // _tableView.tableFooterView = noContentView;
  256. }
  257. }
  258. [self matchDeviceListWithOnCommandsDevices];
  259. } failure:^(id errorObject) {
  260. JDErrorModel *error = (JDErrorModel *)errorObject;
  261. [[JDFacade facade] alert:error.errorMessage];
  262. }];
  263. }
  264. - (void)updateTitle {
  265. if (![JDFacade facade].loginUser.isHomehubOnline) {
  266. _lblTitle.text = @"홈허브 오프라인";
  267. [_lblTitle setColor:kUITextColor02 text:_lblTitle.text];
  268. return;
  269. }
  270. _lblTitle.text = [NSString stringWithFormat:@"장치 전체 %zd", _deviceList.count];
  271. [_lblTitle setColor:kUITextColor02 text:[NSString stringWithFormat:@"%zd", _deviceList.count]];
  272. }
  273. - (void)matchDeviceListWithOnCommandsDevices {
  274. for (DeviceModel *rdevice in _commandArray) {
  275. DeviceModel *matchedDevice = [_deviceList objectByUsingPredicateFormat:@"deviceId == %@ && nodeId == %@", rdevice.deviceId, rdevice.nodeId]; //일치하는 디바이스가 있을 경우, 추가하지 않음.
  276. if (matchedDevice) {
  277. matchedDevice.isRequesting = rdevice.isRequesting;
  278. matchedDevice.requestTime = rdevice.requestTime;
  279. }
  280. }
  281. [_collectionView reloadData];
  282. }
  283. #pragma mark - UICollectionView Delegate
  284. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  285. NSInteger auth = [JDFacade facade].loginUser.level == 90 && !_isDeleteMode; //마스터 권한일 경우,
  286. NSInteger count = _deviceList.count % 2 == 0 ? _deviceList.count : _deviceList.count + auth; //홀수일 경우, 멤버 초대 버튼을 추가해줌.
  287. return count;
  288. }
  289. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  290. UICollectionViewCell *rcell = nil;
  291. if (indexPath.row < _deviceList.count) {
  292. ThingsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
  293. DeviceModel *device =_deviceList[indexPath.row];
  294. cell.indexPath = indexPath;
  295. [cell.btnDevice sd_setImageWithURL:[NSURL URLWithString:device.imageFileName] forState:UIControlStateNormal
  296. placeholderImage:nil
  297. options:SDWebImageRefreshCached
  298. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  299. [cell layoutIfNeeded];
  300. }];
  301. cell.lblDeviceName.text = device.deviceName;
  302. //커맨드 클래스 뷰를 초기화함.
  303. [[cell.controlContainer subviews] enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
  304. UIView *subview = (UIView *)obj;
  305. [subview removeFromSuperview];
  306. }];
  307. cell.btnDelete.hidden = !_isDeleteMode;
  308. cell.controlContainer.hidden = _isDeleteMode;
  309. cell.aiv.hidden = !device.isRequesting;
  310. if (!cell.aiv.hidden) {//show
  311. [cell.aiv startAnimating];
  312. cell.btnDelete.userInteractionEnabled = NO;
  313. } else {//hide
  314. [cell.aiv stopAnimating];
  315. cell.btnDelete.userInteractionEnabled = YES;
  316. }
  317. if (!cell.controlContainer.hidden) {
  318. //허브 On-Off line check
  319. cell.lblDeviceStatus.hidden = !([[JDFacade facade].loginUser.homehubOnlineState isEqualToString:@"OFF"] || [device.onlineState isEqualToString:@"OFF"]);
  320. cell.controlContainer.hidden = !cell.lblDeviceStatus.hidden;
  321. if (cell.controlContainer.hidden) {
  322. cell.lblDeviceStatus.text = @"OFFLINE";
  323. } else {//커맨드 클래스 타입별 컨트롤 호출
  324. CommandClassControlView *controlView = [CommandClassControlView viewForCommandClass:device.cmdclsType];
  325. controlView.device = device;
  326. cell.controlContainer.hidden = !controlView;
  327. if (!cell.controlContainer.hidden) {
  328. UIView *superview = cell.controlContainer;
  329. [superview addSubview:controlView];
  330. [controlView mas_makeConstraints:^(MASConstraintMaker *make) {
  331. make.size.mas_equalTo(controlView.frame.size);
  332. make.center.equalTo(superview);
  333. }];
  334. }
  335. }
  336. } else {
  337. cell.btnDelete.value = device;
  338. if (![cell.btnDelete actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  339. [cell.btnDelete addTarget:self action:@selector(btnDeleteTouched:) forControlEvents:UIControlEventTouchUpInside];
  340. }
  341. }
  342. rcell = cell;
  343. } else {//디바이스 추가 옵션
  344. ThingsAddCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AddCollectionCellIdentifier" forIndexPath:indexPath];
  345. if (![cell.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  346. [cell.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
  347. }
  348. rcell = cell;
  349. }
  350. return rcell;
  351. }
  352. - (void)btnDeleteTouched:(id)sender {
  353. CustomButton *btn = (CustomButton *)sender;
  354. DeviceModel *device = (DeviceModel *)btn.value;
  355. ThingsAddStartViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"ThingsAddStartViewController" storyboardName:@"Things"];
  356. vc.removableDevice = device;
  357. if (device.groupName && ![device.groupName isEmptyString]) {
  358. vc.removableGroups = [_deviceList filteredArrayUsingPredicateFormat:@"groupName == %@", device.groupName]; //group devices
  359. }
  360. vc.providesPresentationContextTransitionStyle = YES;
  361. vc.definesPresentationContext = YES;
  362. [vc setModalPresentationStyle:UIModalPresentationOverCurrentContext];
  363. [self presentViewController:vc animated:NO completion:nil];
  364. }
  365. - (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView
  366. viewForSupplementaryElementOfKind:(NSString *)kind
  367. atIndexPath:(NSIndexPath *)indexPath
  368. {
  369. ThingsCollectionFooterView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter
  370. withReuseIdentifier:@"FooterIdentifier" forIndexPath:indexPath];
  371. if (![footerView.btnAdd actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  372. [footerView.btnAdd addTarget:self action:@selector(addNewDevice) forControlEvents:UIControlEventTouchUpInside];
  373. }
  374. return footerView;
  375. }
  376. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
  377. //FIXME : 권한 추가
  378. // if (_memberList.count % 2 == 1 || [JDFacade facade].loginUser.level < 90 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  379. if (_deviceList.count % 2 == 1 || _isDeleteMode) {//마스터 권한이 아니거나, 짝수가 아닐 경우
  380. return CGSizeZero;
  381. }
  382. return CGSizeMake(IPHONE_WIDTH, 160.0f);
  383. }
  384. - (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
  385. if (IPHONE_WIDTH == 414.0f) {//아이폰 6일 경우,
  386. return CGSizeMake(212.0f, 197.0f);
  387. } else if (IPHONE_WIDTH == 375.0f) {//아이폰 6+일경우
  388. return CGSizeMake(187.5, 197.0f);
  389. }
  390. return CGSizeMake(160.0f, 197.0f);
  391. }
  392. - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
  393. [collectionView deselectItemAtIndexPath:indexPath animated:YES];
  394. if (indexPath.row < _deviceList.count) {//디바이스인 경우,
  395. ThingsCollectionViewCell *cell = (ThingsCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"ThingsCellIdentifier" forIndexPath:indexPath];
  396. DeviceModel *device = _deviceList[indexPath.row];
  397. ThingsDetailViewController *vc = (ThingsDetailViewController *)[CommonUtil instantiateViewControllerWithIdentifier:@"ThingsDetailViewController" storyboardName:@"Things"];
  398. vc.refDevice = device;
  399. [self presentViewController:vc animated:YES completion:nil];
  400. }
  401. }
  402. #pragma mark - UI Events
  403. - (void)btnAddHubTouched:(id)sender {
  404. [[JDFacade facade] gotoHomeHubRegistration];
  405. }
  406. - (IBAction)btnOptionTouched:(id)sender {
  407. [self toggleOptions:sender];
  408. }
  409. - (IBAction)btnCloseOnEditModeTouched:(id)sender {
  410. [self toggleEditMode];
  411. }
  412. #pragma mark - MemoryWarning
  413. - (void)viewWillDisappear:(BOOL)animated {
  414. if (_deviceCommandsBackgroundTimer) {
  415. [_deviceCommandsBackgroundTimer invalidate];
  416. _deviceCommandsBackgroundTimer = nil;
  417. }
  418. }
  419. - (void)didReceiveMemoryWarning
  420. {
  421. [super didReceiveMemoryWarning];
  422. // Dispose of any resources that can be recreated.
  423. }
  424. @end