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'; 20import { GPData } from '../../entity/DatabaseEntity'; 21 22export class NetWork extends BaseProfiler { 23 private netMap: Map<String, String> = new Map(); 24 private catchNetUp = '-1'; 25 private catchNetDown = '-1'; 26 public static instance: NetWork = null; 27 public static getInstance(): NetWork { 28 if (this.instance == null) { 29 this.instance = new NetWork(); 30 } 31 return this.instance; 32 } 33 34 init(): CollectorType { 35 //初始化NET 36 connection.getDefaultNet().then(function (netHandle) { 37 connection.getNetCapabilities(netHandle).then(function (info) { 38 if (info !== undefined) { 39 globalThis.isNetAlive = true; 40 } else { 41 globalThis.isNetAlive = false; 42 } 43 }); 44 }); 45 return CollectorType.TYPE_NET; 46 } 47 48 isSupport(): Boolean { 49 SPLogger.DEBUG(NetWork.name, 'networkInfo support:' + globalThis.isNetAlive); 50 return globalThis.isNetAlive; 51 } 52 53 readData(): GPData { 54 this.handleMessage(); 55 this.netMap.set('netSpeedUp', this.catchNetUp); 56 this.netMap.set('netSpeedDown', this.catchNetDown); 57 return createGPData('NetWork', this.netMap); 58 } 59 60 handleMessage(): void { 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 this.catchNetDown = info.linkDownBandwidthKbps.toString(); 66 this.catchNetUp = info.linkUpBandwidthKbps.toString(); 67 }); 68 }); 69 } 70} 71