refactor: 完善全局配置类型,优化UDP信号获取逻辑

This commit is contained in:
wangzhongjie 2025-03-26 16:07:39 +08:00
parent 8cf0419e12
commit a7a9bd1acc
6 changed files with 123 additions and 27 deletions

View File

@ -1,3 +1,5 @@
import { GlobalConfigType } from '../model';
/* /*
* @Author: wangzhongjie * @Author: wangzhongjie
* @Date: 2024-03-07 10:54:53 * @Date: 2024-03-07 10:54:53
@ -6,12 +8,12 @@
* @Description: 全局配置 * @Description: 全局配置
* @Email: shutdown0630@163.com * @Email: shutdown0630@163.com
*/ */
export const GlobalConfig={ export const GlobalConfig: GlobalConfigType = {
comoonfileWriteAddress: '/mnt/hmdfs/100/account/device_view/local/files/duolun', comoonfileWriteAddress: '/mnt/hmdfs/100/account/device_view/local/files/duolun',
picSavePath: '/storage/cloud/100/files/Photo/', picSavePath: '/storage/cloud/100/files/Photo/',
videoSavePath: '/storage/cloud/100/files/Videos/', videoSavePath: '/storage/cloud/100/files/Videos/',
host: 'http://172.37.55.192:8082', host: 'http://172.37.55.192:8082',
modelNo: "3",
version: { version: {
//杭州 //杭州
hz: { hz: {

View File

@ -7,8 +7,8 @@ import { centerUDPClient, lightUDPClient, objUDPClient } from '../utils/UdpUtils
import Want from '@ohos.app.ability.Want'; import Want from '@ohos.app.ability.Want';
import AbilityConstant from '@ohos.app.ability.AbilityConstant'; import AbilityConstant from '@ohos.app.ability.AbilityConstant';
import { BaseInfoType, CarInfoType, ExaminerInfoType } from '../model'; import { BaseInfoType, CarInfoType, ExaminerInfoType } from '../model';
import DB from '../utils/DbSql';
import { tcpUtil } from '../utils/TcpRequest'; import { tcpUtil } from '../utils/TcpRequest';
import DB from '../utils/DbSql';
export default class EntryAbility extends UIAbility { export default class EntryAbility extends UIAbility {
async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) { async onCreate(want: Want, launchParam: AbilityConstant.LaunchParam) {

View File

@ -133,3 +133,27 @@ export interface EnvironmentConfigurationType {
centerPort?: string, centerPort?: string,
terType?: string terType?: string
} }
//全局配置
export interface GlobalConfigType {
comoonfileWriteAddress?: string
picSavePath?: string
videoSavePath?: string
host?: string
version?: VersionType
// 几代机
modelNo?: string
}
interface VersionType {
hz: VersionInfo;
hlg: VersionInfo;
jn: VersionInfo;
ly: VersionInfo;
sz: VersionInfo;
}
interface VersionInfo {
km2: string[];
km3: string[];
}

View File

@ -2,3 +2,13 @@ export interface TakePhotoCallbackData {
fileSize: number; fileSize: number;
errorCode: number errorCode: number
} }
export interface ObtainSignalDataConfigType {
// UDP配置
udpLocalIp?: string
udpLocalIpPort?: string
udpOppositeIp?: string
udpOppositeIpPort?: string
}

View File

@ -13,7 +13,7 @@ interface IPConfig {
udpOppositeIpPort: string udpOppositeIpPort: string
} }
class UdpClient { export default class UdpClient {
private localIp: string = '' private localIp: string = ''
private localIpPort: string = '' private localIpPort: string = ''
private oppositeIp: string = '' private oppositeIp: string = ''
@ -24,7 +24,9 @@ class UdpClient {
private dealMethod: DealMethod private dealMethod: DealMethod
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
})
} }
async reBind() { async reBind() {
@ -56,12 +58,14 @@ class UdpClient {
return this.udp?.getState().then(() => { return this.udp?.getState().then(() => {
return this.udp.send({ return this.udp.send({
data, data,
address: { address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1 } address: {
address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1
}
}) })
}) })
} }
protected create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) { create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
this.localIp = udpLocalIp this.localIp = udpLocalIp
this.oppositeIp = udpOppositeIp this.oppositeIp = udpOppositeIp
this.localIpPort = udpLocalIpPort this.localIpPort = udpLocalIpPort

View File

@ -0,0 +1,56 @@
import { GlobalConfig } from '../../config'
import { ObtainSignalDataConfigType } from '../../model'
import UdpClient from '../UdpUtils'
class obtainSignalData {
// 三代机UDP
private thirdGenerationMachineUdp: UdpClient
// 三代机UDP配置
private thirdGenerationMachineUdpConfig: ObtainSignalDataConfigType
// 几代机
private modelNo: string = "3"
constructor() {
this.modelNo = GlobalConfig.modelNo
}
// 一些初始化
init(config: ObtainSignalDataConfigType) {
this.thirdGenerationMachineUdpConfig = config
if (this.modelNo === "0") {
} else if (this.modelNo === "1") {
} else if (this.modelNo === "2") {
} else if (this.modelNo === "3") {
// 初始化UDP
this.thirdGenerationMachineUdp = new UdpClient()
this.thirdGenerationMachineUdp.create(config.udpLocalIp, config.udpLocalIpPort, config.udpOppositeIp,
config.udpOppositeIpPort)
}
}
// 获取信号
getData(callback: (data: string) => void) {
// 一代机
// 二代机
// 三代机 通过UDP onMessage获取信号
if (this.modelNo === "3") {
this.thirdGenerationMachineUdp.onMessage((data: ArrayBuffer) => {
const dataView = new DataView(data);
let str = ""
for (let i = 0; i < dataView?.byteLength; ++i) {
let c = String.fromCharCode(dataView?.getUint8(i))
if (c !== "\n") {
str += c
}
}
callback(str)
})
}
// 一体机
}
}
export const ObtainSignalData = new obtainSignalData()