78 lines
1.8 KiB
TypeScript
78 lines
1.8 KiB
TypeScript
|
|
import FileUtil from '../utils/File';
|
||
|
|
import { GlobalConfig } from '../../config';
|
||
|
|
import common from '@ohos.app.ability.common';
|
||
|
|
import UdpByOne from './tool/UdpByOne';
|
||
|
|
|
||
|
|
export default class TerClient {
|
||
|
|
|
||
|
|
//一型机 二型机 三型机 一体机
|
||
|
|
public terType: 0 | 1 | 2 | 3 = 2;
|
||
|
|
public timer: number = 0
|
||
|
|
private UdpByOneClass:UdpByOne
|
||
|
|
|
||
|
|
constructor(context: common.UIAbilityContext) {
|
||
|
|
this.getTerType(context)
|
||
|
|
}
|
||
|
|
|
||
|
|
public receiveMsg(message: string, callBack) {
|
||
|
|
const terType = this.terType;
|
||
|
|
//TODO 临时处理关闭消息接收
|
||
|
|
this.cancelMsg(this.timer)
|
||
|
|
|
||
|
|
switch (terType) {
|
||
|
|
//一型机
|
||
|
|
case 0:
|
||
|
|
const udpClass = this.getFUdp()
|
||
|
|
this.timer = setInterval(() => {
|
||
|
|
const message = udpClass.handleMsg()
|
||
|
|
callBack && callBack(message)
|
||
|
|
}, 200)
|
||
|
|
break;
|
||
|
|
|
||
|
|
//TODO 二型机
|
||
|
|
case 1:
|
||
|
|
break;
|
||
|
|
|
||
|
|
//三型机
|
||
|
|
case 2:
|
||
|
|
this.timer = setInterval(() => {
|
||
|
|
callBack && callBack(message)
|
||
|
|
}, 200)
|
||
|
|
break;
|
||
|
|
|
||
|
|
//TODO 一体机
|
||
|
|
case 3:
|
||
|
|
default:
|
||
|
|
break
|
||
|
|
}
|
||
|
|
|
||
|
|
return this.timer
|
||
|
|
}
|
||
|
|
|
||
|
|
//取消订阅
|
||
|
|
public cancelMsg(timer: number) {
|
||
|
|
clearInterval(timer)
|
||
|
|
}
|
||
|
|
|
||
|
|
//获取后置机设备类型
|
||
|
|
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
|
||
|
|
}
|
||
|
|
|
||
|
|
private async getMessage(udpClass) {
|
||
|
|
const terType = this.terType;
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
//获取一型机单例
|
||
|
|
private getFUdp(){
|
||
|
|
if (!this.UdpByOneClass) {
|
||
|
|
this.UdpByOneClass = new UdpByOne();
|
||
|
|
}
|
||
|
|
return this.UdpByOneClass;
|
||
|
|
}
|
||
|
|
}
|