feat: implement central heartbeat and differential signal classes with UDP/TCP communication
This commit is contained in:
		
							parent
							
								
									1336100978
								
							
						
					
					
						commit
						8489d70c58
					
				| @ -4,11 +4,10 @@ export interface TakePhotoCallbackData { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export interface ObtainSignalDataConfigType { | export interface ObtainSignalDataConfigType { | ||||||
|   //   UDP配置 |   //   三代机后置机UDP配置 | ||||||
|   udpLocalIp?: string |   udpLocalIp?: string | ||||||
|   udpLocalIpPort?: string |   udpLocalIpPort?: string | ||||||
|   udpOppositeIp?: string |   udpOppositeIp?: string | ||||||
|   udpOppositeIpPort?: string |   udpOppositeIpPort?: string | ||||||
| 
 | 
 | ||||||
| 
 |  | ||||||
| } | } | ||||||
| @ -4,7 +4,8 @@ import { BusinessError } from '@ohos.base' | |||||||
| const TAG = '[TcpClient]' | const TAG = '[TcpClient]' | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| class TcpClient { | export default class TcpClient { | ||||||
|  |   private static instance: TcpClient | ||||||
|   private localIp: string = '' |   private localIp: string = '' | ||||||
|   private localIpPort: string = '' |   private localIpPort: string = '' | ||||||
|   private oppositeIp: string = '' |   private oppositeIp: string = '' | ||||||
| @ -12,7 +13,6 @@ class TcpClient { | |||||||
|   private tcpSendNum: number = 0 |   private tcpSendNum: number = 0 | ||||||
|   private tcp: socket.TCPSocket = null |   private tcp: socket.TCPSocket = null | ||||||
|   private events: Array<Function> = [] |   private events: Array<Function> = [] | ||||||
|   private static instance: TcpClient |  | ||||||
| 
 | 
 | ||||||
|   constructor() { |   constructor() { | ||||||
|     if (!TcpClient.instance) { |     if (!TcpClient.instance) { | ||||||
| @ -31,9 +31,13 @@ class TcpClient { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   bindTcp(): Promise<void> { |   bindTcp(): Promise<void> { | ||||||
|     return this.tcp.bind({ address: this.localIp, port: parseInt(this.localIpPort), family: 1 }).then(() => { |     return this.tcp.bind({ | ||||||
|  |       address: this.localIp, port: parseInt(this.localIpPort), family: 1 | ||||||
|  |     }).then(() => { | ||||||
|       return this.tcp.connect({ |       return this.tcp.connect({ | ||||||
|         address: { address: this.oppositeIp, port: parseInt(this.oppositeIpPort) }, |         address: { | ||||||
|  |           address: this.oppositeIp, port: parseInt(this.oppositeIpPort) | ||||||
|  |         }, | ||||||
|         timeout: 6000 |         timeout: 6000 | ||||||
|       }) |       }) | ||||||
|     }).then(() => { |     }).then(() => { | ||||||
| @ -66,7 +70,9 @@ class TcpClient { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   sendMsg(data: string): Promise<void> { |   sendMsg(data: string): Promise<void> { | ||||||
|     return this.tcp?.send({ data }).catch(async (err: BusinessError) => { |     return this.tcp?.send({ | ||||||
|  |       data | ||||||
|  |     }).catch(async (err: BusinessError) => { | ||||||
|       this.tcpSendNum++ |       this.tcpSendNum++ | ||||||
|       if (this.tcpSendNum > 10) { |       if (this.tcpSendNum > 10) { | ||||||
|         this.tcpSendNum = 0 |         this.tcpSendNum = 0 | ||||||
| @ -77,4 +83,4 @@ class TcpClient { | |||||||
|   } |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const tcpClient = new TcpClient() | // export const tcpClient = new TcpClient() | ||||||
| @ -91,25 +91,25 @@ export default class UdpClient { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| //  获取后置机信号 | //  获取后置机信号 | ||||||
| class ObjUdpClient extends UdpClient { | // class ObjUdpClient extends UdpClient { | ||||||
|   async init(context: common.UIAbilityContext) { | //   async init(context: common.UIAbilityContext) { | ||||||
|     try { | //     try { | ||||||
|       const fileUtil = new FileUtils(context) | //       const fileUtil = new FileUtils(context) | ||||||
|       const data = await fileUtil.readFile("" + '/config/ipConfig.txt'); | //       const data = await fileUtil.readFile("" + '/config/ipConfig.txt'); | ||||||
|       if (data?.length > 0) { | //       if (data?.length > 0) { | ||||||
|         const result: IPConfig = JSON.parse(data) | //         const result: IPConfig = JSON.parse(data) | ||||||
|         this.create(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort) | //         this.create(result.udplocalIp, result.udplocalIpPort, result.udpOppositeIp, result.udpOppositeIpPort) | ||||||
|       } | //       } | ||||||
|     } catch (e) { | //     } catch (e) { | ||||||
|       promptAction.showToast({ | //       promptAction.showToast({ | ||||||
|         message: "初始化obj udp失败" | //         message: "初始化obj udp失败" | ||||||
|       }) | //       }) | ||||||
|     } | //     } | ||||||
|   } | //   } | ||||||
| } | // } | ||||||
| 
 | 
 | ||||||
| // 给中心发送消息 | // 给中心发送GPS消息 | ||||||
| class CenterUDPClient extends UdpClient { | class centerUDPClient extends UdpClient { | ||||||
|   async init(context: common.UIAbilityContext) { |   async init(context: common.UIAbilityContext) { | ||||||
|     try { |     try { | ||||||
|       const fileUtil = new FileUtils(context) |       const fileUtil = new FileUtils(context) | ||||||
| @ -146,10 +146,10 @@ class LightUDPClient extends UdpClient { | |||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // obj | // obj | ||||||
| export const objUDPClient = new ObjUdpClient() | // export const objUDPClient = new ObjUdpClient() | ||||||
| 
 | 
 | ||||||
| // 中心 | // 中心GPS | ||||||
| export const centerUDPClient = new CenterUDPClient() | export const CenterUDPClient = new centerUDPClient() | ||||||
| 
 | 
 | ||||||
| // 顶灯 | // 顶灯 | ||||||
| export const lightUDPClient = new LightUDPClient() | export const lightUDPClient = new LightUDPClient() | ||||||
|  | |||||||
							
								
								
									
										45
									
								
								entry/src/main/ets/utils/business/CentralHeartbeat.ets
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								entry/src/main/ets/utils/business/CentralHeartbeat.ets
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,45 @@ | |||||||
|  | // 中心心跳/发送消息 | ||||||
|  | import { EnvironmentConfigurationType } from '../../model'; | ||||||
|  | import UdpClient from '../UdpUtils'; | ||||||
|  | 
 | ||||||
|  | class centralHeartbeat { | ||||||
|  |   private centralHeartbeatUdp: UdpClient; | ||||||
|  |   private timer: number = -1 | ||||||
|  | 
 | ||||||
|  |   constructor() { | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // 初始化 | ||||||
|  |   init() { | ||||||
|  |     let config: EnvironmentConfigurationType = | ||||||
|  |       AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") | ||||||
|  |     this.centralHeartbeatUdp = new UdpClient(); | ||||||
|  |     this.centralHeartbeatUdp.create(config.udplocalIp, config.udplocalIpPort, config.udpOppositeIp, | ||||||
|  |       config.udpOppositeIpPort); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // 接受中心远程指令 | ||||||
|  |   getData(callback: (data: ArrayBuffer) => void) { | ||||||
|  |     this.centralHeartbeatUdp.onMessage((data: ArrayBuffer) => { | ||||||
|  | 
 | ||||||
|  |       callback(data); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // 发送消息 | ||||||
|  |   sendData() { | ||||||
|  |     // 组装消息,一秒发送一次 | ||||||
|  |     let data = "1"; | ||||||
|  |     this.timer = setInterval(() => { | ||||||
|  |       this.centralHeartbeatUdp.sendMsg(data); | ||||||
|  |     }, 1000); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   //  关闭所有动作 | ||||||
|  |   close() { | ||||||
|  |     clearInterval(this.timer); | ||||||
|  |     this.centralHeartbeatUdp.close() | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export const CentralHeartbeat = new centralHeartbeat(); | ||||||
							
								
								
									
										45
									
								
								entry/src/main/ets/utils/business/DifferentialSignal.ets
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										45
									
								
								entry/src/main/ets/utils/business/DifferentialSignal.ets
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,45 @@ | |||||||
|  | //差分信号 | ||||||
|  | import { EnvironmentConfigurationType } from '../../model'; | ||||||
|  | import TcpClient from '../TcpUtils'; | ||||||
|  | 
 | ||||||
|  | class differentialSignal { | ||||||
|  |   private differentialSignalTcp: TcpClient; | ||||||
|  |   private timer: number = -1 | ||||||
|  | 
 | ||||||
|  |   constructor() { | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   init() { | ||||||
|  |     this.differentialSignalTcp = new TcpClient(); | ||||||
|  |     let config: EnvironmentConfigurationType = | ||||||
|  |       AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") | ||||||
|  |     this.differentialSignalTcp.init(config.tcplocalIp, config.tcplocalIpPort, config.tcpOppositeIp, | ||||||
|  |       config.tcpOppositePort); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // 发送消息 | ||||||
|  |   sendData() { | ||||||
|  |     // 组装消息,一秒发送五次 | ||||||
|  |     let data = "1"; | ||||||
|  |     this.timer = setInterval(() => { | ||||||
|  |       this.differentialSignalTcp.sendMsg(data); | ||||||
|  |     }, 200); | ||||||
|  | 
 | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   //   获取消息 | ||||||
|  |   getData(callback: (data: ArrayBuffer) => void) { | ||||||
|  |     this.differentialSignalTcp.onMessage((data: ArrayBuffer) => { | ||||||
|  |       callback(data); | ||||||
|  |     }); | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   // 关闭所有动作 | ||||||
|  |   close() { | ||||||
|  |     clearInterval(this.timer); | ||||||
|  |     this.differentialSignalTcp.close() | ||||||
|  |   } | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | export const DifferentialSignal = new differentialSignal(); | ||||||
| @ -1,12 +1,11 @@ | |||||||
| import { GlobalConfig } from '../../config' | import { GlobalConfig } from '../../config' | ||||||
| import { ObtainSignalDataConfigType } from '../../model' | import { EnvironmentConfigurationType } from '../../model' | ||||||
| import UdpClient from '../UdpUtils' | import UdpClient from '../UdpUtils' | ||||||
| 
 | 
 | ||||||
|  | // 获取PLC GPS信号 | ||||||
| class obtainSignalData { | class obtainSignalData { | ||||||
|   // 三代机UDP |   // 三代机UDP | ||||||
|   private thirdGenerationMachineUdp: UdpClient |   private thirdGenerationMachineUdp: UdpClient | ||||||
|   // 三代机UDP配置 |  | ||||||
|   private thirdGenerationMachineUdpConfig: ObtainSignalDataConfigType |  | ||||||
|   // 几代机 |   // 几代机 | ||||||
|   private modelNo: string = "3" |   private modelNo: string = "3" | ||||||
| 
 | 
 | ||||||
| @ -15,16 +14,16 @@ class obtainSignalData { | |||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   //   一些初始化 |   //   一些初始化 | ||||||
|   init(config: ObtainSignalDataConfigType) { |   init() { | ||||||
|     this.thirdGenerationMachineUdpConfig = config |  | ||||||
|     if (this.modelNo === "0") { |     if (this.modelNo === "0") { | ||||||
|     } else if (this.modelNo === "1") { |     } else if (this.modelNo === "1") { | ||||||
| 
 |  | ||||||
|     } else if (this.modelNo === "2") { |     } else if (this.modelNo === "2") { | ||||||
|     } else if (this.modelNo === "3") { |     } else if (this.modelNo === "3") { | ||||||
|       //   初始化UDP |       //   初始化UDP | ||||||
|  |       let config: EnvironmentConfigurationType = | ||||||
|  |         AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration") | ||||||
|       this.thirdGenerationMachineUdp = new UdpClient() |       this.thirdGenerationMachineUdp = new UdpClient() | ||||||
|       this.thirdGenerationMachineUdp.create(config.udpLocalIp, config.udpLocalIpPort, config.udpOppositeIp, |       this.thirdGenerationMachineUdp.create(config.udplocalIp, config.udplocalIpPort, config.udpOppositeIp, | ||||||
|         config.udpOppositeIpPort) |         config.udpOppositeIpPort) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
| @ -35,7 +34,7 @@ class obtainSignalData { | |||||||
|     //   一代机 |     //   一代机 | ||||||
| 
 | 
 | ||||||
|     //   二代机 |     //   二代机 | ||||||
|      | 
 | ||||||
|     //   三代机 通过UDP onMessage获取信号 |     //   三代机 通过UDP onMessage获取信号 | ||||||
|     if (this.modelNo === "3") { |     if (this.modelNo === "3") { | ||||||
|       this.thirdGenerationMachineUdp.onMessage((data: ArrayBuffer) => { |       this.thirdGenerationMachineUdp.onMessage((data: ArrayBuffer) => { | ||||||
| @ -52,6 +51,20 @@ class obtainSignalData { | |||||||
|     } |     } | ||||||
|     //   一体机 |     //   一体机 | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   //   发送数据 | ||||||
|  |   sendData(data: string) { | ||||||
|  |     //   一代机 | ||||||
|  | 
 | ||||||
|  |     //   二代机 | ||||||
|  | 
 | ||||||
|  |     //   三代机 通过UDP发送数据 | ||||||
|  |     if (this.modelNo === "3") { | ||||||
|  |       this.thirdGenerationMachineUdp.sendMsg(data) | ||||||
|  |     } | ||||||
|  |     //   一体机 | ||||||
|  | 
 | ||||||
|  |   } | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| export const ObtainSignalData = new obtainSignalData() | export const ObtainSignalData = new obtainSignalData() | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user