1/* 2* Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 request from '@ohos.request' 16 17export default class CostTimeCompute { 18 lastTimeStr: string = ""; 19 startDownloadTime: Date = null; 20 timerId: number; 21 costTime: number = 0; 22 private lastTimeChangeCallback: (str: string) => {} 23 private getProgress: () => number 24 private getTotal: () => {} 25 private getCostTime: (str: string) => {} 26 private getSpeedSize: (str: string) => {} 27 private getDownloadTask: () => request.DownloadTask 28 private showChinese: boolean; 29 private lastProgress = 0; 30 31 constructor(lastTimeChangeCallback, getProgress, getTotal, getCostTime = null, showChinese = true, getSpeedSize = null) { 32 this.lastTimeChangeCallback = lastTimeChangeCallback; 33 this.getProgress = getProgress; 34 this.getTotal = getTotal; 35 this.getCostTime = getCostTime; 36 this.getSpeedSize = getSpeedSize; 37 this.showChinese = showChinese; 38 } 39 40 //开始或者恢复剩余时间计算 41 startCostTime() { 42 if (this.costTime == 0) { 43 this.startDownloadTime = new Date(); 44 } else { 45 this.startDownloadTime = new Date(Date.now() - this.costTime); 46 } 47 this.timerId = setInterval(() => { 48 this.computeLastTime(); 49 this.computeSpeed(); 50 }, 1000, 0); 51 } 52 53 cancelCostTime() { 54 this.pauseCostTime(); 55 this.costTime = 0; 56 this.lastTimeStr = ""; 57 this.lastProgress = 0; 58 } 59 60 pauseCostTime() { 61 this.startDownloadTime = null; 62 if (this.timerId != null) { 63 clearInterval(this.timerId); 64 } 65 this.timerId = null; 66 } 67 68 //计算下载速度 69 private computeSpeed() { 70 if (this.getSpeedSize == null) { 71 return; 72 } 73 const newProgress: number = this.getProgress(); 74 const newSize = newProgress - this.lastProgress; 75 this.lastProgress = newProgress; 76 this.getSpeedSize(this.compareSize(newSize)); 77 } 78 79 //计算剩余下载时间 80 private computeLastTime() { 81 this.costTime = Date.now() - this.startDownloadTime.getTime(); 82 if (this.getProgress() != 0) { 83 let lastTime = Math.floor(this.costTime / (this.getProgress() as number) * (this.getTotal() as number)) - this.costTime; 84 this.lastTimeStr = this.getTimeStr(lastTime); 85 this.lastTimeChangeCallback(this.lastTimeStr); 86 } 87 if (this.getCostTime != null) { 88 this.getCostTime(this.getTimeStr(this.costTime)); 89 } 90 } 91 92 private compareSize(bits: number): string { 93 if (bits > 1024 * 1024) { 94 const m = bits / (1024 * 1024); 95 return m.toFixed(1) + "MB/s"; 96 } else if (bits > 1024) { 97 const k = bits / 1024; 98 return k.toFixed(1) + "KB/s"; 99 } else { 100 return bits + "B/s" 101 } 102 } 103 104 private getTimeStr(ms: number) { 105 let second = Math.floor(ms / 1000); 106 const minStr = Math.floor(second / 60); 107 const secStr = Math.floor(second % 60); 108 return this.placeHolder(minStr) + ":" + this.placeHolder(secStr); 109 } 110 111 private placeHolder(n: number): string { 112 if (n <= 0) { 113 return "00"; 114 } 115 return n < 10 ? ('0' + n) : ("" + n) 116 } 117}