| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  | import socket from '@ohos.net.socket'; | 
					
						
							|  |  |  | import { PLCGPSData } from '../../../mock/PLCGPSData'; | 
					
						
							|  |  |  | import FileUtil from '../../../common/utils/File'; | 
					
						
							|  |  |  | import { GlobalConfig } from '../../../config/index'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // import { PLCGPSData } from '../../mock';
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export default class UdpByOne { | 
					
						
							|  |  |  |   //   PLC udp
 | 
					
						
							|  |  |  |   private PLCUDP: any; | 
					
						
							|  |  |  |   // PLC localIp
 | 
					
						
							|  |  |  |   private LocalIp: string = '192.168.7.170'; | 
					
						
							|  |  |  |   // PLC localIpPort
 | 
					
						
							|  |  |  |   private PLCLocalIpPort: string = '31012'; | 
					
						
							|  |  |  |   private OppositeIp: string = '192.168.7.124' | 
					
						
							|  |  |  |   // PLC oppositeIpPort
 | 
					
						
							|  |  |  |   private PLCOppositeIpPort: string = '30012'; | 
					
						
							|  |  |  |   // PLC消息
 | 
					
						
							|  |  |  |   private PLCMsg: ArrayBuffer; | 
					
						
							|  |  |  |   //   GPS udp
 | 
					
						
							|  |  |  |   private GPSUDP: any; | 
					
						
							|  |  |  |   // GPS localIp
 | 
					
						
							|  |  |  |   // GPS localIpPort
 | 
					
						
							|  |  |  |   private GPSLocalIpPort: string = '31013'; | 
					
						
							|  |  |  |   // GPS oppositeIpPort
 | 
					
						
							|  |  |  |   private GPSOppositeIpPort: string = '30013'; | 
					
						
							|  |  |  |   // GPS消息
 | 
					
						
							|  |  |  |   private GPSMsg: any; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   private timer: number; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   constructor() { | 
					
						
							|  |  |  |     this.init() | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   async init(){ | 
					
						
							|  |  |  |     const fileUtil = new FileUtil(globalThis.context) | 
					
						
							|  |  |  |     const data = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt'); | 
					
						
							|  |  |  |     const result = JSON.parse(data) | 
					
						
							|  |  |  |     this.LocalIp = result.udplocalIp; | 
					
						
							|  |  |  |     this.OppositeIp = result.udpOppositeIp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     //   初始化UDP
 | 
					
						
							|  |  |  |     this.PLCUDP = socket.constructUDPSocketInstance(); | 
					
						
							|  |  |  |     // this.PLCUDP.bind(this.PLCLocalIp, this.PLCLocalIpPort);
 | 
					
						
							|  |  |  |     this.PLCUDP.bind({ | 
					
						
							|  |  |  |       address: this.LocalIp, port: parseInt(this.PLCLocalIpPort), family: 1 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  |     this.GPSUDP = socket.constructUDPSocketInstance(); | 
					
						
							|  |  |  |     // this.GPSUDP.bind(this.GPSLocalIp, this.GPSLocalIpPort);
 | 
					
						
							|  |  |  |     this.GPSUDP.bind({ | 
					
						
							|  |  |  |       address: this.LocalIp, port: parseInt(this.GPSLocalIpPort), family: 1 | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.PLCUDP.on("message", (res, remoteInfo) => { | 
					
						
							|  |  |  |       this.PLCMsg = res.message; | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     this.GPSUDP.on("message", (res1, remoteInfo) => { | 
					
						
							|  |  |  |       let dataView = new DataView(res1.message) | 
					
						
							|  |  |  |       let str = "" | 
					
						
							|  |  |  |       for (let i = 0; i < dataView?.byteLength; ++i) { | 
					
						
							|  |  |  |         let c = String.fromCharCode(dataView?.getUint8(i)) | 
					
						
							|  |  |  |         if (c !== "\n") { | 
					
						
							|  |  |  |           str += c | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       this.GPSMsg = str; | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  |   //   重新绑定
 | 
					
						
							|  |  |  |   public rebind() { | 
					
						
							|  |  |  |     this.PLCUDP.bind(this.LocalIp, this.PLCLocalIpPort); | 
					
						
							|  |  |  |     this.GPSUDP.bind(this.LocalIp, this.GPSLocalIpPort); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //   PLC发送消息
 | 
					
						
							|  |  |  |   public sendPLCMsg(msg: string) { | 
					
						
							|  |  |  |     this.PLCUDP.send({ | 
					
						
							|  |  |  |       data: '111111', | 
					
						
							|  |  |  |       address: { | 
					
						
							|  |  |  |         address: this.OppositeIp, | 
					
						
							|  |  |  |         port: parseInt(this.PLCOppositeIpPort), | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //   GPS发送消息
 | 
					
						
							|  |  |  |   public sendGPSMsg(msg: string) { | 
					
						
							|  |  |  |     this.GPSUDP.send({ | 
					
						
							|  |  |  |       data: '111111', | 
					
						
							|  |  |  |       address: { | 
					
						
							|  |  |  |         address: this.OppositeIp, | 
					
						
							|  |  |  |         port: parseInt(this.GPSOppositeIpPort), | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 接受消息
 | 
					
						
							|  |  |  |   public receiveMsg(callback):number { | 
					
						
							|  |  |  |     //TODO 临时处理关闭消息接收
 | 
					
						
							|  |  |  |     this.cancelMsg(this.timer) | 
					
						
							|  |  |  |     this.timer = setInterval(() => { | 
					
						
							|  |  |  |       let newMessage = this.handleMsg() | 
					
						
							|  |  |  |       callback(newMessage) | 
					
						
							|  |  |  |     }, 200) | 
					
						
							|  |  |  |     return this.timer | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //取消订阅
 | 
					
						
							|  |  |  |   public cancelMsg(timer:number){ | 
					
						
							|  |  |  |     clearInterval(timer) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // 处理消息
 | 
					
						
							|  |  |  |   public handleMsg() { | 
					
						
							|  |  |  |     let newMessage = PLCGPSData; | 
					
						
							|  |  |  |     if (this.GPSMsg) { | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |       //let GPRMCMsgArr = GPRMCMatch ? GPRMCMatch === null || GPRMCMatch === void 0 ? void 0 : GPRMCMatch.split(",").slice(0, 14) : [];
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-07 12:56:24 +08:00
										 |  |  |       let GPGGAMatch = this.GPSMsg.match(/\$GPGGA[^$]*/); | 
					
						
							|  |  |  |       let GPGGAMsgArr = GPGGAMatch ? GPGGAMatch[0]?.split(",").slice(0, 15) : []; | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       //   使用正则提取$GPRMC消息
 | 
					
						
							| 
									
										
										
										
											2025-04-07 12:56:24 +08:00
										 |  |  |       let GPRMCMatch = this.GPSMsg.match(/\$GPRMC[^$]*/); | 
					
						
							| 
									
										
										
										
											2025-04-07 18:39:23 +08:00
										 |  |  |       let GPRMCMsgArr = GPRMCMatch ? GPRMCMatch[0]?.split(",").slice(0, 14) : []; | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       //   使用正则表达式提取$GPGST消息
 | 
					
						
							|  |  |  |       let GPGSTMatch = this.GPSMsg.match(/\$GPGST[^$]*/); | 
					
						
							|  |  |  |       let GPGSTMsgArr = GPGSTMatch ? GPGSTMatch[0]?.split(",").slice(0, 9) : []; | 
					
						
							|  |  |  |       //   使用正则提取$PTNL消息
 | 
					
						
							| 
									
										
										
										
											2025-04-07 12:56:24 +08:00
										 |  |  |       let PTNLMatch = this.GPSMsg.match(/\$PTNL[^$]*/); | 
					
						
							|  |  |  |       let PTNLMsgArr = PTNLMatch ? PTNLMatch[0].split(",")?.slice(0, 14) : []; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       //   组合GPS数据
 | 
					
						
							|  |  |  |       // 状态83
 | 
					
						
							|  |  |  |       newMessage[83] = GPGGAMsgArr[6]; | 
					
						
							|  |  |  |       // 收星数84
 | 
					
						
							|  |  |  |       newMessage[84] = GPGGAMsgArr[7]; | 
					
						
							|  |  |  |       // 海拔高85
 | 
					
						
							| 
									
										
										
										
											2025-04-08 14:49:47 +08:00
										 |  |  |       newMessage[85] = GPGGAMsgArr[9]; | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       // 高度差86
 | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |       newMessage[86] = GPGGAMsgArr[11]; | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       // 龄期87
 | 
					
						
							|  |  |  |       newMessage[87] = GPGGAMsgArr[13]; | 
					
						
							|  |  |  |       // 维度因子88
 | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |       newMessage[88] = GPGSTMsgArr[6]; | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       // 经度因子89
 | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |       newMessage[89] = GPGSTMsgArr[7]; | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       // 航向角90
 | 
					
						
							|  |  |  |       newMessage[90] = PTNLMsgArr[3]; | 
					
						
							|  |  |  |       // 俯仰角91
 | 
					
						
							|  |  |  |       newMessage[91] = PTNLMsgArr[5]; | 
					
						
							|  |  |  |       // 航向角状态-收星数92
 | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |       newMessage[92] = PTNLMsgArr[10] + '-' + (PTNLMsgArr[12] && PTNLMsgArr[12].split('*')[0]); | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       //  年月日93 RMCMsgArr[9]为ddmmyy 日月年 转换为年月日
 | 
					
						
							|  |  |  |       newMessage[93] = | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |         GPRMCMsgArr[9] && (GPRMCMsgArr[9].slice(0, 2) + GPRMCMsgArr[9].slice(2, 4) + GPRMCMsgArr[9].slice(4, 6)); | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       // 时分秒94 GPGGAMsgArr[1]为021126.00去掉小数点后的时间
 | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |       newMessage[94] = GPGGAMsgArr[1] && GPGGAMsgArr[1].replace(".", ""); | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |       // 经度95
 | 
					
						
							|  |  |  |       newMessage[95] = GPGGAMsgArr[4]; | 
					
						
							|  |  |  |       // 纬度96
 | 
					
						
							|  |  |  |       newMessage[96] = GPGGAMsgArr[2]; | 
					
						
							|  |  |  |       // 速度97
 | 
					
						
							|  |  |  |       newMessage[97] = GPRMCMsgArr[7]; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     if (this.PLCMsg) { | 
					
						
							|  |  |  |       let dataView = new DataView(this.PLCMsg) | 
					
						
							|  |  |  |       let PLCByteArr = [] | 
					
						
							|  |  |  |       for (let i = 0; i < dataView?.byteLength; ++i) { | 
					
						
							|  |  |  |         let c = dataView?.getUint8(i).toString(2).padStart(8, "0") | 
					
						
							|  |  |  |         PLCByteArr.push(c.toString()) | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       if (PLCByteArr.length < 55) { | 
					
						
							|  |  |  |         return newMessage.join(",") | 
					
						
							|  |  |  |       } | 
					
						
							|  |  |  |       // 左方向灯 2
 | 
					
						
							|  |  |  |       newMessage[2] = PLCByteArr[6][5]; | 
					
						
							|  |  |  |       // 右方向灯 3 .
 | 
					
						
							|  |  |  |       newMessage[3] = PLCByteArr[6][4]; | 
					
						
							|  |  |  |       // 喇叭 4
 | 
					
						
							|  |  |  |       newMessage[4] = PLCByteArr[8][4]; | 
					
						
							|  |  |  |       // 点火1 5
 | 
					
						
							|  |  |  |       newMessage[5] = PLCByteArr[8][7]; | 
					
						
							|  |  |  |       // 点火2 6
 | 
					
						
							|  |  |  |       newMessage[6] = PLCByteArr[8][6]; | 
					
						
							|  |  |  |       // 近光灯 7
 | 
					
						
							|  |  |  |       newMessage[7] = PLCByteArr[6][7]; | 
					
						
							|  |  |  |       // 远光灯 8
 | 
					
						
							|  |  |  |       newMessage[8] = PLCByteArr[6][6]; | 
					
						
							|  |  |  |       // 示廓灯 9
 | 
					
						
							|  |  |  |       newMessage[9] = PLCByteArr[6][2]; | 
					
						
							|  |  |  |       // 雾灯 10
 | 
					
						
							|  |  |  |       // 雨刮器 11
 | 
					
						
							|  |  |  |       newMessage[11] = PLCByteArr[8][5]; | 
					
						
							|  |  |  |       // 脚刹 12
 | 
					
						
							|  |  |  |       newMessage[12] = PLCByteArr[7][5]; | 
					
						
							|  |  |  |       // 手刹 13
 | 
					
						
							|  |  |  |       newMessage[13] = PLCByteArr[7][4]; | 
					
						
							|  |  |  |       // 主驾驶门 14
 | 
					
						
							|  |  |  |       newMessage[14] = PLCByteArr[7][7]; | 
					
						
							|  |  |  |       // NC 15
 | 
					
						
							|  |  |  |       // TODO
 | 
					
						
							|  |  |  |       // SA15 16
 | 
					
						
							|  |  |  |       // TODO
 | 
					
						
							|  |  |  |       // 离合 17
 | 
					
						
							|  |  |  |       newMessage[17] = PLCByteArr[7][6]; | 
					
						
							|  |  |  |       // 副刹车 18
 | 
					
						
							|  |  |  |       newMessage[18] = PLCByteArr[7][3]; | 
					
						
							|  |  |  |       // 安全带 19
 | 
					
						
							|  |  |  |       newMessage[19] = PLCByteArr[7][0]; | 
					
						
							|  |  |  |       // 双跳灯 20
 | 
					
						
							|  |  |  |       newMessage[20] = PLCByteArr[6][3]; | 
					
						
							|  |  |  |       // 其他门 21
 | 
					
						
							|  |  |  |       // TODO
 | 
					
						
							|  |  |  |       // 转速过高 22
 | 
					
						
							|  |  |  |       newMessage[22] = PLCByteArr[9][0]; | 
					
						
							|  |  |  |       // 车速 23
 | 
					
						
							|  |  |  |       newMessage[23] = parseInt(PLCByteArr[11], 2)+''; | 
					
						
							|  |  |  |       // 累计脉冲 24
 | 
					
						
							|  |  |  |       let Data25 = parseInt(PLCByteArr[25], 2); | 
					
						
							|  |  |  |       let Data26 = parseInt(PLCByteArr[26], 2); | 
					
						
							|  |  |  |       let Data27 = parseInt(PLCByteArr[27], 2); | 
					
						
							|  |  |  |       let Data28 = parseInt(PLCByteArr[28], 2); | 
					
						
							|  |  |  |       newMessage[24] = ((Data25 << 24) + (Data26 << 16) + (Data27 << 8) + Data28).toString(); | 
					
						
							|  |  |  |       // 发动机转速 25
 | 
					
						
							|  |  |  |       let Data29 = parseInt(PLCByteArr[29], 2); | 
					
						
							|  |  |  |       let Data30 = parseInt(PLCByteArr[30], 2); | 
					
						
							|  |  |  |       let Data31 = parseInt(PLCByteArr[31], 2); | 
					
						
							|  |  |  |       let Data32 = parseInt(PLCByteArr[32], 2); | 
					
						
							|  |  |  |       newMessage[25] = ((Data29 << 24) + (Data30 << 16) + (Data31 << 8) + Data32).toString(); | 
					
						
							|  |  |  |       // 熄火次数 26
 | 
					
						
							|  |  |  |       newMessage[26] = parseInt(PLCByteArr[33], 2) + ''; | 
					
						
							|  |  |  |       // 方向盘角度 27
 | 
					
						
							|  |  |  |       // TODO 档位 磁档位为外接信号
 | 
					
						
							| 
									
										
										
										
											2025-03-28 15:52:09 +08:00
										 |  |  |       newMessage[28] = (globalThis.chuankoMsg == '0' || globalThis.chuankoMsg == '' ||globalThis.chuankoMsg == undefined)?(parseInt(PLCByteArr[13], 2) + ''): globalThis.chuankoMsg; | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  |       // newMessage[27] = globalThis.chuankoMsg
 | 
					
						
							|  |  |  |       // 超声波1 29
 | 
					
						
							|  |  |  |       newMessage[29] = (PLCByteArr[4][1] >0 ? '300' : '1200') | 
					
						
							|  |  |  |       // 超声波2 30
 | 
					
						
							|  |  |  |       newMessage[30] =  (PLCByteArr[4][0] >0 ? '300': '1200' ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       //TODO 超声波3 临时写死
 | 
					
						
							|  |  |  |       newMessage[31] = '1200' | 
					
						
							|  |  |  |       //TODO 超声波4 临时写死
 | 
					
						
							|  |  |  |       newMessage[32] = '1200' | 
					
						
							|  |  |  |       // 触摸1 33
 | 
					
						
							|  |  |  |       // 触摸2 34
 | 
					
						
							|  |  |  |       // 触摸3 35
 | 
					
						
							|  |  |  |       // SCIO 36
 | 
					
						
							|  |  |  |       // SC1A_C 37
 | 
					
						
							|  |  |  |       // SC1B_C 38
 | 
					
						
							|  |  |  |       // SC2A_C 39
 | 
					
						
							|  |  |  |       // SC2B_C 40
 | 
					
						
							|  |  |  |       // SC3A_C 41
 | 
					
						
							|  |  |  |       // SC3B_C 42
 | 
					
						
							|  |  |  |       // SC4A_C 43
 | 
					
						
							|  |  |  |       // SC4B_C 44
 | 
					
						
							|  |  |  |       // SC5A_C 45
 | 
					
						
							|  |  |  |       // SC5B_C 46
 | 
					
						
							|  |  |  |       // SC6A_C 47
 | 
					
						
							|  |  |  |       // SC6B_C 48
 | 
					
						
							|  |  |  |       // 发送次数 49
 | 
					
						
							|  |  |  |       // 方向盘类型 50
 | 
					
						
							|  |  |  |       // 汽车类型 51
 | 
					
						
							|  |  |  |       // 接口心跳 52
 | 
					
						
							|  |  |  |       // 本机IP 53
 | 
					
						
							|  |  |  |       // 固件版本 54
 | 
					
						
							|  |  |  |       // 按键数值 55
 | 
					
						
							|  |  |  |       // GPS板卡类型 56
 | 
					
						
							|  |  |  |       // GPS板卡软件版本 57
 | 
					
						
							|  |  |  |       // 改正数次数/改正数大小 58
 | 
					
						
							|  |  |  |       // GPS数据次数/数据长度 59
 | 
					
						
							|  |  |  |       // GPS错误次数 60
 | 
					
						
							|  |  |  |       // 已工作时长/设定的工作时长 61
 | 
					
						
							|  |  |  |       // 改正数数据长度*数据长度-基准站RTCM改正数类型 62
 | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2025-04-07 18:39:23 +08:00
										 |  |  |     return newMessage.map(i=>{ | 
					
						
							| 
									
										
										
										
											2025-04-08 13:24:07 +08:00
										 |  |  |       return i === undefined ? '' : i | 
					
						
							| 
									
										
										
										
											2025-04-07 18:39:23 +08:00
										 |  |  |     }).join(",") | 
					
						
							| 
									
										
										
										
											2025-03-25 10:31:44 +08:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //   PLC接收消息
 | 
					
						
							|  |  |  |   public receivePLCMsg() { | 
					
						
							|  |  |  |     this.PLCUDP.on("message", (message, remoteInfo) => { | 
					
						
							|  |  |  |       console.log(`PLC receive message from ${remoteInfo.address}:${remoteInfo.port} : ${message}`); | 
					
						
							|  |  |  |       this.PLCMsg = message; | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   //   GPS接收消息
 | 
					
						
							|  |  |  |   public receiveGPSMsg() { | 
					
						
							|  |  |  |     this.GPSUDP.on("message", (message, remoteInfo) => { | 
					
						
							|  |  |  |       console.log(`GPS receive message from ${remoteInfo.address}:${remoteInfo.port} : ${message}`); | 
					
						
							|  |  |  |       this.GPSMsg = message; | 
					
						
							|  |  |  |     }) | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |