tcp独立进程
This commit is contained in:
parent
6c45615d8c
commit
543e779107
@ -1,58 +1,66 @@
|
|||||||
import TcpClient from './TcpClient';
|
import TcpClient from './TcpClient';
|
||||||
import { getSyncData } from '../service/initable';
|
|
||||||
import hilog from '@ohos.hilog';
|
|
||||||
import FileUtil from '../../common/utils/File'
|
import FileUtil from '../../common/utils/File'
|
||||||
import { GlobalConfig } from '../../config/index'
|
import { GlobalConfig } from '../../config/index'
|
||||||
|
import UdpClientByCenter from './UdpClientByCenter';
|
||||||
|
|
||||||
|
let tcpClient: TcpClient = null
|
||||||
|
let udpGps1: UdpClientByCenter = null
|
||||||
|
let udpGps2: UdpClientByCenter = null
|
||||||
|
let tick: number
|
||||||
|
let lastTime = new Date()
|
||||||
|
|
||||||
export async function getTCP(flag = false) {
|
export async function getTCP(flag = false) {
|
||||||
if (globalThis.TcpClient && globalThis.TcpClient.closeTcp && !flag) {
|
if (tcpClient && !flag) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
globalThis.getCloseTcp = true
|
if (flag) {
|
||||||
|
console.log("tcp 重启服务")
|
||||||
|
}
|
||||||
const fileUtil = new FileUtil(globalThis.context)
|
const fileUtil = new FileUtil(globalThis.context)
|
||||||
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
|
const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
|
||||||
if (data === '' || data === undefined) {
|
if (data === '' || data === undefined) {
|
||||||
globalThis.TcpClient = {}
|
|
||||||
globalThis.TcpClient.onMessage = () => {
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
const result = JSON.parse(data)
|
const result = JSON.parse(data)
|
||||||
console.log("tcp init", result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
|
console.log("tcp init", result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
|
||||||
const tcpClient: TcpClient = new TcpClient(result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
|
tcpClient = new TcpClient(result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort)
|
||||||
globalThis.TcpClient = tcpClient
|
udpGps1 = new UdpClientByCenter(result.udplocalIp, (Number(result.udplocalIpPort) + 10).toString(), result.udpOppositeIp, result.udpOppositeIpPort)
|
||||||
|
console.log("tcp udp port", result.udplocalIp, (Number(result.udplocalIpPort) + 10).toString(), result.udpOppositeIp, result.udpOppositeIpPort)
|
||||||
|
if (result.udpOppositeIp2) {
|
||||||
|
udpGps2 = new UdpClientByCenter(result.udplocalIp, (Number(result.udplocalIpPort) + 11).toString(), result.udpOppositeIp2, (Number(result.udpOppositeIpPort) + 1).toString())
|
||||||
|
}
|
||||||
|
udpGps1?.bindUdp()
|
||||||
|
udpGps2?.bindUdp()
|
||||||
|
await tcpClient?.bindTcp()
|
||||||
|
await tcpClient?.connectTcp()
|
||||||
|
clearInterval(tick)
|
||||||
|
|
||||||
await globalThis.TcpClient.bindTcp()
|
tick = setInterval(() => {
|
||||||
await globalThis.TcpClient.connectTcp()
|
let now = new Date()
|
||||||
await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
|
if (now.getTime() - lastTime.getTime() > (1000 * 6)) {
|
||||||
|
console.log("差分tcp信号丢失")
|
||||||
clearInterval(globalThis.intervalSendMsg)
|
|
||||||
|
|
||||||
globalThis.intervalSendMsg = setInterval(() => {
|
|
||||||
if (!globalThis.getCloseTcp) {
|
|
||||||
globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
|
|
||||||
globalThis.tcpStep += 1
|
|
||||||
if (globalThis.tcpStep > 4) {
|
|
||||||
globalThis.tcpStep = 0
|
|
||||||
getTCP(true)
|
|
||||||
console.log('tcp重连开始')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}, 1000 * 2)
|
|
||||||
globalThis.TcpClient.onError((val) => {
|
|
||||||
setTimeout(() => {
|
|
||||||
getTCP(true)
|
getTCP(true)
|
||||||
}, 1000)
|
return
|
||||||
|
}
|
||||||
|
if (globalThis.carInfo.carNo) {
|
||||||
|
tcpClient?.sendMsg(globalThis.carInfo.carNo) //1002
|
||||||
|
}
|
||||||
|
}, 1000 * 1.5)
|
||||||
|
tcpClient?.onError(() => {
|
||||||
|
getTCP(true)
|
||||||
})
|
})
|
||||||
await globalThis.TcpClient.onMessage((val) => {
|
tcpClient?.onMessage((val) => {
|
||||||
|
lastTime = new Date()
|
||||||
console.log("tcp test 收到差分改正数 length: ", val.byteLength)
|
console.log("tcp test 收到差分改正数 length: ", val.byteLength)
|
||||||
if (val) {
|
if (val) {
|
||||||
globalThis.udpClient?.sendMsg(val, () => {
|
udpGps1?.sendMsg(val, () => {
|
||||||
console.log("tcp test 后置机写入改正数成功")
|
console.log("tcp test 后置机1写入改正数成功")
|
||||||
|
})
|
||||||
|
udpGps2?.sendMsg(val, () => {
|
||||||
|
console.log("tcp test 后置机2写入改正数成功")
|
||||||
})
|
})
|
||||||
globalThis.udpClientGps2?.sendMsg(val)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,7 +23,7 @@ export async function getUDP(context, errorFlag?) {
|
|||||||
} else {
|
} else {
|
||||||
const result = JSON.parse(data)
|
const result = JSON.parse(data)
|
||||||
// 未绑定
|
// 未绑定
|
||||||
console.log(` getUDP has no udp clent and bind `);
|
console.log(`getUDP has no udp clent and bind `);
|
||||||
const udpClient: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort)
|
const udpClient: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort)
|
||||||
udpClient.bindUdp()
|
udpClient.bindUdp()
|
||||||
udpClient.sendMsg('111')
|
udpClient.sendMsg('111')
|
||||||
|
|||||||
@ -12,12 +12,8 @@ export default class TcpClient {
|
|||||||
private localIpPort: string = ''
|
private localIpPort: string = ''
|
||||||
private oppositeIp: string = ''
|
private oppositeIp: string = ''
|
||||||
private oppositeIpPort: string = ''
|
private oppositeIpPort: string = ''
|
||||||
private num: number = 0
|
|
||||||
//重连次数
|
//重连次数
|
||||||
private tcpSendNum: number = 0
|
private tcpSendNum: number = 0
|
||||||
//重连次数
|
|
||||||
private folderPath
|
|
||||||
|
|
||||||
private tcp: socket.TCPSocket = null
|
private tcp: socket.TCPSocket = null
|
||||||
|
|
||||||
constructor(tcplocalIp: string, tcplocalIpPort: string, tcpOppositeIp: string, tcpOppositePort: string) {
|
constructor(tcplocalIp: string, tcplocalIpPort: string, tcpOppositeIp: string, tcpOppositePort: string) {
|
||||||
@ -31,11 +27,8 @@ export default class TcpClient {
|
|||||||
|
|
||||||
onError(callback?) {
|
onError(callback?) {
|
||||||
this.tcp.on('error', err => {
|
this.tcp.on('error', err => {
|
||||||
globalThis.getCloseTcp = true
|
console.log(TAG, 'tcp on error: ', JSON.stringify(err))
|
||||||
console.log(TAG, 'getCloseTtcpOnerror', JSON.stringify(err))
|
callback?.()
|
||||||
setTimeout(async () => {
|
|
||||||
getTCP()
|
|
||||||
}, 2000)
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -43,111 +36,62 @@ export default class TcpClient {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
this.tcp.bind({
|
this.tcp.bind({
|
||||||
address: this.localIp, port: Number(this.localIpPort), family: 1
|
address: this.localIp, port: Number(this.localIpPort), family: 1
|
||||||
}, err => {
|
}).then(() => {
|
||||||
if (err) {
|
resolve(true)
|
||||||
setTimeout(async () => {
|
}).catch(err => {
|
||||||
getTCP()
|
console.log("tcp bind error: ", JSON.stringify(err))
|
||||||
}, 2000)
|
getTCP(true)
|
||||||
console.log('getCloseTBinderror');
|
reject(err)
|
||||||
hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err));
|
|
||||||
resolve(true)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
resolve(false)
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
connectTcp() {
|
connectTcp() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
let promise = this.tcp.connect({
|
this.tcp.connect({
|
||||||
address: {
|
address: {
|
||||||
address: this.oppositeIp, port: Number(this.oppositeIpPort), family: 1
|
address: this.oppositeIp, port: Number(this.oppositeIpPort), family: 1
|
||||||
}, timeout: 1000 * 15
|
}, timeout: 1000 * 15
|
||||||
});
|
}).then(() => {
|
||||||
|
return this.tcp.setExtraOptions({
|
||||||
promise.then(() => {
|
keepAlive: true
|
||||||
this.tcp.setExtraOptions({
|
})
|
||||||
keepAlive: true,
|
}).then(() => {
|
||||||
}, err => {
|
|
||||||
if (err) {
|
|
||||||
console.log('getCloseTconnectsuccess', 'error', globalThis.getCloseTcp)
|
|
||||||
setTimeout(() => {
|
|
||||||
getTCP()
|
|
||||||
resolve(false)
|
|
||||||
}, 9000)
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
globalThis.getCloseTcp = false
|
|
||||||
resolve(true)
|
resolve(true)
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log('socketTag', 'error')
|
console.log("tcp connect or keepAlive error: ", JSON.stringify(err))
|
||||||
setTimeout(() => {
|
getTCP()
|
||||||
getTCP()
|
reject(err)
|
||||||
resolve(false)
|
})
|
||||||
}, 9000)
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
sendMsg(msg: string) {
|
sendMsg(msg: string) {
|
||||||
return new Promise((resolve, reject) => {
|
this.tcp.send({
|
||||||
let promise = this.tcp.send({
|
data: msg
|
||||||
data: msg
|
}).catch(err => {
|
||||||
});
|
console.log("tcp send error: ", JSON.stringify(err))
|
||||||
promise.then(() => {
|
this.tcpSendNum++
|
||||||
console.log(`${TAG} TCP send success`)
|
if (this.tcpSendNum > 10) {
|
||||||
resolve(true)
|
getTCP(true)
|
||||||
}).catch(err => {
|
}
|
||||||
console.log(`${TAG} TCP send error ${JSON.stringify(err)}`)
|
|
||||||
this.tcpSendNum++
|
|
||||||
if (!globalThis.getCloseTcp && this.tcpSendNum > 10) {
|
|
||||||
globalThis.getCloseTcp = true
|
|
||||||
setTimeout(async () => {
|
|
||||||
getTCP(true)
|
|
||||||
}, 3000)
|
|
||||||
this.tcpSendNum = 0
|
|
||||||
return
|
|
||||||
}
|
|
||||||
reject(false)
|
|
||||||
});
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
onMessage(callback?) {
|
onMessage(callback?) {
|
||||||
this.tcp.on('message', value => {
|
this.tcp.on('message', value => {
|
||||||
globalThis.tcpStep = 0
|
|
||||||
globalThis.tcpUdpError = false
|
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
let dataView = new DataView(value.message)
|
let dataView = new DataView(value.message)
|
||||||
const Arraybuffer = value.message.slice(5, dataView?.byteLength);
|
const Arraybuffer = value.message.slice(5, dataView?.byteLength);
|
||||||
|
callback?.(Arraybuffer)
|
||||||
callback && callback(Arraybuffer)
|
|
||||||
} else {
|
} else {
|
||||||
callback && callback('')
|
callback?.('')
|
||||||
}
|
}
|
||||||
clearInterval(globalThis.intervaltcp)
|
|
||||||
globalThis.intervaltcp = setInterval(() => {
|
|
||||||
//程序断开
|
|
||||||
if (globalThis.tcpUdpError) {
|
|
||||||
console.log(TAG, 'tcp信号丢失')
|
|
||||||
prompt.showToast({
|
|
||||||
message: 'tcp信号丢失',
|
|
||||||
duration: 2000
|
|
||||||
});
|
|
||||||
}
|
|
||||||
globalThis.tcpUdpError = true;
|
|
||||||
}, 3000)
|
|
||||||
// callback(value.message)
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
offTcp(callback) {
|
offTcp(callback) {
|
||||||
console.log(TAG, 'tcpofff')
|
console.log(TAG, 'tcp off')
|
||||||
this.tcp.off('message', callback);
|
this.tcp.off('message', callback);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -155,19 +99,13 @@ export default class TcpClient {
|
|||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
console.log(TAG, 'tcpClose')
|
console.log(TAG, 'tcpClose')
|
||||||
this.tcp.close().then(() => {
|
this.tcp.close().then(() => {
|
||||||
globalThis.getCloseTcp = true
|
console.log(TAG, 'tcp close success')
|
||||||
console.log('getCloseTcpsocketTagclose', 'success', globalThis.getCloseTcp)
|
callback?.()
|
||||||
|
|
||||||
console.log(TAG, 'tcpCloseSuccess')
|
|
||||||
callback()
|
|
||||||
resolve(true)
|
resolve(true)
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
setTimeout(async () => {
|
console.log('close tcp error: ', JSON.stringify(err))
|
||||||
getTCP(true)
|
getTCP(true)
|
||||||
}, 9000)
|
reject(err)
|
||||||
console.log('getCloseTcpsocketTagclose', 'error', globalThis.getCloseTcp)
|
|
||||||
|
|
||||||
resolve(false)
|
|
||||||
});
|
});
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -164,6 +164,9 @@ struct Index {
|
|||||||
// A2牵引车2号导航仪
|
// A2牵引车2号导航仪
|
||||||
if (carInfo.kscx == 'A2' && !globalThis.udpClientGps2) {
|
if (carInfo.kscx == 'A2' && !globalThis.udpClientGps2) {
|
||||||
await getUDPGps2(this.context, false)
|
await getUDPGps2(this.context, false)
|
||||||
|
this.serviceExtProxy.processData("1", () => {
|
||||||
|
|
||||||
|
})
|
||||||
}
|
}
|
||||||
getSyncData('MA_SYSSET').then((data: any[]) => {
|
getSyncData('MA_SYSSET').then((data: any[]) => {
|
||||||
data.forEach(sys => {
|
data.forEach(sys => {
|
||||||
@ -283,6 +286,9 @@ struct Index {
|
|||||||
if (carInfo.kscx == 'A2') {
|
if (carInfo.kscx == 'A2') {
|
||||||
console.log("GPS2 udp")
|
console.log("GPS2 udp")
|
||||||
getUDPGps2(this.context, false)
|
getUDPGps2(this.context, false)
|
||||||
|
this.serviceExtProxy.processData("1", () => {
|
||||||
|
|
||||||
|
})
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@ -297,12 +297,28 @@ export default struct SignDisplayCom {
|
|||||||
|
|
||||||
Flex({ direction: FlexDirection.Column }) {
|
Flex({ direction: FlexDirection.Column }) {
|
||||||
ForEach(this.GPSColum, (item) => {
|
ForEach(this.GPSColum, (item) => {
|
||||||
Column() {
|
if (item.key === '龄期') {
|
||||||
Text(`${item.key}:${item.value}`)
|
Column() {
|
||||||
.fontSize((item.key === '龄期' ? 20 : 12) * this.ratio)
|
Text(`${item.key}:${item.value}`)
|
||||||
.lineHeight(20 * this.ratio)
|
.fontSize(20 * this.ratio)
|
||||||
.fontColor(item.key === '龄期' ? "#6b96f2" : '#fff')
|
.lineHeight(20 * this.ratio)
|
||||||
}.height(20 * this.ratio).justifyContent(FlexAlign.Start).width('100%')
|
.fontColor("#6b96f2")
|
||||||
|
}.height(20 * this.ratio).justifyContent(FlexAlign.Start).width('100%')
|
||||||
|
} else if (item.key === "状态") {
|
||||||
|
Column() {
|
||||||
|
Text(`${item.key}:${item.value}`)
|
||||||
|
.fontSize(20 * this.ratio)
|
||||||
|
.lineHeight(20 * this.ratio)
|
||||||
|
.fontColor(item.value != '4' ? "#ff5e5e" : "#6b96f2")
|
||||||
|
}.height(20 * this.ratio).justifyContent(FlexAlign.Start).width('100%')
|
||||||
|
} else {
|
||||||
|
Column() {
|
||||||
|
Text(`${item.key}:${item.value}`)
|
||||||
|
.fontSize(12 * this.ratio)
|
||||||
|
.lineHeight(20 * this.ratio)
|
||||||
|
.fontColor('#fff')
|
||||||
|
}.height(20 * this.ratio).justifyContent(FlexAlign.Start).width('100%')
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user