HomeHubViewController.m 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 "HomeHubUpdateGuideViewController.h"
  16. #import "HomeHubUpdateStartViewController.h"
  17. //#import <LGBluetooth/LGBluetooth.h>
  18. @implementation HomeHubTableViewCell
  19. @end
  20. @implementation HomeHubRegistTableViewCell
  21. - (void)didMoveToSuperview {
  22. [self layoutIfNeeded];
  23. }
  24. @end
  25. @implementation HomeHubInfoTableViewCell
  26. @end
  27. @interface HomeHubViewController () {
  28. BOOL bleState;
  29. DeviceModel *hubInfo;
  30. BLEServiceHandler *bleService;
  31. }
  32. @end
  33. #pragma mark - Class Definition
  34. @implementation HomeHubViewController
  35. - (void)viewDidLoad {
  36. [super viewDidLoad];
  37. // Do any additional setup after loading the view.
  38. [self initUI];
  39. [self prepareViewDidLoad];
  40. }
  41. - (void)viewWillAppear:(BOOL)animated {
  42. [super viewWillAppear:animated];
  43. bleService.delegate = self;
  44. bleState = [[BLEServiceHandler sharedManager] checkBLEStatus];
  45. [self refreshData];
  46. }
  47. - (void)viewWillDisappear:(BOOL)animated {
  48. [super viewWillDisappear:animated];
  49. bleService.delegate = nil;
  50. }
  51. #pragma mark - MemoryWarning
  52. - (void)didReceiveMemoryWarning {
  53. [super didReceiveMemoryWarning];
  54. // Dispose of any resources that can be recreated.
  55. }
  56. - (void)initUI {
  57. UIView *statusBar = [[[UIApplication sharedApplication] valueForKey:@"statusBarWindow"] valueForKey:@"statusBar"];
  58. statusBar.backgroundColor = [UIColor whiteColor];
  59. [self.navigationController.navigationBar setHidden:YES];
  60. self.navigationController.interactivePopGestureRecognizer.enabled = NO;
  61. [self initTableViewAsDefaultStyle:_tableView];
  62. }
  63. - (void)prepareViewDidLoad {
  64. //ble
  65. bleState = NO;
  66. bleService = [BLEServiceHandler sharedManager];
  67. }
  68. - (void)updateHomeHubStatus {
  69. [self refreshData];
  70. }
  71. - (void)refreshData {
  72. if (_selectHub == nil) {
  73. hubInfo = [[JDFacade facade].loginUser getHomeHub];
  74. } else {
  75. hubInfo = [[JDFacade facade].loginUser getHomeHub:_selectHub.deviceId];
  76. }
  77. [[JDFacade facade].loginUser setHomeHubID:hubInfo.deviceId];
  78. _lblTitle.text = hubInfo.deviceName;
  79. NSLog(@"homehubVC hubinfo : %@",hubInfo);
  80. [_tableView reloadData];
  81. }
  82. #pragma mark - UITableView DataSource & Delegate
  83. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  84. return 1;
  85. }
  86. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  87. return 3;
  88. }
  89. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  90. CGFloat height = 86.0f;
  91. if (indexPath.row == 0) {
  92. height = 185.0f;
  93. } else if (indexPath.row == 1){
  94. height = hubInfo.isUpdateNeed ? 219.0f : 182.0f;
  95. } else if (indexPath.row == 2){
  96. height = 164.0f;
  97. }
  98. return height;
  99. }
  100. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  101. UITableViewCell *cell = nil;
  102. if (indexPath.row == 0) {
  103. HomeHubTableViewCell *tcell = (HomeHubTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HubCellIdentifier"];
  104. if (![tcell.btnSet actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  105. [tcell.btnSet addTarget:self action:@selector(btnSetTouched:) forControlEvents:UIControlEventTouchUpInside];
  106. }
  107. [self masterBtn:tcell.btnSet];
  108. if ([hubInfo isDeviceOnlined]) {
  109. tcell.lblNetworkStatus.text = @"온라인";
  110. tcell.lblBrokenTime.text = @"";
  111. tcell.lblNetworkStatus.textColor = kUITextColor04;
  112. tcell.imgvStatus.hidden = YES;
  113. } else {
  114. tcell.lblNetworkStatus.text = @"오프라인";
  115. tcell.lblBrokenTime.text = [CommonUtil connectDataFromStr:hubInfo.deviceOnlineLastDatetime];
  116. tcell.lblNetworkStatus.textColor = kUITextColor07;
  117. tcell.lblBrokenTime.textColor = kUITextColor07;
  118. tcell.imgvStatus.hidden = NO;
  119. }
  120. if([hubInfo isDeviceConn]){
  121. tcell.lblDeviceStatus.text = @"온라인";
  122. tcell.lblDeviceStatus.textColor = kUITextColor04;
  123. } else {
  124. tcell.lblDeviceStatus.text = @"오프라인";
  125. tcell.lblDeviceStatus.textColor = kUITextColor07;
  126. }
  127. cell = tcell;
  128. } else if (indexPath.row == 1) {
  129. HomeHubRegistTableViewCell *tcell = (HomeHubRegistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RegistCellIdentifier"];
  130. tcell.lblFirmCurrentVer.text = [NSString stringWithFormat:@"현재%@/최신%@", hubInfo.firmwareVersion,hubInfo.lastFirmwareVersion];
  131. tcell.lblSoftCurrentVer.text = [NSString stringWithFormat:@"현재%@/최신%@", hubInfo.softwareVersion,hubInfo.lastSoftwareVersion];
  132. tcell.viewUpdate.hidden = !hubInfo.isUpdateNeed;
  133. if (hubInfo.isUpdateSoftware) {
  134. tcell.lblUpdate.text = [NSString stringWithFormat:@"소프트웨어: 업데이트 버전%@",
  135. hubInfo.lastSoftwareVersion];
  136. }
  137. if (hubInfo.isUpdateFirmware) {
  138. tcell.lblUpdate.text = [NSString stringWithFormat:@"펌웨어: 업데이트 버전%@",
  139. hubInfo.lastFirmwareVersion];
  140. }
  141. [self masterBtn:tcell.btnSoftUpdate];
  142. if (![tcell.btnSoftUpdate actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  143. [tcell.btnSoftUpdate addTarget:self action:@selector(btnSofrwareUpdateTouched:) forControlEvents:UIControlEventTouchUpInside];
  144. }
  145. cell = tcell;
  146. } else if (indexPath.row == 2) {
  147. HomeHubInfoTableViewCell *tcell = (HomeHubInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HubInfoCellIdentifier"];
  148. tcell.lblModelName.text = hubInfo.deviceModelId;
  149. tcell.lblSerialNum.text = hubInfo.deviceSn;
  150. cell = tcell;
  151. }
  152. return cell;
  153. }
  154. - (void)masterBtn:(CustomButton *)button{
  155. if ([JDFacade facade].loginUser.level < 90) {
  156. button.hidden = YES;
  157. } else {
  158. button.hidden = NO;
  159. }
  160. }
  161. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  162. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  163. }
  164. - (void)changeDeviceName:(NSString *)deviceName {
  165. _lblTitle.text = deviceName;
  166. NSMutableArray *deviceList = [[JDFacade facade].loginUser.deviceList mutableCopy];
  167. for (NSInteger i = 0;i<deviceList.count;i++) {
  168. DeviceModel *device = [deviceList objectAtIndex:i];
  169. if (EQUALS(device.deviceId, _selectHub.deviceId)) {
  170. DeviceModel *newDeviceModel = [[DeviceModel alloc] initWithDeviceModel:device deviceName:deviceName];
  171. [deviceList replaceObjectAtIndex:i withObject:newDeviceModel];
  172. }
  173. }
  174. [[JDFacade facade].loginUser setDeviceList:deviceList];
  175. }
  176. #pragma mark User Event
  177. - (IBAction)btnEditTitleTouched:(id)sender {
  178. ChangeDeviceNamePopupView *popup = [[ChangeDeviceNamePopupView alloc]initFromNib:[DeviceDetailModel new]];
  179. [popup show];
  180. }
  181. - (void)btnSetTouched:(id)sender {
  182. if (bleState) {
  183. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubSearchViewController" storyboardName:@"HomeHub"];
  184. [self.navigationController pushViewController:vc animated:YES];
  185. }
  186. else {
  187. bleState = [[BLEServiceHandler sharedManager] checkBLEStatus] ;
  188. }
  189. }
  190. //펌웨어 or 소프트웨어 업데이트
  191. - (void)btnSofrwareUpdateTouched:(id)sender{
  192. NSInteger updateType = UPDATE_ALL;
  193. if (hubInfo.isUpdateFirmware && !hubInfo.isUpdateSoftware)
  194. updateType = UPDATE_FIRMWARE;
  195. else if (!hubInfo.isUpdateFirmware && hubInfo.isUpdateSoftware)
  196. updateType = UPDATE_SOFTWARE;
  197. HomeHubUpdateGuideViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateGuideViewController" storyboardName:@"HomeHub"];
  198. vc.updateType = updateType;
  199. vc.hubInfo = hubInfo;
  200. [self.navigationController pushViewController:vc animated:YES];
  201. }
  202. #pragma mark - UI Events
  203. - (IBAction)btnSecureTouched:(id)sender {
  204. }
  205. - (void)btnCloseTouched:(id)sender {
  206. if ([[JDFacade facade].loginUser isMultiHomeHub])
  207. [self.navigationController popViewControllerAnimated:YES];
  208. else
  209. [self dismissViewControllerAnimated:YES completion:nil];
  210. }
  211. #pragma mark - ble service delegate
  212. - (void)BLEStateChange:(BOOL)state {
  213. bleState = state;
  214. }
  215. #pragma mark - SocketService
  216. -(void) receiveSocketData:(NSNotification *)notification {
  217. SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
  218. //NSLog(@"receiveSocketData Result : %@", result);
  219. [self processSocket:result];
  220. }
  221. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result {
  222. //NSLog(@"receiveSocketData Result : %@", result);
  223. [self processSocket:result];
  224. }
  225. - (void)processSocket:(SocketModel*)result {
  226. if (EQUALS(result.messageType, MSG_TYPE_DEVICE_CONN) ||
  227. EQUALS(result.messageType, MSG_TYPE_DEVICE_CONTENT)) {
  228. [self performSelector:@selector(refreshData) withObject:nil afterDelay:1.0f];
  229. }
  230. }
  231. @end