合并dev
This commit is contained in:
		
							parent
							
								
									8cf02ec456
								
							
						
					
					
						commit
						0b2b653b8e
					
				| @ -13,6 +13,7 @@ export default class TcpClient { | |||||||
|   private oppositeIp: string = '' |   private oppositeIp: string = '' | ||||||
|   private oppositeIpPort: string = '' |   private oppositeIpPort: string = '' | ||||||
|   private num: number = 0//重连次数
 |   private num: number = 0//重连次数
 | ||||||
|  |   private folderPath | ||||||
| 
 | 
 | ||||||
|   private tcp: any = null |   private tcp: any = null | ||||||
| 
 | 
 | ||||||
| @ -23,6 +24,7 @@ export default class TcpClient { | |||||||
|     this.oppositeIpPort = tcpOppositePort |     this.oppositeIpPort = tcpOppositePort | ||||||
|     console.log(TAG, 'new Tcp', this.localIp, this.localIpPort, this.oppositeIp, this.oppositeIpPort) |     console.log(TAG, 'new Tcp', this.localIp, this.localIpPort, this.oppositeIp, this.oppositeIpPort) | ||||||
|     this.tcp = socket.constructTCPSocketInstance(); |     this.tcp = socket.constructTCPSocketInstance(); | ||||||
|  |     this.initPath() | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   onError(callback?) { |   onError(callback?) { | ||||||
| @ -72,6 +74,10 @@ export default class TcpClient { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   bindTcp() { |   bindTcp() { | ||||||
|  |     this.writeLog({ | ||||||
|  |       time:dateFormat(new Date()), | ||||||
|  |       message: `${TAG} tcpbind localIp${this.localIp} ${localIpPort}`, | ||||||
|  |     }) | ||||||
|     console.log(TAG, 'tcpbind', this.localIp, 'localIp', this.localIpPort) |     console.log(TAG, 'tcpbind', this.localIp, 'localIp', this.localIpPort) | ||||||
|     return new Promise((resolve, reject) => { |     return new Promise((resolve, reject) => { | ||||||
|       let promise = this.tcp.bind({ |       let promise = this.tcp.bind({ | ||||||
| @ -82,6 +88,10 @@ export default class TcpClient { | |||||||
|           //   time:dateFormat(new Date()),
 |           //   time:dateFormat(new Date()),
 | ||||||
|           //   PLC:`${TAG} testTag tcp bind faile ${JSON.stringify(err)}`,
 |           //   PLC:`${TAG} testTag tcp bind faile ${JSON.stringify(err)}`,
 | ||||||
|           // })
 |           // })
 | ||||||
|  |           this.writeLog({ | ||||||
|  |             time:dateFormat(new Date()), | ||||||
|  |             message: `${TAG} tcpbind error ${JSON.stringify(err)}`, | ||||||
|  |           }) | ||||||
|           console.log('testTag tcp bind faile'); |           console.log('testTag tcp bind faile'); | ||||||
|           globalThis.getCloseTcp = true |           globalThis.getCloseTcp = true | ||||||
|           hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err)); |           hilog.info(0x0000, 'testTag', "tcpBinderror:" + JSON.stringify(err)); | ||||||
| @ -109,6 +119,10 @@ export default class TcpClient { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   connectTcp() { |   connectTcp() { | ||||||
|  |     this.writeLog({ | ||||||
|  |       time:dateFormat(new Date()), | ||||||
|  |       message: `${TAG} tcpConnect oppositeIp ${ this.oppositeIp} ${this.oppositeIpPort}`, | ||||||
|  |     }) | ||||||
|     console.log(TAG, 'tcpConnect', this.oppositeIp, 'localIp', this.oppositeIpPort) |     console.log(TAG, 'tcpConnect', this.oppositeIp, 'localIp', this.oppositeIpPort) | ||||||
|     return new Promise((resolve, reject) => { |     return new Promise((resolve, reject) => { | ||||||
|       let promise = this.tcp.connect({ |       let promise = this.tcp.connect({ | ||||||
| @ -122,6 +136,10 @@ export default class TcpClient { | |||||||
|           keepAlive: true, |           keepAlive: true, | ||||||
|         }, err => { |         }, err => { | ||||||
|           if (err) { |           if (err) { | ||||||
|  |             this.writeLog({ | ||||||
|  |               time:dateFormat(new Date()), | ||||||
|  |               message: `${TAG} TCPconnect error ${JSON.stringify(err)}`, | ||||||
|  |             }) | ||||||
|             // this.writeLog({
 |             // this.writeLog({
 | ||||||
|             //   time:dateFormat(new Date()),
 |             //   time:dateFormat(new Date()),
 | ||||||
|             //   PLC:`${TAG} tcp connect rebind success`,
 |             //   PLC:`${TAG} tcp connect rebind success`,
 | ||||||
| @ -141,6 +159,10 @@ export default class TcpClient { | |||||||
|         globalThis.getCloseTcp = false |         globalThis.getCloseTcp = false | ||||||
|         resolve(true) |         resolve(true) | ||||||
|       }).catch(err => { |       }).catch(err => { | ||||||
|  |         this.writeLog({ | ||||||
|  |           time:dateFormat(new Date()), | ||||||
|  |           message: `${TAG} TCPconnect error2 ${JSON.stringify(err)}`, | ||||||
|  |         }) | ||||||
|         globalThis.getCloseTcp = true |         globalThis.getCloseTcp = true | ||||||
|         this.num++ |         this.num++ | ||||||
|         if(this.num>3){ |         if(this.num>3){ | ||||||
| @ -167,6 +189,10 @@ export default class TcpClient { | |||||||
|       promise.then(() => { |       promise.then(() => { | ||||||
|         reslove(true) |         reslove(true) | ||||||
|       }).catch(err => { |       }).catch(err => { | ||||||
|  |         this.writeLog({ | ||||||
|  |           time:dateFormat(new Date()), | ||||||
|  |           message: `${TAG} TCPsend error ${JSON.stringify(err)}`, | ||||||
|  |         }) | ||||||
|         reslove(false) |         reslove(false) | ||||||
|       }); |       }); | ||||||
|     }) |     }) | ||||||
| @ -180,7 +206,10 @@ export default class TcpClient { | |||||||
|       // })
 |       // })
 | ||||||
|       console.log(TAG, 'Tcponmessage', value) |       console.log(TAG, 'Tcponmessage', value) | ||||||
|       globalThis.tcpUdpError = false |       globalThis.tcpUdpError = false | ||||||
| 
 |       this.writeLog({ | ||||||
|  |         time:dateFormat(new Date()), | ||||||
|  |         message: `${TAG} Tcponmessage ,${value}`, | ||||||
|  |       }) | ||||||
|       // console.log('messageLengt',,value.message.length))
 |       // console.log('messageLengt',,value.message.length))
 | ||||||
|       console.log('testTagtcpmsg') |       console.log('testTagtcpmsg') | ||||||
|       // console.log("on message, message:" + value.message+ ", remoteInfo:" )
 |       // console.log("on message, message:" + value.message+ ", remoteInfo:" )
 | ||||||
| @ -204,18 +233,23 @@ export default class TcpClient { | |||||||
|               duration: 2000 |               duration: 2000 | ||||||
|             }); |             }); | ||||||
|           } |           } | ||||||
|  |         this.writeLog({ | ||||||
|  |           time:dateFormat(new Date()), | ||||||
|  |           message: `${TAG} tcp信号丢失`, | ||||||
|  |         }) | ||||||
|           globalThis.tcpUdpError = true; |           globalThis.tcpUdpError = true; | ||||||
|       },3000) |       },3000) | ||||||
|       // callback(value.message)
 |       // callback(value.message)
 | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
|   async  writeLog(param){ |   async writeLog(param){ | ||||||
|     // return
 |     const fileUtil = new FileUtil(globalThis.context) | ||||||
|     // const fileUtil = new FileUtil(globalThis.context)
 |     fileUtil.editFile(`${this.folderPath}/plcLog.txt`, JSON.stringify(param)+`\n`) | ||||||
|     // const date=dateFormat(new Date).split(' ')[0]
 |   } | ||||||
|     // const folderPath = await fileUtil.initFolder(`/PLC/${date}`);
 |   async initPath(){ | ||||||
|     // fileUtil.editFile(`${folderPath}/plcLog.txt`, JSON.stringify(param)+`\n`)
 |     const fileUtil = new FileUtil(globalThis.context) | ||||||
| 
 |     const date=dateFormat(new Date).split(' ')[0] | ||||||
|  |     this.folderPath = await fileUtil.initFolder(`/PLC/${date}`); | ||||||
|   } |   } | ||||||
|   offTcp(callback) { |   offTcp(callback) { | ||||||
|     console.log(TAG, 'tcpofff') |     console.log(TAG, 'tcpofff') | ||||||
| @ -225,17 +259,24 @@ export default class TcpClient { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   closeTcp(callback) { |   closeTcp(callback) { | ||||||
|  | 
 | ||||||
|     return new Promise((reslove, reject) => { |     return new Promise((reslove, reject) => { | ||||||
|       console.log(TAG, 'tcpClose') |       console.log(TAG, 'tcpClose') | ||||||
|       let promise = this.tcp.close(); |       let promise = this.tcp.close(); | ||||||
|       promise.then(() => { |       promise.then(() => { | ||||||
|         globalThis.getCloseTcp = true |         globalThis.getCloseTcp = true | ||||||
| 
 |         this.writeLog({ | ||||||
|  |           time:dateFormat(new Date()), | ||||||
|  |           message: `${TAG} tcp closeSuccess`, | ||||||
|  |         }) | ||||||
|         console.log(TAG, 'tcpCloseSuccess') |         console.log(TAG, 'tcpCloseSuccess') | ||||||
|         callback() |         callback() | ||||||
|         reslove(true) |         reslove(true) | ||||||
|       }).catch(err => { |       }).catch(err => { | ||||||
|         console.log(TAG, 'tcpClosefailed') |         this.writeLog({ | ||||||
|  |           time:dateFormat(new Date()), | ||||||
|  |           message: `${TAG} tcpclose error ${JSON.stringify(err)}`, | ||||||
|  |         }) | ||||||
|         reslove(false) |         reslove(false) | ||||||
|       }); |       }); | ||||||
|     }) |     }) | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user