IPAddViewController.mm 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. //
  2. // IPAddViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by wujiangbo on 2018/11/14.
  6. // Copyright © 2018年 wujiangbo. All rights reserved.
  7. //
  8. #import "IPAddViewController.h"
  9. #import "DeviceManager.h"
  10. #import <Masonry/Masonry.h>
  11. #import "AddDeviceInputCell.h"
  12. #import "MyStringManager.h"
  13. #import "Header.h"
  14. @interface IPAddViewController ()<UITableViewDelegate,UITableViewDataSource,DeviceManagerDelegate>
  15. {
  16. NSArray *titleArray; //列表数组
  17. DeviceManager *deviceManager; //设备管理器
  18. UITextField *devNameTF; //设备名
  19. UITextField *loginNameTF; //登录名
  20. UITextField *devPswTF; //设备密码
  21. UITextField *devIPTF; //设备ip或者域名
  22. UITextField *devPortTF; //设备端口
  23. }
  24. @property (nonatomic,strong)UITableView *listTableView; //输入列表
  25. @property (nonatomic,strong)UIButton *addBtn; //添加按钮
  26. @end
  27. @implementation IPAddViewController
  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. - (void)setNaviStyle {
  44. self.navigationItem.title = TS("add_by_ip/domain");
  45. UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"new_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
  46. self.navigationItem.leftBarButtonItem = leftBtn;
  47. }
  48. #pragma mark - 控件布局
  49. -(void)configSubView
  50. {
  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(@250);
  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 通过ip/域名d添加
  76. -(void)addBtnClicked{
  77. if (devNameTF.text.length == 0 || devIPTF.text.length == 0) {
  78. [SVProgressHUD showErrorWithStatus:TS("information_incomplete")];
  79. return;
  80. }
  81. enum STR_TYPE curType;
  82. curType = [MyStringManager getStrType:devIPTF.text];
  83. if (curType != STR_TYPE_IP && curType != STR_TYPE_DN) {
  84. [SVProgressHUD showErrorWithStatus:TS("ip/domain_is_error")];
  85. return;
  86. }
  87. [SVProgressHUD show];
  88. //通过ip/m域名添加
  89. [deviceManager addDeviceByDeviceIP:devIPTF.text deviceName:devNameTF.text password:devPswTF.text port:devPortTF.text];
  90. //修改设备名称和设备密码
  91. NSString *devmac = [NSString stringWithFormat:@"%@:%@",devIPTF.text, (devPortTF.text.length == 0) ? @"34567" :devPortTF.text];
  92. [deviceManager changeDevice:devmac devName:devNameTF.text username:loginNameTF.text password:devPswTF.text];
  93. }
  94. #pragma mark - tableViewDataSource/Delegate
  95. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  96. return titleArray.count;
  97. }
  98. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  99. return 50;
  100. }
  101. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  102. AddDeviceInputCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AddDeviceInputCell"];
  103. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  104. NSString *titleStr = titleArray[indexPath.row];
  105. cell.customTitle.text = titleStr;
  106. if ([titleStr isEqualToString:TS("Device_Name")]) {
  107. cell.inputTextField.attributedPlaceholder =
  108. [[NSAttributedString alloc]initWithString:TS("Enter_Device_Name") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  109. devNameTF = cell.inputTextField;
  110. }
  111. else if ([titleStr isEqualToString:TS("IP2")]){
  112. cell.inputTextField.attributedPlaceholder =
  113. [[NSAttributedString alloc]initWithString:TS("Enter_IP_Domain") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  114. devIPTF = cell.inputTextField;
  115. }
  116. else if ([titleStr isEqualToString:TS("UserName")]){
  117. cell.inputTextField.attributedPlaceholder =
  118. [[NSAttributedString alloc]initWithString:TS("Enter_LoginName") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  119. cell.inputTextField.text = @"admin";
  120. cell.inputTextField.enabled = NO;
  121. loginNameTF = cell.inputTextField;
  122. }
  123. else if ([titleStr isEqualToString:TS("Password2")]){
  124. cell.inputTextField.attributedPlaceholder =
  125. [[NSAttributedString alloc]initWithString:TS("Enter_LoginPassword") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  126. devPswTF = cell.inputTextField;
  127. }
  128. else if ([titleStr isEqualToString:TS("Port2")]){
  129. cell.inputTextField.attributedPlaceholder =
  130. [[NSAttributedString alloc]initWithString:TS("Enter_Device_Port") attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:12]}];
  131. devPortTF = cell.inputTextField;
  132. }
  133. return cell;
  134. }
  135. #pragma mark - funsdk 回调处理
  136. - (void)addDeviceResult:(int)result{
  137. }
  138. #pragma mark - 界面和数据初始化
  139. - (void)initDataSource {
  140. titleArray = @[TS("Device_Name"),TS("IP2"),TS("UserName"),TS("Password2"),TS("Port2")];
  141. }
  142. -(UITableView *)listTableView{
  143. if (!_listTableView) {
  144. _listTableView = [[UITableView alloc] init];
  145. _listTableView.delegate = self;
  146. _listTableView.dataSource = self;
  147. _listTableView.scrollEnabled = NO;
  148. [_listTableView registerClass:[AddDeviceInputCell class] forCellReuseIdentifier:@"AddDeviceInputCell"];
  149. }
  150. return _listTableView;
  151. }
  152. -(UIButton *)addBtn{
  153. if (!_addBtn) {
  154. _addBtn = [[UIButton alloc] init];
  155. [_addBtn addTarget:self action:@selector(addBtnClicked) forControlEvents:UIControlEventTouchUpInside];
  156. [_addBtn setTitle:TS("OK") forState:UIControlStateNormal];
  157. [_addBtn setBackgroundColor:GlobalMainColor];
  158. }
  159. return _addBtn;
  160. }
  161. @end