monitor.vue 29.7 KB
Newer Older
wanli's avatar
wanli committed
1 2
<template>
  <div class="app-container">
wanli's avatar
wanli committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16
    <div style="margin-top: 10px">
      <grid-layout
        :layout.sync="layout"
        :col-num="12"
        :row-height="30"
        :is-draggable="draggable"
        :is-resizable="resizable"
        :vertical-compact="true"
        :use-css-transforms="true"
        @layout-created="layoutCreatedEvent"
        @layout-before-mount="layoutBeforeMountEvent"
        @layout-mounted="layoutMountedEvent"
        @layout-ready="layoutReadyEvent"
        @layout-updated="layoutUpdatedEvent"
wanli's avatar
wanli committed
17
      >
wanli's avatar
wanli committed
18 19 20 21 22 23 24 25 26 27 28
        <grid-item
          :x="0"
          :y="0"
          :w="6"
          :h="5"
          i="1"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
wanli's avatar
wanli committed
29
        >
wanli's avatar
wanli committed
30 31
          <p class="item-title">DEVICE</p>
          <div style="padding: 15px">
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
            <el-form :inline="true">
              <el-form-item label="选择设备">
                <el-select
                  size="mini"
                  v-model="device"
                  @change="onSelectChange"
                  placeholder="请选择设备"
                >
                  <el-option
                    v-for="(item, index) in deviceList"
                    :key="index"
                    :label="item"
                    :value="item"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="资源监控报告">
                <el-button size="mini" @click="getReport">导出报告</el-button>
              </el-form-item>
51 52 53 54 55 56 57 58
              <el-form-item label="页面图片显示设置">
                <el-switch
                  v-model="pngShowMode"
                  active-text="自动显示当前页面"
                  inactive-text="手动选择页面"
                >
                </el-switch>
              </el-form-item>
59
            </el-form>
wanli's avatar
wanli committed
60 61 62 63 64 65 66 67 68 69 70 71 72
          </div>
        </grid-item>
        <grid-item
          :x="6"
          :y="0"
          :w="6"
          :h="5"
          i="2"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
wanli's avatar
wanli committed
73
        >
wanli's avatar
wanli committed
74 75 76
          <p class="item-title">SYSTEM</p>
          <el-table
            element-loading-text="Loading"
77
            :data="systemList"
wanli's avatar
wanli committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
            size="mini"
            border
            stripe
            fit
            highlight-current-row
          >
            <el-table-column
              prop="host"
              label="host"
              min-width="150"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              prop="path"
              label="path"
              min-width="150"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              prop="timestamp"
              label="timestamp"
              min-width="150"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column label="imei" min-width="150" show-overflow-tooltip>
              <template slot-scope="scope">{{ scope.row.imei }}</template>
            </el-table-column>
            <el-table-column
              label="free_size"
              min-width="100"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.free_size }}(KB)</template
              >
            </el-table-column>
          </el-table>
        </grid-item>
        <grid-item
          :x="0"
          :y="5"
          :w="6"
          :h="5"
          i="3"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
wanli's avatar
wanli committed
127
        >
wanli's avatar
wanli committed
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
          <p class="item-title">EVM</p>
          <el-table
            element-loading-text="Loading"
            :data="evmList"
            size="mini"
            border
            stripe
            fit
            highlight-current-row
          >
            <el-table-column
              label="heap_map_size"
              min-width="150"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.heap_map_size }}(KB)</template
              >
            </el-table-column>
            <el-table-column
              label="heap_total_size"
              min-width="150"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.heap_total_size }}(KB)</template
              >
            </el-table-column>
            <el-table-column
              label="heap_used_size"
              min-width="150"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.heap_used_size }}(KB)</template
              >
            </el-table-column>
            <el-table-column
              label="stack_total_size"
              min-width="150"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.stack_total_size }}(KB)</template
              >
            </el-table-column>
            <el-table-column
              label="stack_used_size"
              min-width="150"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.stack_used_size }}(KB)</template
              >
            </el-table-column>
          </el-table>
        </grid-item>
        <grid-item
          :x="6"
          :y="5"
          :w="6"
          :h="5"
          i="4"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
wanli's avatar
wanli committed
196
        >
