2025-02-17 10:22:12 +08:00
|
|
|
import socket from '@ohos.net.socket'
|
2025-03-18 19:39:11 +08:00
|
|
|
import common from '@ohos.app.ability.common'
|
2025-03-26 10:37:10 +08:00
|
|
|
import FileUtils from './FileUtils'
|
2025-03-18 19:39:11 +08:00
|
|
|
import promptAction from '@ohos.promptAction'
|
2025-03-19 10:35:16 +08:00
|
|
|
import { CarInfoType } from '../model'
|
2025-03-28 11:33:07 +08:00
|
|
|
import buffer from '@ohos.buffer'
|
2025-02-17 10:22:12 +08:00
|
|
|
|
|
|
|
|
type DealMethod = (value: ArrayBuffer) => string
|
|
|
|
|
|
2025-03-18 19:39:11 +08:00
|
|
|
interface IPConfig {
|
|
|
|
|
udplocalIp: string
|
|
|
|
|
udplocalIpPort: string
|
|
|
|
|
udpOppositeIp: string
|
|
|
|
|
udpOppositeIpPort: string
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-26 16:07:39 +08:00
|
|
|
export default class UdpClient {
|
2025-02-17 10:22:12 +08:00
|
|
|
private localIp: string = ''
|
|
|
|
|
private localIpPort: string = ''
|
|
|
|
|
private oppositeIp: string = ''
|
|
|
|
|
private oppositeIpPort: string = ''
|
|
|
|
|
private udp: socket.UDPSocket = null
|
2025-03-18 19:39:11 +08:00
|
|
|
private messageEvents: Array<Function> = []
|
|
|
|
|
private errorEvents: Array<Function> = []
|
2025-02-17 10:22:12 +08:00
|
|
|
private dealMethod: DealMethod
|
|
|
|
|
|
2025-03-18 19:39:11 +08:00
|
|
|
bindUdp(): Promise<void> {
|
2025-03-26 16:07:39 +08:00
|
|
|
return this.udp.bind({
|
|
|
|
|
address: this.localIp, port: parseInt(this.localIpPort), family: 1
|
|
|
|
|
})
|
2025-03-18 19:39:11 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-17 10:22:12 +08:00
|
|
|
async reBind() {
|
|
|
|
|
await this.close()
|
|
|
|
|
this.udp = socket.constructUDPSocketInstance();
|
2025-03-18 19:39:11 +08:00
|
|
|
this.bindEvent()
|
2025-02-17 10:22:12 +08:00
|
|
|
await this.bindUdp()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close(): Promise<void> {
|
2025-03-18 19:39:11 +08:00
|
|
|
this.udp.off("message")
|
|
|
|
|
this.udp.off("error")
|
2025-02-17 10:22:12 +08:00
|
|
|
return this.udp?.close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDealMethod(fun: DealMethod) {
|
|
|
|
|
this.dealMethod = fun
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMessage(callback: Function) {
|
|
|
|
|
this.messageEvents.push(callback)
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-18 19:39:11 +08:00
|
|
|
onError(callback: Function) {
|
|
|
|
|
this.errorEvents.push(callback)
|
2025-02-17 10:22:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-28 11:33:07 +08:00
|
|
|
sendMsg(data: ArrayBuffer|string): Promise<void> {
|
2025-02-17 10:22:12 +08:00
|
|
|
return this.udp?.getState().then(() => {
|
|
|
|
|
return this.udp.send({
|
|
|
|
|
data,
|
2025-03-26 16:07:39 +08:00
|
|
|
address: {
|
|
|
|
|
address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1
|
|
|
|
|
}
|
2025-02-17 10:22:12 +08:00
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-03-19 10:35:16 +08:00
|
|
|
|
2025-03-26 16:07:39 +08:00
|
|
|
create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
|
2025-03-19 10:35:16 +08:00
|
|
|
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)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-03-18 19:39:11 +08:00
|
|
|
}
|
2025-03-18 17:41:54 +08:00
|
|
|
|
2025-03-26 17:10:22 +08:00
|
|
|
// 获取后置机信号
|
2025-03-27 14:33:40 +08:00
|
|
|
// 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失败"
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// 给中心发送GPS消息
|
|
|
|
|
class centerUDPClient extends UdpClient {
|
2025-03-18 19:39:11 +08:00
|
|
|
async init(context: common.UIAbilityContext) {
|
|
|
|
|
try {
|
|
|
|
|
const fileUtil = new FileUtils(context)
|
|
|
|
|
const data = await fileUtil.readFile("" + '/config/ipConfig.txt');
|
2025-03-26 14:48:56 +08:00
|
|
|
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')
|
2025-03-18 19:39:11 +08:00
|
|
|
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失败"
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-03-26 17:10:22 +08:00
|
|
|
// 顶灯
|
2025-03-18 19:39:11 +08:00
|
|
|
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失败"
|
|
|
|
|
})
|
|
|
|
|
}
|
2025-03-18 17:41:54 +08:00
|
|
|
}
|
2025-02-17 10:22:12 +08:00
|
|
|
}
|
|
|
|
|
|
2025-03-18 19:39:11 +08:00
|
|
|
// obj
|
2025-03-27 14:33:40 +08:00
|
|
|
// export const objUDPClient = new ObjUdpClient()
|
2025-02-17 10:30:31 +08:00
|
|
|
|
2025-03-27 14:33:40 +08:00
|
|
|
// 中心GPS
|
|
|
|
|
export const CenterUDPClient = new centerUDPClient()
|
2025-02-17 10:30:31 +08:00
|
|
|
|
2025-03-18 17:41:54 +08:00
|
|
|
// 顶灯
|
2025-03-18 19:39:11 +08:00
|
|
|
export const lightUDPClient = new LightUDPClient()
|
2025-02-17 10:30:31 +08:00
|
|
|
|