YXSetCodeViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // YXSetCodeViewController.m
  3. // Temperature
  4. //
  5. // Created by TC on 2025/3/29.
  6. //
  7. #import "YXSetCodeViewController.h"
  8. #import "YXSetWifiViewController.h"
  9. #import "OPQRCodeViewController.h"
  10. #import <YBPopupMenu/YBPopupMenu.h>
  11. #import "YXDeviceInfoModel.h"
  12. #import "RDSBindDeviceVC.h"
  13. @interface YXSetCodeViewController ()<OPQRCodeViewControllerDelegate,YBPopupMenuDelegate>
  14. @property (weak, nonatomic) IBOutlet UIView *nameBgView;
  15. @property (weak, nonatomic) IBOutlet UITextField *nameTextField;
  16. /**选择主控**/
  17. @property (weak, nonatomic) IBOutlet UIView *mainBgView;
  18. @property (weak, nonatomic) IBOutlet UILabel *mainLabel;
  19. @property (weak, nonatomic) IBOutlet UIView *codeBgView;
  20. @property (weak, nonatomic) IBOutlet UITextField *codeTextFeild;
  21. @property (weak, nonatomic) IBOutlet UIButton *codeBtn;
  22. @property (weak, nonatomic) IBOutlet UIView *locationBgView;
  23. @property (weak, nonatomic) IBOutlet UITextField *locationTextFeild;
  24. @property (weak, nonatomic) IBOutlet UIButton *locationBtn;
  25. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *mainTopConstraint;
  26. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *codeTopConstraint;
  27. @property (weak, nonatomic) IBOutlet NSLayoutConstraint *locationTopConstraint;
  28. @property (weak, nonatomic) IBOutlet UIButton *confirmBtn;
  29. @property (nonatomic, strong) NSMutableArray *deviceArray;
  30. @property (nonatomic, strong) NSMutableArray *mainArray;
  31. @property (nonatomic, strong) NSMutableArray *mainTitleArray;
  32. @property (nonatomic, strong) RDSRoomModel *model;
  33. @property (nonatomic, strong)YXDeviceInfoModel *mainModel;
  34. @end
  35. @implementation YXSetCodeViewController
  36. - (void)viewDidLoad {
  37. [super viewDidLoad];
  38. self.view.backgroundColor = [UIColor whiteColor];
  39. [self createUI];
  40. [self getDeviceLocation];
  41. [self setDataSource];
  42. }
  43. -(void)createUI
  44. {
  45. // 15 70 125
  46. if ([self.itemModel.is_master integerValue] == 1) {
  47. self.mainBgView.hidden = YES;
  48. self.mainTopConstraint.constant = 0;
  49. if ([self.itemModel.need_device_code integerValue] == 1) {
  50. self.codeBgView.hidden = NO;
  51. self.codeTopConstraint.constant = 15;
  52. self.locationTopConstraint.constant = 70;
  53. }else{
  54. self.codeBgView.hidden = YES;
  55. self.locationTopConstraint.constant = 15;
  56. }
  57. }else{
  58. self.mainBgView.hidden = NO;
  59. self.mainTopConstraint.constant = 15;
  60. if ([self.itemModel.need_device_code integerValue] == 1) {
  61. self.codeBgView.hidden = NO;
  62. self.codeTopConstraint.constant = 70;
  63. self.locationTopConstraint.constant = 125;
  64. }else{
  65. self.codeBgView.hidden = YES;
  66. self.codeTopConstraint.constant = 0;
  67. self.locationTopConstraint.constant = 70;
  68. }
  69. }
  70. self.nameBgView.layer.cornerRadius = 20;
  71. self.nameBgView.layer.borderWidth = 1.0;
  72. self.nameBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor;
  73. self.codeBgView.layer.cornerRadius = 20;
  74. self.codeBgView.layer.borderWidth = 1.0;
  75. self.codeBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor;
  76. self.locationBgView.layer.cornerRadius = 20;
  77. self.locationBgView.layer.borderWidth = 1.0;
  78. self.locationBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor;
  79. self.mainBgView.layer.cornerRadius = 20;
  80. self.mainBgView.layer.borderWidth = 1.0;
  81. self.mainBgView.layer.borderColor = [UIColor colorWithHexString:@"#C7C7C7"].CGColor;
  82. [self.codeBtn addTarget:self action:@selector(erweimasaomiao) forControlEvents:UIControlEventTouchUpInside];
  83. if ([self.itemModel.bluetooth integerValue] == 1) {
  84. [self.confirmBtn setTitle:@"下一步" forState:UIControlStateNormal];
  85. }else{
  86. [self.confirmBtn setTitle:@"确认添加" forState:UIControlStateNormal];
  87. }
  88. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectMainControl)];
  89. [self.mainBgView addGestureRecognizer:tap];
  90. UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectDeviceLocation)];
  91. [self.locationTextFeild addGestureRecognizer:tap2];
  92. }
  93. - (void)setDataSource {
  94. self.nameTextField.text = self.itemModel.name;
  95. }
  96. /**选择主控**/
  97. - (void)selectMainControl {
  98. [RDSDemoApiHelper rds_getDeviceListWithHomeId:TheDataManager.current_home_id roomId:@"" isMaster:YES Success:^(id responseObject) {
  99. if ([responseObject[@"code"] integerValue] == 0) {
  100. NSDictionary *dataDic = responseObject[@"data"];
  101. NSArray *listArray = dataDic[@"list"];
  102. self.mainArray = [YXDeviceInfoModel mj_objectArrayWithKeyValuesArray:listArray];
  103. [self.mainTitleArray removeAllObjects];
  104. for (YXDeviceInfoModel *model in self.mainArray) {
  105. [self.mainTitleArray addObject:model.name];
  106. }
  107. [YBPopupMenu showRelyOnView:self.mainBgView titles:self.mainTitleArray icons:nil menuWidth: SCREEN_WIDTH - 60 otherSettings:^(YBPopupMenu *popupMenu) {
  108. popupMenu.delegate = self;
  109. popupMenu.animationManager.duration = 0.25;
  110. popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"];
  111. popupMenu.tag = 2;
  112. }];
  113. }
  114. } failure:^(NSError *error) {
  115. }];
  116. }
  117. /**选择设备位置**/
  118. - (void)selectDeviceLocation {
  119. [self.view endEditing:YES];
  120. NSMutableArray *titles = [NSMutableArray array];
  121. for (RDSRoomModel *model in self.deviceArray) {
  122. [titles addObject:model.name];
  123. }
  124. [YBPopupMenu showRelyOnView:self.locationBgView titles:titles icons:nil menuWidth: SCREEN_WIDTH - 60 otherSettings:^(YBPopupMenu *popupMenu) {
  125. popupMenu.delegate = self;
  126. popupMenu.animationManager.duration = 0.25;
  127. popupMenu.textColor = [UIColor colorWithHexString:@"#1F1F1F"];
  128. popupMenu.tag = 1;
  129. }];
  130. }
  131. - (void)ybPopupMenu:(YBPopupMenu *)ybPopupMenu didSelectedAtIndex:(NSInteger)index {
  132. NSInteger tag = ybPopupMenu.tag;
  133. if (tag == 1) {
  134. RDSRoomModel *model = self.deviceArray[index];
  135. self.locationTextFeild.text = model.name;
  136. self.model = model;
  137. }
  138. if (tag == 2) {
  139. self.mainModel = self.mainArray[index];
  140. self.mainLabel.text = self.mainModel.name;
  141. self.mainLabel.textColor = [UIColor colorWithHexString:@"#1E1E1E"];
  142. }
  143. }
  144. - (IBAction)confirmBtnClick:(id)sender {
  145. NSString *type_id = @"";
  146. if (self.itemModel.steps.count > 0) {
  147. YXDeviceStepModel *model = self.itemModel.steps[0];
  148. type_id = model.device_type_id;
  149. }
  150. NSString *code = @"";
  151. NSString *subCode = @"";
  152. if ([self.itemModel.is_master integerValue] == 1) {
  153. code = self.codeTextFeild.text;
  154. if (code.length == 0 && [self.itemModel.need_device_code integerValue] == 1) {
  155. //主控
  156. [RDSHudShower showBottomToast:@"主控设备编号为空"];
  157. return;
  158. }
  159. }else{
  160. //分控
  161. code = self.mainModel.device_code;
  162. if (code.length == 0) {
  163. [RDSHudShower showBottomToast:@"主控设备编号为空"];
  164. return;
  165. }
  166. subCode = self.codeTextFeild.text;
  167. if (subCode.length == 0) {
  168. [RDSHudShower showBottomToast:@"分控设备编号为空"];
  169. return;
  170. }
  171. }
  172. if ([self.itemModel.bluetooth integerValue] == 1) {
  173. //主控、永续精灵
  174. YXSetWifiViewController *wifiVc = [[YXSetWifiViewController alloc]init];
  175. wifiVc.itemModel = self.itemModel;
  176. wifiVc.type_id = type_id;
  177. wifiVc.room_id = self.model.record_id;
  178. wifiVc.home_id = self.model.home_id;
  179. if ([self.itemModel.need_device_code integerValue] == 1) {
  180. //主控
  181. wifiVc.deviceCode = code;
  182. }else{
  183. //永续精灵
  184. wifiVc.deviceCode = @"";
  185. }
  186. wifiVc.deviceName = self.nameTextField.text;
  187. wifiVc.bluetooth = self.itemModel.bluetooth;
  188. wifiVc.need_device_code = self.itemModel.need_device_code;
  189. wifiVc.blue_code = self.itemModel.blue_code;
  190. [self.navigationController pushViewController:wifiVc animated:YES];
  191. }else{
  192. //分控
  193. RDSBindDeviceVC *vc = [[RDSBindDeviceVC alloc] init];
  194. vc.deviceCode = code;
  195. vc.is_master = [self.itemModel.is_master boolValue];
  196. vc.deviceName = self.nameTextField.text;
  197. vc.subCode = subCode;
  198. YXDeviceStepModel *model = _itemModel.steps.firstObject;
  199. vc.deviceTypeId = model.device_type_id;
  200. vc.home_id = self.model.home_id;
  201. vc.room_id = self.model.record_id;
  202. vc.type_id = type_id;
  203. vc.isYXJL = NO;
  204. vc.bluetooth = self.itemModel.bluetooth;
  205. vc.need_device_code = self.itemModel.need_device_code;
  206. [self pushViewController:vc animated:YES];
  207. }
  208. }
  209. - (void)erweimasaomiao {
  210. OPQRCodeViewController *vc = [[OPQRCodeViewController alloc] init];
  211. vc.delegate = self;
  212. vc.topOffset = -kNavHeight;
  213. vc.modalPresentationStyle = UIModalPresentationFullScreen;
  214. [self presentViewController:vc animated:YES completion:nil];
  215. }
  216. #pragma mark - OPQRCodeViewControllerDelegate
  217. // 扫码返回字符串
  218. - (void)rdy_didFinishPickingMediaWithInfo:(NSString *)stringValue{
  219. DDLog(@"二维码:%@",stringValue);
  220. if(!kNULLString(stringValue)){
  221. self.codeTextFeild.text = stringValue;
  222. }
  223. }
  224. - (void)getDeviceLocation {
  225. [RDSDemoApiHelper rds_getRoomListWithHomeId:TheDataManager.current_home_id success:^(NSDictionary * responseObject) {
  226. if ([responseObject[@"code"] integerValue] == 0) {
  227. self.deviceArray = [YXHomeRoomModel mj_objectArrayWithKeyValuesArray:responseObject[@"data"]];
  228. for (YXHomeRoomModel *model in self.deviceArray) {
  229. model.devices = [YXHomeDeviceModel mj_objectArrayWithKeyValuesArray:model.devices];
  230. }
  231. }
  232. } failure:^(NSError *error) {
  233. }];
  234. }
  235. - (NSMutableArray *)deviceArray {
  236. if (!_deviceArray) {
  237. _deviceArray = [NSMutableArray array];
  238. }
  239. return _deviceArray;
  240. }
  241. - (NSMutableArray *)mainTitleArray {
  242. if (!_mainTitleArray) {
  243. _mainTitleArray = [NSMutableArray array];
  244. }
  245. return _mainTitleArray;
  246. }
  247. /*
  248. #pragma mark - Navigation
  249. // In a storyboard-based application, you will often want to do a little preparation before navigation
  250. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  251. // Get the new view controller using [segue destinationViewController].
  252. // Pass the selected object to the new view controller.
  253. }
  254. */
  255. @end