济南修改 #25
@ -2,25 +2,24 @@
|
|||||||
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)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@ -31,13 +30,12 @@ export async function getUDP() {
|
|||||||
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)
|
||||||
// {
|
// {
|
||||||
@ -54,24 +52,17 @@ export async function getUDP() {
|
|||||||
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)
|
||||||
//保留原来的
|
|
||||||
// globalThis.udpClient2.onMessage((val)=>{ })
|
|
||||||
// globalThis.udpClient2.onError()
|
|
||||||
// globalThis.udpClient2.setMsgCallBack=()=>{}
|
|
||||||
}, 1000)
|
}, 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)
|
||||||
@ -88,22 +79,23 @@ export async function getUDP2() {
|
|||||||
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 `);
|
|
||||||
if(globalThis.udpClient2)
|
|
||||||
{
|
|
||||||
console.log(` getUDP2 has IPConfigTable ,has udpClient ,close it`);
|
|
||||||
globalThis.udpClient2.closeUdp(async ()=>{ })
|
|
||||||
}
|
|
||||||
globalThis.udpClient2 = {}
|
globalThis.udpClient2 = {}
|
||||||
globalThis.udpClient2.onMessage_2=()=>{}
|
globalThis.udpClient2.onMessage_2 = () => {
|
||||||
globalThis.udpClient2.setMsgCallBack=()=>{}
|
}
|
||||||
|
globalThis.udpClient2.setMsgCallBack = () => {
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -13,10 +13,12 @@
|
|||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
import socket, { UDPSocket } from '@ohos.net.socket';
|
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'
|
||||||
@ -35,22 +37,26 @@ export default class UdpClientByCenter {
|
|||||||
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
|
||||||
@ -78,11 +84,9 @@ 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)}`);
|
||||||
});
|
});
|
||||||
@ -96,15 +100,16 @@ export default class UdpClientByCenter {
|
|||||||
}
|
}
|
||||||
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){
|
||||||
|
|
||||||
@ -120,6 +125,7 @@ export default class UdpClientByCenter {
|
|||||||
return this.Array2Byte(arr).buffer
|
return this.Array2Byte(arr).buffer
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
setMsgHead({id, list, placeId=62, carNo=489}) {
|
setMsgHead({id, list, placeId=62, carNo=489}) {
|
||||||
console.log('globalThis.lshNo', globalThis.lshNo)
|
console.log('globalThis.lshNo', globalThis.lshNo)
|
||||||
let a = string2Bytes(`${id}${fillZero(placeId, 3)}`, 2 * 8);
|
let a = string2Bytes(`${id}${fillZero(placeId, 3)}`, 2 * 8);
|
||||||
@ -129,6 +135,7 @@ export default class UdpClientByCenter {
|
|||||||
let c = string2Bytes(list.length, 2 * 8);
|
let c = string2Bytes(list.length, 2 * 8);
|
||||||
return [...a, ...b, ...c];
|
return [...a, ...b, ...c];
|
||||||
}
|
}
|
||||||
|
|
||||||
setMsgBody({id,list}) {
|
setMsgBody({id,list}) {
|
||||||
let tmpList = []
|
let tmpList = []
|
||||||
tmpList = list
|
tmpList = list
|
||||||
@ -144,19 +151,12 @@ export default class UdpClientByCenter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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({
|
||||||
@ -178,6 +178,7 @@ 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) {
|
||||||
@ -187,6 +188,7 @@ export default class UdpClientByCenter {
|
|||||||
const msgData = this.setWholeMsg(param)
|
const msgData = this.setWholeMsg(param)
|
||||||
this.sendMsg(msgData, param.sendCallback)
|
this.sendMsg(msgData, param.sendCallback)
|
||||||
}
|
}
|
||||||
|
|
||||||
onError_Callback(callback?) {
|
onError_Callback(callback?) {
|
||||||
this.udp.on('error', async err => {
|
this.udp.on('error', async err => {
|
||||||
this.isWorking = false;
|
this.isWorking = false;
|
||||||
@ -194,19 +196,23 @@ export default class UdpClientByCenter {
|
|||||||
callback && callback()
|
callback && callback()
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
onError_resend(callback?) {
|
onError_resend(callback?) {
|
||||||
this.udp.on('error', async err => {
|
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) {
|
setMsgCallBack(callback) {
|
||||||
this.stashFn=callback?callback:()=>{}
|
this.stashFn = callback ? callback : () => {
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
//中心udp回执
|
||||||
onMessage_2(callback, type?) {
|
onMessage_2(callback, type?) {
|
||||||
this.udp.on('message', (value, remoteInfo) => {
|
this.udp.on('message', (value, remoteInfo) => {
|
||||||
console.log('UdpClientByCenter onMessage msg:' + JSON.stringify(value));
|
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++) {
|
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
|
||||||
|
})
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -247,28 +257,9 @@ export default class UdpClientByCenter {
|
|||||||
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')
|
||||||
@ -304,8 +295,8 @@ export default class UdpClientByCenter {
|
|||||||
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];
|
||||||
@ -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;
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user