udp修改

This commit is contained in:
lixiao 2025-03-18 19:39:11 +08:00
parent b6ebb37d11
commit cfe1852b03
3 changed files with 112 additions and 41 deletions

View File

@ -10,6 +10,7 @@ import { GlobalConfig } from '../config/global'
import { tcpUtil } from '../common/utils/TcpRequest';
import DB from '../common/database/DbSql';
import { initTable } from '../common/service/initable';
import { centerUDPClient, lightUDPClient, objUDPClient } from '../utils/UdpUtils';
export default class EntryAbility extends UIAbility {
async onCreate(want, launchParam) {
@ -34,32 +35,36 @@ export default class EntryAbility extends UIAbility {
}
async onWindowStageCreate(windowStage: window.WindowStage) {
// 初始化示例,只需要调用一次
await objUDPClient.init(this.context)
await lightUDPClient.init(this.context)
await centerUDPClient.init(this.context)
// this.context
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
await tcpUtil.init()
AppStorage.setOrCreate('carInfo',{})
AppStorage.setOrCreate('examinerInfo',{})
AppStorage.setOrCreate('lsh','0000000000000')
AppStorage.setOrCreate('statue',1)//考试状态
AppStorage.setOrCreate('signNum',0)//心跳指令编号
AppStorage.setOrCreate('deviceNo',0)//设备号
AppStorage.setOrCreate('baseInfo',{
hasAuth:false,
version:GlobalConfig.version.jn.km3[0],
judgeVersion:GlobalConfig.version.jn.km3[1],
tcpSendNum:0,
videoVersion:'1.0',
AppStorage.SetOrCreate('carInfo', {})
AppStorage.SetOrCreate('examinerInfo', {})
AppStorage.SetOrCreate('lsh', '0000000000000')
AppStorage.SetOrCreate('statue', 1) //考试状态
AppStorage.SetOrCreate('signNum', 0) //心跳指令编号
AppStorage.SetOrCreate('deviceNo', 0) //设备号
AppStorage.SetOrCreate('baseInfo', {
hasAuth: false,
version: GlobalConfig.version.jn.km3[0],
judgeVersion: GlobalConfig.version.jn.km3[1],
tcpSendNum: 0,
videoVersion: '1.0',
ratio: 1700 / 960, //适配比例
pathDir: this.context.filesDir,
context: this.context,
isJudgeInitBool:false,
isJudgeInitBool: false,
})
const windowClass = await windowStage.getMainWindow();
AppStorage.setOrCreate('windowClass',windowClass)
AppStorage.SetOrCreate('windowClass', windowClass)
// await windowClass.setWindowLayoutFullScreen(true)
// await windowClass.setWindowSystemBarEnable([]) //全屏
await windowClass.setWindowSystemBarEnable(['navigation'])

View File

@ -1,43 +1,65 @@
import socket from '@ohos.net.socket'
import common from '@ohos.app.ability.common'
import FileUtils from './fileUtils'
import promptAction from '@ohos.promptAction'
type DealMethod = (value: ArrayBuffer) => string
interface IPConfig {
udplocalIp: string
udplocalIpPort: string
udpOppositeIp: string
udpOppositeIpPort: string
}
class UdpClient {
private localIp: string = ''
private localIpPort: string = ''
private oppositeIp: string = ''
private oppositeIpPort: string = ''
private messageEvents: Array<Function> = []
private udp: socket.UDPSocket = null
private disconnectEvents: Array<Function> = []
private messageEvents: Array<Function> = []
private errorEvents: Array<Function> = []
private dealMethod: DealMethod
init(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
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> {
return this.udp.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 }).then(() => {
try {
this.dealMessage()
return Promise.resolve()
} catch (e) {
return Promise.reject(e)
}
})
return this.udp.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 })
}
async reBind() {
await this.close()
this.udp = socket.constructUDPSocketInstance();
this.bindEvent()
await this.bindUdp()
}
close(): Promise<void> {
this.udp.off("message")
this.udp.off("error")
return this.udp?.close()
}
@ -49,8 +71,8 @@ class UdpClient {
this.messageEvents.push(callback)
}
onDisconnect(callback: Function) {
this.disconnectEvents.push(callback)
onError(callback: Function) {
this.errorEvents.push(callback)
}
sendMsg(data: string): Promise<void> {
@ -61,25 +83,69 @@ class UdpClient {
})
})
}
}
private dealMessage() {
this.udp?.on("message", value => {
let result = this?.dealMethod(value.message)
this.messageEvents.forEach(cb => {
cb(result)
class ObjUdpClient extends UdpClient {
async init(context: common.UIAbilityContext) {
try {
const fileUtil = new FileUtils(context)
const data = await fileUtil.readFile("" + '/config/ipConfig.txt');
if (data?.length > 0) {
const result: IPConfig = JSON.parse(data)
this.create(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort)
}
} catch (e) {
promptAction.showToast({
message: "初始化obj udp失败"
})
})
}
}
}
export const objUDPClient = new UdpClient()
class CenterUDPClient extends UdpClient {
async init(context: common.UIAbilityContext) {
try {
const fileUtil = new FileUtils(context)
const data = await fileUtil.readFile("" + '/config/ipConfig.txt');
const carInfo: {
udpAddress: string
messagePort: string
} = AppStorage.Get('carInfo')
if (data?.length > 0) {
const result: IPConfig = JSON.parse(data)
this.create(result.udplocalIp, '8800', carInfo?.udpAddress, carInfo?.messagePort)
}
} catch (e) {
promptAction.showToast({
message: "初始化中心 udp失败"
})
}
}
}
// 中心心跳
export const centerUDPClient = new UdpClient()
class LightUDPClient extends UdpClient {
async init(context: common.UIAbilityContext) {
try {
const fileUtil = new FileUtils(context)
const data = await fileUtil.readFile("" + '/config/ipConfig.txt');
if (data?.length > 0) {
const result: IPConfig = JSON.parse(data)
this.create(result.udplocalIp, '55509', result.udpOppositeIp, result.udpOppositeIpPort)
}
} catch (e) {
promptAction.showToast({
message: "初始化灯光 udp失败"
})
}
}
}
// obj
export const objUDPClient = new ObjUdpClient()
// 中心
export const centerUDPClient = new CenterUDPClient()
// 中心GPS
// 顶灯
export const lightUDPClient = new UdpClient()
export const lightUDPClient = new LightUDPClient()
// 获取后置机信号
export const judgeUDPClient = new UdpClient()

View File

@ -17,7 +17,7 @@
"abilities": [
{
"name": "EntryAbility",
"srcEntrance": "./ets/entryability/EntryAbility.ts",
"srcEntrance": "./ets/entryability/EntryAbility.ets",
"description": "$string:EntryAbility_desc",
"icon": "$media:logo_app",
"label": "$string:EntryAbility_label",