LANSearchViewController.mm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. //
  2. // LANSearchViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by wujiangbo on 2018/11/15.
  6. // Copyright © 2018年 wujiangbo. All rights reserved.
  7. //
  8. #import "LANSearchViewController.h"
  9. #import "DeviceManager.h"
  10. #import <Masonry/Masonry.h>
  11. #import "SearchDeviceCell.h"
  12. #import "DeviceObject.h"
  13. #import "Header.h"
  14. @interface LANSearchViewController ()<UITableViewDelegate,UITableViewDataSource,DeviceManagerDelegate>
  15. {
  16. NSMutableArray *deviceArray; //设备数组
  17. DeviceManager *deviceManager; //设备管理器
  18. }
  19. @property (nonatomic,strong)UITableView *listTableView; //搜索到的设备列表
  20. @end
  21. @implementation LANSearchViewController
  22. - (void)viewDidLoad {
  23. [super viewDidLoad];
  24. self.view.backgroundColor = [UIColor whiteColor];
  25. //设备管理器
  26. deviceManager = [[DeviceManager alloc] init];
  27. deviceManager.delegate = self;
  28. //设置导航栏
  29. [self setNaviStyle];
  30. [self.view addSubview:self.listTableView];
  31. //控件布局
  32. [self configSubView];
  33. //获取局域网设备
  34. [SVProgressHUD show];
  35. [deviceManager SearchDevice];
  36. }
  37. - (void)setNaviStyle {
  38. self.navigationItem.title = TS("search_wifi_device");
  39. UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"new_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
  40. self.navigationItem.leftBarButtonItem = leftBtn;
  41. }
  42. #pragma mark - button event
  43. -(void)popViewController{
  44. if([SVProgressHUD isVisible]){
  45. [SVProgressHUD dismiss];
  46. }
  47. [self.navigationController popViewControllerAnimated:YES];
  48. }
  49. #pragma mark - 控件布局
  50. -(void)configSubView
  51. {
  52. [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
  53. make.top.equalTo(@64);
  54. make.width.equalTo(self.view.mas_width).multipliedBy(0.9);
  55. make.height.equalTo(self.view.mas_height).offset(-64);
  56. make.centerX.equalTo(self.view.mas_centerX);
  57. }];
  58. }
  59. #pragma mark - tableViewDataSource/Delegate
  60. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  61. return deviceArray.count;
  62. }
  63. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  64. return 80;
  65. }
  66. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  67. SearchDeviceCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SearchDeviceCell"];
  68. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  69. DeviceObject *object = [deviceArray objectAtIndex:indexPath.row];
  70. cell.nameLab.text = [NSString stringWithFormat:@"发现设备%d(%@)",(int)indexPath.row + 1,object.deviceName];
  71. cell.serialNumLab.text = object.deviceMac;
  72. cell.ipLab.text = object.deviceIp;
  73. return cell;
  74. }
  75. -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  76. DeviceObject *object = [deviceArray objectAtIndex:indexPath.row];
  77. [SVProgressHUD show];
  78. [deviceManager addDeviceBySerialNum:object.deviceMac deviceName:object.deviceName type:object.nType];
  79. }
  80. #pragma mark - funsdk回调处理
  81. #pragma mark 局域网搜索设备回调
  82. -(void)searchDevice:(NSMutableArray *)searchArray result:(int)result{
  83. [SVProgressHUD dismiss];
  84. if (result >= 0) {
  85. deviceArray = [searchArray mutableCopy];
  86. [self.listTableView reloadData];
  87. }
  88. else{
  89. [MessageUI ShowErrorInt:result];
  90. }
  91. }
  92. #pragma mark 添加设备回调
  93. - (void)addDeviceResult:(int)result{
  94. }
  95. #pragma mark - lazyload
  96. -(UITableView *)listTableView{
  97. if (!_listTableView) {
  98. _listTableView = [[UITableView alloc] init];
  99. _listTableView.delegate = self;
  100. _listTableView.dataSource = self;
  101. [_listTableView registerClass:[SearchDeviceCell class] forCellReuseIdentifier:@"SearchDeviceCell"];
  102. _listTableView.tableFooterView = [[UIView alloc] init];
  103. }
  104. return _listTableView;
  105. }
  106. @end