udp修改
This commit is contained in:
parent
b6ebb37d11
commit
cfe1852b03
@ -10,6 +10,7 @@ import { GlobalConfig } from '../config/global'
|
|||||||
import { tcpUtil } from '../common/utils/TcpRequest';
|
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';
|
||||||
|
|
||||||
export default class EntryAbility extends UIAbility {
|
export default class EntryAbility extends UIAbility {
|
||||||
async onCreate(want, launchParam) {
|
async onCreate(want, launchParam) {
|
||||||
@ -34,18 +35,22 @@ export default class EntryAbility extends UIAbility {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async onWindowStageCreate(windowStage: window.WindowStage) {
|
async onWindowStageCreate(windowStage: window.WindowStage) {
|
||||||
|
// 初始化示例,只需要调用一次
|
||||||
|
await objUDPClient.init(this.context)
|
||||||
|
await lightUDPClient.init(this.context)
|
||||||
|
await centerUDPClient.init(this.context)
|
||||||
// this.context
|
// this.context
|
||||||
// Main window is created, set main page for this ability
|
// Main window is created, set main page for this ability
|
||||||
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('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('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],
|
||||||
@ -59,7 +64,7 @@ export default class EntryAbility extends UIAbility {
|
|||||||
|
|
||||||
|
|
||||||
const windowClass = await windowStage.getMainWindow();
|
const windowClass = await windowStage.getMainWindow();
|
||||||
AppStorage.setOrCreate('windowClass',windowClass)
|
AppStorage.SetOrCreate('windowClass', windowClass)
|
||||||
// await windowClass.setWindowLayoutFullScreen(true)
|
// await windowClass.setWindowLayoutFullScreen(true)
|
||||||
// await windowClass.setWindowSystemBarEnable([]) //全屏
|
// await windowClass.setWindowSystemBarEnable([]) //全屏
|
||||||
await windowClass.setWindowSystemBarEnable(['navigation'])
|
await windowClass.setWindowSystemBarEnable(['navigation'])
|
||||||
@ -1,43 +1,65 @@
|
|||||||
import socket from '@ohos.net.socket'
|
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
|
type DealMethod = (value: ArrayBuffer) => string
|
||||||
|
|
||||||
|
interface IPConfig {
|
||||||
|
udplocalIp: string
|
||||||
|
udplocalIpPort: string
|
||||||
|
udpOppositeIp: string
|
||||||
|
udpOppositeIpPort: string
|
||||||
|
}
|
||||||
|
|
||||||
class UdpClient {
|
class UdpClient {
|
||||||
private localIp: string = ''
|
private localIp: string = ''
|
||||||
private localIpPort: string = ''
|
private localIpPort: string = ''
|
||||||
private oppositeIp: string = ''
|
private oppositeIp: string = ''
|
||||||
private oppositeIpPort: string = ''
|
private oppositeIpPort: string = ''
|
||||||
private messageEvents: Array<Function> = []
|
|
||||||
private udp: socket.UDPSocket = null
|
private udp: socket.UDPSocket = null
|
||||||
private disconnectEvents: Array<Function> = []
|
private messageEvents: Array<Function> = []
|
||||||
|
private errorEvents: Array<Function> = []
|
||||||
private dealMethod: DealMethod
|
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.localIp = udpLocalIp
|
||||||
this.oppositeIp = udpOppositeIp
|
this.oppositeIp = udpOppositeIp
|
||||||
this.localIpPort = udpLocalIpPort
|
this.localIpPort = udpLocalIpPort
|
||||||
this.oppositeIpPort = udpOppositeIpPort
|
this.oppositeIpPort = udpOppositeIpPort
|
||||||
this.udp = socket.constructUDPSocketInstance();
|
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 }).then(() => {
|
return this.udp.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 })
|
||||||
try {
|
|
||||||
this.dealMessage()
|
|
||||||
return Promise.resolve()
|
|
||||||
} catch (e) {
|
|
||||||
return Promise.reject(e)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async reBind() {
|
async reBind() {
|
||||||
await this.close()
|
await this.close()
|
||||||
this.udp = socket.constructUDPSocketInstance();
|
this.udp = socket.constructUDPSocketInstance();
|
||||||
|
this.bindEvent()
|
||||||
await this.bindUdp()
|
await this.bindUdp()
|
||||||
}
|
}
|
||||||
|
|
||||||
close(): Promise<void> {
|
close(): Promise<void> {
|
||||||
|
this.udp.off("message")
|
||||||
|
this.udp.off("error")
|
||||||
return this.udp?.close()
|
return this.udp?.close()
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -49,8 +71,8 @@ class UdpClient {
|
|||||||
this.messageEvents.push(callback)
|
this.messageEvents.push(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
onDisconnect(callback: Function) {
|
onError(callback: Function) {
|
||||||
this.disconnectEvents.push(callback)
|
this.errorEvents.push(callback)
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMsg(data: string): Promise<void> {
|
sendMsg(data: string): Promise<void> {
|
||||||
@ -61,25 +83,69 @@ class UdpClient {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private dealMessage() {
|
class ObjUdpClient extends UdpClient {
|
||||||
this.udp?.on("message", value => {
|
async init(context: common.UIAbilityContext) {
|
||||||
let result = this?.dealMethod(value.message)
|
try {
|
||||||
this.messageEvents.forEach(cb => {
|
const fileUtil = new FileUtils(context)
|
||||||
cb(result)
|
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失败"
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// 中心心跳
|
class LightUDPClient extends UdpClient {
|
||||||
export const centerUDPClient = new 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()
|
|
||||||
|
|||||||
@ -17,7 +17,7 @@
|
|||||||
"abilities": [
|
"abilities": [
|
||||||
{
|
{
|
||||||
"name": "EntryAbility",
|
"name": "EntryAbility",
|
||||||
"srcEntrance": "./ets/entryability/EntryAbility.ts",
|
"srcEntrance": "./ets/entryability/EntryAbility.ets",
|
||||||
"description": "$string:EntryAbility_desc",
|
"description": "$string:EntryAbility_desc",
|
||||||
"icon": "$media:logo_app",
|
"icon": "$media:logo_app",
|
||||||
"label": "$string:EntryAbility_label",
|
"label": "$string:EntryAbility_label",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user