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 */ 15 16import { 17 BaseStruct, 18 drawFlagLine, drawLines, 19 drawLoading, 20 drawSelection, 21 ns2x, 22 Render, 23 RequestMessage 24} from "./ProcedureWorkerCommon.js"; 25 26export class EnergySystemRender extends Render { 27 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 28 if (req.lazyRefresh) { 29 system(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh); 30 } else { 31 if (!req.useCache) { 32 system(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, false); 33 } 34 } 35 if (req.canvas) { 36 req.context.clearRect(0, 0, req.canvas.width, req.canvas.height); 37 let arr = filter; 38 if (arr.length > 0 && !req.range.refresh && !req.useCache && req.lazyRefresh) { 39 drawLoading(req.context, req.startNS, req.endNS, req.totalNS, req.frame, arr[0].startNS, arr[arr.length - 1].startNS + arr[arr.length - 1].dur) 40 } 41 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 42 req.context.beginPath(); 43 EnergySystemStruct.hoverEnergySystemStruct = undefined; 44 if (req.isHover) { 45 let resultList: any[] = [] 46 for (let re of filter) { 47 if (re.frame && req.hoverX >= re.frame.x && req.hoverX <= re.frame.x + re.frame.width) { 48 EnergySystemStruct.hoverEnergySystemStruct = re; 49 if (resultList.indexOf(re.type) === -1) { 50 resultList.push(re.type) 51 } 52 } 53 } 54 if (EnergySystemStruct.hoverEnergySystemStruct) { 55 if (resultList.indexOf(0) > -1) { 56 EnergySystemStruct.hoverEnergySystemStruct.workScheduler = "survive" 57 } else { 58 EnergySystemStruct.hoverEnergySystemStruct.workScheduler = "dead" 59 } 60 if (resultList.indexOf(1) > -1) { 61 EnergySystemStruct.hoverEnergySystemStruct.power = "survive" 62 } else { 63 EnergySystemStruct.hoverEnergySystemStruct.power = "dead" 64 } 65 if (resultList.indexOf(2) > -1) { 66 EnergySystemStruct.hoverEnergySystemStruct.location = "survive" 67 } else { 68 EnergySystemStruct.hoverEnergySystemStruct.location = "dead" 69 } 70 } 71 } 72 EnergySystemStruct.selectEnergySystemStruct = req.params.selectEnergySystemStruct; 73 for (let re of filter) { 74 EnergySystemStruct.draw(req.context, re) 75 } 76 drawLegend(req); 77 drawSelection(req.context, req.params); 78 req.context.closePath(); 79 drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, req.frame, req.slicesTime); 80 } 81 // @ts-ignore 82 self.postMessage({ 83 id: req.id, 84 type: req.type, 85 results: req.canvas ? undefined : filter, 86 hover: EnergySystemStruct.hoverEnergySystemStruct 87 }); 88 } 89} 90 91export function drawLegend(req: RequestMessage) { 92 let textList = ["WORKSCHEDULER", "POWER_RUNNINGLOCK", "LOCATION"] 93 for (let index = 0; index < textList.length; index++) { 94 let text = req.context.measureText(textList[index]); 95 req.context.fillStyle = EnergySystemStruct.getColor(index); 96 if (textList[index] == "WORKSCHEDULER") { 97 req.context.fillRect(req.context.canvas.width - (EnergySystemStruct.itemNumber * 120), 12, 8, 8); 98 req.context.globalAlpha = 1 99 req.context.textBaseline = "middle" 100 req.context.fillStyle = "#333" 101 req.context.fillText(textList[index], req.context.canvas.width - (EnergySystemStruct.itemNumber * 120) + 10, 18) 102 EnergySystemStruct.currentTextWidth = req.context.canvas.width - (EnergySystemStruct.itemNumber * 120) + 40 + text.width 103 } else { 104 req.context.fillRect(EnergySystemStruct.currentTextWidth, 12, 8, 8); 105 req.context.globalAlpha = 1 106 req.context.fillStyle = "#333" 107 req.context.textBaseline = "middle" 108 req.context.fillText(textList[index], EnergySystemStruct.currentTextWidth + 12, 18); 109 EnergySystemStruct.currentTextWidth = EnergySystemStruct.currentTextWidth + 40 + text.width 110 } 111 } 112} 113 114export function system(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, frame: any, use: boolean) { 115 if (use && res.length > 0) { 116 for (let i = 0; i < res.length; i++) { 117 let item = res[i]; 118 if ((item.startNs || 0) + (item.dur || 0) > (startNS || 0) && (item.startNs || 0) < (endNS || 0)) { 119 EnergySystemStruct.setSystemFrame(item, 10, startNS || 0, endNS || 0, totalNS || 0, frame) 120 } else { 121 item.frame = null; 122 } 123 } 124 return; 125 } 126 res.length = 0; 127 if (list) { 128 for (let index = 0; index < list.length; index++) { 129 let item = list[index]; 130 if ((item.startNs || 0) + (item.dur || 0) > (startNS || 0) && (item.startNs || 0) < (endNS || 0)) { 131 EnergySystemStruct.setSystemFrame(list[index], 10, startNS || 0, endNS || 0, totalNS || 0, frame) 132 res.push(item) 133 } 134 } 135 } 136} 137 138export class EnergySystemStruct extends BaseStruct { 139 static hoverEnergySystemStruct: EnergySystemStruct | undefined; 140 static selectEnergySystemStruct: EnergySystemStruct | undefined; 141 static itemNumber: number = 3 142 static currentTextWidth: number = 0 143 type: number | undefined 144 startNs: number | undefined 145 dur: number | undefined 146 workScheduler: string | undefined 147 power: string | undefined 148 location: string | undefined 149 150 static draw(context2D: CanvasRenderingContext2D, data: EnergySystemStruct) { 151 if (data.frame) { 152 let width = data.frame.width || 0; 153 context2D.globalAlpha = 1.0; 154 context2D.lineWidth = 1; 155 context2D.fillStyle = this.getColor(data.type!); 156 context2D.strokeStyle = this.getColor(data.type!); 157 context2D.fillRect(data.frame.x, data.frame.y + 4, width, data.frame.height) 158 } 159 context2D.globalAlpha = 1.0; 160 context2D.lineWidth = 1; 161 } 162 163 static setSystemFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 164 let startPointX: number; 165 let endPointX: number; 166 if ((node.startNs || 0) < startNS) { 167 startPointX = 0 168 } else { 169 startPointX = ns2x((node.startNs || 0), startNS, endNS, totalNS, frame); 170 } 171 if ((node.startNs || 0) + (node.dur || 0) > endNS) { 172 endPointX = frame.width; 173 } else { 174 endPointX = ns2x((node.startNs || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 175 } 176 let frameWidth: number = endPointX - startPointX <= 1 ? 1 : endPointX - startPointX; 177 if (!node.frame) { 178 node.frame = {}; 179 } 180 node.frame.x = Math.floor(startPointX); 181 if (node.type === 0) { 182 node.frame.y = frame.y + padding * 2.5; 183 } else if (node.type === 1) { 184 node.frame.y = frame.y + (padding * 4.5); 185 } else if (node.type === 2) { 186 node.frame.y = frame.y + (padding * 6.5); 187 } 188 node.frame.width = Math.ceil(frameWidth); 189 node.frame.height = Math.floor(padding); 190 } 191 192 static getColor(textItem: number): string { 193 switch (textItem) { 194 case 0: 195 return "#E64566"; 196 case 1: 197 return "#FFC880"; 198 default: 199 return "#564AF7"; 200 } 201 } 202} 203