wanli's avatar
wanli committed
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
          <p class="item-title">LVGL</p>
          <el-table
            element-loading-text="Loading"
            :data="lvglList"
            size="mini"
            border
            stripe
            fit
            highlight-current-row
          >
            <el-table-column
              label="total_size"
              min-width="100"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.total_size }}(KB)</template
              >
            </el-table-column>
            <el-table-column
              prop="free_cnt"
              label="free_cnt"
              min-width="100"
              show-overflow-tooltip
            ></el-table-column>
            <el-table-column
              label="free_size"
              min-width="120"
              show-overflow-tooltip
            >
              <template slot-scope="scope"
                >{{ scope.row.free_size }}(KB)</template
              >
            </el-table-column>
            <el-table-column label="free_biggest_size" min-width="120">
              <template slot-scope="scope"
                >{{ scope.row.free_biggest_size }}(KB)</template
              >
            </el-table-column>
            <el-table-column label="used_cnt" min-width="100">
              <template slot-scope="scope">{{ scope.row.used_cnt }}</template>
            </el-table-column>
            <el-table-column label="used_pct" min-width="100">
              <template slot-scope="scope"
                >{{ scope.row.used_pct }}(%)</template
              >
            </el-table-column>
            <el-table-column label="frag_pct" min-width="100">
              <template slot-scope="scope"
                >{{ scope.row.frag_pct }}(%)</template
              >
            </el-table-column>
          </el-table>
        </grid-item>
        <grid-item
          :x="0"
          :y="10"
wanli's avatar
wanli committed
254
          :w="8"
wanli's avatar
wanli committed
255
          :h="10"
wanli's avatar
wanli committed
256 257 258 259 260 261
          i="5"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
wanli's avatar
wanli committed
262
        >
263
          <div style="width: 100%; height: 100%; overflow-y: auto">
wanli's avatar
wanli committed
264 265 266 267 268 269 270
            <p class="item-title">APP</p>
            <el-table
              element-loading-text="Loading"
              :data="imageList"
              size="mini"
              border
              fit
wanli's avatar
wanli committed
271
              :row-class-name="tableRowClassName"
272
              @row-click="onTableRowClick"
wanli's avatar
wanli committed
273
            >
wanli's avatar
wanli committed
274 275 276 277 278 279 280 281 282 283
              <el-table-column
                prop="uri"
                label="uri"
                min-width="150"
                show-overflow-tooltip
              ></el-table-column>
              <el-table-column
                label="length"
                min-width="150"
                show-overflow-tooltip
wanli's avatar
wanli committed
284
              >
wanli's avatar
wanli committed
285 286 287 288 289 290 291 292
                <template slot-scope="scope"
                  >{{ scope.row.length }}(KB)</template
                >
              </el-table-column>
              <el-table-column
                label="png_file_size"
                min-width="150"
                show-overflow-tooltip
wanli's avatar
wanli committed
293
              >
wanli's avatar
wanli committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
                <template slot-scope="scope"
                  >{{ scope.row.png_file_size }}(KB)</template
                >
              </el-table-column>
              <el-table-column
                prop="png_total_count"
                label="png_total_count"
                min-width="150"
                show-overflow-tooltip
              ></el-table-column>
              <el-table-column
                label="png_uncompressed_size"
                min-width="150"
                show-overflow-tooltip
              >
                <template slot-scope="scope"
                  >{{ scope.row.png_uncompressed_size }}(KB)</template
                >
              </el-table-column>
            </el-table>
          </div>
wanli's avatar
wanli committed
315
        </grid-item>
wanli's avatar
wanli committed
316 317 318 319 320 321 322 323 324 325 326 327
        <grid-item
          :x="8"
          :y="10"
          :w="4"
          :h="10"
          i="6"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
        >
328
          <div style="width: 100%; height: 100%; overflow-y: auto">
wanli's avatar
wanli committed
329
            <el-table :data="pngList" style="width: 100%">
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
              <el-table-column
                prop="uri"
                label="uri"
                min-width="180"
              ></el-table-column>
              <el-table-column
                prop="filesize"
                label="file size"
                width="100"
              ></el-table-column>
              <el-table-column
                prop="uncompressed_size"
                label="origin size"
                width="100"
              ></el-table-column>
              <el-table-column prop="ratio" label="ratio" width="100">
              </el-table-column>
            </el-table>
wanli's avatar
wanli committed
348 349
          </div>
        </grid-item>
