<template>
  <div class="app-container">
    <div>
      <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>
    </div>
    <h2>REQUEST</h2>
    <el-table
      element-loading-text="Loading"
      :data="request"
      size="mini"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column
        prop="host"
        label="host"
        min-width="180"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="path"
        label="path"
        min-width="180"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        prop="timestamp"
        label="timestamp"
        min-width="180"
        show-overflow-tooltip
      ></el-table-column>
    </el-table>
    <h2>SYSTEM</h2>
    <el-table
      element-loading-text="Loading"
      :data="system"
      size="mini"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column
        label="imei"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{ scope.row.imei }}</template>
      </el-table-column>
      <el-table-column
        label="free_size"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{ scope.row.free_size }}(KB)</template>
      </el-table-column>
    </el-table>
    <h2>LVGL</h2>
    <el-table
      element-loading-text="Loading"
      :data="lvgl"
      size="mini"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column
        label="total_size"
        min-width="180"
        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="180"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="free_size"
        min-width="180"
        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="180"
      >
        <template slot-scope="scope">{{ scope.row.free_biggest_size }}(KB)</template>
      </el-table-column>
      <el-table-column
        label="used_cnt"
        min-width="180"
      >
        <template slot-scope="scope">{{ scope.row.used_cnt }}</template>
      </el-table-column>
      <el-table-column
        label="used_pct"
        min-width="180"
      >
        <template slot-scope="scope">{{ scope.row.used_pct }}(%)</template>
      </el-table-column>
      <el-table-column
        label="frag_pct"
        min-width="180"
      >
        <template slot-scope="scope">{{ scope.row.frag_pct }}(%)</template>
      </el-table-column>
    </el-table>
    <h2>EVM</h2>
    <el-table
      element-loading-text="Loading"
      :data="evm"
      size="mini"
      border
      stripe
      fit
      highlight-current-row
    >
      <!-- <el-table-column
        label="total_size"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{ scope.row.total_size }}(KB)</template>
      </el-table-column>
      <el-table-column
        label="free_size"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{ scope.row.free_size }}(KB)</template>
      </el-table-column> -->
      <el-table-column
        label="heap_map_size"
        min-width="180"
        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="180"
        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="180"
        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="180"
        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="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{ scope.row.stack_used_size }}(KB)</template>
      </el-table-column>
    </el-table>
    <h2>APP</h2>
    <el-table
      element-loading-text="Loading"
      :data="image"
      size="mini"
      border
      stripe
      fit
      highlight-current-row
    >
      <el-table-column
        prop="uri"
        label="uri"
        min-width="180"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="length"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{ scope.row.length }}(KB)</template>
      </el-table-column>
      <el-table-column
        label="png_file_size"
        min-width="180"
        show-overflow-tooltip
      >
        <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="180"
        show-overflow-tooltip
      ></el-table-column>
      <el-table-column
        label="png_uncompressed_size"
        min-width="180"
        show-overflow-tooltip
      >
        <template slot-scope="scope">{{ scope.row.png_uncompressed_size }}(KB)</template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
export default {
  name: "Monitor",
  data() {
    return {
      device: null,
      devices: {},
      deviceList: [],
      system: [],
      lvgl: [],
      evm: [],
      image: [],
      request: [],
      socket: null,
      form: {
        system: ['free_size'],
        lvgl: ['total_size', 'free_size', 'free_biggest_size'],
        evm: ['total_size', 'free_size', 'heap_map_size', 'heap_total_size', 'heap_used_size', 'stack_total_size', 'stack_used_size'],
        image: ['png_uncompressed_size', 'png_file_size', 'length']
      },
    };
  },
  filters: {
    kb(value) {
      return Math.ceil(value / 1024);
    },
  },
  methods: {
    initWebSocket() {
      if ("WebSocket" in window) {
        this.socket = new WebSocket(`ws://${window.location.host}/ws/v1/notify`);
        // this.socket = new WebSocket("ws://localhost:5001/ws/v1/notify");
        this.socket.onopen = () => {
          console.log("连接成功");
          this.sendMsg();
        };
        this.socket.onmessage = (evt) => {
          var message = evt.data;
          message = JSON.parse(message);
          console.log(message);
          this.handleMessage(message);
        };
        this.socket.onclose = function (res) {
          console.log("断开了连接", res);
        };
        this.socket.onerror = function (err) {
          console.log(err);
        };
      } else {
        console.log("浏览器不支持WebSocket");
      }
    },
    sendMsg() {
      var message = "hello,world";
      this.socket.send(message);
    },
    handleMessage(msg) {
      if (!this.deviceList.includes(msg.imei)) {
        this.deviceList.push(msg.imei)
      }
      if (!this.device) {
        this.device = this.deviceList[0]
      }
      this.devices[msg.imei] = msg
      this.processData(this.devices[this.device])
    },
    processData(msg) {
      Object.keys(msg).forEach(item => {
        if (this.form[item]) {
          var keys = this.form[item]
          for(var i = 0; i < keys.length; i++) {
            var k = keys[i]
            if (item == "image") {
              for(var j = 0; j < msg[item].length; j++) {
                msg[item][j][k] = Math.ceil(msg[item][j][k] / 1024)
              }
            } else {
              msg[item][k] = Math.ceil(msg[item][k] / 1024)
            }
          }
        }
      })

      this.system = [{ imei: msg.imei, ...msg.system }];
      this.lvgl = [{ ...msg.lvgl }];
      this.evm = [{ ...msg.evm }];
      this.request = [{ ...msg.request }];
      this.image = msg.image;
    },
    onSelectChange(res) {
      this.device = res
      this.processData(this.devices[this.device])
      console.log(res)
    },
  },
  mounted() {},
  created() {
    this.initWebSocket();
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  & > div.page-wrapper {
    margin: 10px 0px;
  }
}
</style>
