fix: 添加类型定义和优化函数调用,重构UDP相关代码
This commit is contained in:
parent
cfe1852b03
commit
f2bbc93c47
@ -15,6 +15,7 @@
|
|||||||
|
|
||||||
import photoAccessHelper from '@ohos.file.photoAccessHelper';
|
import photoAccessHelper from '@ohos.file.photoAccessHelper';
|
||||||
import dataSharePredicates from '@ohos.data.dataSharePredicates';
|
import dataSharePredicates from '@ohos.data.dataSharePredicates';
|
||||||
|
import common from '@ohos.app.ability.common';
|
||||||
|
|
||||||
export class FileHelper {
|
export class FileHelper {
|
||||||
FILE_ASSET_FETCH_COLUMNS = [
|
FILE_ASSET_FETCH_COLUMNS = [
|
||||||
@ -38,7 +39,7 @@ export class FileHelper {
|
|||||||
private userFileMgr: photoAccessHelper.PhotoAccessHelper = undefined;
|
private userFileMgr: photoAccessHelper.PhotoAccessHelper = undefined;
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
const context=AppStorage.get('context')
|
const context: common.UIAbilityContext = AppStorage.get('context')
|
||||||
this.userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
|
this.userFileMgr = photoAccessHelper.getPhotoAccessHelper(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1 +1,3 @@
|
|||||||
export const DbTag = '[DBTag]';
|
export const DbTag = '[DBTag]';
|
||||||
|
|
||||||
|
export const SerialPortTag = '[SerialPortTag]';
|
||||||
@ -11,9 +11,12 @@ import { tcpUtil } from '../common/utils/TcpRequest';
|
|||||||
import DB from '../common/database/DbSql';
|
import DB from '../common/database/DbSql';
|
||||||
import { initTable } from '../common/service/initable';
|
import { initTable } from '../common/service/initable';
|
||||||
import { centerUDPClient, lightUDPClient, objUDPClient } from '../utils/UdpUtils';
|
import { centerUDPClient, lightUDPClient, objUDPClient } from '../utils/UdpUtils';
|
||||||
|
import Want from '@ohos.app.ability.Want';
|
||||||
|
import AbilityConstant from '@ohos.app.ability.AbilityConstant';
|
||||||
|
import { BaseInfoType, CarInfoType } from '../model';
|
||||||
|
|
||||||
export default class EntryAbility extends UIAbility {
|
export default class EntryAbility extends UIAbility {
|
||||||
async onCreate(want, launchParam) {
|
async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {
|
||||||
try {
|
try {
|
||||||
console.log("sql first")
|
console.log("sql first")
|
||||||
await DB.init(this.context)
|
await DB.init(this.context)
|
||||||
@ -30,7 +33,8 @@ export default class EntryAbility extends UIAbility {
|
|||||||
onDestroy() {
|
onDestroy() {
|
||||||
const arrClose = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00]
|
const arrClose = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x00]
|
||||||
const arrCloseBuffer = Array2Byte(arrClose).buffer
|
const arrCloseBuffer = Array2Byte(arrClose).buffer
|
||||||
globalThis?.lightLineUdp?.send(arrCloseBuffer);
|
// TODO UDP缺失
|
||||||
|
// globalThis?.lightLineUdp?.send(arrCloseBuffer);
|
||||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
|
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,13 +48,13 @@ export default class EntryAbility extends UIAbility {
|
|||||||
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
|
||||||
await tcpUtil.init()
|
await tcpUtil.init()
|
||||||
|
|
||||||
AppStorage.SetOrCreate('carInfo', {})
|
AppStorage.setOrCreate<CarInfoType>('carInfo', {})
|
||||||
AppStorage.SetOrCreate('examinerInfo', {})
|
AppStorage.setOrCreate('examinerInfo', {})
|
||||||
AppStorage.SetOrCreate('lsh', '0000000000000')
|
AppStorage.setOrCreate('lsh', '0000000000000')
|
||||||
AppStorage.SetOrCreate('statue', 1) //考试状态
|
AppStorage.setOrCreate('statue', 1) //考试状态
|
||||||
AppStorage.SetOrCreate('signNum', 0) //心跳指令编号
|
AppStorage.setOrCreate('signNum', 0) //心跳指令编号
|
||||||
AppStorage.SetOrCreate('deviceNo', 0) //设备号
|
AppStorage.setOrCreate('deviceNo', 0) //设备号
|
||||||
AppStorage.SetOrCreate('baseInfo', {
|
AppStorage.setOrCreate<BaseInfoType>('baseInfo', {
|
||||||
hasAuth: false,
|
hasAuth: false,
|
||||||
version: GlobalConfig.version.jn.km3[0],
|
version: GlobalConfig.version.jn.km3[0],
|
||||||
judgeVersion: GlobalConfig.version.jn.km3[1],
|
judgeVersion: GlobalConfig.version.jn.km3[1],
|
||||||
|
|||||||
@ -268,27 +268,4 @@ export const EmptyCandidateObject: User = {
|
|||||||
yycs: ''
|
yycs: ''
|
||||||
}
|
}
|
||||||
|
|
||||||
export const VideoConfigData: VideoConfig = {
|
|
||||||
spls: '',
|
|
||||||
faceFlag: false,
|
|
||||||
pztd: '2',
|
|
||||||
ljlx: '',
|
|
||||||
ip: '192.168.7.112',
|
|
||||||
port: '554',
|
|
||||||
userName: 'admin',
|
|
||||||
pwd: '12345qwe',
|
|
||||||
td1: '1',
|
|
||||||
td2: '2',
|
|
||||||
td3: '3',
|
|
||||||
td4: '4',
|
|
||||||
videoRecord1: false,
|
|
||||||
videoRecord2: false,
|
|
||||||
videoRecord3: false,
|
|
||||||
videoRecord4: false,
|
|
||||||
rlls: '1',
|
|
||||||
spzd4: false,
|
|
||||||
spzd3: false,
|
|
||||||
spzd2: false,
|
|
||||||
spzd1: false,
|
|
||||||
zdyz: '500'
|
|
||||||
}
|
|
||||||
|
|||||||
@ -18,15 +18,6 @@ export interface CarConfigurationParams {
|
|||||||
body: Record<string, string>;
|
body: Record<string, string>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 车辆信息
|
|
||||||
export interface CarInfoType {
|
|
||||||
carId?: string;
|
|
||||||
examinationRoomId?: string;
|
|
||||||
plateNo?: string;
|
|
||||||
carNo?: string;
|
|
||||||
examSubject?: string;
|
|
||||||
isNeedCheck?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -76,6 +76,7 @@ export interface CommonType {
|
|||||||
key: string;
|
key: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 基础信息
|
||||||
export interface BaseInfoType {
|
export interface BaseInfoType {
|
||||||
hasAuth?: boolean,
|
hasAuth?: boolean,
|
||||||
version?: string,
|
version?: string,
|
||||||
@ -95,3 +96,18 @@ export interface RouteParamsType {
|
|||||||
kfdm?: string;
|
kfdm?: string;
|
||||||
type?: string;
|
type?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 车辆信息
|
||||||
|
export interface CarInfoType {
|
||||||
|
carId?: string;
|
||||||
|
examinationRoomId?: string;
|
||||||
|
plateNo?: string;
|
||||||
|
carNo?: string;
|
||||||
|
examSubject?: string;
|
||||||
|
isNeedCheck?: string;
|
||||||
|
udpAddress?: string
|
||||||
|
messagePort?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
export interface ExaminerInfoType {}
|
||||||
95
entry/src/main/ets/utils/SerialPort.ets
Normal file
95
entry/src/main/ets/utils/SerialPort.ets
Normal file
@ -0,0 +1,95 @@
|
|||||||
|
import testNapi from '@ohos.hiserialsdk';
|
||||||
|
import HiSerialSDK from '@ohos.hiserialsdk';
|
||||||
|
import { SerialPortTag } from '../config';
|
||||||
|
|
||||||
|
// 打开串口工具
|
||||||
|
export const OpenSerialPort = (serialPort: string) => {
|
||||||
|
return new Promise<number>((resolve, reject) => {
|
||||||
|
testNapi.SerialOpenAsync(serialPort, (value: number) => {
|
||||||
|
if (value === -1) {
|
||||||
|
console.log(SerialPortTag, "打开串口失败!")
|
||||||
|
reject(-1)
|
||||||
|
}
|
||||||
|
console.log(SerialPortTag, "打开成功!!")
|
||||||
|
resolve(value)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//初始化串口数据
|
||||||
|
export const InitSerialPortData =
|
||||||
|
async (fd: number, speed: number) => {
|
||||||
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
|
testNapi.SerialSetAsync(fd, speed, 0, 8, 1, 0x4e, (value: number) => {
|
||||||
|
// 0 成功 -1失败
|
||||||
|
if (value === 0) {
|
||||||
|
console.log(SerialPortTag, "初始化成功!")
|
||||||
|
resolve(true)
|
||||||
|
}
|
||||||
|
if (value === -1) {
|
||||||
|
console.log(SerialPortTag, "初始化失败!")
|
||||||
|
reject(false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
//发送数据
|
||||||
|
export const SendSerialPortData = (fd: number, data: number[]) => {
|
||||||
|
console.log(SerialPortTag, "wzj-----发送数据")
|
||||||
|
return new Promise<number>((resolve, reject) => {
|
||||||
|
testNapi.SerialSendAsync(fd, data, (value: number) => {
|
||||||
|
if (value === -1) {
|
||||||
|
console.log(SerialPortTag, "发送失败!")
|
||||||
|
reject(-1)
|
||||||
|
}
|
||||||
|
resolve(value)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 接受数据
|
||||||
|
export const ReceiveSerialPortData = (fd: number, timeout: number,) => {
|
||||||
|
console.log(SerialPortTag, "wzj-----接受数据")
|
||||||
|
return new Promise<HiSerialSDK.receiveInfo>((resolve) => {
|
||||||
|
testNapi.SerialRecvAsync(fd, timeout, (value: HiSerialSDK.receiveInfo) => {
|
||||||
|
resolve(value)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
// 主动接受数据
|
||||||
|
export const ReceiveSerialPortDataBySelf = (fd: number, callback: Function) => {
|
||||||
|
testNapi.SerialListenCallbackSet(fd, callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 取消主动接受
|
||||||
|
export const CancelReceiveSerialPortData = async (fd: number) => {
|
||||||
|
return new Promise<boolean>((resolve, reject) => {
|
||||||
|
let result: number = testNapi.SerialListenCallbackCancel(fd)
|
||||||
|
if (result === 0) {
|
||||||
|
resolve(true)
|
||||||
|
} else {
|
||||||
|
reject(false)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将输入字符串转换为ASCII码数组
|
||||||
|
* @param input 要转换的输入字符串
|
||||||
|
* @returns 转换后的ASCII码数组
|
||||||
|
*/
|
||||||
|
export const ConvertToASCII = (input: string): number[] => {
|
||||||
|
return input.split('').map(char => char.charCodeAt(0));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 将输入字符串转换为十六进制(hex)数组
|
||||||
|
* @param input 要转换的输入字符串
|
||||||
|
* @returns 转换后的十六进制数组
|
||||||
|
*/
|
||||||
|
export const ConvertToHex = (input: string): number[] => {
|
||||||
|
return input.split('').map(char => parseInt(char.charCodeAt(0).toString(16), 16));
|
||||||
|
}
|
||||||
|
|
||||||
@ -2,6 +2,7 @@ import socket from '@ohos.net.socket'
|
|||||||
import common from '@ohos.app.ability.common'
|
import common from '@ohos.app.ability.common'
|
||||||
import FileUtils from './fileUtils'
|
import FileUtils from './fileUtils'
|
||||||
import promptAction from '@ohos.promptAction'
|
import promptAction from '@ohos.promptAction'
|
||||||
|
import { CarInfoType } from '../model'
|
||||||
|
|
||||||
type DealMethod = (value: ArrayBuffer) => string
|
type DealMethod = (value: ArrayBuffer) => string
|
||||||
|
|
||||||
@ -22,30 +23,6 @@ class UdpClient {
|
|||||||
private errorEvents: Array<Function> = []
|
private errorEvents: Array<Function> = []
|
||||||
private dealMethod: DealMethod
|
private dealMethod: DealMethod
|
||||||
|
|
||||||
protected create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
|
|
||||||
this.localIp = udpLocalIp
|
|
||||||
this.oppositeIp = udpOppositeIp
|
|
||||||
this.localIpPort = udpLocalIpPort
|
|
||||||
this.oppositeIpPort = udpOppositeIpPort
|
|
||||||
this.udp = socket.constructUDPSocketInstance();
|
|
||||||
this.bindEvent()
|
|
||||||
this.bindUdp()
|
|
||||||
}
|
|
||||||
|
|
||||||
private bindEvent() {
|
|
||||||
this.udp?.on("message", value => {
|
|
||||||
let result = this?.dealMethod(value.message)
|
|
||||||
this.messageEvents.forEach(cb => {
|
|
||||||
cb(result)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
this.udp.on("error", (err) => {
|
|
||||||
this.errorEvents.forEach(cb => {
|
|
||||||
cb(err)
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
bindUdp(): Promise<void> {
|
bindUdp(): Promise<void> {
|
||||||
return this.udp.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 })
|
return this.udp.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 })
|
||||||
}
|
}
|
||||||
@ -83,6 +60,30 @@ class UdpClient {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
|
||||||
|
this.localIp = udpLocalIp
|
||||||
|
this.oppositeIp = udpOppositeIp
|
||||||
|
this.localIpPort = udpLocalIpPort
|
||||||
|
this.oppositeIpPort = udpOppositeIpPort
|
||||||
|
this.udp = socket.constructUDPSocketInstance();
|
||||||
|
this.bindEvent()
|
||||||
|
this.bindUdp()
|
||||||
|
}
|
||||||
|
|
||||||
|
private bindEvent() {
|
||||||
|
this.udp?.on("message", value => {
|
||||||
|
let result = this?.dealMethod(value.message)
|
||||||
|
this.messageEvents.forEach(cb => {
|
||||||
|
cb(result)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
this.udp.on("error", (err) => {
|
||||||
|
this.errorEvents.forEach(cb => {
|
||||||
|
cb(err)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class ObjUdpClient extends UdpClient {
|
class ObjUdpClient extends UdpClient {
|
||||||
@ -107,10 +108,7 @@ class CenterUDPClient extends UdpClient {
|
|||||||
try {
|
try {
|
||||||
const fileUtil = new FileUtils(context)
|
const fileUtil = new FileUtils(context)
|
||||||
const data = await fileUtil.readFile("" + '/config/ipConfig.txt');
|
const data = await fileUtil.readFile("" + '/config/ipConfig.txt');
|
||||||
const carInfo: {
|
const carInfo: CarInfoType = AppStorage.get('carInfo')
|
||||||
udpAddress: string
|
|
||||||
messagePort: string
|
|
||||||
} = AppStorage.Get('carInfo')
|
|
||||||
if (data?.length > 0) {
|
if (data?.length > 0) {
|
||||||
const result: IPConfig = JSON.parse(data)
|
const result: IPConfig = JSON.parse(data)
|
||||||
this.create(result.udplocalIp, '8800', carInfo?.udpAddress, carInfo?.messagePort)
|
this.create(result.udplocalIp, '8800', carInfo?.udpAddress, carInfo?.messagePort)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user