YXDeviceManagerViewController.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. //
  2. // YXDeviceManagerViewController.m
  3. // Temperature
  4. //
  5. // Created by z on 2025/1/18.
  6. //
  7. #import "YXDeviceManagerViewController.h"
  8. #import "YXDeviceTableViewCell.h"
  9. #import "YXBottomItemView.h"
  10. #import "YXDeviceManagerModel.h"
  11. #import <SDWebImage/SDWebImage.h>
  12. #import "YXSelectRoomView.h"
  13. #import "RDSControlWebVC.h"
  14. #import "YXDeviceViewController.h"
  15. #import "RDSAddRoomView.h"
  16. #import "RDSRootControl.h"
  17. @interface YXDeviceManagerViewController ()<UITableViewDataSource,UITableViewDelegate>
  18. @property (weak, nonatomic) UITableView *tableView;
  19. @property (weak, nonatomic) UIView *bottomView;
  20. @property (nonatomic, strong) RDSAddRoomView *addView;
  21. @property (copy, nonatomic) NSArray *dataArray;
  22. @property (strong, nonatomic) YXDeviceManagerModel *selectedModel;
  23. @end
  24. @implementation YXDeviceManagerViewController
  25. - (void)viewDidLoad {
  26. [super viewDidLoad];
  27. self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  28. self.navigationItem.title = @"设备管理";
  29. [self addRightItem];
  30. [self createTableView];
  31. // [self createBottomView];
  32. [self p_getDevices];
  33. }
  34. -(YXDeviceManagerModel *)selectedModel
  35. {
  36. YXDeviceManagerModel *selectedModel;
  37. for (YXDeviceManagerModel *model in self.dataArray) {
  38. if (model.isSelected == YES) {
  39. selectedModel = model;
  40. }
  41. }
  42. return selectedModel;
  43. }
  44. -(void)p_getDevices
  45. {
  46. RDS_WEAKSELF(weakSelf)
  47. [YXDeviceManagerModel getRoomDeviceWithRoomId:@"" finished:^(NSArray * _Nullable devices, NSError * _Nullable error) {
  48. weakSelf.dataArray = devices;
  49. self.addView.hidden = self.dataArray.count == 0 ? NO : YES;
  50. [weakSelf.tableView reloadData];
  51. }];
  52. }
  53. -(void)p_updateDeviceInfoWithRoomId:(NSString *)roomId
  54. {
  55. RDS_WEAKSELF(weakSelf)
  56. [RDSDemoApiHelper rds_updateDevice:self.selectedModel.record_id name:self.selectedModel.name homeId:self.selectedModel.home_id roomId:roomId success:^(id responseObject) {
  57. if ([responseObject[@"code"] intValue] == 9999) {
  58. [RDSRootControl shareControl].isLoginSuccess = NO;
  59. TheDataManager.token = @"";
  60. }
  61. if ([responseObject[@"code"] intValue] == 0) {
  62. [weakSelf.tableView reloadData];
  63. } else{
  64. // NSError *error = [NSError errorWithDomain:responseObject[@"message"] code:[responseObject[@"code"] intValue] userInfo:nil];
  65. [RDSHudShower showBottomToast:responseObject[@"message"]];
  66. }
  67. } failure:^(NSError *error) {
  68. }];
  69. }
  70. -(void)p_updateDevicesSequenceWithArray:(NSArray *)devices
  71. {
  72. NSMutableArray *newDevicesArray = [[NSMutableArray alloc]initWithCapacity:5];
  73. for (int i = 0; i < devices.count; i++) {
  74. YXDeviceManagerModel *model = devices[i];
  75. NSString *deviceId = model.device_code;
  76. [newDevicesArray addObject:deviceId];
  77. }
  78. RDS_WEAKSELF(weakSelf)
  79. [RDSDemoApiHelper rds_putDevicesSequence:newDevicesArray success:^(id responseObject) {
  80. if ([responseObject[@"code"] intValue] == 0) {
  81. weakSelf.dataArray = devices;
  82. [weakSelf.tableView reloadData];
  83. } else{
  84. [RDSHudShower showBottomToast:responseObject[@"message"]];
  85. }
  86. } failure:^(NSError *error) {
  87. }];
  88. }
  89. -(void)p_deleteDevices
  90. {
  91. NSMutableArray *selectedArray = [[NSMutableArray alloc]initWithCapacity:5];
  92. for (int i = 0; i < self.dataArray.count; i++) {
  93. YXDeviceManagerModel *model = selectedArray[i];
  94. NSString *deviceId = model.device_code;
  95. [selectedArray addObject:deviceId];
  96. }
  97. }
  98. -(void)p_deleteDeviceWithIndex:(NSInteger)index
  99. {
  100. YXDeviceManagerModel *model = self.dataArray[index];
  101. RDS_WEAKSELF(weakSelf)
  102. [RDSDemoApiHelper rds_deleteDeviceWithId:model.record_id success:^(id responseObject) {
  103. if ([responseObject[@"code"] intValue] == 0) {
  104. [RDSHudShower showBottomToast:@"删除成功"];
  105. [weakSelf p_getDevices];
  106. } else{
  107. [RDSHudShower showBottomToast:responseObject[@"message"]];
  108. }
  109. } failure:^(NSError *error) {
  110. }];
  111. }
  112. -(void)addRightItem
  113. {
  114. UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
  115. [rightBtn setTitle:@"添加设备" forState:UIControlStateNormal];
  116. [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside];
  117. [rightBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal];
  118. rightBtn.titleLabel.font = [UIFont systemFontOfSize:16];
  119. UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn];
  120. self.navigationItem.rightBarButtonItem = rightItem;
  121. }
  122. -(void)rightBtnAction:(UIButton *)btn
  123. {
  124. YXDeviceViewController *deviceVc = [[YXDeviceViewController alloc]init];
  125. [self pushViewController:deviceVc animated:YES];
  126. }
  127. //-(void)createBottomView
  128. //{
  129. // UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - kTabbarHeight - kNavHeight, self.view.frame.size.width, kTabbarHeight)];
  130. // bottomView.backgroundColor = [UIColor blueColor];
  131. // [self.view addSubview:bottomView];
  132. // self.bottomView = bottomView;
  133. //
  134. // NSArray *bottomArray = @[
  135. // @{
  136. // @"title":@"移到顶部",
  137. // @"pic":@"home_bottom_up",
  138. //
  139. // },
  140. // @{
  141. // @"title":@"修改房间",
  142. // @"pic":@"home_bottom_room"
  143. // },
  144. // @{
  145. // @"title":@"隐藏设备",
  146. // @"pic":@"home_bottom_hidden"
  147. // },
  148. // @{
  149. // @"title":@"删除设备",
  150. // @"pic":@"home_bottom_delete"
  151. // }
  152. // ];
  153. //
  154. // CGFloat width = self.view.frame.size.width/bottomArray.count;
  155. // for (int i = 0; i < bottomArray.count; i++) {
  156. // NSDictionary *dic = bottomArray[i];
  157. // YXBottomItemView *view = [[YXBottomItemView alloc]initWithFrame:CGRectMake(width * i, 0, width, self.bottomView.frame.size.height)];
  158. // view.picImgView.image = [UIImage imageNamed:dic[@"pic"]];
  159. // view.textLabel.text = dic[@"title"];
  160. // [self.bottomView addSubview:view];
  161. // view.backgroundColor = [UIColor whiteColor];
  162. // view.tag = i;
  163. // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bottomItemTapAction:)];
  164. // [view addGestureRecognizer:tap];
  165. //
  166. // }
  167. //}
  168. //
  169. //-(void)bottomItemTapAction:(UITapGestureRecognizer *)tap
  170. //{
  171. // UIView *view = tap.view;
  172. // NSInteger tag = view.tag;
  173. // if (tag == 0) {
  174. // NSMutableArray *newArray = [[NSMutableArray alloc]initWithArray:self.dataArray];
  175. //
  176. // NSMutableArray *newIdArray = [[NSMutableArray alloc]initWithCapacity:5];
  177. // for (int i = 0; i < newArray.count; i++) {
  178. // YXDeviceManagerModel *model = newArray[i];
  179. // if (model.isSelected == YES) {
  180. // [newArray exchangeObjectAtIndex:i withObjectAtIndex:0];
  181. // }
  182. // }
  183. //
  184. // [self p_updateDevicesSequenceWithArray:newArray];
  185. //
  186. // } else if (tag == 1){
  187. // YXSelectRoomView *roomView = [[YXSelectRoomView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
  188. // roomView.roomId = self.selectedModel.room_id;
  189. //
  190. // UIWindow *window = [UIApplication sharedApplication].keyWindow;
  191. // [window addSubview:roomView];
  192. //
  193. // roomView.cancleBtnClick = ^(YXSelectRoomView * view) {
  194. // [view removeFromSuperview];
  195. // };
  196. //
  197. // roomView.sureBtnClick = ^(YXSelectRoomView * _Nonnull view, NSString * _Nonnull roomId) {
  198. // [self p_updateDeviceInfoWithRoomId:roomId];
  199. // };
  200. //
  201. // } else if (tag == 2){
  202. //
  203. // } else if (tag == 3){
  204. // [self p_deleteDevices];
  205. // }
  206. //}
  207. //
  208. //-(void)sureBtnAction:(UIButton *)btn
  209. //{
  210. //
  211. //}
  212. -(void)createTableView
  213. {
  214. UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - kNavHeight - kTabbarHeight) style:UITableViewStyleGrouped];
  215. tableView.delegate = self;
  216. tableView.dataSource = self;
  217. tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
  218. [tableView registerNib:[UINib nibWithNibName:@"YXDeviceTableViewCell" bundle:nil] forCellReuseIdentifier:@"deviceTableViewCellID"];
  219. tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"];
  220. [self.view addSubview:tableView];
  221. self.tableView = tableView;
  222. }
  223. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
  224. {
  225. return 1;
  226. }
  227. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
  228. {
  229. return self.dataArray.count;
  230. }
  231. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
  232. {
  233. YXDeviceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deviceTableViewCellID" forIndexPath:indexPath];
  234. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  235. YXDeviceManagerModel *model = self.dataArray[indexPath.row];
  236. [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]]];
  237. cell.titleLabel.text = model.name;
  238. if (model.show_online.integerValue == 1) {
  239. if (model.is_online.integerValue == 1) {
  240. cell.detailLabel.text = @"设备在线";
  241. } else {
  242. cell.detailLabel.text = @"设备离线";
  243. }
  244. }else{
  245. cell.detailLabel.text = [NSString stringWithFormat:@"温度%@°C | 湿度%@%%", model.temperature, model.humidity];
  246. }
  247. cell.selectedView.hidden = YES;
  248. // if (model.isSelected) {
  249. // cell.selectedView.image = [UIImage imageNamed:@"home_device_selected"];
  250. // }else{
  251. // cell.selectedView.image = [UIImage imageNamed:@"home_device_unselected"];
  252. // }
  253. return cell;
  254. }
  255. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
  256. {
  257. YXDeviceManagerModel *model = self.dataArray[indexPath.row];
  258. RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init];
  259. webVc.requestURL = model.detail_url;
  260. webVc.hidesBottomBarWhenPushed = YES;
  261. [self.navigationController pushViewController:webVc animated:YES];
  262. // for (YXDeviceManagerModel *model in self.dataArray) {
  263. // model.isSelected = NO;
  264. // }
  265. //
  266. // YXDeviceManagerModel *model = self.dataArray[indexPath.row];
  267. // model.isSelected = !model.isSelected;
  268. //
  269. // [tableView reloadData];
  270. }
  271. -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
  272. {
  273. UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)];
  274. return headerView;
  275. }
  276. -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
  277. {
  278. UIView *footerView = [[UIView alloc]init];
  279. return footerView;
  280. }
  281. -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
  282. {
  283. return 15;
  284. }
  285. -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
  286. {
  287. return 0.01;
  288. }
  289. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
  290. {
  291. return 80;
  292. }
  293. #pragma mark - edit
  294. -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
  295. {
  296. return YES;
  297. }
  298. -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
  299. {
  300. return UITableViewCellEditingStyleDelete;
  301. }
  302. -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
  303. {
  304. if (editingStyle == UITableViewCellEditingStyleDelete) {
  305. [self p_deleteDeviceWithIndex:indexPath.row];
  306. }
  307. }
  308. - (RDSAddRoomView *)addView{
  309. if(_addView == nil){
  310. _addView = [RDSAddRoomView rds_loadViewFromNib];
  311. _addView.hidden = YES;
  312. [self.tableView addSubview:_addView];
  313. [_addView mas_makeConstraints:^(MASConstraintMaker *make) {
  314. make.left.equalTo(self.view);
  315. make.right.equalTo(self.view);
  316. make.height.equalTo(@380);
  317. make.top.equalTo(@150);
  318. }];
  319. RDS_WEAKSELF(weakSelf)
  320. _addView.onAddClick = ^{
  321. YXDeviceViewController *deviceVc = [[YXDeviceViewController alloc]init];
  322. [weakSelf pushViewController:deviceVc animated:YES];
  323. };
  324. }
  325. return _addView;
  326. }
  327. /*
  328. #pragma mark - Navigation
  329. // In a storyboard-based application, you will often want to do a little preparation before navigation
  330. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  331. // Get the new view controller using [segue destinationViewController].
  332. // Pass the selected object to the new view controller.
  333. }
  334. */
  335. @end