udp tcp 逻辑优化
This commit is contained in:
		
							parent
							
								
									7e05ec862e
								
							
						
					
					
						commit
						64bd47670f
					
				| @ -5,6 +5,9 @@ import FileUtil from '../../common/utils/File' | ||||
| import { GlobalConfig } from '../../config/index' | ||||
| 
 | ||||
| export async function getTCP(flag = false) { | ||||
|   if (globalThis.TcpClient && globalThis.TcpClient.closeTcp && !flag) { | ||||
|     return | ||||
|   } | ||||
|   globalThis.getCloseTcp = true | ||||
|   const fileUtil = new FileUtil(globalThis.context) | ||||
|   const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt'); | ||||
| @ -14,74 +17,34 @@ export async function getTCP(flag = false) { | ||||
|     } | ||||
|   } else { | ||||
|     const result = JSON.parse(data) | ||||
|     if (globalThis.TcpClient && globalThis.TcpClient.closeTcp && !flag) { | ||||
|       globalThis.TcpClient.closeTcp(async () => { | ||||
|         console.log("tcp init", result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort) | ||||
|         setTimeout(async () => { | ||||
|           const tcpClient: TcpClient = new TcpClient(result.tcplocalIp, result.tcplocalIpPort, result.tcpOppositeIp, result.tcpOppositePort) | ||||
|           globalThis.TcpClient = tcpClient | ||||
|           await globalThis.TcpClient.bindTcp() | ||||
|           await globalThis.TcpClient.connectTcp() | ||||
|     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 | ||||
|     await globalThis.TcpClient.bindTcp() | ||||
|     await globalThis.TcpClient.connectTcp() | ||||
|     await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
 | ||||
| 
 | ||||
|           clearInterval(globalThis.intervalSendmsg) | ||||
|           globalThis.intervalSendmsg = setInterval(() => { | ||||
|             console.log('getCloseTcp', globalThis.getCloseTcp) | ||||
|             if (!globalThis.getCloseTcp) { | ||||
|               globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
 | ||||
|             } | ||||
|           }, 1000 / 3) | ||||
|           globalThis.TcpClient.onError((val) => { | ||||
|             setTimeout(() => { | ||||
|               getTCP() | ||||
|             }, 1000) | ||||
|           }) | ||||
|           await globalThis.TcpClient.onMessage((val) => { | ||||
|             setTimeout(() => { | ||||
|               if (val) { | ||||
|                 console.log('socketTag[PLC.UdpClient] status:', globalThis.udpClient.getStatus()) | ||||
|                 globalThis.udpClient?.sendMsg(val) | ||||
|                 globalThis.udpClientGps2?.sendMsg(val) | ||||
|               } | ||||
|             }, 1000) | ||||
| 
 | ||||
|           }) | ||||
|         }, 2000) | ||||
|       }) | ||||
| 
 | ||||
|     } | ||||
|     else { | ||||
|       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 | ||||
|       await globalThis.TcpClient.bindTcp() | ||||
|       await globalThis.TcpClient.connectTcp() | ||||
|       await globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
 | ||||
| 
 | ||||
|       clearInterval(globalThis.intervalSendmsg) | ||||
|       globalThis.intervalSendmsg = setInterval(() => { | ||||
|         // console.log('getCloseTcp',globalThis.getCloseTcp)
 | ||||
|         if (!globalThis.getCloseTcp) { | ||||
|           globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
 | ||||
|     clearInterval(globalThis.intervalSendmsg) | ||||
|     globalThis.intervalSendmsg = setInterval(() => { | ||||
|       if (!globalThis.getCloseTcp) { | ||||
|         globalThis.TcpClient.sendMsg(globalThis.carInfo.carNo) //1002
 | ||||
|       } | ||||
|     }, 1000 / 3) | ||||
|     globalThis.TcpClient.onError((val) => { | ||||
|       setTimeout(() => { | ||||
|         getTCP() | ||||
|       }, 1000) | ||||
|     }) | ||||
|     await globalThis.TcpClient.onMessage((val) => { | ||||
|       setTimeout(() => { | ||||
|         if (val && globalThis.udpClient?.sendMsg) { | ||||
|           globalThis.udpClient?.sendMsg(val) | ||||
|           globalThis.udpClientGps2?.sendMsg(val) | ||||
|         } | ||||
|       }, 1000 / 3) | ||||
|       globalThis.TcpClient.onError((val) => { | ||||
|         setTimeout(() => { | ||||
|           getTCP() | ||||
|         }, 1000) | ||||
|       }) | ||||
|       await globalThis.TcpClient.onMessage((val) => { | ||||
|         setTimeout(() => { | ||||
|           if (val && globalThis.udpClient?.sendMsg) { | ||||
|             globalThis.udpClient?.sendMsg(val) | ||||
|             globalThis.udpClientGps2?.sendMsg(val) | ||||
|           } | ||||
|         }, 1000) | ||||
|       }, 1000) | ||||
| 
 | ||||
|       }) | ||||
|     } | ||||
|     }) | ||||
|   } | ||||
|   return | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| // globalThis.udpClient.bindUdp()
 | ||||
|  | ||||
| @ -9,9 +9,12 @@ export async function sendMsg(val) { | ||||
| 
 | ||||
| export async function getUDP(context, errorFlag?) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (globalThis.udpClient && globalThis.udpClient.closeUdp && !errorFlag) { | ||||
|       resolve(true) | ||||
|       return | ||||
|     } | ||||
|     const fileUtil = new FileUtil(context) | ||||
|     const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt'); | ||||
|     console.log('ttttt', data) | ||||
|     if (data === '' || data === undefined) { | ||||
|       console.log(` getUDP  has no IPConfigTable `); | ||||
|       globalThis.udpClient = {} | ||||
| @ -19,56 +22,34 @@ export async function getUDP(context, errorFlag?) { | ||||
|       resolve('') | ||||
|     } else { | ||||
|       const result = JSON.parse(data) | ||||
|       console.log(` getUDP  has IPConfigTable `); | ||||
|       if (globalThis.udpClient && globalThis.udpClient.closeUdp) { | ||||
|         console.log(` getUDP  has udclent close and rebind `); | ||||
|         globalThis.udpClient.closeUdp(() => { | ||||
|           setTimeout(() => { | ||||
|             globalThis.udpClient.rebindUdp(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort) | ||||
|             globalThis.udpClient.sendMsg('111', null) | ||||
|             globalThis.host = `http://${result.centerIp}:${result.centerPort}` | ||||
|             globalThis.udpClient.onError_Callback(() => { | ||||
|               console.log('getUDPgetUDPgetUDP') | ||||
|               getUDP(context, true) | ||||
|               if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) { | ||||
|                 globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1 | ||||
|               } | ||||
|             }) | ||||
|             resolve(`http://${result.centerIp}:${result.centerPort}`) | ||||
| 
 | ||||
|           }, 1000) | ||||
|         }) | ||||
| 
 | ||||
|       } | ||||
|       else { | ||||
|         // 未绑定
 | ||||
|         console.log(` getUDP  has no  udclent and  bind `); | ||||
|         console.log('ttttttdata', JSON.stringify(result)) | ||||
|         const udpClient: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort) | ||||
|         udpClient.bindUdp() | ||||
|         udpClient.sendMsg('111') | ||||
|         globalThis.host = `http://${result.centerIp}:${result.centerPort}` | ||||
|         globalThis.udpClient = udpClient | ||||
| 
 | ||||
|         globalThis.udpClient.onMessage_1(() => { | ||||
|           console.log('getUDPonMessage_1msgmsgByGloalUdp') | ||||
|         }) | ||||
|         globalThis.udpClient.onError_Callback(() => { | ||||
|           getUDP(context, true) | ||||
|           if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) { | ||||
|             globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1 | ||||
|           } | ||||
|         }) | ||||
|         getChuankouFn() | ||||
|         resolve(`http://${result.centerIp}:${result.centerPort}`) | ||||
|       } | ||||
|       // 未绑定
 | ||||
|       console.log(` getUDP  has no udp clent and  bind `); | ||||
|       const udpClient: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort) | ||||
|       udpClient.bindUdp() | ||||
|       udpClient.sendMsg('111') | ||||
|       globalThis.host = `http://${result.centerIp}:${result.centerPort}` | ||||
|       globalThis.udpClient = udpClient | ||||
|       globalThis.udpClient.onMessage_1(() => { | ||||
|         console.log('getUDPonMessage_1msgmsgByGloalUdp') | ||||
|       }) | ||||
|       globalThis.udpClient.onError_Callback(() => { | ||||
|         getUDP(context, true) | ||||
|         if (errorFlag && globalThis.udpClient && globalThis.udpClient.onMessage_1) { | ||||
|           globalThis.udpClient.onMessage_1 = globalThis.udpClient.onMessage_1 | ||||
|         } | ||||
|       }) | ||||
|       getChuankouFn() | ||||
|       resolve(`http://${result.centerIp}:${result.centerPort}`) | ||||
|     } | ||||
|     return | ||||
|   }) | ||||
| } | ||||
| 
 | ||||
