447 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			447 lines
		
	
	
		
			13 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| // @ts-nocheck
 | ||
| import promptAction from '@ohos.promptAction'
 | ||
| import router from '@ohos.router'
 | ||
| import { dateFormat, getCurrentTime } from '../utils/tools'
 | ||
| import FileUtil from '../../common/utils/File'
 | ||
| import { takePhoto, deleteAllFileByPiC } from '../../common/service/videoService'
 | ||
| // import { VideoConfigData } from '../../mock';
 | ||
| import fs from '@ohos.file.fs'
 | ||
| import request from '@ohos.request'
 | ||
| import zlib from '@ohos.zlib';
 | ||
| 
 | ||
| import {
 | ||
|   delSyncTable,
 | ||
|   getDataBaseTable,
 | ||
|   getMySystemSetTable,
 | ||
|   upDataZhongxinginitialization,
 | ||
|   upDateTable
 | ||
| } from '../../common/service/initable'
 | ||
| import { GlobalConfig } from '../../config/index'
 | ||
| import testNapi from '@ohos.hiserialsdk'
 | ||
| import fs from '@ohos.file.fs';
 | ||
| 
 | ||
| let num = 0
 | ||
| 
 | ||
| async function getliushuiNum(data) {
 | ||
|   // let data = AppStorage.get('liushuiNo')
 | ||
|   if (data) {
 | ||
|     num = Number(data) + 1
 | ||
|     let str = num.toString()
 | ||
|     for (let i = 0; str.length <= 5; i++) {
 | ||
|       str = '0' + str
 | ||
|     }
 | ||
|     globalThis.lshNo = str
 | ||
|   } else {
 | ||
|     num = 0
 | ||
|     let str = num.toString()
 | ||
|     for (let i = 0; str.length <= 5; i++) {
 | ||
|       str = '0' + str
 | ||
|     }
 | ||
|     globalThis.lshNo = str
 | ||
|   }
 | ||
|   console.log('liushuiNo1',globalThis.lshNo)
 | ||
| }
 | ||
| 
 | ||
| // 打包当天日志文件
 | ||
| async function compressCurrentLogs() {
 | ||
|   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])
 | ||
|       }
 | ||
|     }
 | ||
|   }
 | ||
| }
 | ||
| 
 | ||
| // 将要上传的文件拷贝到缓存目录并压缩
 | ||
| async function getCopyFiles() {
 | ||
|   if (!fs.accessSync(globalThis.context.cacheDir + '/temp')) {
 | ||
|     fs.mkdirSync(globalThis.context.cacheDir + '/temp')
 | ||
|   }
 | ||
|   await compressCurrentLogs()
 | ||
|   let now = new Date()
 | ||
|   const date = `${now.getFullYear()}_${now.getMonth() + 1}_${now.getDate()}`
 | ||
|   let inFile = '/mnt/hmdfs/100/account/device_view/local/files/logs/' + date;
 | ||
|   let outFile = globalThis.context.cacheDir + '/temp/' + date
 | ||
|   if (fs.accessSync(inFile)) {
 | ||
|     fs.copyFileSync(inFile, outFile)
 | ||
|   }
 | ||
|   await compressFile(globalThis.context.cacheDir + '/temp/', globalThis.context.cacheDir + '/' + 'logs.zip')
 | ||
|   fs.rmdirSync(globalThis.context.cacheDir + '/temp');
 | ||
| }
 | ||
| 
 | ||
| export async function uploadLogFile() {
 | ||
|   let filename = 'logs.zip'
 | ||
|   await getCopyFiles()
 | ||
|   let uploadTask: request.UploadTask
 | ||
|   let stat = fs.statSync(globalThis.context.cacheDir + '/' + filename)
 | ||
|   console.log('文件大小:' + stat.size / 1024 / 1024)
 | ||
|   //globalThis.host
 | ||
|   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 }],
 | ||
|   }
 | ||
|   try {
 | ||
|     request.uploadFile(globalThis.context, uploadConfig).then((data) => {
 | ||
|       uploadTask = data;
 | ||
|       uploadTask.on("progress", (size, tot) => {
 | ||
|         console.log('progressing上传中', "进度" + size + '/' + tot)
 | ||
|       })
 | ||
|       uploadTask.on('complete', () => {
 | ||
|         console.log('progressing上传完成')
 | ||
|         fs.rmdirSync(globalThis.context.cacheDir + '/' + 'logs.zip');
 | ||
|       })
 | ||
|       uploadTask.on("headerReceive", header => {
 | ||
|         console.log(JSON.stringify(header))
 | ||
|       })
 | ||
|     }).catch((e) => {
 | ||
|       console.log('progressing上传失败', e)
 | ||
|     })
 | ||
|   } catch (err) {
 | ||
|     console.log('progressing上传失败2', err)
 | ||
|   }
 | ||
| }
 | ||
| 
 | ||
| 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)
 | ||
| }
 | ||
| 
 | ||
| 
 | ||
| export async function deleteAllFIleLog(path) {
 | ||
|   let options = {
 | ||
|     "recursion": false,
 | ||
|     "listNum": 0,
 | ||
|   };
 | ||
| 
 | ||
|   let filenames = fs.listFileSync(path, options);
 | ||
|   console.info("listFile succeed", JSON.stringify(filenames));
 | ||
|   for (let i = 0; i < filenames.length; i++) {
 | ||
|     console.info("listFile succeed", JSON.stringify(filenames[i]));
 | ||
| 
 | ||
|     if (isSevenDaysAgo(filenames[i])) {
 | ||
|       fs.rmdir(path + filenames[i], (err) => {
 | ||
|         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]);
 | ||
|   }
 | ||
| 
 | ||
| }
 | ||
| 
 | ||
| 
 | ||
| function isSevenDaysAgo(date, saveDays?) {
 | ||
|   const today = new Date(); // 当前日期
 | ||
|   const target = new Date(date); // 需要判断的日期
 | ||
|   console.info("listFile succeed1", JSON.stringify(target));
 | ||
|   const diff = today.getTime() - target.getTime(); // 计算两个日期之间的毫秒数差异
 | ||
|   const diffDays = diff / (1000 * 60 * 60 * 24); // 将毫秒转换为天数
 | ||
|   console.info("listFile succeed2", (diffDays));
 | ||
|   // 如果差异天数正好是2,则原日期是当前日期的前2天
 | ||
|   return diffDays > (saveDays || 2);
 | ||
| }
 | ||
| 
 | ||
| export async function setVideoParam() {
 | ||
|   // 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))
 | ||
|   // }
 | ||
| 
 | ||
| 
 | ||
| }
 | ||
| 
 | ||
| let fileFd
 | ||
| 
 | ||
| //配置流水号
 | ||
| export async function setliushuiNum() {
 | ||
|   const data = AppStorage.get('liushuiNo')
 | ||
|   const liushuiDate = AppStorage.get('liushuiDate')
 | ||
|   console.log('liushuiNo',data,'liushuiDate',liushuiDate)
 | ||
|   getliushuiNum(data)
 | ||
|   if (data&&liushuiDate) {
 | ||
|     const date = new Date()
 | ||
|     const time1 = liushuiDate.split(' ')[0]
 | ||
|     const time2 = dateFormat(date).split(' ')[0]
 | ||
|     if (time1 != time2) {
 | ||
|       AppStorage.setOrCreate('liushuiDate', dateFormat(date));
 | ||
|       AppStorage.setOrCreate('liushuiNo', '0');
 | ||
|     }else{
 | ||
|       AppStorage.setOrCreate('liushuiNo', Number(data) + 1);
 | ||
|     }
 | ||
|   } else {
 | ||
|     const date = new Date()
 | ||
|     // PersistentStorage.PersistProp('liushuiNo', 0);
 | ||
|     // PersistentStorage.PersistProp('liushuiDate', dateFormat(date));
 | ||
|     AppStorage.setOrCreate('liushuiNo', '0');
 | ||
|     AppStorage.setOrCreate('liushuiDate', dateFormat(date));
 | ||
|   }
 | ||
| }
 | ||
| 
 | ||
| //双中心表
 | ||
| export async function getDoubleCeneterTable(param) {
 | ||
|   return new Promise(async (reslove, reject) => {
 | ||
|     // MA_SYSSET双中心下做合并表格操作
 | ||
|     delSyncTable('MA_SYSSET', param.context).then(async () => {
 | ||
|       await getDataBaseTable({ tableName: 'MA_SYSSET' }, param)
 | ||
|       await getMySystemSetTable({ tableName: 'MA_SYSSET' }, param)
 | ||
|     })
 | ||
| 
 | ||
|     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) {
 | ||
|       typeObj[key] = await upDateTable({ tableName: key }, param)
 | ||
|       if (!typeObj[key]) {
 | ||
|         promptAction.showToast({
 | ||
|           message: `未能查询到${key}表数据, 请先检查网络是否连接正常`,
 | ||
|           duration: 3000
 | ||
|         });
 | ||
|         reslove(false)
 | ||
|         return
 | ||
|       }
 | ||
|     }
 | ||
|     reslove(true)
 | ||
|     router.pushUrl({
 | ||
|       url: 'pages/ExaminerLogin',
 | ||
|     }, router.RouterMode.Single);
 | ||
|   })
 | ||
| 
 | ||
| }
 | ||
