// // CameraRecordViewController.m // Ifish // // Created by imac on 16/12/9. // Copyright © 2016年 lianxiang. All rights reserved. // #import "CameraRecordViewController.h" #import "P2PClient.h" #import "IfishCameraRecordfirstCell.h" #import "IfishCameraRecordSecondCell.h" #import "IfishCameraRecordThirdCell.h" #define RECODIEECELL_TIME 911 #define RECODIEECELL_BTN 922 #define RECODIEECELL_SWiTCH 933 static const CGFloat ToastFade = 0.2; #import "NSTimer+Convenience.h" #import "IfishCameraRecordFourCell.h" @interface CameraRecordViewController () @property(nonatomic,strong) UITableView *tab; @property(nonatomic) NSInteger second; @property(nonatomic) NSInteger minute; @property(nonatomic,strong) NSTimer*timer; @property(nonatomic,copy) NSString *timestr; @property(nonatomic) BOOL isRecording; @property(nonatomic) NSIndexPath *timerIndex; @property(nonatomic) NSIndexPath *switchIndex; @property(nonatomic) NSIndexPath *buttonIndex; @property(assign) unsigned int remoteRecordState; //本app 需求模式 非摄像头模式 @property(nonatomic) BOOL isManual; @end @implementation CameraRecordViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [self addTitleViewWithTitle:@"手动录制"]; _timestr= @"00:00"; _isRecording = NO; _isManual = NO; [self creatUI]; self.view.backgroundColor = RGB(242, 242, 242); } -(void)goBackAction{ if (_isRecording) { UIAlertController*ac=[UIAlertController alertControllerWithTitle:@"" message:@"是否结束手动录制" preferredStyle:UIAlertControllerStyleAlert]; [self presentViewController:ac animated:YES completion:nil]; [ac addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction*action){ }]]; [ac addAction:[UIAlertAction actionWithTitle:@"结束" style:UIAlertActionStyleDefault handler:^(UIAlertAction*action){ [[P2PClient sharedClient] setRemoteRecordWithId:self.camera.cameraId password:self.camera.cameraPass state:SETTING_VALUE_REMOTE_RECORD_STATE_OFF]; [self.navigationController popViewControllerAnimated:YES]; }]]; }else{ [self.navigationController popViewControllerAnimated:YES]; } } -(void)creatUI{ self.tab = [[UITableView alloc] initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height) style:UITableViewStylePlain]; self.tab.delegate = self; self.tab.dataSource = self; self.tab.separatorStyle = UITableViewCellSeparatorStyleNone; self.tab.showsVerticalScrollIndicator = NO; self.tab.scrollEnabled = NO; self.tab.backgroundColor = RGB(242, 242, 242); [self.view addSubview:self.tab]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } -(void)viewWillAppear:(BOOL)animated{ [super viewWillAppear:animated]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveRemoteMessage:) name:RECEIVE_REMOTE_MESSAGE object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(ack_receiveRemoteMessage:) name:ACK_RECEIVE_REMOTE_MESSAGE object:nil]; [[P2PClient sharedClient] getNpcSettingsWithId:self.camera.cameraId password:self.camera.cameraPass]; // [[P2PClient sharedClient] getSDCardInfoWithId:self.camera.cameraId password:self.camera.cameraPass]; } -(void)viewWillDisappear:(BOOL)animated{ [super viewWillDisappear:animated]; [[NSNotificationCenter defaultCenter] removeObserver:self name:RECEIVE_REMOTE_MESSAGE object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:ACK_RECEIVE_REMOTE_MESSAGE object:nil]; } #pragma mark - receiveRemoteMessage - (void)receiveRemoteMessage:(NSNotification *)notification { NSDictionary *parameter = [notification userInfo]; int key = [[parameter valueForKey:@"key"] intValue]; switch (key) { case RET_GET_SDCARD_INFO: { NSInteger result = [[parameter valueForKey:@"result"] intValue]; dispatch_async(dispatch_get_main_queue(), ^{ if (result == 1) { //[self.view makeToast:@"sdk卡可使用"]; //获取状态信息 // [[P2PClient sharedClient] getNpcSettingsWithId:self.camera.cameraId password:self.camera.cameraPass]; if (_isManual) { if (self.remoteRecordState ==1){ //[self.view makeToast:@"请先关闭自动录制"]; [self showLableAction:@"请先关闭自动录制"]; dispatch_async(dispatch_get_main_queue(), ^{ [self hiddenTostVIew]; }); [self resetTimerlabel]; return ; } NSArray *buttonindex =@[self.buttonIndex]; [self.tab reloadRowsAtIndexPaths:buttonindex withRowAnimation:UITableViewRowAnimationNone]; [[P2PClient sharedClient] setRemoteRecordWithId:self.camera.cameraId password:self.camera.cameraPass state:SETTING_VALUE_REMOTE_RECORD_STATE_ON]; }else{ [self showLableAction:@"请先关闭自动录制"]; dispatch_async(dispatch_get_main_queue(), ^{ [self hiddenTostVIew]; }); [self resetTimerlabel]; } }else{ //sd卡不存在, //停止计时 dispatch_async(dispatch_get_main_queue(), ^{ [self hiddenTostVIew]; }); [self resetTimerlabel]; [self.view makeToast:NSLocalizedString(@"no_storage", nil)]; } }); } break; case RET_GET_NPCSETTINGS_PRERECORD: //预录像开关 获取时 { NSInteger state = [[parameter valueForKey:@"state"] intValue]; NSLog(@"预录像开关 获取时state%ld",(long)state); } break; case RET_SET_NPCSETTINGS_PRERECORD: //预录像开关 设置时 { NSInteger result = [[parameter valueForKey:@"result"] intValue]; if(result==0){ NSLog(@"预录像开关设置result%ld",(long)result); }else{ NSLog(@"预录像开关设置result%ld",(long)result); } } break; case RET_SET_NPCSETTINGS_REMOTE_RECORD: { NSInteger result = [[parameter valueForKey:@"result"] integerValue]; if(result==0){ //设置成功主动获取一次状态 刷新 dispatch_async(dispatch_get_main_queue(), ^{ [self hiddenTostVIew]; }); [[P2PClient sharedClient] getNpcSettingsWithId:self.camera.cameraId password:self.camera.cameraPass]; }else{ NSLog(@"设置失败:%li",(long)result); } } break; case RET_GET_NPCSETTINGS_REMOTE_RECORD: { // NSInteger state = [[parameter valueForKey:@"state"] intValue]; self.remoteRecordState = (unsigned int)state; if (state ==0) { dispatch_async(dispatch_get_main_queue(), ^{ [self resetTimerlabel]; }); }else{ } if (_isManual) { //手动模式开启不刷新switch按钮状态 return; } NSLog(@"获取 remote record state:%li",(long)state); dispatch_async(dispatch_get_main_queue(), ^{ NSArray *index =@[self.switchIndex]; [self.tab reloadRowsAtIndexPaths:index withRowAnimation:UITableViewRowAnimationNone]; // NSArray *buttonindex =@[self.buttonIndex]; // [self.tab reloadRowsAtIndexPaths:buttonindex withRowAnimation:UITableViewRowAnimationNone]; }); } case RET_GET_NPCSETTINGS_RECORD_TYPE: { NSInteger type = [[parameter valueForKey:@"type"] intValue]; //0手动模式 摄像头模式 NSLog(@"获取录像模式类型type%ld",(long)type); } break; case RET_SET_NPCSETTINGS_RECORD_TYPE: { NSInteger result = [[parameter valueForKey:@"result"] intValue]; //0类型设置成功 if (result==0) { //[[P2PClient sharedClient] setRemoteRecordWithId:self.camera.cameraId // password:self.camera.cameraPass state:SETTING_VALUE_REMOTE_RECORD_STATE_ON]; } NSLog(@"设置录像模式类型result%ld",(long)result); } break; default: break; } } #pragma mark -ack_receiveRemoteMessage - (void)ack_receiveRemoteMessage:(NSNotification *)notification{ NSDictionary *parameter = [notification userInfo]; int key = [[parameter valueForKey:@"key"] intValue]; int result = [[parameter valueForKey:@"result"] intValue]; switch (key) { case ACK_RET_GET_SDCARD_INFO: { dispatch_async(dispatch_get_main_queue(), ^{ if(result==1){ [self.view makeToast:NSLocalizedString(@"device_password_error", nil)]; // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // usleep(800000); // // // }); }else if(result==2){ DLog(@"resend do device update"); } }); DLog(@"ACK_RET_GET_SDCARD_INFO:%i",result); } break; case ACK_RET_SET_SDCARD_INFO: { dispatch_async(dispatch_get_main_queue(), ^{ if(result==1){ [self.view makeToast:NSLocalizedString(@"device_password_error", nil)]; // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // usleep(800000); // dispatch_async(dispatch_get_main_queue(), ^{ // }); // }); }else if(result==2){ DLog(@"resend do device update"); } }); } break; default: break; } } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ if (section ==2) { return 2; } return 1; } -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{ return 3; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ if (indexPath.section ==0 && indexPath.row ==0) { IfishCameraRecordfirstCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IfishCameraRecordfirstCell"]; if (!cell) { cell = [[[NSBundle mainBundle]loadNibNamed:@"IfishCameraRecordfirstCell" owner:self options:nil]lastObject]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; return cell; }else if (indexPath.section ==2 && indexPath.row ==0){ IfishCameraRecordSecondCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IfishCameraRecordSecondCell"]; if (!cell) { cell = [[[NSBundle mainBundle]loadNibNamed:@"IfishCameraRecordSecondCell" owner:self options:nil]lastObject]; } cell.selectionStyle = UITableViewCellSelectionStyleNone; cell.tag = RECODIEECELL_TIME; self.timerIndex = indexPath; cell.timeLabe.text = _timestr; _isRecording ? [cell.timeLabe setTextColor:RGB(51, 51, 51)] : [cell.timeLabe setTextColor:RGB(153, 153, 153)]; [cell cellRecordBViewCorner]; return cell; }else if (indexPath.section ==2 && indexPath.row ==1){ IfishCameraRecordThirdCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IfishCameraRecordThirdCell"]; if (!cell) { cell = [[[NSBundle mainBundle]loadNibNamed:@"IfishCameraRecordThirdCell" owner:self options:nil]lastObject]; } cell.tag = RECODIEECELL_BTN; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell.recordBtn addTarget:self action:@selector(recordBtnAction:) forControlEvents:UIControlEventTouchUpInside]; if (_isRecording) { cell.recordBtn.selected = YES; [cell.recordBtn setBackgroundColor:RGB(255,115,92)]; [cell.recordBtn setTitle:@"结束录制" forState:UIControlStateNormal]; }else{ cell.recordBtn.selected = NO; [cell.recordBtn setBackgroundColor:RGB(0,184,239)]; [cell.recordBtn setTitle:@"开始录制" forState:UIControlStateNormal]; } self.buttonIndex =indexPath; [cell cellSetRecordBtnBackViewCorner]; return cell; }else if (indexPath.section ==1 && indexPath.row ==0){ self.switchIndex = indexPath; IfishCameraRecordFourCell *cell = [tableView dequeueReusableCellWithIdentifier:@"IfishCameraRecordFourCell"]; if (!cell) { cell = [[[NSBundle mainBundle]loadNibNamed:@"IfishCameraRecordFourCell" owner:self options:nil]lastObject]; } cell.tag = RECODIEECELL_SWiTCH; cell.selectionStyle = UITableViewCellSelectionStyleNone; [cell.zidongSw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside]; if (self.remoteRecordState==1) { [cell.zidongSw setOn:YES]; }else{ [cell.zidongSw setOn:NO]; } return cell; } return nil; } -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { if (indexPath.section ==0 && indexPath.row ==0) { return kScreenSize.width*0.516; }else if (indexPath.section ==2 && indexPath.row ==0){ return kScreenSize.width*0.357; }else if (indexPath.section ==1 && indexPath.row ==0){ return 46*KWidth_Scale; }else if (indexPath.section ==2 && indexPath.row ==1){ return 76*KWidth_Scale; } return 0; } -(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{ if (section==0) { return kScreenSize.width*0.213; } return 5; } -(UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{ if (section ==0) { UIView *sectionV1=[[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,kScreenSize.width*0.213)]; sectionV1.backgroundColor = RGB(242, 242, 242); return sectionV1; } UIView *sectionV2=[[UIView alloc] initWithFrame:CGRectMake(0, 0,self.view.frame.size.width,5)]; sectionV2.backgroundColor = RGB(242, 242, 242); return sectionV2; } #pragma mark - recordBtnAction -(void)recordBtnAction:(UIButton*)btn{ if (btn.selected) { [self resetTimerlabel]; _isManual = NO; //停止录制 [self startTostVIew]; [[P2PClient sharedClient] setRemoteRecordWithId:self.camera.cameraId password:self.camera.cameraPass state:SETTING_VALUE_REMOTE_RECORD_STATE_OFF]; [self showLableAction:@"录制成功,请在回放中查看"]; }else{ [self startRunTimerlabel]; _isManual = YES; //开始录制 //获取SDCard状态 [self startTostVIew]; [[P2PClient sharedClient] getSDCardInfoWithId:self.camera.cameraId password:self.camera.cameraPass]; } } -(void)startRunTimerlabel { //IfishCameraRecordThirdCell *cell=[self.view viewWithTag:RECODIEECELL_BTN]; //cell.recordBtn.selected =YES; _isRecording=YES; __weak typeof(self)weakSelf=self; NSInteger count = 30*60; //NSInteger count = -1; //NSInteger count = 60; weakSelf.timer = [NSTimer scheduledTimerWithTimeInterval:1 count:count callback:^{ weakSelf.second++; if (weakSelf.second ==60) { weakSelf.second = 0; weakSelf.minute++; } if (weakSelf.minute ==30) { weakSelf.minute =0; weakSelf.second =0; [self resetTimerlabel]; _isManual = NO; //停止录制 [self startTostVIew]; [[P2PClient sharedClient] setRemoteRecordWithId:self.camera.cameraId password:self.camera.cameraPass state:SETTING_VALUE_REMOTE_RECORD_STATE_OFF]; [self showLableAction:@"录制成功,请在回放中查看"]; return ; } _timestr= [IfishHelperUtils formatCameraPalybackTime:weakSelf.minute second:weakSelf.second]; NSArray *index =@[self.timerIndex]; [self.tab reloadRowsAtIndexPaths:index withRowAnimation:UITableViewRowAnimationNone]; }]; [[NSRunLoop currentRunLoop] addTimer: weakSelf.timer forMode:NSRunLoopCommonModes]; // NSArray *buttonindex =@[self.buttonIndex]; // [self.tab reloadRowsAtIndexPaths:buttonindex withRowAnimation:UITableViewRowAnimationNone]; } -(void)resetTimerlabel { _isRecording=NO; if (_timer) { [_timer unfireTimer]; } self.minute =0; self.second =0; _timestr = @"00:00"; NSArray *index =@[self.timerIndex]; [self.tab reloadRowsAtIndexPaths:index withRowAnimation:UITableViewRowAnimationNone]; NSArray *buttonindex =@[self.buttonIndex]; [self.tab reloadRowsAtIndexPaths:buttonindex withRowAnimation:UITableViewRowAnimationNone]; } #pragma mark -加载中 -(void)startTostVIew{ self.progressAlert = [[MBProgressHUD alloc] initWithView:self.view]; [self.view addSubview:self.progressAlert]; self.progressAlert.mode=MBProgressHUDModeIndeterminate; self.progressAlert.customView = [[UIView alloc] init]; //self.progressAlert.labelText = [NSString stringWithFormat:@"%@...",NSLocalizedString(@"validating", nil)]; [self.progressAlert show:YES]; //设置5秒超时 [self.progressAlert hide:YES afterDelay:5]; } -(void)hiddenTostVIew { [self.progressAlert hide:YES]; } #pragma mark - switchAction: -(void)switchAction:(UISwitch*)sender { IfishCameraRecordFourCell *cell =[self.view viewWithTag:RECODIEECELL_SWiTCH]; UISwitch* swt=(UISwitch*)sender; cell.zidongSw = swt; if (_isRecording) { dispatch_async(dispatch_get_main_queue(), ^{ [cell.zidongSw setOn:NO]; }); //[self.view makeToast:@"请先结束手动录制"]; [self showLableAction:@"请先结束手动录制"]; return; } [self startTostVIew]; if (swt.isOn) { _isManual = YES; [[P2PClient sharedClient] setRemoteRecordWithId:self.camera.cameraId password:self.camera.cameraPass state:SETTING_VALUE_REMOTE_RECORD_STATE_OFF]; }else{ _isManual = NO; [[P2PClient sharedClient] setRemoteRecordWithId:self.camera.cameraId password:self.camera.cameraPass state:SETTING_VALUE_REMOTE_RECORD_STATE_ON]; } } - (void)showLableAction:(NSString *)str{ UILabel *laberAction =[[UILabel alloc]init]; CGFloat labW = 200; laberAction.frame =CGRectMake(kScreenSize.width/2 - labW/2,kScreenSize.height*3/4 - 50 , labW, 40); laberAction.clipsToBounds =YES; laberAction.layer.cornerRadius =5; laberAction.layer.backgroundColor = [UIColor blackColor].CGColor; laberAction.alpha =1; laberAction.text =str; laberAction.textAlignment =NSTextAlignmentCenter; //laberAction.backgroundColor =IndicatorBack_COLOR; laberAction.font=[UIFont systemFontOfSize:14]; laberAction.textColor=[UIColor whiteColor]; [self.view addSubview:laberAction]; [UIView animateWithDuration:ToastFade delay:0.0 options:UIViewAnimationOptionCurveEaseOut animations:^{ laberAction.alpha = 1.0; } completion:^(BOOL finished) { [UIView animateWithDuration:ToastFade delay:1.5 options:UIViewAnimationOptionCurveEaseIn animations:^{ laberAction.alpha = 0.0; } completion:^(BOOL finished) { [laberAction removeFromSuperview]; }]; }]; } @end