chart.vue 4.08 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 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 127 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
<template>
  <div class="app-container">
    <el-form :inline="true" :model="form" size="mini">
      <el-form-item label="设备">
        <el-select
          v-model="watch_id"
          filterable
          placeholder="请输入设备名称"
          @change="onChange"
        >
          <el-option
            v-for="(item, index) in watchs"
            :key="index"
            :label="item.imei"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        ><el-button type="primary" @click="onSubmit"
          >查询</el-button
        ></el-form-item
      >
      <el-form-item><el-button @click="onReset">重置</el-button></el-form-item>
    </el-form>
    <LineChart :chartData="evm" :dataList="evmList"></LineChart>
    <LvglChart :chartData="lvgl"></LvglChart>
  </div>
</template>
<script>
import { getWatchList, getMonitorData } from "@/api/index";
import LineChart from "./components/EvmChart";
import LvglChart from "./components/LvglChart";
import { wsNotify } from "@/utils/eventBus.js";
export default {
  name: "AppChart",
  data() {
    return {
      socket: null,
      watchs: [],
      watch_id: null,
      device: null,
      devices: {},
      isLoading: false,
      evm: {},
      evmList: [],
      lvgl: {},
      lvglList: [],
      image: [],
      imageList: [],
      form: {
        start: null,
        end: null,
      },
    };
  },
  components: {
    LineChart,
    LvglChart,
  },
  methods: {
    sendMsg() {
      this.socket.send("hello,world");
    },
    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() {
      let params = {
        watch: this.watch_id,
      };
      if (this.value2 && this.value2.length) {
        if (this.value2.length > 1) {
          params.start = Math.ceil(this.value2[0] / 1000);
          params.end = Math.ceil(this.value2[1] / 1000);
        } else {
          params.start = Math.ceil(this.value2[0] / 1000);
        }
      }
      getMonitorData(params)
        .then((res) => {
          if (res.type == "object") {
            this.evmList = res.data.evm
            this.lvglList = res.data.lvgl
            this.imageList = res.data.image
          } else {
            if (params.category == "evm") this.evmList = res.data
            else if (params.category == "lvgl") this.lvglList = res.data
            else if (params.category == "image") this.imageList = res.data
          }
        })
        .catch((err) => {
          this.$message.warning(err.msg);
        });
    },
    onChange(res) {
      var t = this.watchs.find((item) => {
        return item.id == res;
      });
      if (t) this.device = t.imei;
      this.processData();
    },
    onSubmit() {
      this.queryData();
    },
    onReset(formName) {
      this.$refs[formName].resetFields();
      this.fetchData();
    },
    handleMessage(message) {
      if (!this.device) this.device = message.imei;
      this.devices[message.imei] = message;
      this.processData()
    },
    processData() {
      this.evm = this.devices[this.device].evm;
      this.lvgl = this.devices[this.device].lvgl;
      this.image = this.devices[this.device].image;
    }
  },
  mounted() {},
  created() {
    this.socket = wsNotify;
    wsNotify.eventBus.$on("open", (message) => {
      this.sendMsg();
      this.$nextTick(() => {
        console.log(message);
      });
    });
    wsNotify.eventBus.$on("close", (message) => {
      this.$nextTick(() => {
        console.log(message);
      });
    });
    wsNotify.eventBus.$on("message", (message) => {
      this.$nextTick(() => {
        console.log(message);
        this.handleMessage(message);
      });
    });

    this.fetchData();
  },
};
</script>
<style lang="scss" scoped>
.app-container {
  & > div.page-wrapper {
    margin: 10px 0px;
  }
  #chart1 {
    width: 100%;
    height: 300px;
  }
}
</style>