199 lines
6.0 KiB
TypeScript
199 lines
6.0 KiB
TypeScript
|
|
import systemTime from '@ohos.systemDateTime';
|
||
|
|
import { PLCGPSData } from '../../../mock/PLCGPSData';
|
||
|
|
import {
|
||
|
|
OpenSerialPort,
|
||
|
|
ReceiveSerialPortDataBySelf,
|
||
|
|
InitSerialPortData,
|
||
|
|
CancelReceiveSerialPortData,
|
||
|
|
SendSerialPortData,
|
||
|
|
CloseSerialPort,
|
||
|
|
} from '../../utils/aioTool'
|
||
|
|
|
||
|
|
import { GlobalConfig } from '../../../config'
|
||
|
|
|
||
|
|
export default class AIO {
|
||
|
|
obdFd: number
|
||
|
|
gpsFd: number
|
||
|
|
sensor: Object = {}
|
||
|
|
gps: Object = {}
|
||
|
|
obdStr: string = ''
|
||
|
|
gpsStr: string =''
|
||
|
|
// 定位状态异常次数
|
||
|
|
dwztErrorNumber:number = 0
|
||
|
|
|
||
|
|
constructor() {
|
||
|
|
this.init()
|
||
|
|
}
|
||
|
|
|
||
|
|
async init() {
|
||
|
|
await this.getObd()
|
||
|
|
await this.getGps()
|
||
|
|
}
|
||
|
|
|
||
|
|
public async getObd() {
|
||
|
|
//打开obd串口
|
||
|
|
this.obdFd = await OpenSerialPort(GlobalConfig.serialPortConfig.obd.path);
|
||
|
|
//初始化串口
|
||
|
|
await InitSerialPortData(this.obdFd, 115200);
|
||
|
|
|
||
|
|
//发送一段数据
|
||
|
|
const data = this.convertToASCII('LOG VERSION')
|
||
|
|
const res = await SendSerialPortData(this.obdFd, data)
|
||
|
|
|
||
|
|
ReceiveSerialPortDataBySelf(this.obdFd, async (res1, res2, res3, res4) => {
|
||
|
|
// console.log(res1.toString(), res2.toString(), res3.toString(), "接受数据");
|
||
|
|
let buffer = this.arrayToBuffer(res3)
|
||
|
|
let dataView = new DataView(buffer)
|
||
|
|
let str = ""
|
||
|
|
for (let i = 0; i < dataView?.byteLength; ++i) {
|
||
|
|
let c = String.fromCharCode(dataView?.getUint8(i))
|
||
|
|
if (c !== "\n") {
|
||
|
|
str += c
|
||
|
|
}
|
||
|
|
}
|
||
|
|
//
|
||
|
|
// //获取档位信号
|
||
|
|
// const {sensor} = plcStrToJson(str)
|
||
|
|
// this.sensor = sensor;
|
||
|
|
this.obdStr = str
|
||
|
|
})
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
public async getGps() {
|
||
|
|
//打开gps串口
|
||
|
|
this.gpsFd = await OpenSerialPort(GlobalConfig.serialPortConfig.gps.path);
|
||
|
|
//初始化串口
|
||
|
|
await InitSerialPortData(this.gpsFd, 115200);
|
||
|
|
ReceiveSerialPortDataBySelf(this.gpsFd, async (res1, res2, res3, res4) => {
|
||
|
|
// console.log(res1.toString(), res2.toString(), res3.toString(), "接受数据");
|
||
|
|
let buffer = this.arrayToBuffer(res3)
|
||
|
|
let dataView = new DataView(buffer)
|
||
|
|
let str = ""
|
||
|
|
for (let i = 0; i < dataView?.byteLength; ++i) {
|
||
|
|
let c = String.fromCharCode(dataView?.getUint8(i))
|
||
|
|
if (c !== "\n") {
|
||
|
|
str += c
|
||
|
|
}
|
||
|
|
}
|
||
|
|
this.gpsStr = str;
|
||
|
|
})
|
||
|
|
}
|
||
|
|
|
||
|
|
//副板gps端口重连
|
||
|
|
public reConnect = async () => {
|
||
|
|
const {dwztErrorNumber,gpsFd,getGps} = this;
|
||
|
|
if(dwztErrorNumber >= 25){
|
||
|
|
this.dwztErrorNumber = 0;
|
||
|
|
await CancelReceiveSerialPortData(gpsFd)
|
||
|
|
CloseSerialPort(gpsFd)
|
||
|
|
await getGps()
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
//组装obd&gps数据
|
||
|
|
public handleMsg() {
|
||
|
|
let newMessage = PLCGPSData;
|
||
|
|
|
||
|
|
if (this.obdStr) {
|
||
|
|
const PLCByteArr = this.obdStr.split(',')
|
||
|
|
PLCByteArr.forEach((item, index) => {
|
||
|
|
newMessage[index] = item;
|
||
|
|
//档位
|
||
|
|
if(index === 28){
|
||
|
|
newMessage[28] = (globalThis.chuankoMsg == '0' || globalThis.chuankoMsg == '' ||globalThis.chuankoMsg == undefined)? item: globalThis.chuankoMsg
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
if (this.gpsStr) {
|
||
|
|
|
||
|
|
let GPGGAMatch = this.gpsStr.match(/\$GPGGA[^$]*/);
|
||
|
|
let GPGGAMsgArr = GPGGAMatch ? GPGGAMatch[0]?.split(",").slice(0, 15) : [];
|
||
|
|
// 使用正则提取$GPRMC消息
|
||
|
|
let GPRMCMatch = this.gpsStr.match(/\$GPRMC[^$]*/);
|
||
|
|
let GPRMCMsgArr = GPRMCMatch ? GPRMCMatch[0]?.split(",").slice(0, 14) : [];
|
||
|
|
// 使用正则表达式提取$GPGST消息
|
||
|
|
let GPGSTMatch = this.gpsStr.match(/\$GPGST[^$]*/);
|
||
|
|
let GPGSTMsgArr = GPGSTMatch ? GPGSTMatch[0]?.split(",").slice(0, 9) : [];
|
||
|
|
// 使用正则提取$PTNL消息
|
||
|
|
let PTNLMatch = this.gpsStr.match(/\$PTNL[^$]*/);
|
||
|
|
let PTNLMsgArr = PTNLMatch ? PTNLMatch[0].split(",")?.slice(0, 14) : [];
|
||
|
|
|
||
|
|
//@ts-ignore 板卡类型
|
||
|
|
newMessage[56] = '1'
|
||
|
|
// 组合GPS数据
|
||
|
|
// 状态83
|
||
|
|
newMessage[83] = GPGGAMsgArr[6];
|
||
|
|
// 收星数84
|
||
|
|
newMessage[84] = GPGGAMsgArr[7];
|
||
|
|
// 海拔高85
|
||
|
|
newMessage[85] = GPGGAMsgArr[9];
|
||
|
|
// 高度差86
|
||
|
|
newMessage[86] = GPGGAMsgArr[11];
|
||
|
|
// 龄期87
|
||
|
|
newMessage[87] = GPGGAMsgArr[13];
|
||
|
|
// 维度因子88
|
||
|
|
newMessage[88] = GPGSTMsgArr[6];
|
||
|
|
// 经度因子89
|
||
|
|
newMessage[89] = GPGSTMsgArr[7]
|
||
|
|
// 航向角90
|
||
|
|
newMessage[90] = PTNLMsgArr[3];
|
||
|
|
// 俯仰角91
|
||
|
|
newMessage[91] = PTNLMsgArr[5];
|
||
|
|
// 航向角状态-收星数92
|
||
|
|
newMessage[92] = PTNLMsgArr[10] + '-' + (PTNLMsgArr[12] && PTNLMsgArr[12].split('*')[0]);
|
||
|
|
// 年月日93 RMCMsgArr[9]为ddmmyy 日月年 转换为年月日
|
||
|
|
newMessage[93] =
|
||
|
|
GPRMCMsgArr[9] && (GPRMCMsgArr[9].slice(0, 2) + GPRMCMsgArr[9].slice(2, 4) + GPRMCMsgArr[9].slice(4, 6));
|
||
|
|
// 时分秒94 GPGGAMsgArr[1]为021126.00去掉小数点后的时间
|
||
|
|
newMessage[94] = GPGGAMsgArr[1] && GPGGAMsgArr[1].replace(".", "");
|
||
|
|
// 经度95
|
||
|
|
newMessage[95] = GPGGAMsgArr[4];
|
||
|
|
// 纬度96
|
||
|
|
newMessage[96] = GPGGAMsgArr[2];
|
||
|
|
// 速度97
|
||
|
|
newMessage[97] = GPRMCMsgArr[7];
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
return newMessage.map(item => {
|
||
|
|
return item === undefined?'':item
|
||
|
|
}).join(",")
|
||
|
|
}
|
||
|
|
// 关闭串口和监听
|
||
|
|
public closeMessage = () => {
|
||
|
|
const {obdFd,gpsFd} = this;
|
||
|
|
CancelReceiveSerialPortData(obdFd)
|
||
|
|
CancelReceiveSerialPortData(gpsFd)
|
||
|
|
// CloseSerialPort(obdFd)
|
||
|
|
// CloseSerialPort(gpsFd)
|
||
|
|
}
|
||
|
|
|
||
|
|
//发送差分改正数字
|
||
|
|
public sendDiffCorrections = (data) => {
|
||
|
|
const {gpsFd} = this;
|
||
|
|
let arr = []
|
||
|
|
if(data?.byteLength){
|
||
|
|
// console.info('surenjun 一体机发送差分数据',data?.byteLength)
|
||
|
|
let dataView = new DataView(data)
|
||
|
|
let str = ""
|
||
|
|
for (let i = 0; i < dataView?.byteLength; ++i) {
|
||
|
|
arr.push(dataView?.getUint8(i))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
const temp = SendSerialPortData(gpsFd,arr);
|
||
|
|
}
|
||
|
|
|
||
|
|
public convertToASCII = (input: string): number[] => {
|
||
|
|
return input.split('').map(char => char.charCodeAt(0));
|
||
|
|
}
|
||
|
|
|
||
|
|
arrayToBuffer(arr: number[]): ArrayBuffer {
|
||
|
|
const buffer = new ArrayBuffer(arr.length);
|
||
|
|
const view = new DataView(buffer);
|
||
|
|
arr.forEach((value, index) => {
|
||
|
|
view.setUint8(index, value);
|
||
|
|
});
|
||
|
|
return buffer;
|
||
|
|
}
|
||
|
|
}
|