feiyu02
2025-05-15 026f17ebafb85250d9ae9b71b80ae5f07341c172
src/views/realtimemode/RealtimeMode.vue
@@ -1,9 +1,202 @@
<template>
  <div>RealtimeMode</div>
  <div class="p-events-none m-t-2">
    <el-row justify="center" align="middle" class="top-wrap">
      <DeviceChange @change="onDeviceChange"></DeviceChange>
    </el-row>
    <el-row class="m-t-2">
      <FactorRadio
        :device-type="deviceType"
        v-model="factorType"
      ></FactorRadio>
    </el-row>
    <el-row class="m-t-2">
      <el-col span="1">
        <FactorLegend
          :factor="factorDatas.factor[factorType]"
        ></FactorLegend>
      </el-col>
      <el-col span="1">
        <SourceTrace v-model:factorType="factorType"></SourceTrace>
      </el-col>
    </el-row>
    <DashBoard class="dash-board" :factor-datas="factorDatas"></DashBoard>
    <RealTimeTrend
      class="real-time-trend"
      :factor-datas="factorDatas"
      :device-type="deviceType"
    ></RealTimeTrend>
  </div>
</template>
<script>
import moment from 'moment';
import mapUtil from '@/utils/map/util';
import { useFetchData } from '@/composables/fetchData';
import { TYPE0 } from '@/constant/device-type';
import { defaultOptions } from '@/constant/radio-options';
import { FactorDatas } from '@/model/FactorDatas';
import DashBoard from './component/DashBoard.vue';
import RealTimeTrend from './component/RealTimeTrend.vue';
import DeviceChange from './component/DeviceChange.vue';
import SourceTrace from '@/views/sourcetrace/SourceTrace.vue';
import { realTimeMapAnimation } from '@/utils/map/animation';
import {
  fetchHistoryData,
  startLoopFetchRealTimeData,
  clearFetchingTask
} from '@/utils/factor/data';
import thirdPartyDataApi from '@/api/thirdPartyDataApi';
import websocket from '@/api/websocket';
// const mapAnimation = new MapAnimation();
// 调试模式
const mode = 'debug';
export default {
  name: 'HistoryPage'
  components: { DashBoard, RealTimeTrend, DeviceChange, SourceTrace },
  setup() {
    const { loading, fetchData } = useFetchData(10000);
    return { loading, fetchData };
  },
  data() {
    return {
      // 监测设备类型
      deviceType: TYPE0,
      deviceCode: '',
      // 监测因子的类型编号
      factorType: defaultOptions(TYPE0).value,
      // 新获取的监测数据
      factorDatas: new FactorDatas(),
      // 全部监测数据
      allFactorDatas: new FactorDatas()
    };
  },
  watch: {
    factorType(nV, oV) {
      if (nV != oV) {
        realTimeMapAnimation.setFactorType(nV);
      }
    }
  },
  computed: {
    // 数据最新时间(最新数据的采样时间加1秒;没有数据时,采用当前时间的前6分钟)
    latestTime() {
      if (this.factorDatas.times.length == 0) {
        return moment().subtract(6, 'm').format('YYYY-MM-DD HH:mm:ss');
      } else {
        const _time = this.factorDatas.times[this.factorDatas.times.length - 1];
        return moment(_time).add(1, 's').format('YYYY-MM-DD HH:mm:ss');
      }
    }
  },
  methods: {
    onDeviceChange({ type, deviceCode }) {
      this.deviceType = type;
      this.deviceCode = deviceCode;
      this.clearFetchingTask();
      realTimeMapAnimation.stop();
      this.allFactorDatas.clearData();
      this.factorDatas.clearData();
      this.notFirstFetch = false;
      this.fetchRealTimeData();
    },
    onFetchData(data) {
      // todo 根据设备类型切换地图监测因子展示单选框、折线图复选框、数据表格复选框的因子类型
      // this.deviceType = type;
      const fDatas = new FactorDatas();
      fDatas.setData(data, this.drawMode, () => {
        fDatas.refreshHeight(this.factorType);
        // this.draw();
        this.factorDatas = fDatas;
      });
      if (mode == 'debug') {
        websocket.send(JSON.stringify(data));
      }
    },
    fetchRealTimeData() {
      this.fetchData((page) => {
        const param =
          mode == 'debug'
            ? {
                deviceCode: this.deviceCode,
                startTime: '2025-01-16 11:34:00',
                endTime: '2025-01-16 11:35:00',
                page,
                perPage: 100
              }
            : {
                deviceCode: this.deviceCode,
                page,
                perPage: 100
              };
        return fetchHistoryData(param, false).then((res) => {
          this.onFetchData(res.data);
          this.onMapData(res.data);
          this.fetchNextData();
          thirdPartyDataApi.fetchLatestData(this.deviceType, this.deviceCode);
        });
      });
    },
    clearFetchingTask() {
      clearFetchingTask();
    },
    fetchNextData() {
      startLoopFetchRealTimeData(
        () => {
          return {
            deviceCode: this.deviceCode,
            updateTime: this.latestTime,
            perPage: mode == 'debug' ? 1 : 10
          };
        },
        (res) => {
          this.onFetchData(res.data);
          this.onMapData(res.data);
          thirdPartyDataApi.fetchLatestData(this.deviceType, this.deviceCode);
        },
        mode == 'debug' ? 4000 : undefined
      );
    },
    onMapData(dataList) {
      let startIndex = this.allFactorDatas.length() - 1;
      if (!this.notFirstFetch) {
        startIndex = dataList.length - 2;
        this.notFirstFetch = true;
      }
      startIndex = startIndex < 0 ? 0 : startIndex;
      return new Promise(() => {
        this.allFactorDatas.addData(dataList, this.drawMode, () => {
          realTimeMapAnimation.moveAnimation(
            this.allFactorDatas,
            this.factorType,
            startIndex
          );
        });
      });
    }
  },
  mounted() {
    if (mode == 'debug') {
      websocket.send('start');
    }
  },
  unmounted() {
    this.clearFetchingTask();
    realTimeMapAnimation.stop();
    mapUtil.clearMap();
  }
};
</script>
<style scoped>
.dash-board {
  position: absolute;
  left: 0;
  bottom: 2px;
}
.real-time-trend {
  position: absolute;
  right: 0;
  top: 0;
}
</style>