wanli's avatar
wanli committed
350 351
        <grid-item
          :x="0"
wanli's avatar
wanli committed
352
          :y="20"
wanli's avatar
wanli committed
353 354
          :w="12"
          :h="7"
wanli's avatar
wanli committed
355
          i="7"
wanli's avatar
wanli committed
356 357 358 359 360
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
wanli's avatar
wanli committed
361
        >
wanli's avatar
wanli committed
362 363 364 365
          <SystemChart
            :chartData="system"
            :dataList="systemHistory"
          ></SystemChart>
wanli's avatar
wanli committed
366 367 368
        </grid-item>
        <grid-item
          :x="0"
wanli's avatar
wanli committed
369
          :y="27"
wanli's avatar
wanli committed
370 371 372 373 374 375 376 377
          :w="12"
          :h="7"
          i="7"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
378
        >
wanli's avatar
wanli committed
379
          <EvmChart :chartData="evm" :dataList="evmHistory"></EvmChart>
380 381 382 383 384 385 386 387 388 389 390 391
        </grid-item>
        <grid-item
          :x="0"
          :y="34"
          :w="12"
          :h="7"
          i="8"
          @resize="resizeEvent"
          @move="moveEvent"
          @resized="resizedEvent"
          @container-resized="containerResizedEvent"
          @moved="movedEvent"
wanli's avatar
wanli committed
392
        >
wanli's avatar
wanli committed
393
          <LvglChart :chartData="lvgl" :dataList="lvglHistory"></LvglChart>
wanli's avatar
wanli committed
394 395 396
        </grid-item>
      </grid-layout>
    </div>
wanli's avatar
wanli committed
397 398 399
  </div>
</template>
<script>
400 401 402 403 404
import {
  getWatchList,
  getMonitorData,
  getReport,
  getTemplate,
wanli's avatar
wanli committed
405 406
  setTemplate,
} from "@/api/app-store";
wanli's avatar
wanli committed
407 408
import EvmChart from "./components/EvmChart";
import LvglChart from "./components/LvglChart";
409
import SystemChart from "./components/SystemChart";
wanli's avatar
wanli committed
410
import { GridLayout, GridItem } from "vue-grid-layout";
wanli's avatar
wanli committed
411
import { wsNotify } from "@/utils/eventBus.js";
412
import { deepClone, download } from "@/utils/index";
wanli's avatar
wanli committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
import Database from "@/utils/indexedDB";

const dbObject = {
  dbName: "evmiot", // 数据库名
  version: 1, // 版本号
  primaryKey: "id", // 主键
  keyNames: [
    {
      // 需要存储的数据字段对象
      key: "system.timestamp", // 字段名
      unique: false, // 当前这条数据是否能重复 (最常用) 默认false
    },
    {
      key: "imei",
      unique: false,
    },
  ],
};

432 433
const indexedDb = Database(),
  jsonFile = "evue-monitor.json";
wanli's avatar
wanli committed
434 435
let monitor = new indexedDb(dbObject);

436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
function resetResult() {
  return {
    systemMax: {},
    systemMin: {},
    systemAvg: {},
    systemFirst: {},
    systemLast: {},

    evmMax: {},
    evmMin: {},
    evmAvg: {},
    evmFirst: {},
    evmLast: {},

    lvglMax: {},
    lvglMin: {},
    lvglAvg: {},
    lvglFirst: {},
    lvglLast: {},

    systemImg: null,
    evmImg: null,
    lvglImg: null,

    imageList: [],
  };
}

const result = resetResult();

const system = {
    free_size: null,
    free_space_size: null,
    used_space_size: null,
  },
  evm = {
    heap_map_size: null,
    heap_total_size: null,
    heap_used_size: null,
    stack_total_size: null,
    stack_used_size: null,
  },
  lvgl = {
    frag_pct: null,
    free_biggest_size: null,
    free_cnt: null,
    free_size: null,
    total_size: null,
    used_cnt: null,
    used_pct: null,
  };

