济南修改 #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

@ -1,122 +1,114 @@
//import UdpClient from './UdpClient'; //import UdpClient from './UdpClient';
import UdpClientByCenter from './UdpClientByCenter'; import UdpClientByCenter from './UdpClientByCenter';
import { getSyncData} from '../service/initable' import { getSyncData } from '../service/initable'
import {getChuankouFn} from '../../common/service/indexService' import { getChuankouFn } from '../../common/service/indexService'
export async function sendMsg(val){
export async function sendMsg(val) {
// globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val) // globalThis.udpClient1&&globalThis.udpClient1.sendMsg(val)
} }
export async function getUDP() { export async function getUDP() {
console.log(` getUDP enter`); console.log(` getUDP enter`);
getSyncData('IpConfigTable').then((result:Array<any>)=> getSyncData('IpConfigTable').then((result: Array<any>) => {
{ if (result.length) {
if(result.length)
{
console.log(` getUDP has IPConfigTable `); 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 `); console.log(` getUDP has udclent close and rebind `);
globalThis.udpClient.closeUdp(()=>{ globalThis.udpClient.closeUdp(() => {
setTimeout(()=>{ setTimeout(() => {
globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort) globalThis.udpClient.rebindUdp(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
globalThis.udpClient.sendMsg('111',null) globalThis.udpClient.sendMsg('111', null)
globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}` 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) }, 1000)
}) })
} }
else{ else {
// 未绑定 // 未绑定
console.log(` getUDP has no udclent and bind `); console.log(` getUDP has no udclent and bind `);
const udpClient: UdpClientByCenter =new UdpClientByCenter(result[0].udplocalIp, result[0].udplocalIpPort,result[0].udpOppositeIp,result[0].udpOppositeIpPort) const udpClient: UdpClientByCenter = new UdpClientByCenter(result[0].udplocalIp, result[0].udplocalIpPort, result[0].udpOppositeIp, result[0].udpOppositeIpPort)
udpClient.bindUdp() udpClient.bindUdp()
udpClient.sendMsg('111') udpClient.sendMsg('111')
globalThis.host=`http://${result[0].centerIp}:${result[0].centerPort}` 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 globalThis.udpClient = udpClient
getChuankouFn() getChuankouFn()
} }
}else } else {
{
console.log(` getUDP has no IPConfigTable `); console.log(` getUDP has no IPConfigTable `);
// if(globalThis.udpClient) // if(globalThis.udpClient)
// { // {
// console.log(` getUDP2 has IPConfigTable ,has udpClient close it`); // console.log(` getUDP2 has IPConfigTable ,has udpClient close it`);
// globalThis.udpClient.closeUdp(async ()=>{ }) // globalThis.udpClient.closeUdp(async ()=>{ })
// } // }
globalThis.udpClient={} globalThis.udpClient = {}
globalThis.udpClient.onMessage_1=()=>{} globalThis.udpClient.onMessage_1 = () => {}
globalThis.host='' globalThis.host = ''
} }
}) })
} }
export async function getUDP2() { export async function getUDP2() {
console.log(` getUDP2 enter`); console.log(` getUDP2 enter`);
getSyncData('IpConfigTable').then(async (result:Array<any>)=>{ getSyncData('IpConfigTable').then(async (result: Array<any>) => {
if(result.length) if (result.length) {
{
console.log(` getUDP2 has IPConfigTable `); console.log(` getUDP2 has IPConfigTable `);
if(globalThis.udpClient2) if (globalThis.udpClient2 && globalThis.udpClient2.closeUdp) {
{
console.log(` getUDP2 has udclent ,close and rebind `); console.log(` getUDP2 has udclent ,close and rebind `);
globalThis.udpClient2.closeUdp(async ()=>{ globalThis.udpClient2.closeUdp(async () => {
setTimeout(()=>{ setTimeout(() => {
globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort) globalThis.udpClient2.rebindUdp(result[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort)
//保留原来的 }, 1000)
// globalThis.udpClient2.onMessage((val)=>{ })
// globalThis.udpClient2.onError()
// globalThis.udpClient2.setMsgCallBack=()=>{}
},1000)
}) })
} }
else else {
{
// 未绑定 // 未绑定
console.log(` getUDP2 has no udclent and bind `); console.log(` getUDP2 has no udclent and bind `);
const udpClient2: UdpClientByCenter =new UdpClientByCenter(result[0].udplocalIp, '8800',globalThis.carInfo?.udpAddress,globalThis.carInfo?.messagePort) const udpClient2: UdpClientByCenter = new UdpClientByCenter(result[0].udplocalIp, '8800', globalThis.carInfo?.udpAddress, globalThis.carInfo?.messagePort)
udpClient2.bindUdp() udpClient2.bindUdp()
udpClient2.onError_Callback() udpClient2.onError_Callback()
udpClient2.onMessage_1((val)=>{ udpClient2.onMessage_1((val) => {
if(val.id=='32'){ if (val.id == '32') {
globalThis.signNum=val.body[1] globalThis.signNum = val.body[1]
}else if(val.id=='46'){ } else if (val.id == '46') {
let tmpList = [] let tmpList = []
const str = 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]) 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.send(param)
} }
}) })
globalThis.udpClient2 = udpClient2 globalThis.udpClient2 = udpClient2
} }
}else{ } else {
console.log(` getUDP2 has no IPConfigTable `); globalThis.udpClient2 = {}
if(globalThis.udpClient2) globalThis.udpClient2.onMessage_2 = () => {
{ }
console.log(` getUDP2 has IPConfigTable ,has udpClient close it`); globalThis.udpClient2.setMsgCallBack = () => {
globalThis.udpClient2.closeUdp(async ()=>{ })
} }
globalThis.udpClient2={}
globalThis.udpClient2.onMessage_2=()=>{}
globalThis.udpClient2.setMsgCallBack=()=>{}
} }
}) })
} }
export async function setTopLineUdp(){ export async function setTopLineUdp() {
const config = await getSyncData('IpConfigTable'); 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 {udplocalIp,udpOppositeIp,udpOppositeIpPort} = config[0];
const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509',udpOppositeIp,udpOppositeIpPort) const udpClient: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '55509', udpOppositeIp, udpOppositeIpPort)
udpClient.bindUdp() udpClient.bindUdp()
return { return {
send(bytes){ send(bytes) {
udpClient.sendHeadMsg(bytes) udpClient.sendHeadMsg(bytes)
} }
} }
@ -124,32 +116,32 @@ export async function setTopLineUdp(){
} }
// //
export async function setJudgeUdp(){ export async function setJudgeUdp() {
const config = await getSyncData('IpConfigTable'); const config = await getSyncData('IpConfigTable');
let udpIndex = 0; let udpIndex = 0;
let currentUdpIndex = 0; let currentUdpIndex = 0;
let judgeUdpTimer let judgeUdpTimer
clearInterval(judgeUdpTimer) clearInterval(judgeUdpTimer)
judgeUdpTimer = setInterval(()=>{ judgeUdpTimer = setInterval(() => {
udpIndex += 1; udpIndex += 1;
},1000); }, 1000);
if(config && config[0] && config[0].udplocalIp){ if (config && config[0] && config[0].udplocalIp) {
const {udplocalIp} = config[0]; const {udplocalIp} = config[0];
const udpClientbyCenter: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080',globalThis.carInfo?.gpsAddress,globalThis.carInfo?.hintPort) const udpClientbyCenter: UdpClientByCenter = new UdpClientByCenter(udplocalIp, '8080', globalThis.carInfo?.gpsAddress, globalThis.carInfo?.hintPort)
udpClientbyCenter.bindUdp() udpClientbyCenter.bindUdp()
udpClientbyCenter.onMessage_1((val)=>{ udpClientbyCenter.onMessage_1((val) => {
console.log('valval',val) console.log('valval', val)
}) })
// globalThis.judgeUdpClient = udpClientbyCenter; // globalThis.judgeUdpClient = udpClientbyCenter;
return { return {
send(bytes){ send(bytes) {
if(udpIndex > currentUdpIndex){ if (udpIndex > currentUdpIndex) {
udpClientbyCenter.sendMsgExt({id:45, udpClientbyCenter.sendMsgExt({ id: 45,
list:bytes, list: bytes,
carNo:globalThis.carInfo.carNo, carNo: globalThis.carInfo.carNo,
placeId:globalThis.carInfo.examinationRoomId}) placeId: globalThis.carInfo.examinationRoomId })
currentUdpIndex = udpIndex currentUdpIndex = udpIndex
} }
} }

View File

@ -13,10 +13,12 @@
* limitations under the License. * limitations under the License.
*/ */
import socket ,{UDPSocket} from '@ohos.net.socket'; // @ts-ignore
import socket, { UDPSocket } from '@ohos.net.socket';
import { Array2Byte } from '../utils/tools' import { Array2Byte } from '../utils/tools'
import FileUtil from '../../common/utils/File' import FileUtil from '../../common/utils/File'
import {fillZero,string2Bytes} from '../utils/tools' import { fillZero, string2Bytes } from '../utils/tools'
const TAG = '[UdpDemo.UdpClient]' const TAG = '[UdpDemo.UdpClient]'
import hilog from '@ohos.hilog'; import hilog from '@ohos.hilog';
import prompt from '@ohos.prompt' import prompt from '@ohos.prompt'
@ -27,31 +29,35 @@ export default class UdpClientByCenter {
private localIpPort: string = '' private localIpPort: string = ''
private oppositeIp: string = '' private oppositeIp: string = ''
private oppositeIpPort: string = '' private oppositeIpPort: string = ''
private udpMsg: any='' private udpMsg: any = ''
private num:number = 0 private num: number = 0
private fileUtil: FileUtil private fileUtil: FileUtil
private udp: UDPSocket = null private udp: UDPSocket = null
private sendId: any = 0 private sendId: any = 0
private lsh: string = null private lsh: string = null
private context private context
private stashFn:StashFunction private stashFn: StashFunction
private headLenth:number=9 //消息头长度 private headLenth: number = 9
private isWorking:Boolean = false //消息头长度
private isWorking: Boolean = false
private plcUdpError = false; private plcUdpError = false;
private initParam private initParam
constructor(udplocalIp: string, udplocalIpPort:string,udpOppositeIp: string,udpOppositeIpPort:string) {
constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
this.localIp = udplocalIp this.localIp = udplocalIp
this.oppositeIp = udpOppositeIp this.oppositeIp = udpOppositeIp
this.localIpPort = udplocalIpPort this.localIpPort = udplocalIpPort
this.oppositeIpPort = udpOppositeIpPort this.oppositeIpPort = udpOppositeIpPort
this.stashFn=()=>{} this.stashFn = () => {
}
this.udp = socket.constructUDPSocketInstance(); this.udp = socket.constructUDPSocketInstance();
} }
getStatus()
{ getStatus() {
return this.isWorking return this.isWorking
} }
rebindUdp(localIp: string, localIpPort:string,oppositeIp: string,oppositeIpPort:string){
rebindUdp(localIp: string, localIpPort: string, oppositeIp: string, oppositeIpPort: string) {
this.localIp = localIp this.localIp = localIp
this.oppositeIp = oppositeIp this.oppositeIp = oppositeIp
this.localIpPort = localIpPort this.localIpPort = localIpPort
@ -78,85 +84,79 @@ export default class UdpClientByCenter {
address: this.localIp, port: parseInt(this.localIpPort), family: 1 address: this.localIp, port: parseInt(this.localIpPort), family: 1
}); });
promise.then(() => { promise.then(() => {
// /globalThis.closeHeartSocket=false
this.isWorking = true this.isWorking = true
console.log(`${TAG} udp bind success`); console.log(`${TAG} udp bind success`);
}).catch(err => { }).catch(err => {
// globalThis.closeHeartSocket=true
this.isWorking = false this.isWorking = false
console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`); console.log(`${TAG} udp bind failed:${JSON.stringify(err)}`);
}); });
} }
//异或运算 //异或运算
setMessageExclusive(tmpList){ setMessageExclusive(tmpList) {
let result = tmpList[0]; let result = tmpList[0];
for (let i = 1; i < tmpList.length; i++) { for (let i = 1; i < tmpList.length; i++) {
result = result ^ tmpList[i] result = result ^ tmpList[i]
} }
return [result]; return [result];
} }
Array2Byte(array) { Array2Byte(array) {
var buf = new ArrayBuffer(array.length); var buf = new ArrayBuffer(array.length);
var view = new Uint8Array(buf); 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; view[i] = array[i] & 0xFF;
} }
return view; return view;
} }
//length消息体bufferlength id消息类型id bodyStr消息体string //length消息体bufferlength id消息类型id bodyStr消息体string
// setMsyBody(id,bodyByte){ // setMsyBody(id,bodyByte){
// {id: 31,list:[0,1,'0000000000000'],carNo:489,placeId:62} // {id: 31,list:[0,1,'0000000000000'],carNo:489,placeId:62}
setWholeMsg(params){ setWholeMsg(params) {
let head = this.setMsgHead(params); let head = this.setMsgHead(params);
let headJudge = this.setMessageExclusive(head); let headJudge = this.setMessageExclusive(head);
let body = this.setMsgBody(params); let body = this.setMsgBody(params);
let bodyJudge = this.setMessageExclusive(body); let bodyJudge = this.setMessageExclusive(body);
let end = [13,10]; let end = [13, 10];
const arr=[...head, ...headJudge,...body, ...bodyJudge,...end] const arr = [...head, ...headJudge, ...body, ...bodyJudge, ...end]
console.log('BitArray',arr) console.log('BitArray', arr)
return this.Array2Byte(arr).buffer return this.Array2Byte(arr).buffer
} }
setMsgHead({id, list, placeId=62, carNo=489}){
console.log('globalThis.lshNo',globalThis.lshNo) setMsgHead({id, list, placeId=62, carNo=489}) {
let a = string2Bytes(`${id}${fillZero(placeId,3)}`, 2*8); console.log('globalThis.lshNo', globalThis.lshNo)
console.log('aaa',a) let a = string2Bytes(`${id}${fillZero(placeId, 3)}`, 2 * 8);
let b = string2Bytes(`${fillZero(carNo,4)}${globalThis.lshNo}`, 4*8); console.log('aaa', a)
console.log('bbb',b) let b = string2Bytes(`${fillZero(carNo, 4)}${globalThis.lshNo}`, 4 * 8);
let c = string2Bytes(list.length, 2*8); console.log('bbb', b)
return [...a,...b,...c]; let c = string2Bytes(list.length, 2 * 8);
return [...a, ...b, ...c];
} }
setMsgBody({id,list}) { setMsgBody({id,list}) {
let tmpList = [] let tmpList = []
tmpList = list tmpList = list
return tmpList ; return tmpList;
} }
sendHeadMsg(msgData){ sendHeadMsg(msgData) {
console.log('sendHeadMsg enter') console.log('sendHeadMsg enter')
this.sendMsg(msgData,null) this.sendMsg(msgData, null)
console.log('sendHeadMsg exit') console.log('sendHeadMsg exit')
} }
sendMsg(msg,sendCallback?) { sendMsg(msg, sendCallback?) {
console.log(TAG,'sendMsg enter status:'+this.isWorking+' ip:'+this.oppositeIp+' port:'+this.oppositeIpPort) if (!this.isWorking && sendCallback) {
if(!this.isWorking)
{
console.log(TAG,'sendMsg has not working ,just return status:'+this.isWorking+' ip:'+this.oppositeIp+' port:'+this.oppositeIpPort)
if(sendCallback){
sendCallback() sendCallback()
} }
}
this.udp.getState((err, data) => { this.udp.getState((err, data) => {
if (err) { if (err) {
//globalThis.closeHeartSocket=true
this.isWorking = false this.isWorking = false
console.log('sendMsg getState fail ip:'+this.oppositeIp+' port:'+this.oppositeIpPort);
return; return;
} else { } else {
let promise = this.udp.send({ let promise = this.udp.send({
@ -167,7 +167,7 @@ export default class UdpClientByCenter {
} }
}); });
promise.then(() => { promise.then(() => {
if(sendCallback){ if (sendCallback) {
sendCallback() sendCallback()
} }
console.log(`${TAG}udpCLient udp send success:oppositeIp${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort,${this.localIpPort}`); console.log(`${TAG}udpCLient udp send success:oppositeIp${this.oppositeIp},oppositeIpPort:${this.oppositeIpPort},localIp:${this.localIp},localIpPort,${this.localIpPort}`);
@ -178,100 +178,91 @@ export default class UdpClientByCenter {
console.log('getState success:' + JSON.stringify(data)); console.log('getState success:' + JSON.stringify(data));
}) })
} }
sendMsgExt(param,context?) {
sendMsgExt(param, context?) {
console.log('sendMsgExt enter'); console.log('sendMsgExt enter');
if(context){ if (context) {
this.context=context this.context = context
} }
this.sendId=param.id this.sendId = param.id
const msgData=this.setWholeMsg(param) const msgData = this.setWholeMsg(param)
this.sendMsg(msgData,param.sendCallback) this.sendMsg(msgData, param.sendCallback)
} }
onError_Callback(callback?){
this.udp.on('error',async err => { onError_Callback(callback?) {
this.udp.on('error', async err => {
this.isWorking = false; this.isWorking = false;
console.log('UdpClientByCenter onError err:' + JSON.stringify(err)); console.log('UdpClientByCenter onError err:' + JSON.stringify(err));
callback&&callback() callback && callback()
}); });
} }
onError_resend(callback?){
this.udp.on('error',async err => { onError_resend(callback?) {
this.udp.on('error', async err => {
this.isWorking = false; this.isWorking = false;
console.log(TAG,'udpError',JSON.stringify(err)) console.log(TAG, 'udpError', JSON.stringify(err))
// await this.bindUdp() await this.bindUdp()
// await this.sendMsg('111',null) await this.sendMsg('111', null)
//await this.onMessage_1(callback) await this.onMessage_1(callback)
callback&&callback() callback && callback()
}); });
} }
setMsgCallBack(callback){
this.stashFn=callback?callback:()=>{} setMsgCallBack(callback) {
this.stashFn = callback ? callback : () => {
} }
onMessage_2(callback,type?) { }
this.udp.on('message', (value,remoteInfo) => { //中心udp回执
onMessage_2(callback, type?) {
this.udp.on('message', (value, remoteInfo) => {
console.log('UdpClientByCenter onMessage msg:' + JSON.stringify(value)); console.log('UdpClientByCenter onMessage msg:' + JSON.stringify(value));
let arr=[] let arr = []
let dataView = new DataView(value.message) let dataView = new DataView(value.message)
for (let i = 0;i < dataView?.byteLength; ++i) { for (let i = 0; i < dataView?.byteLength; ++i) {
arr[i]=dataView?.getUint8(i) arr[i] = dataView?.getUint8(i)
} }
let idNum = '0x' + fillZero(arr[1].toString(16),2) + fillZero(arr[0].toString(16),2) ; let idNum = '0x' + fillZero(arr[1].toString(16), 2) + fillZero(arr[0].toString(16), 2);
let id = Math.floor(+idNum/1000) let id = Math.floor(+idNum / 1000)
hilog.info(0x0000, 'bitbit', JSON.stringify(arr)); hilog.info(0x0000, 'bitbit', JSON.stringify(arr));
let lengthNum = '0x' + fillZero(arr[7].toString(16),2) + fillZero(arr[6].toString(16),2) ; let lengthNum = '0x' + fillZero(arr[7].toString(16), 2) + fillZero(arr[6].toString(16), 2);
let length= +lengthNum; let length = +lengthNum;
let list=[] let list = []
for(let i=this.headLenth;i<=this.headLenth+length-1;i++){ for (let i = this.headLenth; i <= this.headLenth + length - 1; i++) {
list.push(arr[i]) list.push(arr[i])
} }
this.stashFn({id,length,body:list,sendId:this.sendId}) this.stashFn({
callback({id,length,body:list,sendId:this.sendId}) id, length, body: list, sendId: this.sendId
})
callback({
id, length, body: list, sendId: this.sendId
})
}); });
} }
closeUdp(callback?) { closeUdp(callback?) {
console.log('UdpClientByCenter enter closeUdp ip:' +this.localIp+' port:'+this.localIpPort); console.log('UdpClientByCenter enter closeUdp ip:' + this.localIp + ' port:' + this.localIpPort);
this.udp.on('close', () => { this.udp.on('close', () => {
console.log('UdpClientByCenter onClose ip:' + this.localIp + ' port:' + this.localIpPort); console.log('UdpClientByCenter onClose ip:' + this.localIp + ' port:' + this.localIpPort);
this.isWorking = false this.isWorking = false
if (callback!=null) { if (callback != null) {
callback() callback()
} }
} }
) )
this.udp.close(err=> { this.udp.close(err => {
if (err) { if (err) {
console.log('UdpClientByCenter closeUdp failed ip:'+this.localIp+' port:'+this.localIpPort+' err:' + JSON.stringify(err)); console.log('UdpClientByCenter closeUdp failed ip:' + this.localIp + ' port:' + this.localIpPort + ' err:' + JSON.stringify(err));
} else { } else {
console.log('UdpClientByCenter closeUdp succeed ip:' +this.localIp+' port:'+this.localIpPort ); 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?) { onMessage_1(callback?) {
this.udp.on('message', value => { this.udp.on('message', value => {
console.log(TAG,'udponmessage') console.log(TAG, 'udponmessage')
// 收到的是ArrayBuffer 需要进行转换解析 // 收到的是ArrayBuffer 需要进行转换解析
this.plcUdpError = false this.plcUdpError = false
if (value) { if (value) {
@ -291,28 +282,28 @@ export default class UdpClientByCenter {
} }
console.log(`${TAG} udp222 on message array buffer:${str}`); console.log(`${TAG} udp222 on message array buffer:${str}`);
strachArr[28]=globalThis.chuankoMsg||'0' strachArr[28] = globalThis.chuankoMsg || '0'
// this.stashFn(str) // this.stashFn(str)
const newArr=JSON.parse(JSON.stringify(strachArr)) const newArr = JSON.parse(JSON.stringify(strachArr))
// this.stashFn=()=>{} // this.stashFn=()=>{}
callback&&callback(newArr.toString()) callback && callback(newArr.toString())
} else { } else {
callback&&callback('') callback && callback('')
} }
console.log('messageTimeEnd') console.log('messageTimeEnd')
this.testIfUdpConnetced(callback) this.testIfUdpConnetced(callback)
}); });
} }
testIfUdpConnetced(callback)
{ testIfUdpConnetced(callback) {
const arrRed = [0x55, 0xaa, 0x01, 0x01, 0x02, 0x00, 0x03, 0x00]; const arrRed = [0x55, 0xaa, 0x01, 0x01, 0x02, 0x00, 0x03, 0x00];
const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00]; const arrBlue = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x01, 0x03, 0x00];
const arrGreen = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x01]; const arrGreen = [0x55, 0xaa, 0x01, 0x00, 0x02, 0x00, 0x03, 0x01];
const arrBlueBuffer = Array2Byte(arrBlue).buffer const arrBlueBuffer = Array2Byte(arrBlue).buffer
const arrRedBuffer = Array2Byte(arrRed).buffer const arrRedBuffer = Array2Byte(arrRed).buffer
const arrGreenBugger = Array2Byte(arrGreen).buffer const arrGreenBugger = Array2Byte(arrGreen).buffer
let num=0 let num = 0
//监听udp是否断开 //监听udp是否断开
clearInterval(globalThis.messageTimer) clearInterval(globalThis.messageTimer)
globalThis.messageTimer = setInterval(() => { globalThis.messageTimer = setInterval(() => {
@ -323,12 +314,12 @@ export default class UdpClientByCenter {
lightLineUdp?.send(this.plcUdpError ? arrRedBuffer : (isJudge ? arrGreenBugger : arrBlueBuffer)); lightLineUdp?.send(this.plcUdpError ? arrRedBuffer : (isJudge ? arrGreenBugger : arrBlueBuffer));
if (this.plcUdpError) { if (this.plcUdpError) {
num++ num++
console.log(TAG,'plc udp信号丢失') console.log(TAG, 'plc udp信号丢失')
if(num==3){ if (num == 3) {
await this.bindUdp() await this.bindUdp()
await this.sendMsg('111',null) await this.sendMsg('111', null)
await this.onMessage_1(callback) await this.onMessage_1(callback)
num=0 num = 0
} }
prompt.showToast({ prompt.showToast({
message: 'plc udp信号丢失', message: 'plc udp信号丢失',
@ -339,12 +330,19 @@ export default class UdpClientByCenter {
}, 2000) }, 2000)
}, 3000) }, 3000)
} }
// initHeartSendMsg(param,context){ // initHeartSendMsg(param,context){
// console.log('1111param',JSON.stringify(param)) // console.log('1111param',JSON.stringify(param))
// this.initParam=param // this.initParam=param
// this.context=context // this.context=context
// } // }
} }
interface StashFunction { interface StashFunction {
(params: { id: number; length: number; body: any[]; sendId: string }): void; (params: {
id: number;
length: number;
body: any[];
sendId: string
}): void;
} }