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