CloudVideoConfig.mm 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. //
  2. // CloudVideoConfig.m
  3. // FunSDKDemo
  4. //
  5. // Created by XM on 2019/1/7.
  6. // Copyright © 2019年 XM. All rights reserved.
  7. //
  8. #import "CloudVideoConfig.h"
  9. #import "FunSDK/FunSDK.h"
  10. #import "FunSDK/Fun_CM.h"
  11. #import "TimeInfo.h"
  12. @interface CloudVideoConfig ()
  13. {
  14. NSMutableArray *fileArray; //搜索某一天的云视频数组结果
  15. NSMutableArray *dateArray; //搜索某一个月哪些天有云视频的结果数组
  16. NSMutableArray *timeArray;
  17. }
  18. @end
  19. @implementation CloudVideoConfig
  20. #pragma mark - 获取传入这一天当月有云视频的日期
  21. - (void)getCloudVideoMonth:(NSDate*)date {
  22. dateArray = [[NSMutableArray alloc] initWithCapacity:0];
  23. //获取通道
  24. ChannelObject *channel = [[DeviceControl getInstance] getSelectChannel];
  25. NSDateComponents * components = [[NSCalendar currentCalendar] components:NSCalendarUnitDay | NSCalendarUnitMonth | NSCalendarUnitYear fromDate:date];
  26. SDK_SYSTEM_TIME nTime;
  27. nTime.year = (int)components.year;
  28. nTime.month = (int)components.month;
  29. nTime.day = 1;
  30. nTime.hour =0;
  31. nTime.minute = 0;
  32. nTime.second = 0;
  33. time_t ToTime_t(SDK_SYSTEM_TIME *time);
  34. int time =(int)ToTime_t(&nTime);
  35. MC_SearchMediaByMoth(self.MsgHandle, SZSTR(channel.deviceMac), 0, "", time,0);
  36. }
  37. #pragma mark 获取传入日期云存储中的云视频
  38. - (void)searchCloudVideo:(NSDate*)date {
  39. fileArray = [[NSMutableArray alloc] initWithCapacity:0];
  40. timeArray = [[NSMutableArray alloc] initWithCapacity:0];
  41. ChannelObject *channel = [[DeviceControl getInstance] getSelectChannel];
  42. self.devID = channel.deviceMac;
  43. SDK_SYSTEM_TIME beginNTime;
  44. SDK_SYSTEM_TIME endNTime;
  45. NSCalendar *calendar =[NSCalendar currentCalendar];
  46. NSDateComponents* compt = [calendar components:(NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond) fromDate:date];
  47. //这里搜索一整天的,也可以根据需要自己设置搜索具体时间
  48. beginNTime.year = (int)compt.year;
  49. beginNTime.month = (int)compt.month;
  50. beginNTime.day = (int)compt.day;
  51. beginNTime.hour = 0;
  52. beginNTime.minute = 0;
  53. beginNTime.second = 0;
  54. endNTime.year = (int)compt.year;
  55. endNTime.month = (int)compt.month;
  56. endNTime.day = (int)compt.day;
  57. endNTime.hour = 23;
  58. endNTime.minute = 59;
  59. endNTime.second = 59;
  60. time_t ToTime_t(SDK_SYSTEM_TIME *time);
  61. int beginTime = (int)ToTime_t(&beginNTime);
  62. time_t ToTime_t(SDK_SYSTEM_TIME *time);
  63. int endTime = (int)ToTime_t(&endNTime);
  64. MC_SearchMediaByTime(self.MsgHandle, SZSTR(channel.deviceMac), -1,"main",beginTime,endTime,0);
  65. }
  66. #pragma mark - 下载云视频缩略图
  67. - (void)downloadSmallCloudThumb:(CLouldVideoResource*)resource {
  68. ChannelObject *channel = [[DeviceControl getInstance] getSelectChannel];
  69. NSString *thumbPath = [NSString thumbnailPath];
  70. NSString *pictureFilePath = [thumbPath stringByAppendingFormat:@"/%@.jpg",resource.indexFile];
  71. MC_DownloadThumbnail(self.MsgHandle, SZSTR(channel.deviceMac),SZSTR(resource.JsonStr), SZSTR(pictureFilePath), 120, 90, 0);
  72. }
  73. #pragma mark 下载云视频文件
  74. - (void)downloadCloudVideoFile:(CLouldVideoResource*)resource {
  75. //存储路径
  76. NSString *directoryPath = [NSString getVideoPath];
  77. NSString *timeString = [NSString stringWithFormat:@"%04d-%02d-%02d %02d:%02d:%02d",resource.year,resource.month,resource.day,resource.hour,resource.minute,resource.second];
  78. resource.storePath = [directoryPath stringByAppendingFormat:@"/%@.mp4",timeString];
  79. //云视频时间段
  80. SDK_SYSTEM_TIME beginTime;
  81. beginTime.year = resource.year;
  82. beginTime.month = resource.month;
  83. beginTime.day = resource.day;
  84. beginTime.hour = resource.hour;
  85. beginTime.minute = resource.minute;
  86. beginTime.second = resource.second;
  87. beginTime.isdst = resource.isdst;
  88. time_t ToTime_t(SDK_SYSTEM_TIME *time);
  89. int beginTimeInt = (int)ToTime_t(&beginTime);
  90. SDK_SYSTEM_TIME endTime;
  91. [resource.endTime componentsSeparatedByString:@":"];
  92. [resource.endDate componentsSeparatedByString:@"-"];
  93. endTime.year = [[[resource.endDate componentsSeparatedByString:@"-"] firstObject] intValue];
  94. endTime.month = [[[resource.endDate componentsSeparatedByString:@"-"] objectAtIndex:1] intValue];
  95. endTime.day = [[[resource.endDate componentsSeparatedByString:@"-"] lastObject] intValue];
  96. endTime.hour = [[[resource.endTime componentsSeparatedByString:@":"] firstObject] intValue];
  97. endTime.minute = [[[resource.endTime componentsSeparatedByString:@":"] objectAtIndex:1] intValue];
  98. endTime.second = [[[resource.endTime componentsSeparatedByString:@":"] lastObject] intValue];
  99. endTime.isdst = resource.isdst;
  100. time_t ToTime_t(SDK_SYSTEM_TIME *time);
  101. int endTimeInt = (int)ToTime_t(&endTime);
  102. //开始下载
  103. FUN_MediaCloudRecordDownload(self.MsgHandle, SZSTR(resource.devId), 0, "", beginTimeInt, endTimeInt, SZSTR(resource.storePath),0);
  104. }
  105. #pragma mark - 读取获取到的一个月份中,有云视频的日期数组
  106. - (NSMutableArray*)getMonthVideoArray {
  107. if (dateArray) {
  108. return dateArray;
  109. }
  110. return [NSMutableArray array];
  111. }
  112. #pragma mark 读取获取到的传入一天中的云视频数组
  113. - (NSMutableArray*)getCloudVideoFileArray {
  114. if (fileArray) {
  115. return fileArray;
  116. }
  117. return [NSMutableArray array];
  118. }
  119. #pragma mark 读取这一天有录像的时间段
  120. - (NSMutableArray*)getVideoTimeArray {
  121. if (timeArray) {
  122. return timeArray;
  123. }
  124. return [NSMutableArray array];
  125. }
  126. #pragma mark - OnFunSDKResult
  127. -(void)OnFunSDKResult:(NSNumber *)pParam{
  128. NSInteger nAddr = [pParam integerValue];
  129. MsgContent *msg = (MsgContent *)nAddr;
  130. switch ( msg->id ) {
  131. #pragma mark 搜索有云视频的日期回调
  132. case EMSG_MC_SearchMediaByMoth:{
  133. if ( msg->param1 >= 0 ) {
  134. NSData *data = [[[NSString alloc]initWithUTF8String:msg->szStr] dataUsingEncoding:NSUTF8StringEncoding];
  135. if ( data == nil ){
  136. if ([self.delegate respondsToSelector:@selector(getCloudMonthResult:)]) {
  137. [self.delegate getCloudMonthResult:0];
  138. }
  139. return;
  140. }
  141. NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  142. if ( appData == nil) {
  143. if ([self.delegate respondsToSelector:@selector(getCloudMonthResult:)]) {
  144. [self.delegate getCloudMonthResult:0];
  145. }
  146. return;
  147. }
  148. NSDictionary *dic = [appData objectForKey:@"AlarmCenter"];
  149. NSArray *dateArr = [[dic objectForKey:@"Body"] objectForKey:@"Date"];
  150. if (!dateArr || dateArr.count<=0) {
  151. if ([self.delegate respondsToSelector:@selector(getCloudMonthResult:)]) {
  152. [self.delegate getCloudMonthResult:0];
  153. }
  154. return;
  155. }
  156. for (int i = 0; i<dateArr.count; i++) {
  157. [dateArray addObject:[dateArr[i] objectForKey:@"Time"]];
  158. }
  159. if ([self.delegate respondsToSelector:@selector(getCloudMonthResult:)]) {
  160. [self.delegate getCloudMonthResult:msg->param1];
  161. }
  162. }
  163. }
  164. break;
  165. #pragma mark 搜索云视频文件回调
  166. case EMSG_MC_SearchMediaByTime:{
  167. NSInteger _add = 0;
  168. if ( msg->param1 < 0) {
  169. }else{
  170. NSData *data = [[[NSString alloc]initWithUTF8String:msg->szStr] dataUsingEncoding:NSUTF8StringEncoding];
  171. if ( data == nil ){
  172. if ([self.delegate respondsToSelector:@selector(getCloudVideoResult:)]) {
  173. [self.delegate getCloudVideoResult:msg->param1];
  174. }
  175. if ([self.timeDelegate respondsToSelector:@selector(getCloudVideoTimeResult:)]) {
  176. [self.timeDelegate getCloudVideoTimeResult:msg->param1];
  177. }
  178. return;
  179. }
  180. NSDictionary *appData = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
  181. if ( appData == nil ||![appData objectForKey:@"AlarmCenter"]) {
  182. if ([self.delegate respondsToSelector:@selector(getCloudVideoResult:)]) {
  183. [self.delegate getCloudVideoResult:msg->param1];
  184. }
  185. if ([self.timeDelegate respondsToSelector:@selector(getCloudVideoTimeResult:)]) {
  186. [self.timeDelegate getCloudVideoTimeResult:msg->param1];
  187. }
  188. return;
  189. }
  190. NSDictionary *body = [[appData objectForKey:@"AlarmCenter"] objectForKey:@"Body"];
  191. if ( body == nil) {
  192. if ([self.delegate respondsToSelector:@selector(getCloudVideoResult:)]) {
  193. [self.delegate getCloudVideoResult:msg->param1];
  194. }
  195. if ([self.timeDelegate respondsToSelector:@selector(getCloudVideoTimeResult:)]) {
  196. [self.timeDelegate getCloudVideoTimeResult:msg->param1];
  197. }
  198. return;
  199. }
  200. NSArray *videoArray = [body objectForKey:@"VideoArray"];
  201. if (videoArray.count <= 0 || videoArray == nil) {
  202. if ([self.delegate respondsToSelector:@selector(getCloudVideoResult:)]) {
  203. [self.delegate getCloudVideoResult:msg->param1];
  204. }
  205. if ([self.timeDelegate respondsToSelector:@selector(getCloudVideoTimeResult:)]) {
  206. [self.timeDelegate getCloudVideoTimeResult:msg->param1];
  207. }
  208. return;
  209. }
  210. if (self.timeDelegate) {
  211. for (int i = 0; i < 1440; i ++) {
  212. [self createVideoDataWithType:TYPE_NONE andStartTime:(i*60) andEndTime:((i*60) + 60) toArray:timeArray];
  213. }
  214. }
  215. for (int i = 0; i<videoArray.count; i++) {
  216. CLouldVideoResource* resource = [[CLouldVideoResource alloc] init];
  217. NSDictionary *infoDic = videoArray[i];
  218. NSString *begin = [infoDic objectForKey:@"StartTime"];
  219. NSString *beginDateString = [[begin componentsSeparatedByString:@" "] firstObject];
  220. NSString *beginTimeStr =[[begin componentsSeparatedByString:@" "] lastObject];
  221. NSString *stop = [infoDic objectForKey:@"StopTime"];
  222. NSString *stopDateString = [[stop componentsSeparatedByString:@" "] firstObject];
  223. NSString *stopTimeStr =[[stop componentsSeparatedByString:@" "] lastObject];
  224. resource.JsonStr = [self convertToJSONData:videoArray[i]];
  225. resource.devId = self.devID;
  226. resource.beginDate = beginDateString;
  227. resource.beginTime = beginTimeStr;
  228. resource.endDate = stopDateString;
  229. resource.endTime = stopTimeStr;
  230. resource.indexFile = [infoDic objectForKey:@"IndexFile"];
  231. NSArray *dateArray = [beginDateString componentsSeparatedByString:@"-"];
  232. NSArray *timeArrays = [beginTimeStr componentsSeparatedByString:@":"];
  233. resource.year = [[dateArray firstObject] intValue];
  234. resource.month = [dateArray[1] intValue];
  235. resource.day = [[dateArray lastObject] intValue];
  236. resource.hour = [[timeArrays firstObject] intValue];
  237. resource.minute = [timeArrays[1] intValue];
  238. resource.second =[[timeArrays lastObject] intValue];
  239. if (self.delegate) { //设备云视频配置界面查询
  240. [fileArray addObject:resource];
  241. }else if (self.timeDelegate) { //云视频回放查询录像时间
  242. int startTime = resource.minute + resource.hour *60;
  243. NSArray *endtimeArray = [stopTimeStr componentsSeparatedByString:@":"];
  244. int endMin = 0;
  245. int endHou = 0;
  246. if (endtimeArray.count >= 2) {
  247. endMin =[[endtimeArray objectAtIndex:1] intValue];
  248. endHou =[[endtimeArray firstObject] intValue];
  249. }
  250. int endTime = endHou *60 + endMin;
  251. for (int i = 0; i< endTime-startTime+1; i++) {
  252. TimeInfo *info = [[TimeInfo alloc] init];
  253. info.type = TYPE_NORMAL;
  254. info.start_Time = (startTime+i)*60;
  255. info.end_Time = ((startTime+i) + 1)*60;
  256. if (timeArray.count > startTime+i) {
  257. [timeArray replaceObjectAtIndex:startTime+i withObject:info];
  258. }
  259. _add = (startTime+i-1)*60/4;
  260. }
  261. }
  262. }
  263. }
  264. if ([self.delegate respondsToSelector:@selector(getCloudVideoResult:)]) {
  265. [self.delegate getCloudVideoResult:msg->param1];
  266. }
  267. if ([self.timeDelegate respondsToSelector:@selector(addTimeDelegate:)]) {
  268. [self.timeDelegate addTimeDelegate:_add];
  269. }
  270. if ([self.timeDelegate respondsToSelector:@selector(getCloudVideoTimeResult:)]) {
  271. [self.timeDelegate getCloudVideoTimeResult:msg->param1];
  272. }
  273. }
  274. break;
  275. #pragma mark 缩略图下载回调
  276. case EMSG_MC_DownloadMediaThumbnail:{
  277. NSString *path = @"";
  278. if (msg->param1 >=0) {
  279. //显示图片
  280. path = NSSTR(msg->szStr);
  281. }
  282. if ([self.delegate respondsToSelector:@selector(downloadSmallCloudThumbResult: path:)]) {
  283. [self.delegate downloadSmallCloudThumbResult:msg->param1 path:path];
  284. }
  285. }
  286. break;
  287. #pragma mark 文件开始下载回调(开始下载或者失败)
  288. case EMSG_ON_FILE_DOWNLOAD: {
  289. if ([self.delegate respondsToSelector:@selector(downloadCloudVideoStartResult:)]) {
  290. [self.delegate downloadCloudVideoStartResult:msg->param1];
  291. }
  292. }
  293. break;
  294. #pragma mark 下载进度
  295. case EMSG_ON_FILE_DLD_POS: {
  296. int download = msg->param2;
  297. int total = msg->param1;
  298. if ( total>0 ) {
  299. float progress = download/(float)total;
  300. if ([self.delegate respondsToSelector:@selector(downloadCloudVideoProgress:)]) {
  301. [self.delegate downloadCloudVideoProgress:progress];
  302. }
  303. }
  304. }
  305. break;
  306. #pragma mark 下载云视频完成
  307. case EMSG_ON_FILE_DLD_COMPLETE: {
  308. NSString *path = @"";
  309. if (msg->param1 >=0) {
  310. //显示图片
  311. path = NSSTR(msg->szStr);
  312. }
  313. if ([self.delegate respondsToSelector:@selector(downloadCloudVideoComplete:path:)]) {
  314. [self.delegate downloadCloudVideoComplete:msg->param1 path:path];
  315. }
  316. }
  317. break;
  318. default:
  319. break;
  320. }
  321. }
  322. -(NSString*)convertToJSONData:(id)infoDict {
  323. NSError *error;
  324. NSData *jsonData = [NSJSONSerialization dataWithJSONObject:infoDict
  325. options:NSJSONWritingPrettyPrinted
  326. error:&error];
  327. NSString *jsonString = @"";
  328. if (! jsonData) {
  329. NSLog(@"Got an error: %@", error);
  330. }else {
  331. jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  332. }
  333. jsonString = [jsonString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]]; //去除掉首尾的空白字符和换行字符
  334. [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
  335. return jsonString;
  336. }
  337. //存储到 _array_Video数组中
  338. -(void)createVideoDataWithType:(enum Video_Type)type andStartTime:(int)ss andEndTime:(int)es toArray:(NSMutableArray *)array {
  339. //开辟内存
  340. TimeInfo *info = [[TimeInfo alloc] init];
  341. info.type = type;
  342. info.start_Time = ss;
  343. info.end_Time = es;
  344. [array addObject:info];
  345. }
  346. @end