HomeHubViewController.m 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // HomeHubViewController.m
  3. // kneet2
  4. //
  5. // Created by Jason Lee on 10/28/15.
  6. // Copyright © 2015 ntels. All rights reserved.
  7. //
  8. #import "HomeHubViewController.h"
  9. #import "RequestHandler.h"
  10. #import "DeviceModel.h"
  11. #import "CustomTableView.h"
  12. #import "CustomButton.h"
  13. #import "Reachability.h"
  14. #import "ChangeDeviceNamePopupView.h"
  15. //#import <LGBluetooth/LGBluetooth.h>
  16. @implementation HomeHubTableViewCell
  17. @end
  18. @implementation HomeHubRegistTableViewCell
  19. - (void)didMoveToSuperview {
  20. [self layoutIfNeeded];
  21. }
  22. @end
  23. @implementation HomeHubInfoTableViewCell
  24. @end
  25. @interface HomeHubViewController () {
  26. BOOL bleState;
  27. DeviceModel *hubInfo;
  28. BLEServiceHandler *bleService;
  29. }
  30. @end
  31. #pragma mark - Class Definition
  32. @implementation HomeHubViewController
  33. - (void)viewDidLoad {
  34. [super viewDidLoad];
  35. // Do any additional setup after loading the view.
  36. [self initUI];
  37. [self prepareViewDidLoad];
  38. }
  39. - (void)viewWillAppear:(BOOL)animated {
  40. [super viewWillAppear:animated];
  41. bleService.delegate = self;
  42. bleState = [[BLEServiceHandler sharedManager] checkBLEStatus];
  43. }
  44. - (void)viewWillDisappear:(BOOL)animated {
  45. [super viewWillDisappear:animated];
  46. bleService.delegate = nil;
  47. }
  48. - (void)initUI {
  49. UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
  50. statusBar.backgroundColor = [UIColor whiteColor];
  51. [self.navigationController.navigationBar setHidden:YES];
  52. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  53. [self initTableViewAsDefaultStyle:_tableView];
  54. }
  55. - (void)prepareViewDidLoad {
  56. //ble
  57. bleState = NO;
  58. bleService = [BLEServiceHandler sharedManager];
  59. if (_selectHub == nil) {
  60. hubInfo = [[JDFacade facade].loginUser getHomeHub];
  61. } else {
  62. hubInfo = _selectHub;
  63. }
  64. [[JDFacade facade].loginUser setHomeHubID:hubInfo.deviceId];
  65. _lblTitle.text = hubInfo.deviceName;
  66. }
  67. - (void)updateHomeHubStatus {
  68. [_tableView reloadData];
  69. }
  70. #pragma mark - UITableView DataSource & Delegate
  71. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  72. return 1;
  73. }
  74. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  75. return 3;
  76. }
  77. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  78. CGFloat height = 86.0f;
  79. if (indexPath.row == 0) {
  80. height = 185.0f;
  81. } else if (indexPath.row == 1){
  82. height = hubInfo.isUpdateNeed ? 219.0f : 182.0f;
  83. } else if (indexPath.row == 2){
  84. height = 164.0f;
  85. }
  86. return height;
  87. }
  88. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  89. UITableViewCell *cell = nil;
  90. if (indexPath.row == 0) {
  91. HomeHubTableViewCell *tcell = (HomeHubTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HubCellIdentifier"];
  92. if (![tcell.btnSet actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  93. [tcell.btnSet addTarget:self action:@selector(btnSetTouched:) forControlEvents:UIControlEventTouchUpInside];
  94. }
  95. [self masterBtn:tcell.btnSet];
  96. if ([hubInfo isDeviceOnlined]) {
  97. tcell.lblNetworkStatus.text = @"온라인";
  98. tcell.lblBrokenTime.text = @"";
  99. tcell.lblNetworkStatus.textColor = kUITextColor04;
  100. tcell.imgvStatus.hidden = YES;
  101. } else {
  102. tcell.lblNetworkStatus.text = @"오프라인";
  103. tcell.lblBrokenTime.text = [CommonUtil connectDataFromStr:hubInfo.deviceOnlineLastDatetime];
  104. tcell.lblNetworkStatus.textColor = kUITextColor07;
  105. tcell.lblBrokenTime.textColor = kUITextColor07;
  106. tcell.imgvStatus.hidden = NO;
  107. }
  108. if([hubInfo isDeviceConn]){
  109. tcell.lblDeviceStatus.text = @"온라인";
  110. tcell.lblDeviceStatus.textColor = kUITextColor04;
  111. } else {
  112. tcell.lblDeviceStatus.text = @"오프라인";
  113. tcell.lblDeviceStatus.textColor = kUITextColor07;
  114. }
  115. cell = tcell;
  116. } else if (indexPath.row == 1) {
  117. HomeHubRegistTableViewCell *tcell = (HomeHubRegistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RegistCellIdentifier"];
  118. // tcell.lblDate.text = [CommonUtil formattedDate3:[JDFacade facade].loginUser.homehubCreateDatetime];
  119. tcell.lblFirmNewestVer.text = hubInfo.lastFirmwareVersion;
  120. tcell.lblFirmCurrentVer.text = hubInfo.firmwareVersion;
  121. tcell.lblSoftNewestVer.text = hubInfo.lastSoftwareVersion;
  122. tcell.lblSoftCurrentVer.text = hubInfo.softwareVersion;
  123. tcell.viewUpdate.hidden = !hubInfo.isUpdateNeed;
  124. [self masterBtn:tcell.btnSoftUpdate];
  125. if (![tcell.btnSoftUpdate actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  126. [tcell.btnSoftUpdate addTarget:self action:@selector(btnSofrwareUpdateTouched:) forControlEvents:UIControlEventTouchUpInside];
  127. }
  128. cell = tcell;
  129. } else if (indexPath.row == 2) {
  130. HomeHubInfoTableViewCell *tcell = (HomeHubInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HubInfoCellIdentifier"];
  131. tcell.lblModelName.text = hubInfo.deviceModelId;
  132. tcell.lblSerialNum.text = hubInfo.deviceSn;
  133. cell = tcell;
  134. }
  135. return cell;
  136. }
  137. - (void)masterBtn:(CustomButton *)button{
  138. if ([JDFacade facade].loginUser.level < 90) {
  139. button.hidden = YES;
  140. } else {
  141. button.hidden = NO;
  142. }
  143. }
  144. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  145. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  146. }
  147. - (void)changeDeviceName:(NSString *)deviceName {
  148. _lblTitle.text = deviceName;
  149. NSMutableArray *deviceList = [[JDFacade facade].loginUser.deviceList mutableCopy];
  150. for (NSInteger i = 0;i<deviceList.count;i++) {
  151. DeviceModel *device = [deviceList objectAtIndex:i];
  152. if (EQUALS(device.deviceId, _selectHub.deviceId)) {
  153. DeviceModel *newDeviceModel = [[DeviceModel alloc] initWithDeviceModel:device deviceName:deviceName];
  154. [deviceList replaceObjectAtIndex:i withObject:newDeviceModel];
  155. }
  156. }
  157. [[JDFacade facade].loginUser setDeviceList:deviceList];
  158. }
  159. #pragma mark User Event
  160. - (IBAction)btnEditTitleTouched:(id)sender {
  161. ChangeDeviceNamePopupView *popup = [[ChangeDeviceNamePopupView alloc]initFromNib:[DeviceDetailModel new]];
  162. popup.delegate = self;
  163. [popup showChangeDevice:hubInfo];
  164. }
  165. - (void)btnSetTouched:(id)sender {
  166. if (bleState) {
  167. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubSearchViewController" storyboardName:@"HomeHub"];
  168. [self.navigationController pushViewController:vc animated:YES];
  169. }
  170. else {
  171. bleState = [[BLEServiceHandler sharedManager] checkBLEStatus] ;
  172. }
  173. }
  174. - (void)btnFirmwareUpdateTouched:(id)sender{
  175. NSLog(@"펌웨어 업데이트");
  176. }
  177. - (void)btnSofrwareUpdateTouched:(id)sender{
  178. NSLog(@"소프트웨어 업데이트");
  179. }
  180. #pragma mark - UI Events
  181. - (IBAction)btnSecureTouched:(id)sender {
  182. }
  183. - (void)btnCloseTouched:(id)sender {
  184. if ([[JDFacade facade].loginUser isMultiHomeHub])
  185. [self.navigationController popViewControllerAnimated:YES];
  186. else
  187. [self dismissViewControllerAnimated:YES completion:nil];
  188. }
  189. #pragma mark - ble service delegate
  190. - (void)BLEStateChange:(BOOL)state {
  191. NSLog(@"home hub vc ble state change!") ;
  192. bleState = state;
  193. }
  194. #pragma mark - MemoryWarning
  195. - (void)didReceiveMemoryWarning
  196. {
  197. [super didReceiveMemoryWarning];
  198. // Dispose of any resources that can be recreated.
  199. }
  200. @end