1/* 2 * Copyright (C) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15import { createGPData } from '../base/BaseProfilerUtils'; 16import { BaseProfiler } from '../base/BaseProfiler' 17import { CollectorType } from '../base/ProfilerConstant' 18import connection from '@ohos.net.connection' 19import SPLogger from '../../../common/utils/SPLogger' 20 21export class NetWork extends BaseProfiler{ 22 private netMap: Map<String, String> = new Map 23 private catchNetUp = "-1" 24 private catchNetDown = "-1" 25 public static instance: NetWork = null 26 public static getInstance() { 27 if (this.instance == null) { 28 this.instance = new NetWork() 29 } 30 return this.instance 31 } 32 33 init() { 34 //初始化NET 35 connection.getDefaultNet().then(function (netHandle) { 36 connection.getNetCapabilities(netHandle).then(function (info) { 37 if(info != undefined){ 38 globalThis.isNetAlive = true 39 }else{ 40 globalThis.isNetAlive = false 41 } 42 }) 43 }) 44 return CollectorType.TYPE_NET 45 } 46 47 isSupport() { 48 SPLogger.DEBUG(NetWork.name, "networkInfo support:" + globalThis.isNetAlive) 49 return globalThis.isNetAlive 50 } 51 52 readData() { 53 this.handleMessage() 54 this.netMap.set("netSpeedUp",this.catchNetUp) 55 this.netMap.set("netSpeedDown",this.catchNetDown) 56 return createGPData("NetWork", this.netMap) 57 } 58 59 handleMessage(){ 60 var that = this 61 connection.getDefaultNet().then(function (netHandle) { 62 connection.getNetCapabilities(netHandle).then(function (info) { 63 SPLogger.DEBUG(NetWork.name,"networkInfo up:" + info["linkUpBandwidthKbps"] ) 64 SPLogger.DEBUG(NetWork.name,"networkInfo down:" + info["linkDownBandwidthKbps"]) 65 that.catchNetDown = info["linkDownBandwidthKbps"].toString() 66 that.catchNetUp = info["linkUpBandwidthKbps"].toString() 67 }) 68 }) 69 } 70 71} 72