RealTimeData.vue
4.22 KB
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
<script lang="ts">
import { defineComponent, reactive } from 'vue';
import { BasicTable, useTable } from '/@/components/Table';
import { realTimeDataColumns } from './config';
import { useWebSocket } from '@vueuse/core';
import { JWT_TOKEN_KEY } from '/@/enums/cacheEnum';
import { getAuthCache } from '/@/utils/auth';
import type { socketDataType } from './types';
import { useGlobSetting } from '/@/hooks/setting';
import { useI18n } from '/@/hooks/web/useI18n';
export default defineComponent({
name: 'RealTimeData',
components: {
BasicTable,
},
props: {
deviceDetail: {
type: Object,
required: true,
},
},
setup(props) {
const { t } = useI18n();
const token: string = getAuthCache(JWT_TOKEN_KEY);
const { socketUrl } = useGlobSetting();
const state: any = reactive({
server: `${socketUrl}${token}`,
sendValue: JSON.stringify({
attrSubCmds: [],
tsSubCmds: [
{
entityType: 'DEVICE',
entityId: props.deviceDetail.tbDeviceId,
scope: 'LATEST_TELEMETRY',
cmdId: 1,
},
],
historyCmds: [],
entityDataCmds: [],
entityDataUnsubscribeCmds: [],
alarmDataCmds: [],
alarmDataUnsubscribeCmds: [],
entityCountCmds: [],
entityCountUnsubscribeCmds: [],
}),
recordList: Array<socketDataType>(),
});
const [registerTable, { getForm, setTableData }] = useTable({
columns: realTimeDataColumns,
showTableSetting: true,
bordered: true,
showIndexColumn: false,
dataSource: state.recordList,
useSearchForm: true,
formConfig: {
labelWidth: 100,
schemas: [
{
field: 'key',
label: t('edge.instance.search.keyValue'),
component: 'Input',
colProps: { span: 6 },
componentProps: {
placeholder: t('edge.instance.search.keyValuePlaceholder'),
maxLength: 255,
},
},
],
// 自定义前端查询
async submitFunc() {
const { getFieldsValue } = getForm();
const { key } = getFieldsValue();
if (!key) {
setTableData(state.recordList);
return;
}
let newRecordList = [];
let len = state.recordList.length;
for (let i = 0; i < len; i++) {
if (
state.recordList[i].key.indexOf(key) >= 0 ||
state.recordList[i].value.indexOf(key) >= 0
) {
newRecordList.push(state.recordList[i] as any as never);
}
}
setTableData(newRecordList);
},
async resetFunc() {
setTableData(state.recordList);
},
},
});
const { send, close } = useWebSocket(state.server, {
onConnected() {
send(state.sendValue);
},
onMessage(_, e) {
const { data } = JSON.parse(e.data);
const newArray: socketDataType[] = [];
for (const key in data) {
const [time, value] = data[key].flat(1);
let obj = {
key,
time,
value,
};
if (state.recordList.length === 0) {
state.recordList.unshift(obj);
} else {
newArray.push(obj);
}
}
newArray.forEach((item) => {
let flag = false;
state.recordList.forEach((item1) => {
if (item1.key === item.key) {
item1.value = item.value;
item1.time = item.time;
flag = true;
}
});
if (!flag) {
state.recordList.unshift(item);
}
});
},
onDisconnected() {
close();
},
onError() {},
});
return {
registerTable,
};
},
});
</script>
<template>
<div style="background-color: #f0f2f5">
<BasicTable @register="registerTable" />
</div>
</template>