305 lines
11 KiB
TypeScript
Raw Normal View History

2024-02-22 10:40:35 +08:00
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
import onvifclient from '@ohos.onvifclient';
import fs from '@ohos.file.fs'
2024-03-12 15:32:48 +08:00
// @ts-ignore
import photoAccessHelper from '@ohos.file.photoAccessHelper'
import dataSharePredicates from '@ohos.data.dataSharePredicates'
2024-08-29 08:10:20 +08:00
import { dateFormat, getCurrentTime, isSevenDaysAgo } from '../utils/tools'
2024-03-12 15:32:48 +08:00
// import rtsp_server from '@ohos.rtsprecord';
2024-02-26 15:01:27 +08:00
import record from '@ohos.rtsprecord';
2024-05-09 13:42:56 +08:00
import { FileHelper } from './FileHelper';
2024-08-29 08:10:20 +08:00
import FileUtil from '../utils/File';
import { GlobalConfig } from '../../config';
2024-05-09 13:42:56 +08:00
2024-06-27 20:53:36 +08:00
const rtsp_server = record.createServer();
2024-02-22 10:40:35 +08:00
//开始录屏
2024-03-12 15:32:48 +08:00
const FILE_ASSET_FETCH_COLUMNS = [photoAccessHelper.PhotoKeys.URI,
2024-08-29 08:10:20 +08:00
photoAccessHelper.PhotoKeys.PHOTO_TYPE,
photoAccessHelper.PhotoKeys.DISPLAY_NAME,
photoAccessHelper.PhotoKeys.SIZE,
photoAccessHelper.PhotoKeys.DATE_ADDED,
photoAccessHelper.PhotoKeys.DATE_MODIFIED,
photoAccessHelper.PhotoKeys.DURATION,
photoAccessHelper.PhotoKeys.WIDTH,
photoAccessHelper.PhotoKeys.HEIGHT,
photoAccessHelper.PhotoKeys.DATE_TAKEN,
photoAccessHelper.PhotoKeys.ORIENTATION,
photoAccessHelper.PhotoKeys.FAVORITE,
photoAccessHelper.PhotoKeys.TITLE,
photoAccessHelper.PhotoKeys.POSITION,
photoAccessHelper.PhotoKeys.DATE_TRASHED,
photoAccessHelper.PhotoKeys.HIDDEN];
2024-06-27 20:53:36 +08:00
// const rtsp_server = record.createServer();
2024-03-12 15:32:48 +08:00
2024-12-24 16:43:59 +08:00
export async function startRecordVideo(param, td, context, dir, path?, index?) {
2024-08-29 08:10:20 +08:00
return new Promise(async (reslove, reject) => {
// const fileUtil = new FileUtil(globalThis.context)
// const date=dateFormat(new Date).split(' ')[0]
// const folderPath = await fileUtil.initFolder(`/PLC/${date}/${dir}`);
2024-02-22 10:40:35 +08:00
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${td}/main/av_stream`;
2024-02-26 15:01:27 +08:00
if (rtsp_server != null) {
2024-03-12 15:32:48 +08:00
console.log(`Rtsprecord startRecordVideo begin,video_uri`, video_uri);
const num = Math.floor(Math.random() * 10000)
2024-08-29 08:10:20 +08:00
let fileName
if (!path) {
fileName = `movie_record${num}.mp4`
} else {
2024-12-24 16:43:59 +08:00
fileName = `${path}_${index || num}.mp4`
2024-08-29 08:10:20 +08:00
}
2024-03-12 15:32:48 +08:00
// @ts-ignore
var recordResult = rtsp_server.startRecordVideo(context, video_uri, fileName, dir);
2024-02-26 15:01:27 +08:00
const handleId = recordResult.dataInt;
reslove(handleId)
}
else {
console.log(`Rtsprecord startRecordVideo rtsp_server isnull`);
reslove(0)
}
console.log(`Rtsprecord startRecordVideo end`);
2024-02-22 10:40:35 +08:00
})
}
//结束录屏
2024-08-29 08:10:20 +08:00
export async function endRecordVideo(record_handleObj) {
for (let key in record_handleObj) {
if (rtsp_server != null && record_handleObj[key] > 0) {
console.log(`Rtsprecord endRecordVideo begin`);
// @ts-ignore
var recordResult = rtsp_server.endRecordVideo(record_handleObj[key]);
console.log(`Rtsprecord endRecordVideo record_handle` + record_handleObj[key].dataInt + ` filename:` + record_handleObj[key].fileName);
}
else {
console.log(`Rtsprecord endRecordVideo handleId isnull`);
}
2024-02-22 10:40:35 +08:00
}
}
2024-08-29 08:10:20 +08:00
export async function saveStartRecordVideo(path) {
return new Promise(async (reslove, reject) => {
const fileUtil = new FileUtil(globalThis.context)
const fileHelper = new FileHelper();
const date = dateFormat(new Date).split(' ')[0]
fileHelper.createAlbum(date);
// const folderPath = await fileUtil.initFolder(`/${path}/${date}`);
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/config3.txt');
const param = JSON.parse(data)
const record_handle = {
1: 0,
2: 1,
3: 2,
4: 3
}
for (let i = 1; i <= 4; i++) {
if (param['videoRecord'+i]) {
2024-12-24 16:43:59 +08:00
console.log('pathpath0', path)
2024-08-29 08:10:20 +08:00
2024-12-24 16:43:59 +08:00
record_handle[i] = await getfilehandleCode(i, param, date, path, i)
2024-08-29 08:10:20 +08:00
}
}
reslove(record_handle)
})
}
2024-12-24 16:43:59 +08:00
async function getfilehandleCode(td, param, dir, path, index) {
2024-08-29 08:10:20 +08:00
return new Promise(async (reslove, reject) => {
2024-12-24 16:43:59 +08:00
console.log('pathpath1', path)
2024-08-29 08:10:20 +08:00
2024-12-24 16:43:59 +08:00
const record_handle = await startRecordVideo(param, td, globalThis.context, dir, path, index)
2024-08-29 08:10:20 +08:00
// this.rocordHandleObj['rocord_handle'+td] = record_handle
reslove(record_handle)
})
}
2024-12-24 16:43:59 +08:00
export async function getUserAlbumItemByDisplayName(displayName: string, day?, type?): Promise<photoAccessHelper.Album> {
2024-08-29 08:10:20 +08:00
let fetchResult = null
let album: photoAccessHelper.Album = null
try {
console.log('getUserAlbumItemByDisplayName');
let predicates = new dataSharePredicates.DataSharePredicates();
predicates.equalTo(photoAccessHelper.AlbumKeys.ALBUM_NAME, displayName)
let fetchOptions = {
fetchColumns: [],
predicates: predicates
};
const userFileMgr = photoAccessHelper.getPhotoAccessHelper(globalThis.context);
fetchResult = await userFileMgr.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, fetchOptions);
console.log('get getUserAlbumItemByDisplayName, count: ' + JSON.stringify(fetchResult));
if (fetchResult.getCount() > 0) {
const albums = await fetchResult.getLastObject();
2024-12-24 16:43:59 +08:00
console.log('get getUserAlbumItemByDisplayName album111', albums.albumName)
2024-09-05 08:56:01 +08:00
2024-12-24 16:43:59 +08:00
if (isSevenDaysAgo(albums.albumName, day)) {
deleteAllFileByPiC(albums.albumName, type)
2024-08-29 08:10:20 +08:00
}
console.log('get getUserAlbumItemByDisplayName album',)
}
} catch (err) {
console.log('get Album getUserAlbumItemByDisplayName fetchResult failed with err: ' + err);
} finally {
if (fetchResult != null) {
fetchResult.close();
}
}
return album;
}
2024-02-22 10:40:35 +08:00
/**
*
* @param param
* @param context
* @param type=0base64
*/
2024-08-29 08:10:20 +08:00
interface takePhotoParam {
name?: string,
base64?: string,
fileSize?: number,
errorCode?: number
2024-05-09 13:42:56 +08:00
}
2024-08-29 08:10:20 +08:00
2024-05-09 13:42:56 +08:00
/**
*
* @param param
* @param context
* @param dir
* @param flag=1 //1同步调用 0异步线程调用
* @param callback
* @returns
*/
2024-05-16 09:53:10 +08:00
const fileHelper = new FileHelper();
2024-08-29 08:10:20 +08:00
2024-12-24 16:43:59 +08:00
export async function delPic(day,type) {
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(globalThis.context);
console.info('albumGetAssetsDemoCallback');
let predicates: dataSharePredicates.DataSharePredicates = new dataSharePredicates.DataSharePredicates();
let albumFetchOptions: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
let fetchOption: photoAccessHelper.FetchOptions = {
fetchColumns: [],
predicates: predicates
};
let albumList: photoAccessHelper.FetchResult<photoAccessHelper.Album> = await phAccessHelper.getAlbums(photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.USER_GENERIC, albumFetchOptions);
let albums = await albumList.getAllObjects();
for(let i=0;i<=albums.length;i++){
let albumName=albums[i].albumName
console.log('albumNamealbumName',albumName)
if (isSevenDaysAgo(albumName, day)&&albumName!='jt'&&albumName!='pz') {
deleteAllFileByPiC(albums.albumName, type)
}
}
return
}
2024-08-29 08:10:20 +08:00
2024-12-24 16:43:59 +08:00
export async function takePhoto(param, context, dir, flag = 1, callback?) {
2024-08-29 08:10:20 +08:00
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${param.pztd}/main/av_stream`;
// var video_uri = `rtsp://admin:openharmony1@192.168.1.66:554/Streaming/Channels/3`;
const num = Math.floor(Math.random() * 10000)
const fileName = `picture_record${num}.jpg`
console.log('baoyihubaoyihu', video_uri, flag)
console.log(`baoyihu Rtsprecord baohaowen getVideoSnapshot fileName` + fileName);
// @ts-ignore
// var snapResult = rtsp_server.getVideoSnapshot(context, video_uri, '', dir);
if (flag == 0) {
rtsp_server.detectVideoSnapshotSize(video_uri, fileName, (err, snapResult) => {
console.log("baohaowen_detectLoop round end size1:" + snapResult.fileSize);
callback({ fileSize: snapResult.fileSize, errorCode: snapResult.errorCode })
2024-05-09 13:42:56 +08:00
});
2024-05-16 09:53:10 +08:00
}
2024-08-29 08:10:20 +08:00
else {
console.log('baoyihubaoyihu1', video_uri, flag)
2024-10-28 13:47:03 +08:00
return new Promise<takePhotoParam>(async (resolve, reject) => {
const time = await getCurrentTime()
const date = time.split(' ')[0]
2024-12-24 16:43:59 +08:00
await fileHelper.createAlbum(date)
rtsp_server.getVideoSnapshot(context, video_uri, fileName, '/jt', true, (err, snapResult) => {
2024-08-29 08:10:20 +08:00
console.log('baoyihubaoyihu', video_uri, flag, JSON.stringify(snapResult))
console.log('getPhtot', JSON.stringify(snapResult))
2024-12-24 16:43:59 +08:00
if(snapResult.result&&snapResult.errorCode==0){
resolve({
base64: snapResult.dataString,
name: snapResult.fileName,
fileSize: snapResult.fileSize,
errorCode: snapResult.errorCode
})
}else{
takePhoto(param, context, dir, flag, callback)
// reject()
}
2024-05-09 13:42:56 +08:00
});
})
}
2024-05-16 09:53:10 +08:00
console.log('getmyLog03')
2024-03-12 15:32:48 +08:00
2024-02-22 10:40:35 +08:00
}
2024-08-29 08:10:20 +08:00
2024-05-27 17:25:20 +08:00
//type:1 是图片 2是视频
2024-08-29 08:10:20 +08:00
export async function deleteAllFileByPiC(dirName, type = 1) {
2024-05-16 09:53:10 +08:00
// const fileHelper = new FileHelper();
2024-08-29 08:10:20 +08:00
fileHelper.deleteFileOfAlbum(dirName, type);
2024-02-26 15:01:27 +08:00
}
2024-08-29 08:10:20 +08:00
export async function deleteAllVideos(context, type: photoAccessHelper.AlbumType, subType: photoAccessHelper.AlbumSubtype): Promise<void> {
2024-03-12 15:32:48 +08:00
let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album> = null;
try {
const userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
fetchResult = await userFileMgr.getAlbums(type, subType);
console.log('baoyihu get Album fetchResult, count: ' + fetchResult.getCount());
for (let i = 0; i < fetchResult.getCount(); i++) {
let albumAsset: photoAccessHelper.Album = await fetchResult.getObjectByPosition(i);
let photoFetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> = null;
let count = 0;
try {
let predicates = new dataSharePredicates.DataSharePredicates();
let fetchOptions = {
fetchColumns: FILE_ASSET_FETCH_COLUMNS,
predicates: predicates
};
photoFetchResult = await albumAsset.getAssets(fetchOptions);
count = photoFetchResult.getCount();
console.log('baoyihu photoFetchResult count: ' + count);
var all_fileAsset = await photoFetchResult.getAllObjects();
var uri_array = []
console.log('baoyihu batch delete begin');
for (let onfile of all_fileAsset) {
console.log('baoyihu push one uri : ' + onfile.uri);
uri_array.push(onfile.uri);
}
await userFileMgr.deleteAssets(uri_array);
2024-02-22 10:40:35 +08:00
2024-03-12 15:32:48 +08:00
console.log('baoyihu batch delete end');
} catch (err) {
console.log('baoyihu get Album getPhotoAssets failed with err: ' + err);
} finally {
if (photoFetchResult != null) {
photoFetchResult.close();
}
}
}
} catch (err) {
console.log('baoyihu get Album fetchResult failed with err: ' + err);
} finally {
if (fetchResult != null) {
fetchResult.close();
}
}
}