| export async function getUDPGps2(context, errorFlag?) { | ||||
|   return new Promise(async (resolve, reject) => { | ||||
|     if (globalThis.udpClientGps2 && globalThis.udpClientGps2.closeUdp && !errorFlag) { | ||||
|       resolve(true) | ||||
|       return | ||||
|     } | ||||
|     const fileUtil = new FileUtil(context) | ||||
|     const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt'); | ||||
|     console.log('ttttt', data) | ||||
| @ -77,49 +58,31 @@ export async function getUDPGps2(context, errorFlag?) { | ||||
|       resolve('') | ||||
|     } else { | ||||
|       const result = JSON.parse(data) | ||||
|       if (globalThis.udpClientGps2 && globalThis.udpClientGps2.closeUdp) { | ||||
|         console.log(` getUDPGPS2  has udclent close and rebind `); | ||||
|         globalThis.udpClientGps2.closeUdp(() => { | ||||
|           setTimeout(() => { | ||||
|             globalThis.udpClientGps2.rebindUdp(result.udplocalIp, Number(result.udplocalIpPort) + 1, result.udpOppositeIp2, Number(result.udpOppositeIpPort) + 1) | ||||
|             globalThis.udpClientGps2.sendMsg('111', null) | ||||
|             globalThis.udpClientGps2.onError_Callback(() => { | ||||
|               console.log('getUDPGPS') | ||||
|               getUDP(context, true) | ||||
|               if (errorFlag && globalThis.udpClientGps2 && globalThis.udpClientGps2.onMessage_1) { | ||||
|                 globalThis.udpClientGps2.onMessage_1 = globalThis.udpClientGps2.onMessage_1 | ||||
|               } | ||||
|             }) | ||||
|             resolve(`http://${result.centerIp}:${result.centerPort}`) | ||||
|           }, 1000) | ||||
|         }) | ||||
|       // 未绑定
 | ||||
|       console.log(` getUDPGPS2  has no  udclent and  bind `); | ||||
|       const udpClient: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, (Number(result.udplocalIpPort) + 1).toString(), result.udpOppositeIp2, (Number(result.udpOppositeIpPort) + 1).toString()) | ||||
|       udpClient.bindUdp() | ||||
|       udpClient.sendMsg('111') | ||||
|       globalThis.udpClientGps2 = udpClient | ||||
| 
 | ||||
|       } | ||||
|       else { | ||||
|         // 未绑定
 | ||||
|         console.log(` getUDPGPS2  has no  udclent and  bind `); | ||||
|         const udpClient: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, (Number(result.udplocalIpPort) + 1).toString(), result.udpOppositeIp2, (Number(result.udpOppositeIpPort) + 1).toString()) | ||||
|         udpClient.bindUdp() | ||||
|         udpClient.sendMsg('111') | ||||
|         globalThis.udpClientGps2 = udpClient | ||||
| 
 | ||||
|         globalThis.udpClientGps2.onMessage_1(() => { | ||||
|           console.log('getUDPGPS2 onMessage_1 MsgByGlobalUdpGPS2') | ||||
|         }) | ||||
|         globalThis.udpClientGps2.onError_Callback(() => { | ||||
|           getUDPGps2(context, true) | ||||
|           if (errorFlag && globalThis.udpClientGps2 && globalThis.udpClientGps2.onMessage_1) { | ||||
|             globalThis.udpClientGps2.onMessage_1 = globalThis.udpClientGps2.onMessage_1 | ||||
|           } | ||||
|         }) | ||||
|         resolve(`http://${result.centerIp}:${result.centerPort}`) | ||||
|       } | ||||
|       globalThis.udpClientGps2.onMessage_1(() => { | ||||
|         console.log('getUDPGPS2 onMessage_1 MsgByGlobalUdpGPS2') | ||||
|       }) | ||||
|       globalThis.udpClientGps2.onError_Callback(() => { | ||||
|         getUDPGps2(context, true) | ||||
|         if (errorFlag && globalThis.udpClientGps2 && globalThis.udpClientGps2.onMessage_1) { | ||||
|           globalThis.udpClientGps2.onMessage_1 = globalThis.udpClientGps2.onMessage_1 | ||||
|         } | ||||
|       }) | ||||
|       resolve(`http://${result.centerIp}:${result.centerPort}`) | ||||
|     } | ||||
|     return | ||||
|   }) | ||||
| } | ||||
| 
 | ||||
