244 lines
7.9 KiB
TypeScript
244 lines
7.9 KiB
TypeScript
/*
|
|
* Copyright (c) 2022 Huawei Device Co., Ltd.
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
import socket from '@ohos.net.socket';
|
|
import fs from '@ohos.file.fs'
|
|
import mediaLibrary from '@ohos.multimedia.mediaLibrary'
|
|
import FileUtil from '../../common/utils/File'
|
|
import {fillZero,string2Bytes} from '../utils/tools'
|
|
|
|
const TAG = '[UdpDemo.UdpClient]'
|
|
// import common from '@ohos.app.ability.common';
|
|
|
|
import prompt from '@ohos.prompt'
|
|
import call from '@ohos.telephony.call';
|
|
|
|
|
|
export default class UdpClientByCenter {
|
|
private localIp: string = ''
|
|
private localIpPort: string = ''
|
|
private oppositeIp: string = ''
|
|
private oppositeIpPort: string = ''
|
|
private udpMsg: any=''
|
|
private num:number = 0
|
|
private fileUtil: FileUtil
|
|
private udp: any = null
|
|
private sendId: any = 0
|
|
private lsh: string = null
|
|
private context
|
|
private stashFn
|
|
constructor(udplocalIp: string, udplocalIpPort:string,udpOppositeIp: string,udpOppositeIpPort:string) {
|
|
this.localIp = udplocalIp
|
|
this.oppositeIp = udpOppositeIp
|
|
this.localIpPort = udplocalIpPort
|
|
this.oppositeIpPort = udpOppositeIpPort
|
|
setInterval(()=>{
|
|
this.getliushuiNum()
|
|
},1000)
|
|
this.udp = socket.constructUDPSocketInstance();
|
|
}
|
|
rebindUdp(localIp: string, localIpPort:string,oppositeIp: string,oppositeIpPort:string){
|
|
this.localIp = localIp
|
|
this.oppositeIp = oppositeIp
|
|
this.localIpPort = localIpPort
|
|
this.oppositeIpPort = oppositeIpPort
|
|
|
|
let promise = this.udp.bind({
|
|
// address: '192.168.7.170', port: 20122, 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)}`);
|
|
});
|
|
}
|
|
bindUdp() {
|
|
let promise = this.udp.bind({
|
|
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)}`);
|
|
});
|
|
}
|
|
async getliushuiNum(){
|
|
this.fileUtil = new FileUtil(this.context)
|
|
const data = await this.fileUtil.readFile('/mnt/hmdfs/100/account/device_view/localfiles/files/config/liushui.txt');
|
|
if(data === '' || data === undefined){
|
|
this.num=0
|
|
let str=this.num.toString()
|
|
for(let i=0;str.length<=5;i++){
|
|
str='0'+str
|
|
}
|
|
this.lsh=str
|
|
// return str
|
|
}else{
|
|
this.num=Number(JSON.parse(data).value)+1
|
|
let str=this.num.toString()
|
|
for(let i=0;str.length<=5;i++){
|
|
str='0'+str
|
|
}
|
|
this.lsh=str
|
|
// return str
|
|
}
|
|
}
|
|
//异或运算
|
|
setMessageExclusive(tmpList){
|
|
let result = tmpList[0];
|
|
for (let i = 1; i < tmpList.length; i++) {
|
|
result = result ^ tmpList[i]
|
|
}
|
|
return [result];
|
|
}
|
|
Array2Byte(array) {
|
|
var buf = new ArrayBuffer(array.length);
|
|
var view = new Uint8Array(buf);
|
|
for (var i = 0; i != array.length; ++i)
|
|
{
|
|
view[i] = array[i] & 0xFF;
|
|
}
|
|
return view;
|
|
}
|
|
//length消息体bufferlength id消息类型id bodyStr消息体string
|
|
// setMsyBody(id,bodyByte){
|
|
|
|
// {id: 31,list:[0,1,'0000000000000'],carNo:489,placeId:62}
|
|
setWholeMsg(params){
|
|
let head = this.setMsgHead(params);
|
|
let headJudge = this.setMessageExclusive(head);
|
|
let body = this.setMsgBody(params);
|
|
let bodyJudge = this.setMessageExclusive(body);
|
|
let end = [13,10];
|
|
const arr=[...head, ...headJudge,...body, ...bodyJudge,...end]
|
|
console.log('BitArray',arr)
|
|
return this.Array2Byte(arr).buffer
|
|
|
|
}
|
|
setMsgHead({id, list, placeId=62, carNo=489}){
|
|
let a = string2Bytes(`${id}${fillZero(placeId,3)}`, 2*8);
|
|
let b = string2Bytes(`${carNo}${this.lsh}`, 4*8);
|
|
|
|
let c = string2Bytes(list.length, 2*8);
|
|
return [...a,...b,...c];
|
|
}
|
|
setMsgBody({id,list}) {
|
|
let tmpList = []
|
|
tmpList = list
|
|
|
|
return tmpList ;
|
|
|
|
}
|
|
|
|
sendHeadMsg(msgData){
|
|
//udpOppositeIp
|
|
let promise = this.udp.send({
|
|
data: msgData,
|
|
address: {
|
|
address: this.oppositeIp,
|
|
port: parseInt(this.oppositeIpPort),
|
|
}
|
|
});
|
|
promise.then(() => {
|
|
console.log(`${TAG} udpLine send success:`);
|
|
}).catch(err => {
|
|
console.log(`${TAG} udpLine send fail:${JSON.stringify(err)}`);
|
|
});
|
|
}
|
|
|
|
sendMsg(param,context?) {
|
|
if(context){
|
|
this.context=context
|
|
}
|
|
this.sendId=param.id
|
|
console.log('sendMsg',JSON.stringify(param))
|
|
const msgData=this.setWholeMsg(param)
|
|
// const msgData=this.setMsyBody('31','010000000000000')
|
|
let promise = this.udp.send({
|
|
data: msgData,
|
|
address: {
|
|
address: this.oppositeIp,
|
|
port: parseInt(this.oppositeIpPort),
|
|
}
|
|
});
|
|
this.stashFn=param.callback?param.callback:()=>{}
|
|
promise.then(() => {
|
|
if(this.sendId=='46'&¶m.callback){
|
|
param.callback()
|
|
}
|
|
console.log(`${TAG} udp send success:`);
|
|
}).catch(err => {
|
|
console.log(`${TAG} udp send fail:${JSON.stringify(err)}`);
|
|
});
|
|
}
|
|
onMessage(callback,type?) {
|
|
this.udp.on('message', value => {
|
|
let arr=[]
|
|
let dataView = new DataView(value.message)
|
|
for (let i = 0;i < dataView?.byteLength; ++i) {
|
|
arr[i]=dataView?.getUint8(i)
|
|
}
|
|
let idNum = '0x' + fillZero(arr[1].toString(16),2) + fillZero(arr[0].toString(16),2) ;
|
|
let id = Math.floor(+idNum/1000)
|
|
console.log('bitbit',arr)
|
|
|
|
let lengthNum = '0x' + fillZero(arr[7].toString(16),2) + fillZero(arr[6].toString(16),2) ;
|
|
let length= +lengthNum;
|
|
let list=[]
|
|
for(let i=9;i<=9+length-1;i++){
|
|
list.push(arr[i])
|
|
}
|
|
this.stashFn({id,length,body:list,sendId:this.sendId})
|
|
this.stashFn=()=>{}
|
|
callback({id,length,body:list,sendId:this.sendId})
|
|
|
|
});
|
|
}
|
|
|
|
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');
|
|
// });
|
|
}
|
|
});
|
|
|
|
}
|
|
} |