index.vue
5.57 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<script lang="ts" setup>
import { computed, onMounted, onUnmounted, ref, toRaw, unref } from 'vue'
import type { ECharts, EChartsOption } from 'echarts'
import { init } from 'echarts'
import { getDefaultOption } from './index.config'
import type { CreateComponentType, RenderComponentExposeType } from '@/core/Library/types'
import { useOnMessage } from '@/core/Library/hook/useOnMessage'
import type { NodeDataDataSourceJsonType } from '@/api/node/model'
import type { SubscriptionData } from '@/core/websocket/type/message'
import { SocketSubscriberEnum } from '@/enums/datasource'
import { formatToDateTime } from '@/utils/dateUtil'
import type { CommandSource } from '@/core/websocket/processor'
import { useLatestMultipleMessageValue } from '@/core/Library/hook/useLatestMessageValue'
import { useProductsStore } from '@/store/modules/products'
interface IList {
time: string | number
}
const props = defineProps<{
config: CreateComponentType
}>()
const getCellBounds = computed(() => props.config.cellBounds || { width: 300, height: 300, x: 0, y: 0 })
const chartElRef = ref<Nullable<HTMLDivElement>>()
const chartInstance = ref<Nullable<ECharts>>()
const maxLength = ref<number>(20)
const timeList = ref<number[] | string[] | any>([])// 存储XAxis的值
const seriesData = ref<any>([])// 存储series的值
const titleATTR = ref<string[] | any>([])
const productsStore = useProductsStore()
function initChartInstance() {
chartInstance.value = init(unref(chartElRef))
chartInstance.value.setOption(getDefaultOption())
}
const handleHistoryData = (commandSource: CommandSource, message: SubscriptionData, attr: string[] | string) => {
const { data } = commandSource || {}
const { deviceName } = data as NodeDataDataSourceJsonType
(attr as string[]).forEach((item) => {
timeList.value.push(...message[item].map((item1) => {
const [ts] = item1
return ts
}))
})
timeList.value = Array.from(new Set(timeList.value))?.sort((a: any, b: any) => a - b)// 去重获取到的时间
seriesData.value.forEach((item: any) => {
item.data = unref(timeList).map((time: any) => ({ name: time, value: null }))
})
seriesData.value.forEach((item: any) => {
item.data.forEach((item1: any) => {
message[item.attribute].forEach((messageItem) => {
const [ts, value] = messageItem
if (item1.name === ts) {
item1.value = value || undefined
item1.name = formatToDateTime(item1.name)
}
})
})
})
unref(chartInstance)?.setOption({
title: {
text: `${deviceName || ''}`,
},
xAxis: {
data: toRaw(unref(timeList.value.map((item: string | number) => formatToDateTime(item)))),
},
legend: {
data: unref(titleATTR),
} as any,
series: toRaw(
unref(seriesData).map((item: { type: string; name: string; data: any }) => {
const { type, name, data } = item
return {
type,
name,
data,
}
}),
),
} as EChartsOption)
}
// const contentDataStore = useContentDataStoreWithOut()
const handlerTimeSeriesData = (commandSource: CommandSource, message: SubscriptionData, attr: string[] | string) => {
const { data } = commandSource
const { deviceName } = data as NodeDataDataSourceJsonType
const list: IList | any = {}// 记录时间
useLatestMultipleMessageValue(message, attr as any, (attribute, timespan, value) => {
list.time = timespan || list.time
seriesData.value.forEach((item: any) => {
if (item.attribute === attribute) {
item.data.push({
name: formatToDateTime(list.time),
value: value || undefined,
})
}
if (item.data.length > unref(maxLength))
item.data.shift()
})
})
list.time && timeList.value.push(formatToDateTime(list.time))
if (unref(timeList).length > unref(maxLength))
timeList.value.shift()
unref(chartInstance)?.setOption({
title: {
text: `${deviceName || ''}`,
},
xAxis: {
data: toRaw(unref(timeList)),
},
legend: {
data: unref(titleATTR),
} as any,
series: toRaw(
unref(seriesData).map((item: { type: string; name: string; data: any }) => {
const { type, name, data } = item
return {
type,
name,
data,
}
}),
),
} as EChartsOption)
}
const { onMessage } = useOnMessage({
onReceiveDataSourceMessage(commandSource, message) {
const { data } = commandSource
const { chartOption, attr, deviceProfileId } = data as NodeDataDataSourceJsonType
const { queryType } = chartOption || {}
if (!seriesData.value.length) {
(attr as string[]).forEach((item: string) => {
titleATTR.value.push(productsStore.getObjectModelByIdWithIdentifier(deviceProfileId, item)?.functionName)
seriesData.value.push({ attribute: item, data: [], type: 'line', name: productsStore.getObjectModelByIdWithIdentifier(deviceProfileId, item)?.functionName })
})
}
if (queryType === SocketSubscriberEnum.TS_SUB_CMDS)
handlerTimeSeriesData(commandSource, message.data, attr)
else if (queryType === SocketSubscriberEnum.HISTORY_CMDS)
handleHistoryData(commandSource, message.data, attr)
},
})
onMounted(() => {
initChartInstance()
})
onUnmounted(() => {
chartInstance.value?.dispose()
chartInstance.value = null
})
defineExpose<RenderComponentExposeType>({ onMessage })
</script>
<template>
<div class="w-full h-full justify-center flex items-center">
<div
ref="chartElRef" :style="{ width: `${getCellBounds.width}px`, height: `${getCellBounds.height}px` }"
class="w-full h-full"
/>
</div>
</template>