// // YXDeviceManagerViewController.m // Temperature // // Created by z on 2025/1/18. // #import "YXDeviceManagerViewController.h" #import "YXDeviceTableViewCell.h" #import "YXBottomItemView.h" #import "YXDeviceManagerModel.h" #import #import "YXSelectRoomView.h" #import "RDSControlWebVC.h" #import "YXDeviceViewController.h" #import "RDSAddRoomView.h" #import "RDSRootControl.h" @interface YXDeviceManagerViewController () @property (weak, nonatomic) UITableView *tableView; @property (weak, nonatomic) UIView *bottomView; @property (nonatomic, strong) RDSAddRoomView *addView; @property (copy, nonatomic) NSArray *dataArray; @property (strong, nonatomic) YXDeviceManagerModel *selectedModel; @end @implementation YXDeviceManagerViewController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; self.navigationItem.title = @"设备管理"; [self addRightItem]; [self createTableView]; // [self createBottomView]; [self p_getDevices]; } -(YXDeviceManagerModel *)selectedModel { YXDeviceManagerModel *selectedModel; for (YXDeviceManagerModel *model in self.dataArray) { if (model.isSelected == YES) { selectedModel = model; } } return selectedModel; } -(void)p_getDevices { RDS_WEAKSELF(weakSelf) [YXDeviceManagerModel getRoomDeviceWithRoomId:@"" finished:^(NSArray * _Nullable devices, NSError * _Nullable error) { weakSelf.dataArray = devices; self.addView.hidden = self.dataArray.count == 0 ? NO : YES; [weakSelf.tableView reloadData]; }]; } -(void)p_updateDeviceInfoWithRoomId:(NSString *)roomId { RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_updateDevice:self.selectedModel.record_id name:self.selectedModel.name homeId:self.selectedModel.home_id roomId:roomId success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 9999) { [RDSRootControl shareControl].isLoginSuccess = NO; TheDataManager.token = @""; } if ([responseObject[@"code"] intValue] == 0) { [weakSelf.tableView reloadData]; } else{ // NSError *error = [NSError errorWithDomain:responseObject[@"message"] code:[responseObject[@"code"] intValue] userInfo:nil]; [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)p_updateDevicesSequenceWithArray:(NSArray *)devices { NSMutableArray *newDevicesArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < devices.count; i++) { YXDeviceManagerModel *model = devices[i]; NSString *deviceId = model.device_code; [newDevicesArray addObject:deviceId]; } RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_putDevicesSequence:newDevicesArray success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { weakSelf.dataArray = devices; [weakSelf.tableView reloadData]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)p_deleteDevices { NSMutableArray *selectedArray = [[NSMutableArray alloc]initWithCapacity:5]; for (int i = 0; i < self.dataArray.count; i++) { YXDeviceManagerModel *model = selectedArray[i]; NSString *deviceId = model.device_code; [selectedArray addObject:deviceId]; } } -(void)p_deleteDeviceWithIndex:(NSInteger)index { YXDeviceManagerModel *model = self.dataArray[index]; RDS_WEAKSELF(weakSelf) [RDSDemoApiHelper rds_deleteDeviceWithId:model.record_id success:^(id responseObject) { if ([responseObject[@"code"] intValue] == 0) { [RDSHudShower showBottomToast:@"删除成功"]; [weakSelf p_getDevices]; } else{ [RDSHudShower showBottomToast:responseObject[@"message"]]; } } failure:^(NSError *error) { }]; } -(void)addRightItem { UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [rightBtn setTitle:@"添加设备" forState:UIControlStateNormal]; [rightBtn addTarget:self action:@selector(rightBtnAction:) forControlEvents:UIControlEventTouchUpInside]; [rightBtn setTitleColor:[UIColor colorWithHexString:@"#267AFF"] forState:UIControlStateNormal]; rightBtn.titleLabel.font = [UIFont systemFontOfSize:16]; UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:rightBtn]; self.navigationItem.rightBarButtonItem = rightItem; } -(void)rightBtnAction:(UIButton *)btn { YXDeviceViewController *deviceVc = [[YXDeviceViewController alloc]init]; [self pushViewController:deviceVc animated:YES]; } //-(void)createBottomView //{ // UIView *bottomView = [[UIView alloc]initWithFrame:CGRectMake(0, SCREEN_HEIGHT - kTabbarHeight - kNavHeight, self.view.frame.size.width, kTabbarHeight)]; // bottomView.backgroundColor = [UIColor blueColor]; // [self.view addSubview:bottomView]; // self.bottomView = bottomView; // // NSArray *bottomArray = @[ // @{ // @"title":@"移到顶部", // @"pic":@"home_bottom_up", // // }, // @{ // @"title":@"修改房间", // @"pic":@"home_bottom_room" // }, // @{ // @"title":@"隐藏设备", // @"pic":@"home_bottom_hidden" // }, // @{ // @"title":@"删除设备", // @"pic":@"home_bottom_delete" // } // ]; // // CGFloat width = self.view.frame.size.width/bottomArray.count; // for (int i = 0; i < bottomArray.count; i++) { // NSDictionary *dic = bottomArray[i]; // YXBottomItemView *view = [[YXBottomItemView alloc]initWithFrame:CGRectMake(width * i, 0, width, self.bottomView.frame.size.height)]; // view.picImgView.image = [UIImage imageNamed:dic[@"pic"]]; // view.textLabel.text = dic[@"title"]; // [self.bottomView addSubview:view]; // view.backgroundColor = [UIColor whiteColor]; // view.tag = i; // UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(bottomItemTapAction:)]; // [view addGestureRecognizer:tap]; // // } //} // //-(void)bottomItemTapAction:(UITapGestureRecognizer *)tap //{ // UIView *view = tap.view; // NSInteger tag = view.tag; // if (tag == 0) { // NSMutableArray *newArray = [[NSMutableArray alloc]initWithArray:self.dataArray]; // // NSMutableArray *newIdArray = [[NSMutableArray alloc]initWithCapacity:5]; // for (int i = 0; i < newArray.count; i++) { // YXDeviceManagerModel *model = newArray[i]; // if (model.isSelected == YES) { // [newArray exchangeObjectAtIndex:i withObjectAtIndex:0]; // } // } // // [self p_updateDevicesSequenceWithArray:newArray]; // // } else if (tag == 1){ // YXSelectRoomView *roomView = [[YXSelectRoomView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)]; // roomView.roomId = self.selectedModel.room_id; // // UIWindow *window = [UIApplication sharedApplication].keyWindow; // [window addSubview:roomView]; // // roomView.cancleBtnClick = ^(YXSelectRoomView * view) { // [view removeFromSuperview]; // }; // // roomView.sureBtnClick = ^(YXSelectRoomView * _Nonnull view, NSString * _Nonnull roomId) { // [self p_updateDeviceInfoWithRoomId:roomId]; // }; // // } else if (tag == 2){ // // } else if (tag == 3){ // [self p_deleteDevices]; // } //} // //-(void)sureBtnAction:(UIButton *)btn //{ // //} -(void)createTableView { UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - kNavHeight - kTabbarHeight) style:UITableViewStyleGrouped]; tableView.delegate = self; tableView.dataSource = self; tableView.separatorStyle = UITableViewCellSeparatorStyleNone; [tableView registerNib:[UINib nibWithNibName:@"YXDeviceTableViewCell" bundle:nil] forCellReuseIdentifier:@"deviceTableViewCellID"]; tableView.backgroundColor = [UIColor colorWithHexString:@"#F9F9F9"]; [self.view addSubview:tableView]; self.tableView = tableView; } - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return 1; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.dataArray.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { YXDeviceTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"deviceTableViewCellID" forIndexPath:indexPath]; cell.selectionStyle = UITableViewCellSelectionStyleNone; YXDeviceManagerModel *model = self.dataArray[indexPath.row]; [cell.picImgView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", TheApiHelper.baseUrlHeadImg, model.icon]]]; cell.titleLabel.text = model.name; if (model.show_online.integerValue == 1) { if (model.is_online.integerValue == 1) { cell.detailLabel.text = @"设备在线"; } else { cell.detailLabel.text = @"设备离线"; } }else{ cell.detailLabel.text = [NSString stringWithFormat:@"温度%@°C | 湿度%@%%", model.temperature, model.humidity]; } cell.selectedView.hidden = YES; // if (model.isSelected) { // cell.selectedView.image = [UIImage imageNamed:@"home_device_selected"]; // }else{ // cell.selectedView.image = [UIImage imageNamed:@"home_device_unselected"]; // } return cell; } - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { YXDeviceManagerModel *model = self.dataArray[indexPath.row]; RDSControlWebVC *webVc = [[RDSControlWebVC alloc]init]; webVc.requestURL = model.detail_url; webVc.hidesBottomBarWhenPushed = YES; [self.navigationController pushViewController:webVc animated:YES]; // for (YXDeviceManagerModel *model in self.dataArray) { // model.isSelected = NO; // } // // YXDeviceManagerModel *model = self.dataArray[indexPath.row]; // model.isSelected = !model.isSelected; // // [tableView reloadData]; } -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section { UIView *headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 15)]; return headerView; } -(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section { UIView *footerView = [[UIView alloc]init]; return footerView; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section { return 15; } -(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section { return 0.01; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 80; } #pragma mark - edit -(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return YES; } -(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath { return UITableViewCellEditingStyleDelete; } -(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath { if (editingStyle == UITableViewCellEditingStyleDelete) { [self p_deleteDeviceWithIndex:indexPath.row]; } } - (RDSAddRoomView *)addView{ if(_addView == nil){ _addView = [RDSAddRoomView rds_loadViewFromNib]; _addView.hidden = YES; [self.tableView addSubview:_addView]; [_addView mas_makeConstraints:^(MASConstraintMaker *make) { make.left.equalTo(self.view); make.right.equalTo(self.view); make.height.equalTo(@380); make.top.equalTo(@150); }]; RDS_WEAKSELF(weakSelf) _addView.onAddClick = ^{ YXDeviceViewController *deviceVc = [[YXDeviceViewController alloc]init]; [weakSelf pushViewController:deviceVc animated:YES]; }; } return _addView; } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end