wanli's avatar
wanli committed
488
export default {
wanli's avatar
wanli committed
489
  name: "Monitor",
wanli's avatar
wanli committed
490 491
  data() {
    return {
wanli's avatar
wanli committed
492
      watchs: [],
wanli's avatar
wanli committed
493
      pngList: [],
wanli's avatar
wanli committed
494
      globalData: null,
wanli's avatar
wanli committed
495 496
      device: null,
      devices: {},
wanli's avatar
wanli committed
497
      deviceList: null,
498 499 500 501 502
      system: {
        free_size: null,
        free_space_size: null,
        used_space_size: null,
      },
503
      systemList: [],
wanli's avatar
wanli committed
504 505 506
      systemHistory: [],
      evmHistory: [],
      lvglHistory: [],
507 508 509 510 511 512 513
      evm: {
        heap_map_size: null,
        heap_total_size: null,
        heap_used_size: null,
        stack_total_size: null,
        stack_used_size: null,
      },
wanli's avatar
wanli committed
514 515 516 517 518
      evmList: [],
      lvgl: {},
      lvglList: [],
      image: {},
      imageList: [],
519
      pngShowMode: true,
520
      currentPngList: [],
wanli's avatar
wanli committed
521 522
      socket: null,
      form: {
523
        system: ["free_size", "free_space_size", "used_space_size"],
wanli's avatar
wanli committed
524 525 526 527 528 529 530 531 532
        lvgl: ["total_size", "free_size", "free_biggest_size"],
        evm: [
          "heap_map_size",
          "heap_total_size",
          "heap_used_size",
          "stack_total_size",
          "stack_used_size",
        ],
        image: ["png_uncompressed_size", "png_file_size", "length"],
wanli's avatar
wanli committed
533
      },
wanli's avatar
wanli committed
534
      layout: [
535 536 537 538
        { x: 0, y: 0, w: 6, h: 5, i: "1", static: false },
        { x: 6, y: 0, w: 6, h: 5, i: "2", static: true },
        { x: 0, y: 5, w: 6, h: 5, i: "3", static: false },
        { x: 6, y: 5, w: 6, h: 5, i: "4", static: false },
wanli's avatar
wanli committed
539 540 541 542 543
        { x: 0, y: 10, w: 8, h: 10, i: "5", static: false },
        { x: 8, y: 10, w: 4, h: 10, i: "6", static: false },
        { x: 0, y: 20, w: 12, h: 7, i: "7", static: false },
        { x: 0, y: 27, w: 12, h: 7, i: "8", static: false },
        { x: 0, y: 34, w: 12, h: 7, i: "9", static: false },
wanli's avatar
wanli committed
544 545 546
      ],
      draggable: true,
      resizable: true,
wanli's avatar
wanli committed
547 548
    };
  },
wanli's avatar
wanli committed
549 550 551 552 553
  components: {
    GridLayout,
    GridItem,
    EvmChart,
    LvglChart,
554
    SystemChart,
wanli's avatar
wanli committed
555
  },
wanli's avatar
wanli committed
556
  methods: {
557
    onTableRowClick(row) {
558 559
      this.pngShowMode = false;
      this.pngList = this.currentPngList[row.uri];
560
    },
561 562 563 564 565 566 567 568 569 570
    getTemplate() {
      getTemplate()
        .then((res) => {
          console.log(res);
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },
    getReport() {
571 572 573 574 575 576 577 578
      setTemplate({ templateName: jsonFile })
        .then((res) => {
          console.log(res.msg);
        })
        .catch((err) => {
          console.error(err.msg);
        });

579
      wsNotify.eventBus.$emit("exportImg");
580 581 582 583 584 585
      monitor
        .getAllData((params) => {
          return params.imei && params.imei == this.device;
        })
        .then((res) => {
          console.log(res);
586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608

          // 最大值 最小值 平均值 第一次 最后一次
          const systemList = [],
            evmList = [],
            lvglList = [];
          let appList = [];

          res.forEach((item) => {
            systemList.push(
              Object.assign(
                { ts: Date.parse(item.system.timestamp) },
                item.system
              )
            );
            evmList.push(
              Object.assign({ ts: Date.parse(item.system.timestamp) }, item.evm)
            );
            lvglList.push(
              Object.assign(
                { ts: Date.parse(item.system.timestamp) },
                item.lvgl
              )
            );
wanli's avatar
wanli committed
609
          });
610

wanli's avatar
wanli committed
611 612
          appList = appList.concat(
            this.imageList.map((img) => {
613 614 615
              if (img.png_detail && img.png_detail.length) {
                result.imageList = result.imageList.concat(
                  img.png_detail.map((p) => {
wanli's avatar
wanli committed
616
                    p.page = img.uri;
617 618 619 620
                    return p;
                  })
                );
              }
wanli's avatar
wanli committed
621 622 623 624 625 626
              return Object.assign({
                ts: Date.parse(this.globalData.system.timestamp),
                ...img,
              });
            })
          );
627

wanli's avatar
wanli committed
628
          result.appList = appList;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670

          Object.keys(evm).forEach((k) => {
            result.evmFirst[k] = 0;
            result.evmLast[k] = 0;
            let first = 0,
              last = Date.now();
            const t = evmList.map((item) => {
              if (item.ts > first) {
                first = item.ts;
                result.evmFirst[k] = item[k];
              }
              if (item.ts < last) {
                last = item.ts;
                result.evmLast[k] = item[k];
              }
              return item[k];
            });

            result.evmMax[k] = Math.max.apply(null, t);
            result.evmMin[k] = Math.min.apply(null, t);
            result.evmAvg[k] = Math.ceil(
              t.reduce((prev, curr) => prev + curr) / t.length
            );
          });

          Object.keys(lvgl).forEach((k) => {
            result.lvglFirst[k] = 0;
            result.lvglLast[k] = 0;
            let first = 0,
              last = Date.now();
            const t = lvglList.map((item) => {
              if (item.ts > first) {
                first = item.ts;
                result.lvglFirst[k] = item[k];
              }
              if (item.ts < last) {
                last = item.ts;
                result.lvglLast[k] = item[k];
              }
              return item[k];
            });

wanli's avatar
wanli committed
671
            result.lvglMax[k] = Math.max.apply(null, t);
672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694
            result.lvglMin[k] = Math.min.apply(null, t);
            result.lvglAvg[k] = Math.ceil(
              t.reduce((prev, curr) => prev + curr) / t.length
            );
          });

          Object.keys(system).forEach((k) => {
            result.systemFirst[k] = 0;
            result.systemLast[k] = 0;
            let first = 0,
              last = Date.now();
            const t = systemList.map((item) => {
              if (item.ts > first) {
                first = item.ts;
                result.systemFirst[k] = item[k];
              }
              if (item.ts < last) {
                last = item.ts;
                result.systemLast[k] = item[k];
              }
              return item[k];
            });

wanli's avatar
wanli committed
695
            result.systemMax[k] = Math.max.apply(null, t);
696 697 698 699 700 701
            result.systemMin[k] = Math.min.apply(null, t);
            result.systemAvg[k] = Math.ceil(
              t.reduce((prev, curr) => prev + curr) / t.length
            );
          });

wanli's avatar
wanli committed
702 703
          result.imei = this.globalData.imei;
          result.timestamp = this.globalData.system.timestamp;
704

wanli's avatar
wanli committed
705 706
          return getReport({
            templateJson: jsonFile,
707
            dataJson: result,
wanli's avatar
wanli committed
708
          });
709 710
        })
        .then((res) => {
wanli's avatar
wanli committed
711 712 713 714 715 716 717 718
          if (res.code == 200) {
            download(res.data.file, res.data.url)
              .then((res) => {
                console.log(res);
              })
              .catch((err) => {
                console.error(err);
              });
719
          }
720
          console.log(res);
721 722
        })
        .catch((err) => {
wanli's avatar
wanli committed
723
          console.error(err);
724 725
        });
    },
wanli's avatar
wanli committed
726 727 728
    tableRowClassName({ row }) {
      return row.highlight ? "success-row" : "";
    },
wanli's avatar
wanli committed
729
    moveEvent(i, newX, newY) {
wanli's avatar
wanli committed
730
      console.log(i, newX, newY);
wanli's avatar
wanli committed
731
    },
wanli's avatar
wanli committed
732
    movedEvent(i, newX, newY) {
wanli's avatar
wanli committed
733
      console.log(i, newX, newY);
wanli's avatar
wanli committed
734
    },
wanli's avatar
wanli committed
735
    resizeEvent(i, newH, newW, newHPx, newWPx) {
wanli's avatar
wanli committed
736
      console.log(i, newH, newW, newHPx, newWPx);
wanli's avatar
wanli committed
737
    },
wanli's avatar
wanli committed
738
    resizedEvent(i, newX, newY, newHPx, newWPx) {
wanli's avatar
wanli committed
739
      console.log(i, newX, newY, newHPx, newWPx);
wanli's avatar
wanli committed
740
    },
wanli's avatar
wanli committed
741
    containerResizedEvent(i, newH, newW, newHPx, newWPx) {
wanli's avatar
wanli committed
742
      console.log(i, newH, newW, newHPx, newWPx);
wanli's avatar
wanli committed
743
    },
wanli's avatar
wanli committed
744
    layoutCreatedEvent(newLayout) {
wanli's avatar
wanli committed
745 746
      console.log("Created layout: ", newLayout);
    },
wanli's avatar
wanli committed
747
    layoutBeforeMountEvent(newLayout) {
wanli's avatar
wanli committed
748 749
      console.log("beforeMount layout: ", newLayout);
    },
wanli's avatar
wanli committed
750
    layoutMountedEvent(newLayout) {
wanli's avatar
wanli committed
751 752
      console.log("Mounted layout: ", newLayout);
    },
wanli's avatar
wanli committed
753
    layoutReadyEvent(newLayout) {
wanli's avatar
wanli committed
754 755
      console.log("Ready layout: ", newLayout);
    },
wanli's avatar
wanli committed
756
    layoutUpdatedEvent(newLayout) {
wanli's avatar
wanli committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804
      console.log("Updated layout: ", newLayout);
    },
    fetchData() {
      this.isLoading = true;
      getWatchList()
        .then((res) => {
          if (res.code == 200) this.watchs = res.data;
        })
        .catch((err) => {
          this.$message.warning(err.msg);
        })
        .finally(() => {
          this.isLoading = false;
        });
    },
    queryData() {
      getMonitorData({
        watch: this.device,
      })
        .then((res) => {
          if (res.type == "object") {
            this.evmList = res.data.evm;
            this.lvglList = res.data.lvgl;
            this.imageList = res.data.image;
          }
        })
        .catch((err) => {
          this.$message.warning(err.msg);
        });
    },
    onChange(res) {
      if (!res) return null;

      var t = this.watchs.find((item) => {
        return item.id == res;
      });
      if (t) this.device = t.imei;

      // 清空之前数据
      this.resetData();
    },
    onSubmit() {
      this.queryData();
    },
    onReset(formName) {
      this.$refs[formName].resetFields();
      this.fetchData();
    },
wanli's avatar
wanli committed
805
    sendMsg() {
wanli's avatar
wanli committed
806 807 808 809
      let message = JSON.stringify({
        type: "auth",
        token: this.$store.getters.token,
      });
wanli's avatar
wanli committed
810 811 812
      this.socket.send(message);
    },
    handleMessage(msg) {
813 814 815 816
      if (msg.code === 401) {
        this.$store.dispatch("user/removeRole");
        this.$store.dispatch("user/removeToken");
        // this.$router.push("/login?action=refresh");
817
        wsNotify.$emit("reconnect");
818
      }
wanli's avatar
wanli committed
819
      if (msg.type !== "report" || !msg.imei) return null;
wanli's avatar
wanli committed
820 821

      // 将设备发送过来的消息存储到浏览器中
wanli's avatar
wanli committed
822
      // 这里可以优化,将所有数据,保存到indexed datebase中
823
      const m = deepClone(msg);
wanli's avatar
wanli committed
824
      if (!this.deviceList) {
wanli's avatar
wanli committed
825
        this.deviceList = [];
wanli's avatar
wanli committed
826 827
      }
      if (!this.deviceList.includes(msg.imei)) {
wanli's avatar
wanli committed
828
        this.deviceList.push(msg.imei);
wanli's avatar
wanli committed
829
      }
wanli's avatar
wanli committed
830
      if (!this.device) {
wanli's avatar
wanli committed
831 832
        if (this.deviceList && this.deviceList.length)
          this.device = this.deviceList[0];
wanli's avatar
wanli committed
833
        else this.device = msg.imei;
wanli's avatar
wanli committed
834
      }
wanli's avatar
wanli committed
835

wanli's avatar
wanli committed
836 837 838 839
      // 处理单位
      this.processData(m);
      if (monitor.db) monitor.set(m);

wanli's avatar
wanli committed
840 841 842 843 844
      // 如果接收到的数据不是当前选中的设备,那么则直接丢弃
      if (msg.imei != this.device) {
        return null;
      }

wanli's avatar
wanli committed
845
      this.globalData = msg;
846
      deepClone(msg).image.forEach((item) => {
847
        if (item.png_detail && item.png_detail.length) {
848
          this.currentPngList[item.uri] = item.png_detail;
849
        } else {
850
          this.currentPngList[item.uri] = [];
851
        }
852
      });
853
      this.resetData(m);
wanli's avatar
wanli committed
854 855
    },
    processData(msg) {
wanli's avatar
wanli committed
856 857 858 859
      function isNumber(value) {
        return typeof value === "number" && !isNaN(value);
      }

wanli's avatar
wanli committed
860
      if (!msg) return null;
wanli's avatar
wanli committed
861
      Object.keys(msg).forEach((item) => {
wanli's avatar
wanli committed
862
        if (this.form[item]) {
wanli's avatar
wanli committed
863 864 865
          var keys = this.form[item];
          for (var i = 0; i < keys.length; i++) {
            var k = keys[i];
wanli's avatar
wanli committed
866
            if (item == "image") {
wanli's avatar
wanli committed
867
              for (var j = 0; j < msg[item].length; j++) {
wanli's avatar
wanli committed
868 869
                if (isNumber(msg[item][j][k]))
                  msg[item][j][k] = Math.ceil(msg[item][j][k] / 1024);
wanli's avatar
wanli committed
870 871
              }
            } else {
wanli's avatar
wanli committed
872
              msg[item][k] = Math.ceil(msg[item][k] / 1024);
wanli's avatar
wanli committed
873 874 875
            }
          }
        }
wanli's avatar
wanli committed
876
      });
wanli's avatar
wanli committed
877 878 879 880 881

      msg.image.forEach((item) => {
        if (item.png_detail && item.png_detail.length) {
          item.png_detail = item.png_detail.map((png) => {
            png.ratio = Math.floor(png.ratio * 100) / 100;
wanli's avatar
wanli committed
882 883
            // png.filesize = Math.floor(png.filesize / 1024);
            // png.uncompressed_size = Math.floor(png.uncompressed_size / 1024);
wanli's avatar
wanli committed
884 885 886 887
            return png;
          });
        }
      });
wanli's avatar
wanli committed
888 889 890
    },
    onSelectChange(res) {
      this.device = res;
wanli's avatar
wanli committed
891

892 893 894 895
      // 清空图表数据
      wsNotify.eventBus.$emit("clear-system-chart");
      wsNotify.eventBus.$emit("clear-evm-chart");
      wsNotify.eventBus.$emit("clear-lvgl-chart");
wanli's avatar
wanli committed
896

897
      // 清空各个表格数据
898 899
      this.imageList = [];
      this.pngList = [];
900

wanli's avatar
wanli committed
901 902
      // this.processData(this.devices[this.device]);
      // this.resetData();
wanli's avatar
wanli committed
903
    },
904
    resetData(m) {
wanli's avatar
wanli committed
905
      wsNotify.eventBus.$emit("resize");
wanli's avatar
wanli committed
906

907 908
      this.evmList = [{ ...m.evm }];
      this.lvglList = [{ ...m.lvgl }];
wanli's avatar
wanli committed
909 910
      this.systemList = [
        {
911 912
          imei: m.imei,
          ...m.system,
wanli's avatar
wanli committed
913 914
        },
      ];
wanli's avatar
wanli committed
915

wanli's avatar
wanli committed
916 917
      // 这里需要特殊处理下,先判断uri是否存在,不存在则添加,存在则更新
      let uris = [];
wanli's avatar
wanli committed
918 919 920
      this.imageList.forEach((item) => {
        item.highlight = false;
        uris.push(item.uri);
wanli's avatar
wanli committed
921
      });
922 923
      m.image &&
        m.image.forEach((item) => {
wanli's avatar
wanli committed
924 925 926 927 928 929 930
          if (item.png_uncompressed_size > 0) {
            item.highlight = true;
          } else {
            item.highlight = false;
          }

          const target = this.imageList.find((img) => img.uri === item.uri);
wanli's avatar
wanli committed
931
          if (target) {
wanli's avatar
wanli committed
932 933 934
            target.length = item.length;
            target.png_total_count = item.png_total_count;
            target.highlight = false;
wanli's avatar
wanli committed
935
            if (item.png_uncompressed_size) {
wanli's avatar
wanli committed
936
              target.highlight = true;
wanli's avatar
wanli committed
937 938 939 940
              if (item.png_uncompressed_size !== target.png_uncompressed_size) {
                target.png_uncompressed_size = item.png_uncompressed_size;
                if (this.pngShowMode) this.pngList = item.png_detail || [];
              }
wanli's avatar
wanli committed
941
            }
wanli's avatar
wanli committed
942 943 944 945 946
            if (
              item.png_file_size &&
              item.png_file_size !== target.png_file_size
            )
              target.png_file_size = item.png_file_size;
wanli's avatar
wanli committed
947
          } else {
wanli's avatar
wanli committed
948
            this.imageList.push(item);
wanli's avatar
wanli committed
949 950 951
          }
        });

wanli's avatar
wanli committed
952 953 954 955 956
      if (m) {
        if (m.evm) this.evm = m.evm;
        if (m.lvgl) this.lvgl = m.lvgl;
        if (m.image) this.image = m.image;
        if (m.system) this.system = m.system;
wanli's avatar
wanli committed
957
      }
wanli's avatar
wanli committed
958
    },
wanli's avatar
wanli committed
959
  },
960
  mounted() {},
wanli's avatar
wanli committed
961 962
  destroyed() {
    // 页面关闭则销毁该数据库
963 964
    monitor.deleteDB();
    // monitor.clearData();
965
    // monitor.close();
wanli's avatar
wanli committed
966
  },
wanli's avatar
wanli committed
967
  created() {
wanli's avatar
wanli committed
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
    monitor
      .init()
      .then((res) => {
        console.log(res);
        if (monitor.db) {
          monitor.set({
            system: {
              free_size: 0,
              free_space_size: 0,
              used_space_size: 0,
            },
          });
        }
      })
      .catch((err) => {
983
        console.error(err);
wanli's avatar
wanli committed
984
      });
985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009

    this.socket = wsNotify;
    wsNotify.eventBus.$on("exported", (res) => {
      if (res.type === "evm") result.evmImg = res.data;
      else if (res.type === "lvgl") result.lvglImg = res.data;
      else if (res.type === "system") result.systemImg = res.data;
    });

    wsNotify.eventBus.$on("open", (message) => {
      this.sendMsg();
      this.$nextTick(() => {
        console.log(message);
      });
      // 这里启动一个定时器,10秒钟后,如果没有消息进来,说明当前没有在线设备
    });
    wsNotify.eventBus.$on("close", (message) => {
      this.$nextTick(() => {
        console.log(message);
      });
    });
    wsNotify.eventBus.$on("message", (message) => {
      this.$nextTick(() => {
        this.handleMessage(deepClone(message));
      });
    });
wanli's avatar
wanli committed
1010 1011 1012 1013
  },
};
</script>
<style lang="scss" scoped>
wanli's avatar
wanli committed
1014 1015
@import "./style.css";

wanli's avatar
wanli committed
1016 1017 1018 1019 1020
.app-container {
  & > div.page-wrapper {
    margin: 10px 0px;
  }
}
wanli's avatar
wanli committed
1021 1022

.vue-grid-layout {
wanli's avatar
wanli committed
1023
  background: none;
wanli's avatar
wanli committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
}
.vue-grid-item:not(.vue-grid-placeholder) {
  background: #fff;
  border: 0px solid #eee;
}
.vue-grid-item .resizing {
  opacity: 0.9;
}
.vue-grid-item .static {
  background: #cce;
}
.vue-grid-item .text {
  font-size: 24px;
  text-align: center;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  height: 100%;
  width: 100%;
}
.vue-grid-item .item-title {
  margin-left: 15px;
}
.vue-grid-item .no-drag {
  height: 100%;
  width: 100%;
}
.vue-grid-item .minMax {
  font-size: 12px;
}
.vue-grid-item .add {
  cursor: pointer;
}
.vue-draggable-handle {
  position: absolute;
  width: 20px;
  height: 20px;
  top: 0;
  left: 0;
  background: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='10'><circle cx='5' cy='5' r='5' fill='#999999'/></svg>")
    no-repeat;
  background-position: bottom right;
  padding: 0 8px 8px 0;
  background-repeat: no-repeat;
  background-origin: content-box;
  box-sizing: border-box;
  cursor: pointer;
}
wanli's avatar
wanli committed
1075
</style>