501 lines
15 KiB
TypeScript
Raw Normal View History

2024-02-22 10:40:35 +08:00
// @ts-nocheck
import promptAction from '@ohos.promptAction'
2024-08-05 09:47:44 +08:00
import router from '@ohos.router'
import { dateFormat ,getCurrentTime} from '../utils/tools'
2024-02-22 10:40:35 +08:00
import FileUtil from '../../common/utils/File'
2024-10-28 13:47:03 +08:00
import { takePhoto, deleteAllFileByPiC } from '../../common/service/videoService'
2024-12-29 21:43:12 +08:00
// import { VideoConfigData } from '../../mock';
import request from '@ohos.request'
import Prompt from '@system.prompt';
2025-07-22 18:43:54 +08:00
import util from '@ohos.util';
2024-05-09 13:42:56 +08:00
2024-02-22 10:40:35 +08:00
import {
2024-07-29 09:51:49 +08:00
delSyncTable,
2024-05-09 13:42:56 +08:00
getDataBaseTable,
getMySystemSetTable,
2024-07-29 09:51:49 +08:00
upDataZhongxinginitialization,
2025-07-22 18:43:54 +08:00
getSyncData,
2024-07-29 09:51:49 +08:00
upDateTable
2024-02-22 10:40:35 +08:00
} from '../../common/service/initable'
2024-05-09 13:42:56 +08:00
import { GlobalConfig } from '../../config/index'
2024-05-16 09:53:10 +08:00
import testNapi from '@ohos.hiserialsdk'
2024-08-20 09:12:02 +08:00
import fs from '@ohos.file.fs';
import zlib from '@ohos.zlib';
2024-07-29 09:51:49 +08:00
let num = 0
2024-12-29 21:43:12 +08:00
async function getliushuiNum(data) {
// let data = AppStorage.get('liushuiNo')
if (data) {
num = Number(data) + 1
2024-07-29 09:51:49 +08:00
let str = num.toString()
for (let i = 0; str.length <= 5; i++) {
str = '0' + str
2024-05-09 13:42:56 +08:00
}
2024-07-29 09:51:49 +08:00
globalThis.lshNo = str
} else {
2024-12-29 21:43:12 +08:00
num = 0
2024-07-29 09:51:49 +08:00
let str = num.toString()
for (let i = 0; str.length <= 5; i++) {
str = '0' + str
2024-05-09 13:42:56 +08:00
}
2024-07-29 09:51:49 +08:00
globalThis.lshNo = str
2024-05-09 13:42:56 +08:00
}
2024-12-29 21:43:12 +08:00
2024-05-09 13:42:56 +08:00
}
2024-10-28 13:47:03 +08:00
const TAG = '[UPLOAD]'
export async function uploadLogFile() {
if(globalThis.upload){
return
}
globalThis.upload = true
Prompt.showToast({
message: '正在上传考车日志,请耐心等待...',
duration: 3000
});
const filename = await getCopyFiles()
const filePath = globalThis.context.cacheDir + '/' + filename
let stat = fs.statSync(filePath)
console.log(TAG,'上传压缩包文件大小:' + (Math.ceil(stat.size / 1024 / 1024)) + 'M')
// const currentFilename = globalThis.carInfo.plateNo + '_' + await getCurrentTime(1)+ '_log.zip';
// console.log(TAG,'upload log currentFilename=>' ,currentFilename )xxx
let uploadConfig: request.UploadConfig = {
header: { 'Accept': '*/*', 'Content-Type': 'multipart/form-data' },
url: `${globalThis.host}/der2/services/upload?fileName=${filename}`,
files: [{ filename, name: filename, uri: `internal://cache/${filename}`, type: "zip" }],
data: [{ name: filename, value: filename }],
}
request.uploadFile(globalThis.context, uploadConfig).then(data =>{
let uploadTask: request.UploadTask = data;
console.log(TAG,'start upload')
const callback = (size, tot) => {
console.log(TAG + '文件上传中', "进度" + size + '/' + tot)
}
//on("progress") 存在bug
let timer = setInterval(()=>{
uploadTask.on("progress", callback)
uploadTask.off('progress', callback)
},1000)
uploadTask.on('complete', () => {
console.log(TAG + '文件上传完成')
clearInterval(timer)
globalThis.upload = false
fs.rmdirSync(filePath);
Prompt.showToast({
message: '考车日志上传成功',
duration: 3000
});
})
uploadTask.on("fail", (taskInfo) => {
console.log(TAG + '文件上传失败' ,JSON.stringify(taskInfo))
Prompt.showToast({
message: '考车日志上传失败',
duration: 3000
});
clearInterval(timer)
globalThis.upload = false
})
uploadTask.on("headerReceive", header => {
console.log(TAG + 'headerReceive=>',JSON.stringify(header))
})
})
}
// 将要上传的文件拷贝到缓存目录并压缩
async function getCopyFiles() {
const absolutePath = '/mnt/hmdfs/100/account/device_view/local/files/duolun/logs'
const tempPath = globalThis.context.cacheDir + '/temp';
if (!fs.accessSync(tempPath)) {
fs.mkdirSync(tempPath)
}
let listFileOption = {
recursion: true,
listNum: 0,
filter: {suffix: [".txt"],}
};
const list = fs.listFileSync(absolutePath,listFileOption);
console.log(TAG,'开始备份log文件,数量:' + list.length)
list.forEach((folder=>{
const folders = folder.split('/');
let path = tempPath
folders.forEach(((item,index)=>{
if(index !== 0 && index !== folders.length-1){
path += `/${item}`;
if (!fs.accessSync(path)) {
fs.mkdirSync(path);
}
}
}))
fs.copyFileSync(absolutePath + folder, tempPath + folder)
}))
console.log(TAG,'log文件备份完成')
const time = await getCurrentTime();
const nowDate = time.split(' ')[0].split('-').join('')
const filenames = await fs.listFile('/data/log/hilog');
for (let i = 0; i < filenames.length; i++) {
if (filenames[i].split('.')[0] == 'hilog') {
const date = filenames[i].split('.')[2].split('-')[0]
if (date == nowDate) {
fs.copyFileSync('/data/log/hilog/' + filenames[i], globalThis.context.cacheDir + '/temp/' + filenames[i])
}
}
}
console.log(TAG,'开始压缩文件')
const currentFilename = globalThis.carInfo.carNo + '_' + await getCurrentTime(1)+ '.zip';
await compressFile(tempPath, globalThis.context.cacheDir + '/' + currentFilename)
console.log(TAG,'压缩完成')
fs.rmdirSync(globalThis.context.cacheDir + '/temp');
return currentFilename
}
// 打包当天日志文件
async function compressCurrentLogs() {
const time = await getCurrentTime();
const nowDate = time.split(' ')[0].split('-').join('')
const filenames = await fs.listFile('/data/storage/100/local/files/duolun/logs');
let options: zlib.Options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
for (let i = 0; i < filenames.length; i++) {
if (filenames[i].split('.')[0] == 'hilog') {
const date = filenames[i].split('.')[2].split('-')[0]
if (date == nowDate) {
fs.copyFileSync('/data/log/hilog/' + filenames[i], globalThis.context.cacheDir + '/temp/' + filenames[i])
}
}
}
console.log('upload log copy files success')
}
export async function compressFile(inFile, outFile) {
let options: zlib.Options = {
level: zlib.CompressLevel.COMPRESS_LEVEL_DEFAULT_COMPRESSION,
memLevel: zlib.MemLevel.MEM_LEVEL_DEFAULT,
strategy: zlib.CompressStrategy.COMPRESS_STRATEGY_DEFAULT_STRATEGY
};
return zlib.compressFile(inFile, outFile, options)
}
2024-10-28 13:47:03 +08:00
export async function deleteAllFIleLog(path) {
2024-08-20 09:12:02 +08:00
let options = {
"recursion": false,
"listNum": 0,
};
2024-08-29 08:10:20 +08:00
2024-08-21 11:57:03 +08:00
let filenames = fs.listFileSync(path, options);
2024-10-28 13:47:03 +08:00
console.info("listFile succeed", JSON.stringify(filenames));
2024-08-20 09:12:02 +08:00
for (let i = 0; i < filenames.length; i++) {
2024-10-28 13:47:03 +08:00
console.info("listFile succeed", JSON.stringify(filenames[i]));
2024-08-22 15:24:15 +08:00
2024-10-28 13:47:03 +08:00
if (isSevenDaysAgo(filenames[i])) {
fs.rmdir(path + filenames[i], (err) => {
2024-08-20 09:12:02 +08:00
if (err) {
console.error("rmdir failed with error message: " + err.message + ", error code: " + err.code);
} else {
console.info("rmdir succeed");
}
});
}
// console.info("filename: %s", filenames[i]);
}
2024-07-29 09:51:49 +08:00
2024-08-20 09:12:02 +08:00
}
2024-08-29 11:04:10 +08:00
2024-10-28 13:47:03 +08:00
2024-12-29 21:43:12 +08:00
function isSevenDaysAgo(date, saveDays?) {
2024-08-20 09:12:02 +08:00
const today = new Date(); // 当前日期
const target = new Date(date); // 需要判断的日期
2024-10-28 13:47:03 +08:00
console.info("listFile succeed1", JSON.stringify(target));
2024-08-20 09:12:02 +08:00
const diff = today.getTime() - target.getTime(); // 计算两个日期之间的毫秒数差异
const diffDays = diff / (1000 * 60 * 60 * 24); // 将毫秒转换为天数
2024-10-28 13:47:03 +08:00
console.info("listFile succeed2", (diffDays));
// 如果差异天数正好是2则原日期是当前日期的前2天
2024-12-29 21:43:12 +08:00
return diffDays > (saveDays || 2);
2024-08-20 09:12:02 +08:00
}
2024-10-28 13:47:03 +08:00
export async function setVideoParam() {
2024-12-29 21:43:12 +08:00
// return
// const fileUtil = new FileUtil(context)
// console.log('configconfig',)
// try {
// const data = await this.fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/config3.txt');
//
// } catch (error) {
// const param: VideoConfig = VideoConfigData
// const folderPath = await fileUtil.initFolder(`/config`);
// fileUtil.addFile(`${folderPath}/config3.txt`, JSON.stringify(param))
// }
2024-10-28 13:47:03 +08:00
}
2024-12-29 21:43:12 +08:00
let fileFd
2025-01-03 17:24:11 +08:00
2024-02-22 10:40:35 +08:00
//配置流水号
2024-12-29 21:43:12 +08:00
export async function setliushuiNum() {
const data = AppStorage.get('liushuiNo')
const liushuiDate = AppStorage.get('liushuiDate')
getliushuiNum(data)
2025-01-20 08:50:40 +08:00
if (data && liushuiDate) {
2024-05-09 13:42:56 +08:00
const date = new Date()
2024-12-29 21:43:12 +08:00
const time1 = liushuiDate.split(' ')[0]
2024-05-09 13:42:56 +08:00
const time2 = dateFormat(date).split(' ')[0]
2024-12-29 21:43:12 +08:00
if (time1 != time2) {
AppStorage.setOrCreate('liushuiDate', dateFormat(date));
AppStorage.setOrCreate('liushuiNo', '0');
2025-01-20 08:50:40 +08:00
} else {
2024-12-29 21:43:12 +08:00
AppStorage.setOrCreate('liushuiNo', Number(data) + 1);
2024-05-09 13:42:56 +08:00
}
2024-12-29 21:43:12 +08:00
} else {
2024-02-22 10:40:35 +08:00
const date = new Date()
2024-12-29 21:43:12 +08:00
// PersistentStorage.PersistProp('liushuiNo', 0);
// PersistentStorage.PersistProp('liushuiDate', dateFormat(date));
AppStorage.setOrCreate('liushuiNo', '0');
AppStorage.setOrCreate('liushuiDate', dateFormat(date));
2024-05-09 13:42:56 +08:00
}
2024-02-22 10:40:35 +08:00
}
//双中心表
2024-06-27 20:53:36 +08:00
export async function getDoubleCeneterTable(param) {
2024-05-09 13:42:56 +08:00
return new Promise(async (reslove, reject) => {
// MA_SYSSET双中心下做合并表格操作
2024-07-29 14:29:32 +08:00
delSyncTable('MA_SYSSET', param.context).then(async () => {
await getDataBaseTable({ tableName: 'MA_SYSSET' }, param)
await getMySystemSetTable({ tableName: 'MA_SYSSET' }, param)
})
2024-05-09 13:42:56 +08:00
const typeObj = {
'MA_MARKRULE': false,
'MA_SYSTEMPARM': false,
'MA_MAP_COLLECT': false,
'MA_MAP_COLLECT_SHAPE': false,
'MA_MAP_ITEMCLASS': false,
'MA_MAP_POINT': false,
'MA_MAP_POINT_ITEM': false,
'MA_MAP_ROAD': false,
'MA_MAP_ROAD_LANE': false,
'MA_MAP_SUBITEM': false,
'ES_CARINFO': false,
'ES_EXAMPOINTDETAIL': false,
'MA_MARKRULESET': false,
'ES_CAR_VIDEO_PARAMETER': false,
'MA_CDSBINFO': false,
'MA_ITEMINFO': false,
'MA_T_CARPARMSET': false
}
for (let key in typeObj) {
2024-08-05 09:47:44 +08:00
typeObj[key] = await upDateTable({ tableName: key }, param)
2024-05-09 13:42:56 +08:00
if (!typeObj[key]) {
promptAction.showToast({
message: `未能查询到${key}表数据, 请先检查网络是否连接正常`,
duration: 3000
});
reslove(false)
return
}
}
reslove(true)
2024-08-05 09:47:44 +08:00
router.pushUrl({
url: 'pages/ExaminerLogin',
}, router.RouterMode.Single);
2024-05-09 13:42:56 +08:00
})
2024-02-22 10:40:35 +08:00
}
2024-07-29 09:51:49 +08:00
2024-02-22 10:40:35 +08:00
//单中心存表
2024-06-27 20:53:36 +08:00
export async function getSingleCenterTable(param) {
2025-01-20 08:50:40 +08:00
return new Promise((resolve, reject) => {
console.log('teststet0')
upDataZhongxinginitialization(param).then((result) => {
if (result === true) {
2025-01-20 08:50:40 +08:00
resolve(true)
} else {
console.log('联网更新失败,请检查网络后重新更新')
resolve(result)
2025-01-20 08:50:40 +08:00
}
}).catch((error) => {
resolve(false)
console.log(error)
})
2024-05-09 13:42:56 +08:00
})
2024-03-12 15:32:48 +08:00
}
2024-05-09 13:42:56 +08:00
2024-06-27 20:53:36 +08:00
2024-05-09 13:42:56 +08:00
let interval
export async function takePhotoFn(context) {
let param = {
videoNum: '1',
spls: '1',
wz: '0,0',
faceFlag: false,
shuiying: true,
pztd: '2',
ljlx: '',
ip: '192.168.36.94',
port: '554',
userName: 'admin',
pwd: '12345qwe',
td1: '1',
td2: '2',
td3: '3',
td4: '4',
videoRecord1: false,
videoRecord2: true,
videoRecord3: false,
videoRecord4: false,
text1: '',
text2: '',
text3: '',
dolt: '',
fontSize: '',
rlls: '1',
spzd4: false,
spzd3: false,
spzd2: false,
spzd1: false,
zdyz: '5',
}
2024-07-29 09:51:49 +08:00
globalThis.spzd = {
spzd1: false,
spzd2: false,
spzd3: false,
spzd4: false,
2024-05-09 13:42:56 +08:00
}
globalThis.takePhotoNum = 0
const map = {}
const fileUtil = new FileUtil(context)
const fileData = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/config3.txt');
console.log('sizesize', fileData)
param = JSON.parse(fileData)
console.log('paramparam')
clearInterval(interval)
2024-05-16 09:53:10 +08:00
interval = setTimeout(async () => {
2024-05-09 13:42:56 +08:00
const arr = ['spzd1', 'spzd2', 'spzd3', 'spzd4']
for (let key in map) {
map[key] = false
// param[key] = false
}
2024-07-29 09:51:49 +08:00
for (let i = 0; i <= arr.length - 1; i++) {
2024-08-08 18:09:06 +08:00
let key1 = arr[i]
if (param[key1]) {
2024-05-09 13:42:56 +08:00
param.pztd = param[`td${Number(i) + 1}`]
2024-10-28 13:47:03 +08:00
takePhoto(param, context, 'jt/', 0, (data) => {
if (data.errorCode != 0) {
console.log('拍照失败')
return
}
2024-12-29 21:43:12 +08:00
console.log('takePhototakePhoto', JSON.stringify(data))
const zdyz = globalThis.param854Str ? Number(globalThis.param854Str) : Number(param.zdyz)
console.log('takePhototakePhoto', JSON.stringify(zdyz),)
2024-12-24 16:43:59 +08:00
2024-10-28 17:11:22 +08:00
if (Number(data.fileSize) <= (zdyz * 1000)) {
2024-08-08 18:09:06 +08:00
map[key1] = true
2024-05-09 13:42:56 +08:00
promptAction.showToast({
message: `视频遮挡`,
duration: 3000
});
2024-08-08 18:09:06 +08:00
globalThis.spzd[key1] = true
2024-05-09 13:42:56 +08:00
} else {
2024-08-08 18:09:06 +08:00
map[key1] = false
globalThis.spzd[key1] = false
2024-05-09 13:42:56 +08:00
}
2024-07-29 09:51:49 +08:00
setTimeout(() => {
2024-05-16 09:53:10 +08:00
takePhotoFn()
2024-07-29 09:51:49 +08:00
}, 3000)
2024-05-09 13:42:56 +08:00
})
2024-08-08 18:09:06 +08:00
2024-05-09 13:42:56 +08:00
}
}
globalThis.takePhotoNum++
2024-05-16 09:53:10 +08:00
if (globalThis.takePhotoNum >= 20) {
2024-05-09 13:42:56 +08:00
globalThis.takePhotoNum = 0
2024-05-16 09:53:10 +08:00
// deleteAllFileByPiC('jt')
2024-05-09 13:42:56 +08:00
}
}, 3000)
2024-05-16 09:53:10 +08:00
}
2024-07-29 09:51:49 +08:00
2024-05-16 09:53:10 +08:00
let fd
2024-07-29 09:51:49 +08:00
const devPath = "/dev/ttyS3"
2024-05-30 15:52:03 +08:00
function openChuankouFn(callback) {
2025-06-28 09:54:28 +08:00
getSyncData('ES_CARINFO').then(result => {
const carInfo = result[0] || {};
globalThis.carInfo.kscx = carInfo.kscx
console.info('srj ES_CARINFO=>',JSON.stringify(carInfo))
if(carInfo.kscx == 'C1'){
testNapi.SerialOpenAsync(devPath, (fd) => {
globalThis.fd = fd;
globalThis.num = 0
let parity = 0x4e; // 'N'
let ret = testNapi.SerialSetAsync(globalThis.fd, 115200, 0, 8, 1, parity, (ret) => {
callback()
});
2025-06-18 14:21:05 +08:00
});
2025-06-28 09:54:28 +08:00
}
})
2024-05-16 09:53:10 +08:00
}
2024-05-27 17:25:20 +08:00
2024-05-16 09:53:10 +08:00
function getChuankouFnMsg() {
let timeout = 50000; // 2秒超时
let databuff = [0x61, 0xAA, 0x0A, 0X15, 0X00]; // send ABCDE
2024-07-29 09:51:49 +08:00
testNapi.SerialSendAsync(globalThis.fd, databuff, (ret) => {
2024-05-29 10:13:37 +08:00
testNapi.SerialRecvAsync(globalThis.fd, timeout, (revTestInfo) => {
const message = revTestInfo?.recevedBuf?.toString()
2025-07-22 18:43:54 +08:00
let msgBuf = util.TextDecoder.create().decodeWithStream(new Uint8Array(revTestInfo?.recevedBuf))
if (message == '' || msgBuf == '') {
2024-07-29 09:51:49 +08:00
globalThis.num = 1
2024-05-29 10:13:37 +08:00
// clearInterval(chuankou)
testNapi.SerialClose(globalThis.fd);
2024-07-29 09:51:49 +08:00
globalThis.fd = null
2025-01-20 08:50:40 +08:00
setTimeout(() => {
2025-01-12 19:44:59 +08:00
getChuankouFn()
2025-01-20 08:50:40 +08:00
}, 2000)
2024-05-29 10:13:37 +08:00
return
}
const msg = message?.split(',')
2025-07-22 18:43:54 +08:00
let dang = Number(msgBuf?.split(",")?.[1]?.split('\r')[0] || 0)
globalThis.chuankoMsg =( msg[9] === '' || msg[9] >10 ) ? dang: msg[9]
2024-07-29 09:51:49 +08:00
setTimeout(() => {
2024-05-29 10:13:37 +08:00
getChuankouFnMsg()
2024-07-29 09:51:49 +08:00
}, 500)
2024-05-29 10:13:37 +08:00
// hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync callback in');
// hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedLen = %{public}d', revTestInfo.recevedLen);
// hilog.info(0x0000, 'testTag', 'Test NAPI SerialRecvAsync recevedBuf = %{public}s', revTestInfo.recevedBuf.toString());
});
});
// let revTestInfo = testNapi?.SerialRecv(globalThis.fd, timeout);
2024-05-16 09:53:10 +08:00
}
2024-07-29 09:51:49 +08:00
2024-05-27 17:25:20 +08:00
let chuankou
2024-07-29 09:51:49 +08:00
export async function getChuankouFn() {
if (globalThis.fd) {
2024-05-16 09:53:10 +08:00
return
}
2024-05-30 15:52:03 +08:00
openChuankouFn(getChuankouFnMsg)
2024-05-29 10:13:37 +08:00
// clearInterval(chuankou)
// chuankou=setInterval(()=>{
2024-05-30 15:52:03 +08:00
// getChuankouFnMsg()
2024-05-29 10:13:37 +08:00
// },1000)
2024-05-16 09:53:10 +08:00
2024-05-09 13:42:56 +08:00
}