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, 19 drawLines, 20 drawLoading, 21 drawSelection, 22 ns2x, 23 Render, 24 RequestMessage 25} from "./ProcedureWorkerCommon.js"; 26 27export class EnergyStateRender extends Render { 28 render(req: RequestMessage, list: Array<any>, filter: Array<any>) { 29 if (req.lazyRefresh) { 30 state(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, req.useCache || !req.range.refresh); 31 } else { 32 if (!req.useCache) { 33 state(list, filter, req.startNS, req.endNS, req.totalNS, req.frame, false); 34 } 35 } 36 if (req.canvas) { 37 req.context.clearRect(0, 0, req.canvas.width, req.canvas.height); 38 let arr = filter; 39 if (arr.length > 0 && !req.range.refresh && !req.useCache && req.lazyRefresh) { 40 drawLoading(req.context, req.startNS, req.endNS, req.totalNS, req.frame, arr[0].startNS, 41 arr[arr.length - 1].startNS + arr[arr.length - 1].dur) 42 } 43 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 44 req.context.beginPath(); 45 EnergyStateStruct.maxState = req.params.maxState; 46 EnergyStateStruct.maxStateName = req.params.maxStateName; 47 drawLines(req.context, req.xs, req.frame.height, req.lineColor) 48 EnergyStateStruct.hoverEnergyStateStruct = undefined; 49 if (req.isHover) { 50 for (let re of filter) { 51 if (re.frame && req.hoverX >= re.frame.x && req.hoverX <= re.frame.x + re.frame.width && req.hoverY >= re.frame.y && req.hoverY <= re.frame.y + re.frame.height) { 52 EnergyStateStruct.hoverEnergyStateStruct = re; 53 break; 54 } 55 } 56 } 57 EnergyStateStruct.selectEnergyStateStruct = req.params.selectEnergyStateStruct; 58 for (let re of filter) { 59 EnergyStateStruct.draw(req.context, re) 60 } 61 drawSelection(req.context, req.params); 62 req.context.closePath(); 63 if (EnergyStateStruct.maxStateName != "enable" && EnergyStateStruct.maxStateName != "disable") { 64 let s = EnergyStateStruct.maxStateName 65 let textMetrics = req.context.measureText(s); 66 req.context.globalAlpha = 1.0 67 req.context.fillStyle = "#f0f0f0" 68 req.context.fillRect(0, 5, textMetrics.width + 8, 18) 69 req.context.fillStyle = "#333" 70 req.context.textBaseline = "middle" 71 req.context.fillText(s, 4, 5 + 9) 72 } 73 drawFlagLine(req.context, req.flagMoveInfo, req.flagSelectedInfo, req.startNS, req.endNS, req.totalNS, 74 req.frame, req.slicesTime); 75 } 76 // @ts-ignore 77 self.postMessage({ 78 id: req.id, 79 type: req.type, 80 results: req.canvas ? undefined : filter, 81 hover: EnergyStateStruct.hoverEnergyStateStruct 82 }); 83 } 84} 85 86export function state(list: Array<any>, res: Array<any>, startNS: number, endNS: number, totalNS: number, 87 frame: any, use: boolean) { 88 if (use && res.length > 0) { 89 for (let i = 0; i < res.length; i++) { 90 let item = res[i]; 91 if ((item.startNs || 0) + (item.dur || 0) > (startNS || 0) && (item.startNs || 0) < (endNS || 0)) { 92 EnergyStateStruct.setStateFrame(item, 5, startNS || 0, endNS || 0, 93 totalNS || 0, frame) 94 } else { 95 item.frame = null; 96 } 97 } 98 return; 99 } 100 res.length = 0; 101 if (list) { 102 for (let index = 0; index < list.length; index++) { 103 let item = list[index]; 104 if (index === list.length - 1) { 105 item.dur = (endNS || 0) - (item.startNs || 0) 106 } else { 107 item.dur = (list[index + 1].startNs || 0) - (item.startNs || 0) 108 } 109 if ((item.startNs || 0) + (item.dur || 0) > (startNS || 0) && (item.startNs || 0) < (endNS || 0)) { 110 EnergyStateStruct.setStateFrame(list[index], 5, startNS || 0, endNS || 0, 111 totalNS || 0, frame) 112 if (index > 0 && ((list[index - 1].frame?.x || 0) == (list[index].frame?.x || 0) && (list[index - 1].frame?.width || 0) == (list[index].frame?.width || 0))) { 113 114 } else { 115 res.push(item) 116 } 117 } 118 } 119 } 120} 121 122export class EnergyStateStruct extends BaseStruct { 123 static maxState: number = 0 124 static maxStateName: string = "0" 125 static hoverEnergyStateStruct: EnergyStateStruct | undefined; 126 static selectEnergyStateStruct: EnergyStateStruct | undefined; 127 type: string | undefined 128 value: number | undefined 129 startNs: number | undefined 130 dur: number | undefined 131 132 sensorType: number | undefined 133 pkg_name: string | undefined 134 deviceState: number | undefined 135 deviceType: number | undefined 136 137 static draw(context2D: CanvasRenderingContext2D, data: EnergyStateStruct) { 138 if (data.frame) { 139 let width = data.frame.width || 0; 140 let drawColor = this.setDrawColor(data.type!); 141 context2D.fillStyle = drawColor; 142 context2D.strokeStyle = drawColor; 143 context2D.globalAlpha = 1.0; 144 context2D.lineWidth = 1; 145 let drawHeight: number = Math.floor(((data.value || 0) * (data.frame.height || 0)) / EnergyStateStruct.maxState); 146 if (EnergyStateStruct.maxStateName === "enable" || EnergyStateStruct.maxStateName === "disable") { 147 if (data.value == 0) { 148 drawHeight = data.frame.height; 149 context2D.fillRect(data.frame.x, data.frame.y + 4, width, data.frame.height) 150 } 151 } else { 152 context2D.fillRect(data.frame.x, data.frame.y + data.frame.height - drawHeight + 4, width, drawHeight) 153 } 154 if (data.startNs === EnergyStateStruct.hoverEnergyStateStruct?.startNs) { 155 let pointy = data.frame.y + data.frame.height + 4 156 if (data.value == 0) { 157 pointy -= drawHeight 158 } 159 context2D.beginPath() 160 context2D.arc(data.frame.x, pointy, 3, 0, 2 * Math.PI, true) 161 context2D.fill() 162 context2D.globalAlpha = 1.0; 163 context2D.stroke(); 164 context2D.beginPath() 165 context2D.moveTo(data.frame.x + 3, pointy); 166 context2D.lineWidth = 3; 167 context2D.lineTo(data.frame.x + width, pointy) 168 context2D.stroke(); 169 } 170 } 171 context2D.globalAlpha = 1.0; 172 context2D.lineWidth = 1; 173 } 174 175 static setStateFrame(node: any, padding: number, startNS: number, endNS: number, totalNS: number, frame: any) { 176 let startPointX: number, endPointX: number 177 178 if ((node.startNs || 0) < startNS) { 179 startPointX = 0 180 } else { 181 startPointX = ns2x((node.startNs || 0), startNS, endNS, totalNS, frame); 182 } 183 if ((node.startNs || 0) + (node.dur || 0) > endNS) { 184 endPointX = frame.width; 185 } else { 186 endPointX = ns2x((node.startNs || 0) + (node.dur || 0), startNS, endNS, totalNS, frame); 187 } 188 let frameWidth: number = endPointX - startPointX <= 1 ? 1 : endPointX - startPointX; 189 if (!node.frame) { 190 node.frame = {}; 191 } 192 node.frame.x = Math.floor(startPointX); 193 node.frame.y = frame.y + padding; 194 node.frame.width = Math.ceil(frameWidth); 195 node.frame.height = Math.floor(frame.height - padding * 2); 196 } 197 198 static setDrawColor(eventType: string): string { 199 switch (eventType) { 200 case 'BRIGHTNESS_NIT': 201 return "#92D6CC"; 202 case 'SIGNAL_LEVEL': 203 return "#61CFBE" 204 case 'WIFI_EVENT_RECEIVED': 205 return "#46B1E3" 206 case 'AUDIO_STREAM_CHANGE': 207 return "#ED6F21" 208 case 'WIFI_STATE': 209 return "#61CFBE" 210 case 'LOCATION_SWITCH_STATE': 211 return "#61CFBE" 212 case 'SENSOR_STATE': 213 return "#61CFBE" 214 default: 215 return "#61CFBE" 216 } 217 } 218}