| 
 | ||
| //单中心存表
 | ||
| export async function getSingleCenterTable(param) {
 | ||
|   return new Promise((reslove, reject) => {
 | ||
|     try {
 | ||
|       console.log('teststet0')
 | ||
|       upDataZhongxinginitialization(param).then((result) => {
 | ||
|         console.log('kkkkkkkkkkk', result)
 | ||
| 
 | ||
|         if (result) {
 | ||
|           reslove(true)
 | ||
|         } else {
 | ||
|           console.log('联网更新失败,请检查网络后重新更新')
 | ||
|           reslove(false)
 | ||
|         }
 | ||
|       }).catch((error) => {
 | ||
|         reslove({
 | ||
|           success:false,
 | ||
|           error:error.initializationRsp.head
 | ||
|         })
 | ||
|       })
 | ||
|     } catch (e) {
 | ||
|       reslove(false)
 | ||
|     }
 | ||
|   })
 | ||
| 
 | ||
| }
 | ||
| 
 | ||
| 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',
 | ||
|   }
 | ||
|   globalThis.spzd = {
 | ||
|     spzd1: false,
 | ||
|     spzd2: false,
 | ||
|     spzd3: false,
 | ||
|     spzd4: false,
 | ||
|   }
 | ||
|   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)
 | ||
|   interval = setTimeout(async () => {
 | ||
|     const arr = ['spzd1', 'spzd2', 'spzd3', 'spzd4']
 | ||
| 
 | ||
|     for (let key in map) {
 | ||
|       map[key] = false
 | ||
|       // param[key] = false
 | ||
|     }
 | ||
|     for (let i = 0; i <= arr.length - 1; i++) {
 | ||
|       let key1 = arr[i]
 | ||
|       if (param[key1]) {
 | ||
|         param.pztd = param[`td${Number(i) + 1}`]
 | ||
|         takePhoto(param, context, 'jt/', 0, (data) => {
 | ||
|           if (data.errorCode != 0) {
 | ||
|             console.log('拍照失败')
 | ||
|             return
 | ||
|           }
 | ||
|           const zdyz = globalThis.param854Str ? Number(globalThis.param854Str) : Number(param.zdyz)
 | ||
|           console.log('takePhototakePhoto', JSON.stringify(data))
 | ||
| 
 | ||
|           if (Number(data.fileSize) <= (zdyz * 1000)) {
 | ||
|             map[key1] = true
 | ||
|             promptAction.showToast({
 | ||
|               message: `视频遮挡`,
 | ||
|               duration: 3000
 | ||
|             });
 | ||
|             globalThis.spzd[key1] = true
 | ||
|           } else {
 | ||
|             map[key1] = false
 | ||
|             globalThis.spzd[key1] = false
 | ||
|           }
 | ||
|           setTimeout(() => {
 | ||
|             takePhotoFn()
 | ||
|           }, 3000)
 | ||
|         })
 | ||
| 
 | ||
|       }
 | ||
|     }
 | ||
|     globalThis.takePhotoNum++
 | ||
|     if (globalThis.takePhotoNum >= 20) {
 | ||
|       globalThis.takePhotoNum = 0
 | ||
|       // deleteAllFileByPiC('jt')
 | ||
|     }
 | ||
|   }, 3000)
 | ||
