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 {BaseElement, element} from "../../../base-ui/BaseElement.js"; 17import LitSwitch from "../../../base-ui/switch/lit-switch.js"; 18import "../../../base-ui/select/LitAllocationSelect.js"; 19 20import "../../../base-ui/switch/lit-switch.js"; 21import {LitAllocationSelect} from "../../../base-ui/select/LitAllocationSelect.js"; 22import {SpRecordTrace} from "../SpRecordTrace.js"; 23import {Cmd} from "../../../command/Cmd.js"; 24import {CmdConstant} from "../../../command/CmdConstant.js"; 25import {HdcDeviceManager} from "../../../hdc/HdcDeviceManager.js"; 26 27@element("sp-vm-tracker") 28export class SpVmTracker extends BaseElement { 29 private processInput: LitAllocationSelect | undefined | null; 30 private selectProcess: HTMLInputElement | undefined | null; 31 private configList: Array<any> = []; 32 33 set startSamp(start: boolean) { 34 if (start) { 35 this.setAttribute("startSamp", ""); 36 this.selectProcess!.removeAttribute("readonly"); 37 } else { 38 this.removeAttribute("startSamp") 39 this.selectProcess!.setAttribute("readonly", "readonly") 40 } 41 } 42 43 get process():string{ 44 if (this.processInput!.value.length > 0) { 45 let result = this.processInput!.value.match(/\((.+?)\)/g) 46 if (result) { 47 return result.toString().replace("(", "").replace(")", "") 48 } else { 49 return this.processInput!.value; 50 } 51 } 52 return "" 53 } 54 55 get startSamp(): boolean { 56 return this.hasAttribute("startSamp"); 57 } 58 59 initElements(): void { 60 this.initConfigList(); 61 let configList = this.shadowRoot?.querySelector<HTMLDivElement>(".configList"); 62 this.configList.forEach(config => { 63 let div = document.createElement("div"); 64 if (config.hidden) { 65 div.className = "config-div hidden"; 66 } else { 67 div.className = "config-div"; 68 } 69 let headDiv = document.createElement("div"); 70 div.appendChild(headDiv); 71 let title = document.createElement("span"); 72 title.className = "title"; 73 title.textContent = config.title; 74 headDiv.appendChild(title); 75 let des = document.createElement("span"); 76 des.textContent = config.des; 77 des.className = "des"; 78 headDiv.appendChild(des); 79 switch (config.type) { 80 case "select": 81 let html1 = ''; 82 html1 += `<lit-allocation-select style="width: 100%;" rounded="" default-value="" class="select config" placement="bottom" title="${config.title}" placeholder="${config.selectArray[0]}">` 83 html1 += `</lit-allocation-select>` 84 div.innerHTML = div.innerHTML + html1; 85 break; 86 case "switch": 87 let switch1 = document.createElement("lit-switch") as LitSwitch; 88 switch1.className = "config"; 89 switch1.title = config.title; 90 if (config.value) { 91 switch1.checked = true; 92 } else { 93 switch1.checked = false; 94 } 95 if (config.title == "Start VM Tracker Record") { 96 switch1.addEventListener("change", (event: any) => { 97 let detail = event.detail; 98 if (detail.checked) { 99 this.startSamp = true; 100 this.unDisable(); 101 } else { 102 this.startSamp = false; 103 this.disable(); 104 } 105 }) 106 } 107 headDiv.appendChild(switch1); 108 break; 109 default: 110 break; 111 } 112 configList!.appendChild(div); 113 }); 114 this.processInput = this.shadowRoot?.querySelector<LitAllocationSelect>("lit-allocation-select[title='Process']"); 115 let inputDiv = this.processInput?.shadowRoot?.querySelector('.multipleSelect') as HTMLDivElement 116 this.selectProcess = this.processInput!.shadowRoot?.querySelector("input") as HTMLInputElement; 117 let processData: Array<string> = [] 118 inputDiv!.addEventListener('mousedown', ev => { 119 if (SpRecordTrace.serialNumber == '') { 120 this.processInput!.processData = [] 121 this.processInput!.initData() 122 } 123 }) 124 inputDiv!.addEventListener('mouseup', () => { 125 if (SpRecordTrace.serialNumber == '') { 126 this.processInput!.processData = [] 127 this.processInput!.initData() 128 } else { 129 if (SpRecordTrace.isVscode) { 130 let cmd = Cmd.formatString(CmdConstant.CMD_GET_PROCESS_DEVICES, [SpRecordTrace.serialNumber]) 131 Cmd.execHdcCmd(cmd, (res: string) => { 132 processData = [] 133 let lineValues: string[] = res.replace(/\r\n/g, "\r").replace(/\n/g, "\r").split(/\r/); 134 for (let lineVal of lineValues) { 135 if (lineVal.indexOf("__progname") != -1 || lineVal.indexOf("PID CMD") != -1) { 136 continue; 137 } 138 let process: string[] = lineVal.trim().split(" "); 139 if (process.length == 2) { 140 let processId = process[0] 141 let processName = process[1] 142 processData.push(processName + "(" + processId + ")") 143 } 144 } 145 if (processData.length > 0 && this.startSamp) { 146 this.processInput!.setAttribute("readonly", "readonly") 147 } 148 this.processInput!.processData = processData 149 this.processInput!.initData() 150 }) 151 } else { 152 HdcDeviceManager.connect(SpRecordTrace.serialNumber).then(conn => { 153 if (conn) { 154 HdcDeviceManager.shellResultAsString(CmdConstant.CMD_GET_PROCESS, false).then(res => { 155 processData = [] 156 if (res) { 157 let lineValues: string[] = res.replace(/\r\n/g, "\r").replace(/\n/g, "\r").split(/\r/); 158 for (let lineVal of lineValues) { 159 if (lineVal.indexOf("__progname") != -1 || lineVal.indexOf("PID CMD") != -1) { 160 continue; 161 } 162 let process: string[] = lineVal.trim().split(" "); 163 if (process.length == 2) { 164 let processId = process[0] 165 let processName = process[1] 166 processData.push(processName + "(" + processId + ")") 167 } 168 } 169 } 170 if (processData.length > 0 && this.startSamp) { 171 this.selectProcess!.setAttribute("readonly", "readonly") 172 } 173 this.processInput!.processData = processData 174 this.processInput!.initData() 175 }) 176 } 177 }) 178 } 179 } 180 }); 181 this.disable(); 182 } 183 184 private unDisable() { 185 let configVal = this.shadowRoot?.querySelectorAll<HTMLElement>(".config"); 186 configVal!.forEach(configVal1 => { 187 configVal1.removeAttribute("disabled") 188 }) 189 } 190 191 private disable() { 192 let configVal = this.shadowRoot?.querySelectorAll<HTMLElement>(".config"); 193 configVal!.forEach(configVal1 => { 194 if (configVal1.title != "Start VM Tracker Record") { 195 configVal1.setAttribute("disabled", '') 196 } 197 }) 198 } 199 200 initConfigList(): void { 201 this.configList = [ 202 { 203 title: "Start VM Tracker Record", 204 des: "", 205 hidden: false, 206 type: "switch", 207 value: false 208 }, 209 { 210 title: "Process", 211 des: "Record process", 212 hidden: false, 213 type: "select", 214 selectArray: [ 215 '' 216 ] 217 } 218 ] 219 } 220 221 initHtml(): string { 222 return ` 223 <style> 224 :host{ 225 display: inline-block; 226 width: 100%; 227 height: 100%; 228 background: var(--dark-background3,#FFFFFF); 229 border-radius: 0px 16px 16px 0px; 230 } 231 232 .root { 233 padding-top: 30px; 234 padding-left: 54px; 235 margin-right: 30px; 236 font-size:16px; 237 margin-bottom: 30px; 238 } 239 240 .config-div { 241 width: 80%; 242 display: flex; 243 flex-direction: column; 244 margin-top: 5vh; 245 margin-bottom: 5vh; 246 gap: 25px; 247 } 248 249 .title { 250 opacity: 0.9; 251 font-family: Helvetica-Bold; 252 font-size: 18px; 253 text-align: center; 254 line-height: 40px; 255 font-weight: 700; 256 margin-right: 10px; 257 } 258 259 .des { 260 opacity: 0.6; 261 font-family: Helvetica; 262 font-size: 14px; 263 text-align: center; 264 line-height: 35px; 265 font-weight: 400; 266 } 267 268 .select { 269 border-radius: 15px; 270 } 271 272 lit-switch { 273 display:inline; 274 float: right; 275 height: 38px; 276 margin-top: 10px; 277 } 278 input { 279 height: 25px; 280 outline:none; 281 border-radius: 16px; 282 text-indent:2% 283 } 284 input::-webkit-input-placeholder{ 285 color:var(--bark-prompt,#999999); 286 } 287 288 .input { 289 border: 1px solid var(--dark-background5,#ccc); 290 font-family: Helvetica; 291 font-size: 14px; 292 color: var(--dark-color1,#212121); 293 text-align: left; 294 line-height: 20px; 295 font-weight: 400; 296 } 297 298 :host([startSamp]) .input { 299 background: var(--dark-background5,#FFFFFF); 300 } 301 302 :host(:not([startSamp])) .input { 303 color: #999999; 304 } 305 </style> 306 <div class="root"> 307 <div class="configList"> 308 </div> 309 </div> 310 `; 311 } 312}