济南修改 #25

Merged
wangzhongjie merged 4 commits from lv_chengmai into main 2024-07-09 14:29:14 +08:00
2 changed files with 377 additions and 387 deletions
Showing only changes of commit acc9f03109 - Show all commits

View File

@ -2,25 +2,24 @@
import UdpClientByCenter from './UdpClientByCenter';
import { getSyncData } from '../service/initable'
import { getChuankouFn } from '../../common/service/indexService'
export async function sendMsg(val) {
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
}
export async function getUDP() {
console.log(` getUDP enter`);
getSyncData('IpConfigTable').then((result:Array<any>)=>
{
if(result.length)
{
getSyncData('IpConfigTable').then((result: Array<any>) => {
if (result.length) {
console.log(` getUDP has IPConfigTable `);
if(globalThis.udpClient&&globalThis.udpClient.closeUdp)
{
if (globalThis.udpClient && globalThis.udpClient.closeUdp) {
console.log(` getUDP has udclent close and rebind `);
globalThis.udpClient.closeUdp(() => {
setTimeout(() => {
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
globalThis.udpClient.sendMsg('111', null)
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
// globalThis.udpClient.onError_resend(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
globalThis.udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
}, 1000)
})
}
@ -31,13 +30,12 @@ export async function getUDP() {
udpClient.bindUdp()
udpClient.sendMsg('111')
globalThis.host = `http://${result[0].centerIp}:${result[0].centerPort}`
// udpClient.onError_resend(globalThis.udpClient.onMessage?globalThis.udpClient.onMessage:()=>{})
// udpClient.onError_resend(globalThis.udpClient.onMessage_1?globalThis.udpClient.onMessage_1:()=>{})
globalThis.udpClient = udpClient
getChuankouFn()
}
}else
{
} else {
console.log(` getUDP has no IPConfigTable `);
// if(globalThis.udpClient)
// {
@ -54,24 +52,17 @@ export async function getUDP() {
export async function getUDP2() {
console.log(` getUDP2 enter`);
getSyncData('IpConfigTable').then(async (result: Array<any>) => {
if(result.length)
{
if (result.length) {
console.log(` getUDP2 has IPConfigTable `);
if(globalThis.udpClient2)
{
if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp) {
console.log(` getUDP2 has udclent ,close and rebind `);
globalThis.udpClient2.closeUdp(async () => {
setTimeout(() => {
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort)
//保留原来的
// globalThis.udpClient2.onMessage((val)=>{ })
// globalThis.udpClient2.onError()
// globalThis.udpClient2.setMsgCallBack=()=>{}
}, 1000)
})
}
else
{
else {
// 未绑定
console.log(` getUDP2 has no udclent and bind `);
const udpClient2: UdpClientByCenter = new UdpClientByCenter(result[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort)
@ -88,22 +79,23 @@ export async function getUDP2() {
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)
}
})
globalThis.udpClient2 = udpClient2
}
} else {
console.log(` getUDP2 has no IPConfigTable `);
if(globalThis.udpClient2)
{
console.log(` getUDP2 has IPConfigTable ,has udpClient close it`);
globalThis.udpClient2.closeUdp(async ()=>{ })
}
globalThis.udpClient2 = {}
globalThis.udpClient2.onMessage_2=()=>{}
globalThis.udpClient2.setMsgCallBack=()=>{}
globalThis.udpClient2.onMessage_2 = () => {
}
globalThis.udpClient2.setMsgCallBack = () => {
}
}
})
}

View File

@ -13,10 +13,12 @@
* limitations under the License.
*/
// @ts-ignore
import socket, { UDPSocket } from '@ohos.net.socket';
import { Array2Byte } from '../utils/tools'
import FileUtil from '../../common/utils/File'
import { fillZero, string2Bytes } from '../utils/tools'
const TAG = '[UdpDemo.UdpClient]'
import hilog from '@ohos.hilog';
import prompt from '@ohos.prompt'
@ -35,22 +37,26 @@ export default class UdpClientByCenter {
private lsh: string = null
private context
private stashFn: StashFunction
private headLenth:number=9 //消息头长度
private headLenth: number = 9
//消息头长度
private isWorking: Boolean = false
private plcUdpError = false;
private initParam
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
this.localIp = udplocalIp
this.oppositeIp = udpOppositeIp
this.localIpPort = udplocalIpPort
this.oppositeIpPort = udpOppositeIpPort
this.stashFn=()=>{}
this.stashFn = () => {
}
this.udp = socket.constructUDPSocketInstance();
}
getStatus()
{
getStatus() {
return this.isWorking
}
rebindUdp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) {
this.localIp = localIp
this.oppositeIp = oppositeIp
@ -78,11 +84,9 @@ export default class UdpClientByCenter {
address: this.localIp, port: parseInt(this.localIpPort), family: 1
});
promise.then(() => {
// /globalThis.closeHeartSocket=false
this.isWorking = true
console.log(`${TAG} udp bind success`);
}).catch(err => {
// globalThis.closeHeartSocket=true
this.isWorking = false
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
});
@ -96,15 +100,16 @@ export default class UdpClientByCenter {
}
return [result];
}
Array2Byte(array) {
var buf = new ArrayBuffer(array.length);
var view = new Uint8Array(buf);
for (var i = 0; i != array.length; ++i)
{
for (var i = 0; i != array.length; ++i) {
view[i] = array[i] & 0xFF;
}
return view;
}
//length消息体bufferlength id消息类型id bodyStr消息体string
// setMsyBody(id,bodyByte){
@ -120,6 +125,7 @@ export default class UdpClientByCenter {
return this.Array2Byte(arr).buffer
}
setMsgHead({id, list, placeId=62, carNo=489}) {
console.log('globalThis.lshNo', globalThis.lshNo)
let a = string2Bytes(`${id}${fillZero(placeId, 3)}`, 2 * 8);
@ -129,6 +135,7 @@ export default class UdpClientByCenter {
let c = string2Bytes(list.length, 2 * 8);
return [...a, ...b, ...c];
}
setMsgBody({id,list}) {
let tmpList = []
tmpList = list
@ -144,19 +151,12 @@ export default class UdpClientByCenter {
}
sendMsg(msg, sendCallback?) {
console.log(TAG,'sendMsg enter status:'+this.isWorking+' ip:'+this.oppositeIp+' port:'+this.oppositeIpPort)
if(!this.isWorking)
{
console.log(TAG,'sendMsg has not working ,just return status:'+this.isWorking+' ip:'+this.oppositeIp+' port:'+this.oppositeIpPort)
if(sendCallback){
if (!this.isWorking && sendCallback) {
sendCallback()
}
}
this.udp.getState((err, data) => {
if (err) {
//globalThis.closeHeartSocket=true
this.isWorking = false
console.log('sendMsg getState fail ip:'+this.oppositeIp+' port:'+this.oppositeIpPort);
return;
} else {
let promise = this.udp.send({
@ -178,6 +178,7 @@ export default class UdpClientByCenter {
console.log('getState success:' + JSON.stringify(data));
})
}
sendMsgExt(param, context?) {
console.log('sendMsgExt enter');
if (context) {
@ -187,6 +188,7 @@ export default class UdpClientByCenter {
const msgData = this.setWholeMsg(param)
this.sendMsg(msgData, param.sendCallback)
}
onError_Callback(callback?) {
this.udp.on('error', async err => {
this.isWorking = false;
@ -194,19 +196,23 @@ export default class UdpClientByCenter {
callback && callback()
});
}
onError_resend(callback?) {
this.udp.on('error', async err => {
this.isWorking = false;
console.log(TAG, 'udpError', JSON.stringify(err))
// await this.bindUdp()
// await this.sendMsg('111',null)
//await this.onMessage_1(callback)
await this.bindUdp()
await this.sendMsg('111', null)
await this.onMessage_1(callback)
callback && callback()
});
}
setMsgCallBack(callback) {
this.stashFn=callback?callback:()=>{}
this.stashFn = callback ? callback : () => {
}
}
//中心udp回执
onMessage_2(callback, type?) {
this.udp.on('message', (value, remoteInfo) => {
console.log('UdpClientByCenter onMessage msg:' + JSON.stringify(value));
@ -225,8 +231,12 @@ export default class UdpClientByCenter {
for (let i = this.headLenth; i <= this.headLenth + length - 1; i++) {
list.push(arr[i])
}
this.stashFn({id,length,body:list,sendId:this.sendId})
callback({id,length,body:list,sendId:this.sendId})
this.stashFn({
id, length, body: list, sendId: this.sendId
})
callback({
id, length, body: list, sendId: this.sendId
})
});
}
@ -247,28 +257,9 @@ export default class UdpClientByCenter {
console.log('UdpClientByCenter closeUdp succeed ip:' + this.localIp + ' port:' + this.localIpPort);
}
})
// globalThis.closeHeartSocket=true
//
// this.udp.close(err=>{
// if(err){
//
// }else{
// this.udp.getState((err, data) => {
// if (err) {
// return;
// }else{
// globalThis.closeHeartSocket=true
// if(!data.isisClose){
// setTimeout(()=>{
// callback()
// },1000)
// }
// }
// })
// }
// });
}
//plc
onMessage_1(callback?) {
this.udp.on('message', value => {
console.log(TAG, 'udponmessage')
@ -304,8 +295,8 @@ export default class UdpClientByCenter {
this.testIfUdpConnetced(callback)
});
}
testIfUdpConnetced(callback)
{
testIfUdpConnetced(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];
@ -339,12 +330,19 @@ export default class UdpClientByCenter {
}, 2000)
}, 3000)
}
// initHeartSendMsg(param,context){
// console.log('1111param',JSON.stringify(param))
// this.initParam=param
// this.context=context
// }
}
interface StashFunction {
(params: { id: number; length: number; body: any[]; sendId: string }): void;
(params: {
id: number;
length: number;
body: any[];
sendId: string
}): void;
}