| }
 | ||
| 
 | ||
| let fd
 | ||
| const devPath = "/dev/ttyS3"
 | ||
| 
 | ||
| function openChuankouFn(callback) {
 | ||
|   // console.log('SerialOpen in indexservice, path=' + devPath)
 | ||
| 
 | ||
|   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()
 | ||
|     });
 | ||
|   });
 | ||
| }
 | ||
| 
 | ||
| function getChuankouFnMsg() {
 | ||
|   let timeout = 50000; // 2秒超时
 | ||
|   let databuff = [0x61, 0xAA, 0x0A, 0X15, 0X00]; // send ABCDE
 | ||
|   // console.log('fdfd', globalThis.fd)
 | ||
|   // console.log('zzc 1 try send msg')
 | ||
|   testNapi.SerialSendAsync(globalThis.fd, databuff, (ret) => {
 | ||
|     // console.log('zzc 2 send finished')
 | ||
|     // console.log('zzc 3 try receive msg')
 | ||
|     testNapi.SerialRecvAsync(globalThis.fd, timeout, (revTestInfo) => {
 | ||
|       // console.log('zzc 4 received msg')
 | ||
|       // console.log('revTestInfo', revTestInfo.recevedBuf.length)
 | ||
| 
 | ||
|       const message = revTestInfo?.recevedBuf?.toString()
 | ||
|       // console.log('chuankou', message)
 | ||
|       if (message == '') {
 | ||
|         // console.log('zzc error msg is emptry')
 | ||
|         globalThis.num = 1
 | ||
|         // console.log('zzc 9 num=3 close serial')
 | ||
|         // clearInterval(chuankou)
 | ||
|         testNapi.SerialClose(globalThis.fd);
 | ||
|         globalThis.fd = null
 | ||
|         getChuankouFn()
 | ||
|         return
 | ||
|       }
 | ||
|       const msg = message?.split(',')
 | ||
|       if (!msg?.length) {
 | ||
| 
 | ||
|       } else if (msg[0] != '98' || msg[1] != '85' || msg.length < 9) {
 | ||
| 
 | ||
|       } else if (msg.length < 12) {
 | ||
| 
 | ||
|       } else {
 | ||
|         globalThis.chuankoMsg = msg[9]
 | ||
|       }
 | ||
| 
 | ||
|       // console.log('zzc 5 sleep 1s')
 | ||
|       setTimeout(() => {
 | ||
|         getChuankouFnMsg()
 | ||
|       }, 500)
 | ||
| 
 | ||
|       // 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);
 | ||
| 
 | ||
| }
 | ||
| 
 | ||
| let chuankou
 | ||
| 
 | ||
| export async function getChuankouFn() {
 | ||
|   if (globalThis.fd) {
 | ||
|     return
 | ||
|   }
 | ||
|   openChuankouFn(getChuankouFnMsg)
 | ||
|   // clearInterval(chuankou)
 | ||
|   // chuankou=setInterval(()=>{
 | ||
|   // getChuankouFnMsg()
 | ||
|   // },1000)
 | ||
| 
 | ||
| } |