312 lines
11 KiB
Plaintext
312 lines
11 KiB
Plaintext
import photoAccessHelper from '@ohos.file.photoAccessHelper';
|
||
import dataSharePredicates from '@ohos.data.dataSharePredicates';
|
||
import { dateFormat, getCurrentTime } from '../common/utils/tools';
|
||
import record from '@ohos.rtsprecord';
|
||
import { FileHelper } from '../common/service/FileHelper';
|
||
import FileUtil from '../common/utils/File';
|
||
import { GlobalConfig } from '../config';
|
||
import promptAction from '@ohos.promptAction';
|
||
import common from '@ohos.app.ability.common';
|
||
import { IsDaysAgo } from './Common';
|
||
|
||
const rtsp_server = record.createServer();
|
||
//开始录屏
|
||
const FILE_ASSET_FETCH_COLUMNS = [
|
||
photoAccessHelper.PhotoKeys.URI,
|
||
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
|
||
];
|
||
|
||
// const rtsp_server = record.createServer();
|
||
|
||
export async function startRecordVideo(param, td, context, dir, path?, index?) {
|
||
return new Promise(async (reslove, reject) => {
|
||
// const date=dateFormat(new Date).split(' ')[0]
|
||
|
||
// const folderPath = await fileUtil.initFolder(`/PLC/${date}/${dir}`);
|
||
var video_uri = `rtsp://${param.userName}:${param.pwd}@${param.ip}:${param.port}/h264/ch${td}/main/av_stream`;
|
||
if (rtsp_server != null) {
|
||
console.log(`Rtsprecord startRecordVideo begin,video_uri`, video_uri);
|
||
const num = Math.floor(Math.random() * 10000)
|
||
let fileName
|
||
const time = await getCurrentTime()
|
||
const date = time.split(' ')[0]
|
||
if (!path) {
|
||
fileName = `${date}_movie_record${num}.mp4`
|
||
} else {
|
||
fileName = `${date}_${path}_${index || num}.mp4`
|
||
}
|
||
|
||
var recordResult = rtsp_server.startRecordVideo(context, video_uri, fileName, date, dir);
|
||
const handleId = recordResult.dataInt;
|
||
reslove(handleId)
|
||
} else {
|
||
console.log(`Rtsprecord startRecordVideo rtsp_server isnull`);
|
||
reslove(0)
|
||
}
|
||
console.log(`Rtsprecord startRecordVideo end`);
|
||
})
|
||
|
||
}
|
||
|
||
//结束录屏
|
||
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`);
|
||
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`);
|
||
}
|
||
}
|
||
|
||
}
|
||
|
||
|
||
export async function saveStartRecordVideo(path) {
|
||
return new Promise(async (reslove, reject) => {
|
||
const context = AppStorage.get('context')
|
||
const fileUtil = new FileUtil(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]) {
|
||
console.log('pathpath0', path)
|
||
|
||
record_handle[i] = await getfilehandleCode(i, param, date, path, i)
|
||
}
|
||
}
|
||
reslove(record_handle)
|
||
})
|
||
}
|
||
|
||
async function getfilehandleCode(td, param, dir, path, index) {
|
||
return new Promise(async (reslove, reject) => {
|
||
console.log('pathpath1', path)
|
||
const context = AppStorage.get('context')
|
||
|
||
const record_handle = await startRecordVideo(param, td, context, dir, path, index)
|
||
// this.rocordHandleObj['rocord_handle'+td] = record_handle
|
||
reslove(record_handle)
|
||
})
|
||
|
||
}
|
||
|
||
|
||
export async function getUserAlbumItemByDisplayName(displayName: string, day?,
|
||
type?): Promise<photoAccessHelper.Album> {
|
||
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 context = AppStorage.get('context')
|
||
const userFileMgr = photoAccessHelper.getPhotoAccessHelper(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();
|
||
console.log('get getUserAlbumItemByDisplayName album111', albums.albumName)
|
||
|
||
if (IsDaysAgo(albums.albumName, day)) {
|
||
deleteAllFileByPiC(albums.albumName, type)
|
||
}
|
||
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;
|
||
}
|
||
|
||
/**
|
||
*拍照
|
||
* @param param
|
||
* @param context
|
||
* @param type=0不获取base64
|
||
*/
|
||
interface takePhotoParam {
|
||
name?: string,
|
||
base64?: string,
|
||
fileSize?: number,
|
||
errorCode?: number
|
||
}
|
||
|
||
/**
|
||
*
|
||
* @param param
|
||
* @param context
|
||
* @param dir
|
||
* @param flag=1 //1同步调用 0异步线程调用
|
||
* @param callback
|
||
* @returns
|
||
*/
|
||
const fileHelper = new FileHelper();
|
||
|
||
export async function delPic(day: number, type: number) {
|
||
const context: common.UIAbilityContext = AppStorage.get('context')
|
||
let phAccessHelper = photoAccessHelper.getPhotoAccessHelper(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
|
||
// isSevenDaysAgo(albumName, day)
|
||
console.log('albumNamealbumName', albumName)
|
||
if (IsDaysAgo(albumName, day) && albumName != 'jt' && albumName != 'pz') {
|
||
deleteAllFileByPiC(albumName, type)
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
export async function takePhoto(param, context, dir, flag = 1, callback?) {
|
||
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);
|
||
// 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 })
|
||
});
|
||
} else {
|
||
return new Promise<takePhotoParam>(async (resolve, reject) => {
|
||
const time = await getCurrentTime()
|
||
const date = time.split(' ')[0]
|
||
let dirName = dir ? dir : date
|
||
rtsp_server.getVideoSnapshot(context, video_uri, fileName, dirName, true, async (err, snapResult) => {
|
||
if (snapResult.result && snapResult.errorCode == 0) {
|
||
resolve({
|
||
base64: snapResult.dataString,
|
||
name: snapResult.fileName,
|
||
fileSize: snapResult.fileSize,
|
||
errorCode: snapResult.errorCode
|
||
})
|
||
} else {
|
||
promptAction.showToast({
|
||
message: `拍照失败`,
|
||
duration: 3000
|
||
});
|
||
reject(false)
|
||
// reject()
|
||
}
|
||
|
||
});
|
||
})
|
||
}
|
||
}
|
||
|
||
export async function deleteAllPicturesFn() {
|
||
fileHelper.deleteAllPictures();
|
||
const context = AppStorage.get('context')
|
||
deleteAllVideos(context, photoAccessHelper.AlbumType.USER, photoAccessHelper.AlbumSubtype.VIDEO)
|
||
|
||
}
|
||
|
||
//type:1 是图片 2是视频
|
||
export async function deleteAllFileByPiC(dirName, type = 1) {
|
||
// const fileHelper = new FileHelper();
|
||
fileHelper.deleteFileOfAlbum(dirName, type);
|
||
}
|
||
|
||
export async function deleteAllVideos(context, type: photoAccessHelper.AlbumType,
|
||
subType: photoAccessHelper.AlbumSubtype): Promise<void> {
|
||
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);
|
||
|
||
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();
|
||
}
|
||
}
|
||
} |