xiaoyi
5 天以前 a4025834e2304dc57f6e98d58feeb9416bd90609
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
<script lang="ts" setup>
  import type { VxeTableGridOptions } from '#/adapter/vxe-table';
  import type { MesDvTelemetryApi } from '#/api/mes/dv/telemetry';
 
  import { onBeforeUnmount, ref } from 'vue';
 
  import { Page } from '@vben/common-ui';
  import { useVbenVxeGrid } from '#/adapter/vxe-table';
 
  import { Button, message, Switch, Tabs, Tag } from 'ant-design-vue';
  import dayjs from 'dayjs';
 
  import {
    getLatestTelemetry,
    getTelemetryPage,
    pullTelemetry,
  } from '#/api/mes/dv/telemetry';
 
  import { useHistoryColumns, useHistoryGridFormSchema, useLatestColumns } from './data';
 
  const activeTab = ref<'latest' | 'history'>('latest');
  const autoRefresh = ref(false);
  const pulling = ref(false);
  const lastUpdateTime = ref<Date>();
  let refreshTimer: ReturnType<typeof setInterval> | undefined;
 
  const [LatestGrid, latestGridApi] = useVbenVxeGrid({
    gridOptions: {
      columns: useLatestColumns(),
      height: 'auto',
      keepSource: true,
      proxyConfig: {
        ajax: {
          query: async () => await getLatestTelemetry(),
        },
      },
      rowConfig: { keyField: 'id', isHover: true },
      toolbarConfig: { refresh: true },
    } as VxeTableGridOptions<MesDvTelemetryApi.Telemetry>,
  });
 
  const [HistoryGrid, historyGridApi] = useVbenVxeGrid({
    formOptions: { schema: useHistoryGridFormSchema() },
    gridOptions: {
      columns: useHistoryColumns(),
      height: 'auto',
      keepSource: true,
      proxyConfig: {
        ajax: {
          query: async ({ page }, formValues) =>
            await getTelemetryPage({
              pageNo: page.currentPage,
              pageSize: page.pageSize,
              ...formValues,
            }),
        },
      },
      rowConfig: { keyField: 'id', isHover: true },
      toolbarConfig: { refresh: true, search: true },
    } as VxeTableGridOptions<MesDvTelemetryApi.Telemetry>,
  });
 
  /** 拉取最新数采数据并刷新当前 Tab */
  async function handlePull() {
    pulling.value = true;
    try {
      const result = await pullTelemetry();
      lastUpdateTime.value = result.pullTime;
      message.success(
        `拉取成功,新增 ${result.recordCount} 条,涉及设备 ${result.deviceCount} 台`,
      );
      refreshActive();
    } finally {
      pulling.value = false;
    }
  }
 
  /** 刷新当前 Tab 数据 */
  function refreshActive() {
    if (activeTab.value === 'latest') {
      latestGridApi.query();
    } else {
      historyGridApi.query();
    }
  }
 
  /** 切换实时自动刷新(每 30 秒) */
  function toggleAutoRefresh(checked: boolean) {
    if (checked) {
      refreshTimer = setInterval(() => {
        if (activeTab.value === 'latest') {
          latestGridApi.query();
        }
      }, 30_000);
    } else if (refreshTimer) {
      clearInterval(refreshTimer);
      refreshTimer = undefined;
    }
  }
 
  onBeforeUnmount(() => {
    if (refreshTimer) {
      clearInterval(refreshTimer);
    }
  });
</script>
 
<template>
  <Page auto-content-height>
    <div class="p-4">
      <div class="mb-4 flex items-center gap-4">
        <Button type="primary" :loading="pulling" @click="handlePull">
          拉取最新数据
        </Button>
        <Switch
          v-model:checked="autoRefresh"
          checked-children="实时自动刷新"
          un-checked-children="实时自动刷新"
          @change="toggleAutoRefresh"
        />
        <span v-if="lastUpdateTime" class="text-sm text-gray-500">
          最近拉取:{{ dayjs(lastUpdateTime).format('YYYY-MM-DD HH:mm:ss') }}
        </span>
      </div>
      <Tabs v-model:active-key="activeTab">
        <Tabs.TabPane key="latest" tab="实时数据">
          <LatestGrid table-title="实时数据">
            <template #anomaly="{ row }">
              <Tag :color="row.whetherAnomaly ? 'red' : 'green'">
                {{ row.whetherAnomaly ? '异常' : '正常' }}
              </Tag>
            </template>
          </LatestGrid>
        </Tabs.TabPane>
        <Tabs.TabPane key="history" tab="历史记录">
          <HistoryGrid table-title="历史记录">
            <template #anomaly="{ row }">
              <Tag :color="row.whetherAnomaly ? 'red' : 'green'">
                {{ row.whetherAnomaly ? '异常' : '正常' }}
              </Tag>
            </template>
          </HistoryGrid>
        </Tabs.TabPane>
      </Tabs>
    </div>
  </Page>
</template>