HomeHubViewController.m 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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)refreshData {
  69. if (_selectHub == nil) {
  70. hubInfo = [[JDFacade facade].loginUser getHomeHub];
  71. } else {
  72. hubInfo = [[JDFacade facade].loginUser getHomeHub:_selectHub.deviceId];
  73. }
  74. [[JDFacade facade].loginUser setHomeHubID:hubInfo.deviceId];
  75. _lblTitle.text = hubInfo.deviceName;
  76. NSLog(@"homehubVC hubinfo : %@",hubInfo);
  77. [_tableView reloadData];
  78. }
  79. #pragma mark - UITableView DataSource & Delegate
  80. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  81. return 1;
  82. }
  83. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  84. return 3;
  85. }
  86. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  87. CGFloat height = 86.0f;
  88. if (indexPath.row == 0) {
  89. height = 185.0f;
  90. } else if (indexPath.row == 1){
  91. height = hubInfo.isUpdateNeed ? 219.0f : 182.0f;
  92. } else if (indexPath.row == 2){
  93. height = 164.0f;
  94. }
  95. return height;
  96. }
  97. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  98. UITableViewCell *cell = nil;
  99. if (indexPath.row == 0) {
  100. HomeHubTableViewCell *tcell = (HomeHubTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HubCellIdentifier"];
  101. if (![tcell.btnSet actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  102. [tcell.btnSet addTarget:self action:@selector(btnSetTouched:) forControlEvents:UIControlEventTouchUpInside];
  103. }
  104. [self masterBtn:tcell.btnSet];
  105. if ([hubInfo isDeviceOnlined]) {
  106. tcell.lblNetworkStatus.text = @"온라인";
  107. tcell.lblBrokenTime.text = @"";
  108. tcell.lblNetworkStatus.textColor = kUITextColor04;
  109. tcell.imgvStatus.hidden = YES;
  110. } else {
  111. tcell.lblNetworkStatus.text = @"오프라인";
  112. tcell.lblBrokenTime.text = [CommonUtil connectDataFromStr:hubInfo.deviceOnlineLastDatetime];
  113. tcell.lblNetworkStatus.textColor = kUITextColor07;
  114. tcell.lblBrokenTime.textColor = kUITextColor07;
  115. tcell.imgvStatus.hidden = NO;
  116. }
  117. if([hubInfo isDeviceConn]){
  118. tcell.lblDeviceStatus.text = @"온라인";
  119. tcell.lblDeviceStatus.textColor = kUITextColor04;
  120. } else {
  121. tcell.lblDeviceStatus.text = @"오프라인";
  122. tcell.lblDeviceStatus.textColor = kUITextColor07;
  123. }
  124. cell = tcell;
  125. } else if (indexPath.row == 1) {
  126. HomeHubRegistTableViewCell *tcell = (HomeHubRegistTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"RegistCellIdentifier"];
  127. tcell.lblFirmCurrentVer.text = hubInfo.firmwareVersion;
  128. tcell.lblSoftCurrentVer.text = hubInfo.softwareVersion;
  129. tcell.viewUpdate.hidden = !hubInfo.isUpdateNeed;
  130. if (hubInfo.isUpdateSoftware) {
  131. tcell.lblUpdate.text = [NSString stringWithFormat:@"소프트웨어: 업데이트 버전%@",
  132. hubInfo.lastSoftwareVersion];
  133. }
  134. if (hubInfo.isUpdateFirmware) {
  135. tcell.lblUpdate.text = [NSString stringWithFormat:@"펌웨어: 업데이트 버전%@",
  136. hubInfo.lastFirmwareVersion];
  137. }
  138. [self masterBtn:tcell.btnSoftUpdate];
  139. if (![tcell.btnSoftUpdate actionsForTarget:self forControlEvent:UIControlEventTouchUpInside]) {
  140. [tcell.btnSoftUpdate addTarget:self action:@selector(btnSofrwareUpdateTouched:) forControlEvents:UIControlEventTouchUpInside];
  141. }
  142. cell = tcell;
  143. } else if (indexPath.row == 2) {
  144. HomeHubInfoTableViewCell *tcell = (HomeHubInfoTableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"HubInfoCellIdentifier"];
  145. tcell.lblModelName.text = hubInfo.deviceModelId;
  146. tcell.lblSerialNum.text = hubInfo.deviceSn;
  147. cell = tcell;
  148. }
  149. return cell;
  150. }
  151. - (void)masterBtn:(CustomButton *)button{
  152. if ([JDFacade facade].loginUser.level < 90) {
  153. button.hidden = YES;
  154. } else {
  155. button.hidden = NO;
  156. }
  157. }
  158. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  159. [super tableView:tableView didSelectRowAtIndexPath:indexPath];
  160. }
  161. - (void)changeDeviceName:(NSString *)deviceName {
  162. _lblTitle.text = deviceName;
  163. NSMutableArray *deviceList = [[JDFacade facade].loginUser.deviceList mutableCopy];
  164. for (NSInteger i = 0;i<deviceList.count;i++) {
  165. DeviceModel *device = [deviceList objectAtIndex:i];
  166. if (EQUALS(device.deviceId, _selectHub.deviceId)) {
  167. DeviceModel *newDeviceModel = [[DeviceModel alloc] initWithDeviceModel:device deviceName:deviceName];
  168. [deviceList replaceObjectAtIndex:i withObject:newDeviceModel];
  169. }
  170. }
  171. [[JDFacade facade].loginUser setDeviceList:deviceList];
  172. }
  173. #pragma mark User Event
  174. - (IBAction)btnEditTitleTouched:(id)sender {
  175. ChangeDeviceNamePopupView *popup = [[ChangeDeviceNamePopupView alloc]initFromNib:[DeviceDetailModel new]];
  176. popup.delegate = self;
  177. [popup showChangeDevice:hubInfo];
  178. }
  179. - (void)btnSetTouched:(id)sender {
  180. if (bleState) {
  181. UIViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubSearchViewController" storyboardName:@"HomeHub"];
  182. [self.navigationController pushViewController:vc animated:YES];
  183. }
  184. else {
  185. bleState = [[BLEServiceHandler sharedManager] checkBLEStatus] ;
  186. }
  187. }
  188. //펌웨어 or 소프트웨어 업데이트
  189. - (void)btnSofrwareUpdateTouched:(id)sender{
  190. NSInteger updateType = UPDATE_ALL;
  191. if (hubInfo.isUpdateFirmware && !hubInfo.isUpdateSoftware)
  192. updateType = UPDATE_FIRMWARE;
  193. else if (!hubInfo.isUpdateFirmware && hubInfo.isUpdateSoftware)
  194. updateType = UPDATE_SOFTWARE;
  195. HomeHubUpdateGuideViewController *vc = [CommonUtil instantiateViewControllerWithIdentifier:@"HomeHubUpdateGuideViewController" storyboardName:@"HomeHub"];
  196. vc.updateType = updateType;
  197. vc.hubInfo = hubInfo;
  198. [self.navigationController pushViewController:vc animated:YES];
  199. }
  200. #pragma mark - UI Events
  201. - (IBAction)btnSecureTouched:(id)sender {
  202. }
  203. - (void)btnCloseTouched:(id)sender {
  204. if ([[JDFacade facade].loginUser isMultiHomeHub])
  205. [self.navigationController popViewControllerAnimated:YES];
  206. else
  207. [self dismissViewControllerAnimated:YES completion:nil];
  208. }
  209. #pragma mark - ble service delegate
  210. - (void)BLEStateChange:(BOOL)state {
  211. NSLog(@"home hub vc ble state change!") ;
  212. bleState = state;
  213. }
  214. #pragma mark - SocketService
  215. -(void) receiveSocketData:(NSNotification *)notification {
  216. SocketModel *result = [[SocketModel alloc] initWithDictionary:notification.object error:nil];
  217. //NSLog(@"receiveSocketData Result : %@", result);
  218. [self processSocket:result];
  219. }
  220. - (void) socketDidReceiveMessage:(id)message result:(SocketModel *)result {
  221. //NSLog(@"receiveSocketData Result : %@", result);
  222. [self processSocket:result];
  223. }
  224. - (void)processSocket:(SocketModel*)result {
  225. if (EQUALS(result.messageType, MSG_TYPE_DEVICE_CONN) ||
  226. EQUALS(result.messageType, MSG_TYPE_DEVICE_CONTENT)) {
  227. [self performSelector:@selector(refreshData) withObject:nil afterDelay:1.0f];
  228. }
  229. }
  230. @end