feat: 修复一些问题

This commit is contained in:
wangzhongjie 2024-02-20 10:22:33 +08:00
parent ed60046db6
commit ed236df1d6
8 changed files with 262 additions and 211 deletions

View File

@ -1,6 +1,6 @@
import UdpClient from './UdpClient';
import UdpClientByCenter from './UdpClientByCenter';
import { getSyncData} from '../service/initable'
import { getSyncData } from '../service/initable'
import { dateFormat } from '../../common/utils/tools'
import prompt from '@ohos.prompt'
import TcpClient from './TcpClient';
@ -28,17 +28,18 @@ import TcpClient from './TcpClient';
// }
// })
export async function sendMsg(val){
globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
export async function sendMsg(val) {
globalThis.udpClient1 && globalThis.udpClient1.sendMsg(val)
}
export async function getUDP() {
getSyncData('IpConfigTable').then((result:Array<any>)=>{
console.log('result222',JSON.stringify(result))
if(result.length){
if(globalThis.udpClient&&globalThis.udpClient.closeUdp){
globalThis.udpClient.closeUdp(()=>{
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort)
export async function getUDP() {
getSyncData('IpConfigTable').then((result: Array<any>) => {
console.log('result222', JSON.stringify(result))
if (result.length) {
if (globalThis.udpClient && globalThis.udpClient.closeUdp) {
globalThis.udpClient.closeUdp(() => {
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
// globalThis.udpClient.rebindUdp('192.168.7.170', '20122','192.168.7.124','20022')
// setInterval(()=>{
// globalThis.udpClient.sendMsg('111')
@ -47,40 +48,41 @@ export async function getUDP() {
// globalThis.udpClient = udpClient
})
}else{
} else {
const udpClient: UdpClient =new UdpClient(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort)
const udpClient: UdpClient = new UdpClient(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
udpClient.bindUdp()
udpClient.sendMsg('111')
globalThis.udpClient = udpClient
}
// globalThis.udpClient = new UdpClient(result[0].localIp, result[0].localIpPort,result[0].oppositeIp,result[0].oppositeIpPort)
}else{
globalThis.udpClient={}
globalThis.udpClient.onMessage=()=>{}
} else {
globalThis.udpClient = {}
globalThis.udpClient.onMessage = () => {
}
}
})
}
export async function getUDP2() {
getSyncData('IpConfigTable').then(async (result:Array<any>)=>{
console.log('result222',JSON.stringify(result))
if(result.length){
if(globalThis.udpClient2&&globalThis.udpClient2.closeUdp){
globalThis.udpClient2.closeUdp(async ()=>{
getSyncData('IpConfigTable').then(async (result: Array<any>) => {
console.log('result222', JSON.stringify(result))
if (result.length) {
if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp) {
globalThis.udpClient2.closeUdp(async () => {
// globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800','112.80.35.83','11056')
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',result[0].centerIp,result[0].heartPort)
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800', result[0].centerIp, result[0].heartPort)
// globalThis.udpClient2.rebindUdp(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort)
// globalThis.udpClient2.rebindUdp(result[0].udplocalIp, result[0].udpLocalIpByGPS,result[0].udpOppositeIp,result[0].udpOppositeIpPortByGPS)
globalThis.udpClient2.onMessage((val)=>{
globalThis.udpClient2.onMessage((val) => {
// globalThis.udpClnt2.sendMsg(heartMsg(context))
})
})
}else{
} else {
// const udpClient: UdpClient =new UdpClient(result[0].udplocalIp, result[0].udpLocalIpByGPS,result[0].udpOppositeIp,result[0].udpOppositeIpPortByGPS)
const udpClient: UdpClientByCenter =new UdpClientByCenter(result[0].udplocalIp, '8800',result[0].centerIp,result[0].heartPort)
const udpClient: UdpClientByCenter = new UdpClientByCenter(result[0].udplocalIp, '8800', result[0].centerIp, result[0].heartPort)
// const udpClient: UdpClientByCenter =new UdpClientByCenter(result[0].udplocalIp, '8800','112.80.35.83','11056')
// const udpClient: UdpClient =new UdpClient(result[0].tcplocalIp, result[0].tcplocalIpPort,result[0].tcpOppositeIp,result[0].tcpOppositePort)
@ -89,20 +91,25 @@ export async function getUDP2() {
// globalThis.udpClient2.sendMsg(heartMsg(context))
globalThis.udpClient2.onMessage((val)=>{
globalThis.udpClient2.onMessage((val) => {
if(val.id=='32'){
globalThis.signNum=val.body[1]
}else if(val.id=='46'){
if (val.id == '32') {
globalThis.signNum = val.body[1]
} else if (val.id == '46') {
let tmpList = []
console.log('lsh',globalThis.lsh)
console.log('lsh', globalThis.lsh)
const str = globalThis.lsh
for (let i = 0;i < str.length; i++) {
for (let i = 0; i < str.length; i++) {
tmpList.push(this.string2Bytes(str.charCodeAt(i), 1 * 8)[0])
}
const param = { id: 47, list: tmpList, carNo: globalThis.carInfo.carNo, placeId: globalThis.carInfo.examinationRoomId }
const param = {
id: 47,
list: tmpList,
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId
}
globalThis.udpClient2.send(param)
}
@ -113,12 +120,14 @@ export async function getUDP2() {
// },1000)
})
}
}else{
globalThis.udpClient2={}
globalThis.udpClient2.onMessage=()=>{}
} else {
globalThis.udpClient2 = {}
globalThis.udpClient2.onMessage = () => {
}
}
})
}
function fillZero(str, len) {
str = str + '';
if (str.length > len || !len) {
@ -133,6 +142,7 @@ function fillZero(str, len) {
return zeroStr + str;
}
// function getByteToInt(arr){
// let a = list[0];
// let b = list[1];
@ -140,17 +150,17 @@ function fillZero(str, len) {
// let id = Math.floor(+c/1000)
// }
export async function getUDPByTopLine() {
getSyncData('IpConfigTable').then((result:Array<any>)=>{
if(result.length){
if(globalThis.udpClientByTopLine&&globalThis.udpClientByTopLine.closeUdp){
globalThis.udpClientByTopLine.closeUdp(()=>{
getSyncData('IpConfigTable').then((result: Array<any>) => {
if (result.length) {
if (globalThis.udpClientByTopLine && globalThis.udpClientByTopLine.closeUdp) {
globalThis.udpClientByTopLine.closeUdp(() => {
globalThis.udpClientByTopLine.rebindUdp(result[0].udplocalIp,'5509',result[0].udpOppositeIp,result[0].udpOppositeIpPort)
globalThis.udpClientByTopLine.rebindUdp(result[0].udplocalIp, '5509', result[0].udpOppositeIp, result[0].udpOppositeIpPort)
// globalThis.udpClient.rebindUdp('192.168.7.170', '20122','192.168.7.124','20022')
})
}else{
} else {
const udpClient: UdpClient =new UdpClient(result[0].udplocalIp, '55509',result[0].udpOppositeIp,result[0].udpOppositeIpPort)
const udpClient: UdpClient = new UdpClient(result[0].udplocalIp, '55509', result[0].udpOppositeIp, result[0].udpOppositeIpPort)
// const udpClient: UdpClient =new UdpClient(result[0].udplocalIp, '8800','192.168.36.9','11111')
// const udpClient: UdpClient =new UdpClient('192.168.7.170', '20122','192.168.7.124','20022')
// globalThis.udpClient2.rebindUdp('192.168.7.170', '20122','192.168.7.124','20022')
@ -160,22 +170,24 @@ export async function getUDPByTopLine() {
}
// globalThis.udpClient = new UdpClient(result[0].localIp, result[0].localIpPort,result[0].oppositeIp,result[0].oppositeIpPort)
}else{
globalThis.udpClientByTopLine={}
globalThis.udpClientByTopLine.onMessage=()=>{}
} else {
globalThis.udpClientByTopLine = {}
globalThis.udpClientByTopLine.onMessage = () => {
}
}
})
}
export async function setTopLineUdp(){
export async function setTopLineUdp() {
const config = await getSyncData('IpConfigTable');
if(config && config[0] && config[0].udplocalIp){
if (config && config[0] && config[0].udplocalIp) {
const {udplocalIp,udpOppositeIp,udpOppositeIpPort} = config[0];
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509',udpOppositeIp,udpOppositeIpPort)
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509', udpOppositeIp, udpOppositeIpPort)
// const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8800','192.168.36.4','33333')
udpClient.bindUdp()
return {
send(bytes){
send(bytes) {
udpClient.sendHeadMsg(bytes)
}
}
@ -183,21 +195,26 @@ export async function setTopLineUdp(){
}
export async function setJudgeUdp(){
export async function setJudgeUdp() {
const config = await getSyncData('IpConfigTable');
if(config && config[0] && config[0].udplocalIp){
if (config && config[0] && config[0].udplocalIp) {
const {udplocalIp,centerIp,gpsPort} = config[0];
// const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080','192.168.32.167','33333')
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080',centerIp,gpsPort)
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080', centerIp, gpsPort)
// const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8800','192.168.36.4','33333')
udpClient.bindUdp()
udpClient.onMessage((val)=>{
udpClient.onMessage((val) => {
})
globalThis.judgeUdpClient = udpClient;
return {
send(bytes){
console.log('carInfo111',JSON.stringify(globalThis.carInfo))
udpClient.sendMsg({id:45,list:bytes,carNo:globalThis.carInfo.carNo,placeId:globalThis.carInfo.examinationRoomId})
send(bytes) {
console.log('carInfo111', JSON.stringify(globalThis.carInfo))
udpClient.sendMsg({
id: 45,
list: bytes,
carNo: globalThis.carInfo.carNo,
placeId: globalThis.carInfo.examinationRoomId
})
}
}
}

View File

@ -14,164 +14,166 @@
*/
import socket from '@ohos.net.socket';
import {Array2Byte} from '../utils/tools'
import { Array2Byte } from '../utils/tools'
const TAG = '[PLC.UdpClient]'
import prompt from '@ohos.prompt'
export default class UdpClient {
private localIp: string = ''
private localIpPort: string = ''
private oppositeIp: string = ''
private oppositeIpPort: string = ''
private localIp: string = ''
private localIpPort: string = ''
private oppositeIp: string = ''
private oppositeIpPort: string = ''
private udp: any = null
private udp: any = null
constructor(udplocalIp: string, udplocalIpPort:string,udpOppositeIp: string,udpOppositeIpPort:string) {
this.localIp = udplocalIp
this.oppositeIp = udpOppositeIp
this.localIpPort = udplocalIpPort
this.oppositeIpPort = udpOppositeIpPort
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
this.localIp = udplocalIp
this.oppositeIp = udpOppositeIp
this.localIpPort = udplocalIpPort
this.oppositeIpPort = udpOppositeIpPort
this.udp = socket.constructUDPSocketInstance();
}
rebindUdp(localIp: string, localIpPort:string,oppositeIp: string,oppositeIpPort:string){
this.udp = socket.constructUDPSocketInstance();
}
this.localIp = localIp
this.oppositeIp = oppositeIp
this.localIpPort = localIpPort
this.oppositeIpPort = oppositeIpPort
rebindUdp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) {
let promise = this.udp.bind({
// address: '192.168.7.170', port: 20122, family: 1
// address: '192.168.7.170', port: 31013, family: 1
address: this.localIp, port: parseInt(this.localIpPort), family: 1
});
promise.then(() => {
console.log(`${TAG} udp bind success`);
}).catch(err => {
this.localIp = localIp
this.oppositeIp = oppositeIp
this.localIpPort = localIpPort
this.oppositeIpPort = oppositeIpPort
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
});
}
bindUdp() {
console.log('localIp',this.localIp)
console.log('localIpPort',this.localIpPort)
let promise = this.udp.bind({
// address: '192.168.7.170', port: 20122, family: 1
// address: '192.168.7.170', port: 31013, family: 1
address: this.localIp, port: parseInt(this.localIpPort), family: 1
});
promise.then(() => {
console.log(`${TAG} udp bind success`);
}).catch(err => {
let promise = this.udp.bind({
// address: '192.168.7.170', port: 20122, family: 1
// address: '192.168.7.170', port: 31013, family: 1
address: this.localIp, port: parseInt(this.localIpPort), family: 1
});
promise.then(() => {
console.log(`${TAG} udp bind success`);
}).catch(err => {
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
});
}
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
});
}
sendMsg(msg: string) {
let promise = this.udp.send({
data: msg,
address: {
// address: '192.168.7.124',
// port: 30013,
// address: '192.168.7.124',
// port: 20022,
address: this.oppositeIp,
port: parseInt(this.oppositeIpPort),
family: 1
bindUdp() {
console.log('localIp', this.localIp)
console.log('localIpPort', this.localIpPort)
let promise = this.udp.bind({
// address: '192.168.7.170', port: 20122, family: 1
// address: '192.168.7.170', port: 31013, family: 1
address: this.localIp, port: parseInt(this.localIpPort), family: 1
});
promise.then(() => {
console.log(`${TAG} udp bind success`);
}).catch(err => {
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
});
}
sendMsg(msg: string) {
let promise = this.udp.send({
data: msg,
address: {
// address: '192.168.7.124',
// port: 30013,
// address: '192.168.7.124',
// port: 20022,
address: this.oppositeIp,
port: parseInt(this.oppositeIpPort),
family: 1
}
});
promise.then(() => {
console.log(`${TAG} ${this.oppositeIpPort} udp send success:${msg}`);
}).catch(err => {
console.log(`${TAG} udp send fail:${JSON.stringify(err)}`);
});
}
onMessage(callback) {
this.udp.on('message', value => {
// 收到的是ArrayBuffer 需要进行转换解析
globalThis.plcUdpError = false
if (value) {
let dataView = new DataView(value.message)
// console.log(`${TAG} udp message length:${dataView?.byteLength}`);
let str = ""
for (let i = 0; i < dataView?.byteLength; ++i) {
let c = String.fromCharCode(dataView?.getUint8(i))
if (c !== "\n") {
str += c
}
}
console.log(`${TAG} udp on message array buffer:${str}`);
const strachArr = str.split(',')
if (strachArr[0] != '#DN_GD') {
return
}
callback(str)
} else {
callback('')
}
});
const arrRed = [0x55, 0xaa, 0x01, 0x01, 0x02, 0x00, 0x03, 0x00];
const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00];
const arrGreen = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x01];
const arrBlueBuffer = Array2Byte(arrBlue).buffer
const arrRedBuffer = Array2Byte(arrRed).buffer
const arrGreenBugger = Array2Byte(arrGreen).buffer
//监听udp是否断开
clearInterval(globalThis.messageTimer)
globalThis.messageTimer = setInterval(() => {
const lightLineUdp = globalThis.lightLineUdp
const isJudge = globalThis.isJudge
setTimeout(() => {
//程序断开
lightLineUdp?.send(globalThis.plcUdpError ? arrRedBuffer : (isJudge ? arrGreenBugger : arrBlueBuffer));
if (globalThis.plcUdpError) {
prompt.showToast({
message: 'plc udp信号丢失',
duration: 2000
});
}
globalThis.plcUdpError = true;
}, 2000)
}, 3000)
}
closeUdp(callback) {
this.udp.close(err => {
if (err) {
} else {
this.udp.getState((err, data) => {
if (err) {
console.log('getState fail');
return;
} else {
if (!data.isisClose) {
setTimeout(() => {
callback()
}, 1000)
}
});
promise.then(() => {
console.log(`${TAG} ${this.oppositeIpPort} udp send success:${msg}`);
}).catch(err => {
console.log(`${TAG} udp send fail:${JSON.stringify(err)}`);
});
}
}
console.log('getState success:' + JSON.stringify(data));
})
// let promise = this.udp.getState({});
// promise.then(data => {
//
// console.log('getState success:' + JSON.stringify(data));
// }).catch(err => {
// callback()
// console.log('getState fail');
// });
}
});
onMessage(callback) {
this.udp.on('message', value => {
// 收到的是ArrayBuffer 需要进行转换解析
globalThis.plcUdpError = false
if (value) {
let dataView = new DataView(value.message)
// console.log(`${TAG} udp message length:${dataView?.byteLength}`);
let str = ""
for (let i = 0;i < dataView?.byteLength; ++i) {
let c = String.fromCharCode(dataView?.getUint8(i))
if (c !== "\n") {
str += c
}
}
console.log(`${TAG} udp on message array buffer:${str}`);
const strachArr=str.split(',')
if (strachArr[0] != '#DN_GD') {
return
}
callback(str)
}else{
callback('')
}
});
const arrRed= [0x55, 0xaa, 0x01, 0x01, 0x02, 0x00, 0x03, 0x00];
const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00];
const arrGreen =[0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x01];
const arrBlueBuffer= Array2Byte(arrBlue).buffer
const arrRedBuffer = Array2Byte(arrRed).buffer
const arrGreenBugger = Array2Byte(arrGreen).buffer
//监听udp是否断开
clearInterval(globalThis.messageTimer)
globalThis.messageTimer = setInterval(()=>{
const lightLineUdp = globalThis.lightLineUdp
const isJudge = globalThis.isJudge
setTimeout(()=>{
//程序断开
lightLineUdp?.send( globalThis.plcUdpError?arrRedBuffer:(isJudge?arrGreenBugger:arrBlueBuffer));
if(globalThis.plcUdpError){
prompt.showToast({
message: 'plc udp信号丢失',
duration: 2000
});
}
globalThis.plcUdpError = true;
},2000)
},3000)
}
closeUdp(callback) {
this.udp.close(err=>{
if(err){
}else{
this.udp.getState((err, data) => {
if (err) {
console.log('getState fail');
return;
}else{
if(!data.isisClose){
setTimeout(()=>{
callback()
},1000)
}
}
console.log('getState success:' + JSON.stringify(data));
})
// let promise = this.udp.getState({});
// promise.then(data => {
//
// console.log('getState success:' + JSON.stringify(data));
// }).catch(err => {
// callback()
// console.log('getState fail');
// });
}
});
}
}
}

View File

@ -18,7 +18,7 @@ const PERMISSIONS: Array<string> = [
'ohos.permission.CAMERA']
@Component
struct FaceCompare {
export default struct FaceCompare {
constructor() {
super()
}
@ -340,4 +340,4 @@ struct FaceCompare {
}
}
export default FaceCompare
// export default FaceCompare

View File

@ -400,11 +400,11 @@ struct FaceCompare {
return
}
this.param = JSON.parse(data)
this.param.userName = JSON.parse(data).userName
this.param.ip = JSON.parse(data).ip
this.param.pwd = JSON.parse(data).pwd
this.param.port = JSON.parse(data).port
this.param.rlls = JSON.parse(data).rlls
// this.param.userName = JSON.parse(data).userName
// this.param.ip = JSON.parse(data).ip
// this.param.pwd = JSON.parse(data).pwd
// this.param.port = JSON.parse(data).port
// this.param.rlls = JSON.parse(data).rlls
console.log('this.parm', this.param.userName, this.param.pwd, this.param.ip, this.param.port, this.param.rlls)
this.controller.start()
// this.video_url = `rtsp://${this.param.userName}:${this.param.pwd}@${this.param.ip}:${this.param.port}/h264/ch${this.param.rlls}/main/av_stream`;

View File

@ -0,0 +1,31 @@
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'
export default function GlobleUdpTest() {
describe('GlobleUdpTest', () => {
// Defines a test suite. Two parameters are supported: test suite name and test suite function.
beforeAll(() => {
// Presets an action, which is performed only once before all test cases of the test suite start.
// This API supports only one parameter: preset action function.
})
beforeEach(() => {
// Presets an action, which is performed before each unit test case starts.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: preset action function.
})
afterEach(() => {
// Presets a clear action, which is performed after each unit test case ends.
// The number of execution times is the same as the number of test cases defined by **it**.
// This API supports only one parameter: clear action function.
})
afterAll(() => {
// Presets a clear action, which is performed after all test cases of the test suite end.
// This API supports only one parameter: clear action function.
})
it('assertEqual', 0, () => {
// Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
let a = 'test'
// Defines a variety of assertion methods, which are used to declare expected boolean conditions.
expect(a).assertEqual('test')
})
})
}

View File

@ -1,5 +1,7 @@
import GlobleUdpTest from './GlobleUdp.test'
import abilityTest from './Ability.test'
export default function testsuite() {
abilityTest()
GlobleUdpTest()
}

View File

@ -1,6 +1,6 @@
{
"hvigorVersion": "2.1.1",
"hvigorVersion": "3.0.9",
"dependencies": {
"@ohos/hvigor-ohos-plugin": "2.1.1"
"@ohos/hvigor-ohos-plugin": "3.0.9"
}
}
}

3
hvigor/hvigor-wrapper.js Normal file → Executable file

File diff suppressed because one or more lines are too long