144 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			144 lines
		
	
	
		
			3.5 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import FileUtil from '../utils/File';
 | 
						|
import { GlobalConfig } from '../../config';
 | 
						|
import common from '@ohos.app.ability.common';
 | 
						|
import UdpByOne from './f&S/UdpByOne';
 | 
						|
import AIO from './aio/aioClass'
 | 
						|
import GpsTcpClient from '../utils/GpsTcpClient'
 | 
						|
 | 
						|
export default class RearEndUnitsTool {
 | 
						|
  //一型机 二型机 三型机 一体机
 | 
						|
  public terType: 0 | 1 | 2 | 3 = 2;
 | 
						|
  public timer: number = 0
 | 
						|
  public diffTimer: number = 0
 | 
						|
  private UdpByOneClass: UdpByOne
 | 
						|
  public  AioClass: AIO
 | 
						|
  public  GpsTcpClientClass: GpsTcpClient
 | 
						|
  private diffData: number[]
 | 
						|
 | 
						|
  constructor(context: common.UIAbilityContext) {
 | 
						|
    this.getTerType(context)
 | 
						|
  }
 | 
						|
 | 
						|
  public receiveMsg = (callBack)=> {
 | 
						|
    const terType = this.terType;
 | 
						|
    //TODO 临时处理关闭消息接收
 | 
						|
    this.cancelMsg()
 | 
						|
 | 
						|
    switch (terType) {
 | 
						|
    //一型机
 | 
						|
      case 0: {
 | 
						|
        const udpClass = this.getFUdp({
 | 
						|
          type: 1
 | 
						|
        })
 | 
						|
        this.timer = setInterval(() => {
 | 
						|
          const message = udpClass.handleMsg()
 | 
						|
          callBack && callBack(message)
 | 
						|
        }, 200)
 | 
						|
      }
 | 
						|
        break;
 | 
						|
 | 
						|
    //二型机
 | 
						|
      case 1: {
 | 
						|
        const udpClass = this.getFUdp({
 | 
						|
          type: 2
 | 
						|
        })
 | 
						|
        this.timer = setInterval(() => {
 | 
						|
          const message = udpClass.handleMsg()
 | 
						|
          callBack && callBack(message)
 | 
						|
        }, 200)
 | 
						|
        break;
 | 
						|
      }
 | 
						|
        break;
 | 
						|
 | 
						|
    //三型机
 | 
						|
      case 2:
 | 
						|
    // this.timer = setInterval(() => {
 | 
						|
    //   callBack && callBack(message)
 | 
						|
    // }, 200)
 | 
						|
    // break;
 | 
						|
 | 
						|
    //一体机
 | 
						|
      case 3:
 | 
						|
        const aioClass = this.getAio()
 | 
						|
        this.timer = setInterval(() => {
 | 
						|
          const message = aioClass.handleMsg()
 | 
						|
          callBack && callBack(message)
 | 
						|
        }, 200)
 | 
						|
 | 
						|
        this.diffTimer = setInterval(()=>{
 | 
						|
          aioClass.sendDiffCorrections(this.diffData)
 | 
						|
        },1000)
 | 
						|
      default:
 | 
						|
        break
 | 
						|
    }
 | 
						|
 | 
						|
    return this.timer
 | 
						|
  }
 | 
						|
 | 
						|
  //取消订阅
 | 
						|
  public cancelMsg() {
 | 
						|
    clearInterval(this.timer)
 | 
						|
    clearInterval(this.diffTimer)
 | 
						|
  }
 | 
						|
 | 
						|
  //获取后置机设备类型
 | 
						|
  private async getTerType(context) {
 | 
						|
    const fileUtil = new FileUtil(context)
 | 
						|
    const config = await fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/ipConfig.txt');
 | 
						|
    const result = JSON.parse(config || '{}')
 | 
						|
    //默认设置为三代机
 | 
						|
    this.terType = result.terType
 | 
						|
    return this.terType
 | 
						|
  }
 | 
						|
 | 
						|
  //获取一型机&二型机单例
 | 
						|
  private getFUdp({type}) {
 | 
						|
    if (!this.UdpByOneClass) {
 | 
						|
      this.UdpByOneClass = new UdpByOne(type);
 | 
						|
    }
 | 
						|
    return this.UdpByOneClass;
 | 
						|
  }
 | 
						|
 | 
						|
  // 获取一体机
 | 
						|
  private getAio =()=> {
 | 
						|
    if (!this.AioClass) {
 | 
						|
      this.AioClass = new AIO()
 | 
						|
    }
 | 
						|
    return this.AioClass;
 | 
						|
  }
 | 
						|
 | 
						|
  //获取转发gps差分的tcp
 | 
						|
  private getGpsTcp =()=>{
 | 
						|
    if(!this.GpsTcpClientClass){
 | 
						|
      this.GpsTcpClientClass = new GpsTcpClient()
 | 
						|
    }
 | 
						|
    return this.GpsTcpClientClass
 | 
						|
  }
 | 
						|
 | 
						|
  // 转发差分改正数
 | 
						|
  public sendDiffCorrections = (message) => {
 | 
						|
    const type = this.terType
 | 
						|
    //差分改正数截取前5位
 | 
						|
    let dataView = new DataView(message)
 | 
						|
    const Arraybuffer = message.slice(5, dataView?.byteLength);
 | 
						|
 | 
						|
    switch (type){
 | 
						|
    //一型机
 | 
						|
      case 0:
 | 
						|
        const GpsTcpClientClass = this.getGpsTcp()
 | 
						|
        GpsTcpClientClass.sendGpsMsg(Arraybuffer);break
 | 
						|
 | 
						|
    //二型机
 | 
						|
      case 1: globalThis.udpClient?.sendMsg(Arraybuffer);break
 | 
						|
 | 
						|
    //三型机
 | 
						|
      case 2: globalThis.udpClient?.sendMsg(Arraybuffer);break
 | 
						|
 | 
						|
    //一体机的差分不需要截取
 | 
						|
      case 3: this.diffData = message;break
 | 
						|
    }
 | 
						|
 | 
						|
  }
 | 
						|
 | 
						|
  public send
 | 
						|
} |