/* * 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 { 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 udp: any = null // private stashFn:StashFunction constructor(udplocalIp: string, udplocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) { this.localIp = udplocalIp this.oppositeIp = udpOppositeIp this.localIpPort = udplocalIpPort this.oppositeIpPort = udpOppositeIpPort // this.stashFn=()=>{} 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: '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)}`); }); } 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)}`); }); } setMsgCallBack(callback){ // this.stashFn=callback?callback:()=>{} } 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 } // this.stashFn(str) // this.stashFn=()=>{} 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'); // }); } }); } } interface StashFunction { (str: string) }