DeviceListViewController.mm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. //
  2. // DeviceListViewController.m
  3. // FunSDKDemo
  4. //
  5. // Created by Levi on 2018/5/18.
  6. // Copyright © 2018年 Levi. All rights reserved.
  7. //
  8. #import "DeviceListTableViewCell.h"
  9. #import "DeviceListViewController.h"
  10. #import "PlayViewController.h"
  11. #import "DeviceManager.h"
  12. #import "DeviceInfoEditViewController.h"
  13. #import "Header.h"
  14. #import "DeviceAddViewController.h"
  15. #import "UserAccountModel.h"
  16. @interface DeviceListViewController ()<UITableViewDelegate,UITableViewDataSource,DeviceManagerDelegate,UserAccountModelDelegate>
  17. {
  18. NSMutableArray *deviceArray; //设备信息数组
  19. int selectNum; //当前选择的设置
  20. UserAccountModel *accountModel; //账号相关功能接口管理器
  21. }
  22. @property (nonatomic, strong) UIBarButtonItem *rightBarBtn;
  23. @property (nonatomic, strong) UITableView *devListTableView;
  24. @end
  25. @implementation DeviceListViewController
  26. - (UITableView *)devListTableView {
  27. if (!_devListTableView) {
  28. _devListTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight ) style:UITableViewStylePlain];
  29. _devListTableView.delegate = self;
  30. _devListTableView.dataSource = self;
  31. _devListTableView.rowHeight = 50;
  32. [_devListTableView registerClass:[DeviceListTableViewCell class] forCellReuseIdentifier:@"cell"];
  33. UILongPressGestureRecognizer *longPressGr = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressToDo:)];
  34. longPressGr.minimumPressDuration = 0.5;
  35. [_devListTableView addGestureRecognizer:longPressGr];
  36. _devListTableView.estimatedRowHeight = 0;
  37. _devListTableView.estimatedSectionHeaderHeight = 0;
  38. _devListTableView.estimatedSectionFooterHeight = 0;
  39. }
  40. return _devListTableView;
  41. }
  42. - (void)viewDidLoad {
  43. [super viewDidLoad];
  44. // 登录
  45. [SVProgressHUD showWithMaskType:SVProgressHUDMaskTypeBlack];
  46. //账号相关功能接口管理器
  47. accountModel = [[UserAccountModel alloc] init];
  48. accountModel.delegate = self;
  49. [accountModel loginWithName:@"海盛海H123" andPassword:@"hsh123456"];
  50. [[NSUserDefaults standardUserDefaults] setObject:@"海盛海H123" forKey:@"DemoUser"];
  51. [[NSUserDefaults standardUserDefaults] setObject:@"hsh123456" forKey:@"DemoPassword"];
  52. [[NSUserDefaults standardUserDefaults] synchronize];
  53. //设置导航栏样式
  54. [self setNaviStyle];
  55. }
  56. //accountModel 的代理
  57. - (void)loginWithNameDelegate:(long)reslut {
  58. [SVProgressHUD dismiss];
  59. //如果成功,dismiss,回到主界面
  60. NSLog(@"------登录结果%ld",reslut);
  61. if (reslut >= 0) {
  62. //[self dismissViewControllerAnimated:NO completion:nil];
  63. // [self.navigationController popViewControllerAnimated:YES];
  64. //保存当前登录的用户名和密码
  65. NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  66. NSMutableDictionary *dic = [[defaults objectForKey:@"DemoUserInfo"] mutableCopy];
  67. if (dic == nil) {
  68. dic = [[NSMutableDictionary alloc] init];
  69. }
  70. [dic setObject:@"hsh123456" forKey:@"海盛海H123"];
  71. [defaults setObject:dic forKey:@"DemoUserInfo"];
  72. //配置子试图
  73. [self configSubView];
  74. //读取设备数据
  75. [self getDevicelist];
  76. //刷新读取到的设备的在线状态
  77. [self getdeviceState:nil];
  78. // return;
  79. }
  80. //如果失败,不做操作,留在登陆界面
  81. // [MessageUI ShowErrorInt:(int)reslut];
  82. }
  83. - (void)getDevicelist {
  84. deviceArray = [[DeviceControl getInstance] currentDeviceArray];
  85. if (deviceArray == nil) {
  86. deviceArray = [[NSMutableArray alloc] initWithCapacity:0];
  87. }
  88. }
  89. -(void)getdeviceState:(NSString*)deviceMac {
  90. //刷新读取到的设备状态
  91. DeviceManager *manager = [DeviceManager getInstance];
  92. manager.delegate = self;
  93. [manager getDeviceState:deviceMac];
  94. }
  95. - (void)setNaviStyle {
  96. self.navigationItem.title = TS("DeviceList");
  97. UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"new_back.png"] style:UIBarButtonItemStylePlain target:self action:@selector(popViewController)];
  98. self.navigationItem.leftBarButtonItem = leftBtn;
  99. self.rightBarBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(refreshDeviceList)];
  100. self.navigationItem.rightBarButtonItem = self.rightBarBtn;
  101. self.rightBarBtn.width = 15;
  102. self.rightBarBtn.tintColor = [UIColor greenColor];
  103. }
  104. - (void)configSubView {
  105. [self.view addSubview:self.devListTableView];
  106. }
  107. -(void)popViewController{
  108. if([SVProgressHUD isVisible]){
  109. [SVProgressHUD dismiss];
  110. }
  111. [self.navigationController popViewControllerAnimated:YES];
  112. DeviceManager *manager = [DeviceManager getInstance];
  113. manager.delegate = nil;
  114. }
  115. #pragma mark - tableview长按手势响应,删除和编辑设备
  116. -(void)longPressToDo:(UILongPressGestureRecognizer *)gesture{
  117. if(gesture.state != UIGestureRecognizerStateBegan){
  118. return;
  119. }
  120. NSIndexPath *indexPath ;
  121. CGPoint point = [gesture locationInView:self.devListTableView];
  122. indexPath = [self.devListTableView indexPathForRowAtPoint:point];
  123. if(indexPath == nil){
  124. return;
  125. }
  126. NSLog(@"%ld",(long)indexPath.row);
  127. DeviceObject *devObject = [deviceArray objectAtIndex:indexPath.section];;
  128. NSString *title = [NSString stringWithFormat:@"%@%@",TS(""), devObject.deviceName];
  129. UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:@"" preferredStyle:UIAlertControllerStyleActionSheet];
  130. UIAlertAction *cancel = [UIAlertAction actionWithTitle:TS("Cancel") style:UIAlertActionStyleCancel handler:nil];
  131. UIAlertAction *deleteBtn = [UIAlertAction actionWithTitle:TS("Delete") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  132. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:TS("warning") message:TS("Are_you_sure_to_delete_device2") preferredStyle:UIAlertControllerStyleAlert];
  133. UIAlertAction *cancelButton = [UIAlertAction actionWithTitle:TS("Cancel") style:UIAlertActionStyleCancel handler:nil];
  134. UIAlertAction *deleteButton = [UIAlertAction actionWithTitle:TS("OK") style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
  135. //发送删除命令
  136. DeviceManager *manager = [DeviceManager getInstance];
  137. [manager deleteDeviceWithDevMac:devObject.deviceMac];
  138. }];
  139. [alertController addAction:cancelButton];
  140. [alertController addAction:deleteButton];
  141. [self presentViewController:alertController animated:YES completion:nil];
  142. }];
  143. UIAlertAction *settingBtn = [UIAlertAction actionWithTitle:TS("Edit") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  144. //跳转到编辑界面
  145. DeviceInfoEditViewController *editVC = [[DeviceInfoEditViewController alloc] init];
  146. editVC.devObject = devObject;
  147. editVC.editSuccess = ^{
  148. //编辑成功,刷新界面
  149. deviceArray = [[DeviceControl getInstance] currentDeviceArray];
  150. [self.devListTableView reloadData];
  151. };
  152. [self.navigationController pushViewController:editVC animated:YES];
  153. }];
  154. [alert addAction:cancel];
  155. [alert addAction:deleteBtn];
  156. [alert addAction:settingBtn];
  157. if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
  158. [alert setModalPresentationStyle:UIModalPresentationPopover];
  159. UIPopoverPresentationController *popPresenter = [alert
  160. popoverPresentationController];
  161. popPresenter.sourceView = self.view;
  162. popPresenter.sourceRect = CGRectMake(self.view.bounds.size.width/2.0, self.view.bounds.size.height, 1.0, 1.0);
  163. popPresenter.permittedArrowDirections = UIPopoverArrowDirectionAny;
  164. }
  165. [self presentViewController:alert animated:YES completion:nil];
  166. }
  167. #pragma mark -- UiTableViewDelegate/DataSource
  168. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  169. DeviceObject *devObject = [deviceArray objectAtIndex:indexPath.section];
  170. if (devObject == nil) {
  171. return;
  172. }
  173. if (devObject.state <=0 ) {
  174. //提示设备不在线
  175. [SVProgressHUD showErrorWithStatus:TS("EE_DVR_CONNECT_DEVICE_ERROR") duration:2.0];
  176. //刷新当前设备状态,如果是门铃的话,可能已经处于休眠状态而没有实时刷新
  177. [[DeviceManager getInstance] getDeviceState:devObject.deviceMac];
  178. return;
  179. }
  180. if (devObject.info.eFunDevState == 4) {//睡眠中
  181. [self getdeviceState:devObject.deviceMac];
  182. [SVProgressHUD showWithStatus:TS("Refresh_State")];
  183. //获取设备状态和门铃睡眠状态,然后return
  184. }else if (devObject.info.eFunDevState == 3) {//深度睡眠
  185. //直接return,深度睡眠需要设备端唤醒
  186. [SVProgressHUD showErrorWithStatus:TS("DEV_SLEEP_AND_CAN_NOT_WAKE_UP") duration:3.0];
  187. return;
  188. }else if (devObject.info.eFunDevState == 2) {//睡眠
  189. //唤醒睡眠,然后继续其他处理
  190. [SVProgressHUD showWithStatus:TS("Waking_up")];
  191. [[DeviceManager getInstance] deviceWeakUp:devObject.deviceMac];
  192. return;
  193. }
  194. [SVProgressHUD showWithStatus:TS("Get_Channle")];
  195. //现获取设备通道信息,多通道预览时需要。如果不需要支持多通道预览,则可以不获取通道信息,直接打开0通道
  196. selectNum = (int)indexPath.section;
  197. [[DeviceManager getInstance] getDeviceChannel:devObject.deviceMac];
  198. //获取成功之后,在回调接口中进入预览界面 - (void)getDeviceChannel:(NSString *)sId result:(int)result
  199. }
  200. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  201. return [deviceArray count];
  202. }
  203. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  204. return 1;
  205. }
  206. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  207. return 110;
  208. }
  209. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  210. return 15;
  211. }
  212. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  213. DeviceListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
  214. cell.selectionStyle = UITableViewCellSelectionStyleNone;
  215. DeviceObject *devObject = [deviceArray objectAtIndex:indexPath.section];
  216. if (devObject != nil) {
  217. cell.devSNLab.text = devObject.deviceMac;
  218. cell.devName.text = devObject.deviceName;
  219. [cell setDeviceState:devObject.state];
  220. [cell setSleepType:devObject.info.eFunDevState];
  221. cell.devTypeLab.text = [NSString getDeviceType:devObject.nType];
  222. cell.devImageV.image = [UIImage imageNamed:[NSString getDeviceImageType:devObject.nType]];
  223. }
  224. return cell;
  225. }
  226. #pragma - mark -- 刷新设备列表
  227. - (void)refreshDeviceList {
  228. // [self getdeviceState:nil];
  229. DeviceAddViewController *add = [[DeviceAddViewController alloc] init];
  230. [self.navigationController pushViewController:add animated:YES];
  231. }
  232. #pragma - mark 获取设备在线状态结果
  233. - (void)getDeviceState:(NSString *)sId result:(int)result {
  234. [self.devListTableView reloadData];
  235. }
  236. #pragma - mark 设备唤醒结果
  237. - (void)deviceWeakUp:(NSString *)sId result:(int)result {
  238. if (result < 0) {
  239. [MessageUI ShowErrorInt:result];
  240. return;
  241. }
  242. [SVProgressHUD dismiss];
  243. DeviceObject *object = [[DeviceControl getInstance] GetDeviceObjectBySN:sId];
  244. object.info.eFunDevState = 1;
  245. [self.devListTableView reloadData];
  246. }
  247. #pragma mark 获取设备通道结果
  248. - (void)getDeviceChannel:(NSString *)sId result:(int)result {
  249. if (result <= 0) {
  250. if(result == EE_DVR_PASSWORD_NOT_VALID)//密码错误,弹出密码修改框
  251. {
  252. [SVProgressHUD dismiss];
  253. UIAlertController *alert = [UIAlertController alertControllerWithTitle:TS("EE_DVR_PASSWORD_NOT_VALID") message:sId preferredStyle:UIAlertControllerStyleAlert];
  254. [alert addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
  255. textField.placeholder = TS("set_new_psd");
  256. }];
  257. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:TS("Cancel") style:UIAlertActionStyleCancel handler:nil];
  258. UIAlertAction *okAction = [UIAlertAction actionWithTitle:TS("OK") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  259. UITextField *passWordTextField = alert.textFields.firstObject;
  260. DeviceObject *devObject = [deviceArray objectAtIndex:selectNum];
  261. DeviceManager *manager = [DeviceManager getInstance];
  262. //点击确定修改密码
  263. [manager changeDevicePsw:sId loginName:devObject.loginName password:passWordTextField.text];
  264. }];
  265. [alert addAction:cancelAction];
  266. [alert addAction:okAction];
  267. [self presentViewController:alert animated:YES completion:nil];
  268. return;
  269. }
  270. [MessageUI ShowErrorInt:result];
  271. return;
  272. }
  273. [SVProgressHUD dismiss];
  274. //获取通道信息成功,进入预览界面
  275. DeviceObject *object = [[DeviceControl getInstance] GetDeviceObjectBySN:sId];
  276. //这里只选了当前设备的第一个通道进行预览,有些设备可能包含多个通道
  277. [[DeviceControl getInstance] setSelectChannel:[object.channelArray firstObject]];
  278. PlayViewController *playVC = [[PlayViewController alloc] init];
  279. [self.navigationController pushViewController:playVC animated:YES];
  280. }
  281. #pragma mark 删除设备结果
  282. - (void)deleteDevice:(NSString *)sId result:(int)result{
  283. if (result >= 0) {
  284. [SVProgressHUD showSuccessWithStatus:TS("Success")];
  285. deviceArray = [[DeviceControl getInstance] currentDeviceArray];
  286. [self.devListTableView reloadData];
  287. }else{
  288. [MessageUI ShowErrorInt:result];
  289. }
  290. }
  291. @end