SerialNumAddViewController.mm 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // SerialNumAddViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by wujiangbo on 2018/11/12.
  6. // Copyright © 2018年 wujiangbo. All rights reserved.
  7. //
  8. #import "SerialNumAddViewController.h"
  9. #import "AddDeviceInputCell.h"
  10. #import <Masonry/Masonry.h>
  11. #import "OriginalScaner.h"
  12. #import "DeviceManager.h"
  13. #import "MyStringManager.h"
  14. #import "Header.h"
  15. @interface SerialNumAddViewController ()<UITableViewDelegate,UITableViewDataSource,DeviceManagerDelegate>
  16. {
  17. NSArray *titleArray; //列表数组
  18. DeviceManager *deviceManager; //设备管理器
  19. UITextField *devNameTF; //设备名
  20. UITextField *loginNameTF; //登录名
  21. UITextField *devPswTF; //设备密码
  22. UITextField *devSerialTF; //设备序列号
  23. }
  24. @property (nonatomic,strong)UITableView *listTableView; //输入列表
  25. @property (nonatomic,strong)UIButton *addBtn; //添加按钮
  26. @end
  27. @implementation SerialNumAddViewController
  28. - (void)viewDidLoad {
  29. [super viewDidLoad];
  30. self.view.backgroundColor = [UIColor whiteColor];
  31. //设备管理器
  32. deviceManager = [[DeviceManager alloc] init];
  33. deviceManager.delegate = self;
  34. //设置导航栏
  35. [self setNaviStyle];
  36. //初始化数据
  37. [self initDataSource];
  38. [self.view addSubview:self.listTableView];
  39. [self.view addSubview:self.addBtn];
  40. //控件布局
  41. [self configSubView];
  42. }
  43. // 序列号添加
  44. - (void)setNaviStyle {
  45. self.navigationItem.title = TS("add_by_serialNumber");
  46. UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"new_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
  47. self.navigationItem.leftBarButtonItem = leftBtn;
  48. }
  49. #pragma mark - 控件布局
  50. -(void)configSubView{
  51. [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  52. make.top.equalTo(@110);
  53. make.width.equalTo(self.view.mas_width).multipliedBy(0.9);
  54. make.height.equalTo(@200);
  55. make.centerX.equalTo(self.view.mas_centerX);
  56. }];
  57. [self.addBtn mas_makeConstraints:^(MASConstraintMaker *make) {
  58. make.top.equalTo(self.listTableView.mas_bottom).offset(50);
  59. make.width.equalTo(self.view.mas_width).multipliedBy(0.9);
  60. make.height.equalTo(@45);
  61. make.centerX.equalTo(self.view);
  62. }];
  63. }
  64. #pragma mark - button event
  65. -(void)popViewController{
  66. if([SVProgressHUD isVisible]){
  67. [SVProgressHUD dismiss];
  68. }
  69. [self.navigationController popViewControllerAnimated:YES];
  70. }
  71. #pragma mark 点击空白处关闭键盘
  72. -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
  73. [self.view endEditing:YES];
  74. }
  75. #pragma mark 通过序列号添加设备
  76. -(void)addBtnClicked{
  77. // 先判断信息是否填写完整
  78. if (devNameTF.text.length == 0 || devSerialTF.text.length == 0) {
  79. // 你填写的信息不完整
  80. [SVProgressHUD showErrorWithStatus:TS("Info_Error1")];
  81. return;
  82. }
  83. enum STR_TYPE curType;
  84. curType = [MyStringManager getStrType:devSerialTF.text];
  85. if (curType != STR_TYPE_SN) {
  86. // 参数异常
  87. [SVProgressHUD showErrorWithStatus:TS("EE_CLOUD_PARAM_INVALID")];
  88. return;
  89. }
  90. [SVProgressHUD show];
  91. NSLog(@"%@----%@",devSerialTF.text,devNameTF.text);
  92. NSLog(@"%@----%@",loginNameTF.text,devPswTF.text);
  93. //通过序列号添加
  94. [deviceManager addDeviceByDeviseSerialnumber:devSerialTF.text deviceName:devNameTF.text devType:0];
  95. //修改设备名称和设备密码
  96. [deviceManager changeDevicePsw:devSerialTF.text loginName:loginNameTF.text password:devPswTF.text];
  97. }
  98. #pragma mark 点击二维码扫描
  99. -(void)codeBtnClick{
  100. OriginalScaner *scaner = [[OriginalScaner alloc] init];
  101. scaner.myScanerFinishedBlock = ^(NSString *code){
  102. NSArray* arrayInfo = [deviceManager decodeDevInfo:code];
  103. //根据扫描二维码结果显示设备名和设备序列号
  104. devNameTF.text = arrayInfo[0];
  105. devSerialTF.text = arrayInfo[0];
  106. };
  107. [self.navigationController pushViewController:scaner animated:NO];
  108. }
  109. #pragma mark - tableViewDataSource/Delegate
  110. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  111. return titleArray.count;
  112. }
  113. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  114. return 50;
  115. }
  116. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  117. AddDeviceInputCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddDeviceInputCell"];
  118. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  119. NSString *titleStr = titleArray[indexPath.row];
  120. cell.customTitle.text = titleStr;
  121. if ([titleStr isEqualToString:TS("Device_Name")]) {
  122. cell.inputTextField.attributedPlaceholder =
  123. [[NSAttributedString alloc]initWithString:TS("Enter_Device_Name") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  124. devNameTF = cell.inputTextField;
  125. }
  126. else if ([titleStr isEqualToString:TS("serial_number")]){
  127. cell.inputTextField.attributedPlaceholder =
  128. [[NSAttributedString alloc]initWithString:TS("Enter_serial_number") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  129. devSerialTF = cell.inputTextField;
  130. //添加扫描二维码按钮
  131. if (!cell.codeBtn) {
  132. cell.codeBtn = [[UIButton alloc] initWithFrame:CGRectMake(ScreenWidth* 0.9 - 40, 10, 30, 30)];
  133. [cell.codeBtn setImage:[UIImage imageNamed:@"QRCode.png"] forState:UIControlStateNormal];
  134. [cell.codeBtn addTarget:self action:@selector(codeBtnClick) forControlEvents:UIControlEventTouchUpInside];
  135. [cell addSubview:cell.codeBtn];
  136. }
  137. }
  138. else if ([titleStr isEqualToString:TS("UserName")]){
  139. cell.inputTextField.attributedPlaceholder =
  140. [[NSAttributedString alloc]initWithString:TS("Enter_LoginName") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  141. cell.inputTextField.text = @"admin";
  142. cell.inputTextField.enabled = NO;
  143. loginNameTF = cell.inputTextField;
  144. }
  145. else if ([titleStr isEqualToString:TS("Password2")]){
  146. cell.inputTextField.attributedPlaceholder =
  147. [[NSAttributedString alloc]initWithString:TS("Enter_LoginPassword") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  148. devPswTF = cell.inputTextField;
  149. }
  150. return cell;
  151. }
  152. #pragma mark - funsdk 回调处理
  153. - (void)addDeviceResult:(int)result{
  154. }
  155. #pragma mark - 界面和数据初始化
  156. - (void)initDataSource {
  157. titleArray = @[TS("Device_Name"),TS("serial_number"),TS("UserName"),TS("Password2")];
  158. }
  159. -(UITableView *)listTableView{
  160. if (!_listTableView) {
  161. _listTableView = [[UITableView alloc] init];
  162. _listTableView.delegate = self;
  163. _listTableView.dataSource = self;
  164. _listTableView.scrollEnabled = NO;
  165. [_listTableView registerClass:[AddDeviceInputCell class] forCellReuseIdentifier:@"AddDeviceInputCell"];
  166. }
  167. return _listTableView;
  168. }
  169. -(UIButton *)addBtn{
  170. if (!_addBtn) {
  171. _addBtn = [[UIButton alloc] init];
  172. [_addBtn addTarget:self action:@selector(addBtnClicked) forControlEvents:UIControlEventTouchUpInside];
  173. [_addBtn setTitle:TS("OK") forState:UIControlStateNormal];
  174. [_addBtn setBackgroundColor:GlobalMainColor];
  175. }
  176. return _addBtn;
  177. }
  178. @end