| export async function getUDP2(context, errorFlag?) { | ||||
|   if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp && !errorFlag) { | ||||
|     return | ||||
|   } | ||||
|   const fileUtil = new FileUtil(context) | ||||
|   const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt'); | ||||
|   if (data === '' || data === undefined) { | ||||
| @ -129,74 +92,56 @@ export async function getUDP2(context, errorFlag?) { | ||||
|   } else { | ||||
|     const result = JSON.parse(data) | ||||
|     console.log(` getUDP2  has IPConfigTable `); | ||||
|     if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp) { | ||||
|       console.log(` getUDP2  has udclent ,close and rebind `); | ||||
|       globalThis.udpClient2.closeUdp(async () => { | ||||
|         setTimeout(() => { | ||||
|           globalThis.udpClient2.rebindUdp(result.udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort) | ||||
|           globalThis.udpClient2.onError_Callback(() => { | ||||
|             getUDP2(context, true); | ||||
|             if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) { | ||||
|               globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2 | ||||
|             } | ||||
|           }) | ||||
|         }, 1000) | ||||
|       }) | ||||
|     } | ||||
|     else { | ||||
|       // 未绑定
 | ||||
|       console.log(` getUDP2  has no  udclent and  bind `); | ||||
|       const udpClient2: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort) | ||||
|       console.info('surenjun udp2=> ', globalThis.carInfo?.messagePort) | ||||
|       udpClient2.bindUdp() | ||||
|       udpClient2.onError_Callback(() => { | ||||
|         getUDP2(context, true); | ||||
|         if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) { | ||||
|           globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2 | ||||
|     // 未绑定
 | ||||
|     console.log(` getUDP2  has no  udclent and  bind `); | ||||
|     const udpClient2: UdpClientByCenter = new UdpClientByCenter(result.udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort) | ||||
|     console.info('surenjun udp2=> ', globalThis.carInfo?.messagePort) | ||||
|     udpClient2.bindUdp() | ||||
|     udpClient2.onError_Callback(() => { | ||||
|       getUDP2(context, true); | ||||
|       if (errorFlag && globalThis.udpClient2 && globalThis.udpClient2.onMessage_2) { | ||||
|         globalThis.udpClient2.onMessage_2 = globalThis.udpClient2.onMessage_2 | ||||
|       } | ||||
|     }) | ||||
|     if (!errorFlag) { | ||||
|       udpClient2.onMessage_2((val) => { | ||||
|         if (val.id == '32') { | ||||
|           globalThis.signNum = val.body[1] | ||||
|           if (val.body[0] == 5) { | ||||
|             console.info('surenjun', `接收到平台扣分请求,请求指令为:${val.body[1]}`) | ||||
|             globalThis.udpEvent.sendOnKf(val.body[1]) | ||||
|           } | ||||
|           if (val.body[0] == 11) { | ||||
|             console.info('surenjun', 'GlobalUdp收到beginExam事件') | ||||
|             globalThis.udpEvent.sendBeginExam(val.body[1]) | ||||
|           } | ||||
|           if (val.body[0] == 12) { | ||||
|             console.info('surenjun', 'GlobalUdp收到endExam事件') | ||||
|             globalThis.udpEvent.sendEndExam(val.body[1]) | ||||
|           } | ||||
|         } | ||||
|         if (val.id == '36') { | ||||
|           //获取远程扣分内容
 | ||||
|           console.info('surenjun', '考车收到远程扣分项目内容,扣分代码:' + val.body[0]) | ||||
|           globalThis.udpEvent.sendKfContent(val.body[0]) | ||||
|         } else if (val.id == '46') { | ||||
|           let tmpList = [] | ||||
|           const str = globalThis.lsh | ||||
|           for (let i = 0; i < str.length; i++) { | ||||
|             tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0]) | ||||
|           } | ||||
|           const param = { | ||||
|             id: 47, | ||||
|             list: tmpList, | ||||
|             carNo: globalThis.carInfo.carNo, | ||||
|             placeId: globalThis.carInfo.examinationRoomId | ||||
|           } | ||||
|           globalThis.udpClient2.send(param) | ||||
|         } | ||||
|       }) | ||||
|       if (!errorFlag) { | ||||
|         udpClient2.onMessage_2((val) => { | ||||
|           if (val.id == '32') { | ||||
|             globalThis.signNum = val.body[1] | ||||
|             if (val.body[0] == 5) { | ||||
|               console.info('surenjun', `接收到平台扣分请求,请求指令为:${val.body[1]}`) | ||||
|               globalThis.udpEvent.sendOnKf(val.body[1]) | ||||
|             } | ||||
|             if (val.body[0] == 11) { | ||||
|               console.info('surenjun', 'GlobalUdp收到beginExam事件') | ||||
|               globalThis.udpEvent.sendBeginExam(val.body[1]) | ||||
|             } | ||||
|             if (val.body[0] == 12) { | ||||
|               console.info('surenjun', 'GlobalUdp收到endExam事件') | ||||
|               globalThis.udpEvent.sendEndExam(val.body[1]) | ||||
|             } | ||||
|           } | ||||
|           if (val.id == '36') { | ||||
|             //获取远程扣分内容
 | ||||
|             console.info('surenjun', '考车收到远程扣分项目内容,扣分代码:' + val.body[0]) | ||||
|             globalThis.udpEvent.sendKfContent(val.body[0]) | ||||
|           } else if (val.id == '46') { | ||||
|             let tmpList = [] | ||||
|             const str = globalThis.lsh | ||||
|             for (let i = 0; i < str.length; i++) { | ||||
|               tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0]) | ||||
|             } | ||||
|             const param = { | ||||
|               id: 47, | ||||
|               list: tmpList, | ||||
|               carNo: globalThis.carInfo.carNo, | ||||
|               placeId: globalThis.carInfo.examinationRoomId | ||||
|             } | ||||
|             globalThis.udpClient2.send(param) | ||||
|           } | ||||
|         }) | ||||
|       } | ||||
|       globalThis.udpClient2 = udpClient2 | ||||
| 
 | ||||
|     } | ||||
|     globalThis.udpClient2 = udpClient2 | ||||
|   } | ||||
|   return | ||||
| } | ||||
| 
 | ||||
| export async function setTopLineUdp() { | ||||
| @ -227,7 +172,6 @@ export async function setJudgeUdp() { | ||||
|   // const config = await getSyncData('IpConfigTable');
 | ||||
|   let udpIndex = 0; | ||||
|   let currentUdpIndex = 0; | ||||
|   let judgeUdpTimer | ||||
|   clearInterval(globalThis.judgeUdpTimer) | ||||
|   globalThis.judgeUdpTimer = setInterval(() => { | ||||
|     udpIndex += 1; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user