Alarm.vue
6.33 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<template>
<div class="bg-neutral-100 dark:text-gray-300 dark:bg-dark-700">
<BasicTable @register="registerTable">
<template #action="{ record }">
<TableAction
:actions="[
{
label: '详情',
icon: 'ant-design:eye-outlined',
onClick: handleDetail.bind(null, record),
},
]"
/>
</template>
<template #details="{ record }">
<a-button type="link" class="ml-2" @click="handleViewAlarmDetails(record)">
查看告警详情
</a-button>
</template>
</BasicTable>
<AlarmDetailModal @register="registerDetailModal" @success="handleSuccess" />
</div>
</template>
<script lang="ts">
import { defineComponent, h, nextTick } from 'vue';
import { BasicTable, useTable, TableAction } from '/@/components/Table';
import { alarmColumns, alarmSearchSchemas } from '../../config/detail.config';
import { getDeviceAlarm } from '/@/api/device/deviceManager';
import AlarmDetailModal from '../modal/AlarmDetailModal.vue';
import { useModal } from '/@/components/Modal';
import { Modal } from 'ant-design-vue';
import { JsonPreview } from '/@/components/CodeEditor';
import { getDeviceDetail } from '/@/api/device/deviceManager';
import { getAttribute } from '/@/api/ruleengine/ruleengineApi';
import {
operationNumber_OR_TIME,
operationString,
operationBoolean,
} from '/@/views/rule/linkedge/config/formatData';
export default defineComponent({
name: 'DeviceManagement',
components: {
BasicTable,
TableAction,
AlarmDetailModal,
},
props: {
id: {
type: String,
required: true,
},
},
setup(props) {
const [registerTable, { reload }] = useTable({
api: getDeviceAlarm,
columns: alarmColumns,
useSearchForm: true,
formConfig: {
labelWidth: 100,
schemas: alarmSearchSchemas,
fieldMapToTime: [['alarmTime', ['startTime', 'endTime'], 'x']],
},
showTableSetting: true,
bordered: true,
showIndexColumn: false,
beforeFetch: (data) => {
Reflect.set(data, 'deviceId', props.id);
},
actionColumn: {
width: 200,
title: '操作',
slots: { customRender: 'action' },
fixed: 'right',
},
});
const [registerDetailModal, { openModal }] = useModal();
const handleDetail = (record: Recordable) => {
openModal(true, record);
};
const handleSuccess = () => {
reload();
};
const findName = (item: any, curr: any) => {
return item.attribute.find((item) => item.identifier === curr?.key)?.name;
};
const findLogin = (curr: any) => {
return [...operationNumber_OR_TIME, ...operationString, ...operationBoolean].find(
(item) => item.value === curr?.logic
)?.symbol;
};
const findAttribute = (item: any, curr: any) => {
item.attribute.find((findItem) => findItem.identifier === curr?.key);
};
const findValue = (item: any, curr: any) => {
return {
['触发属性']: findName(item, curr),
['触发条件']: `${findLogin(curr)}${curr?.logicValue}`,
['触发值']: `${curr?.realValue}${
findAttribute(item, curr)?.detail?.dataType?.specs?.unit?.key ?? ''
}`,
};
};
const handleAlarmText = (text: string) => (text === 'triggerData' ? '触发器' : '执行条件');
const handleViewAlarmDetails = async (record: Recordable) => {
await nextTick();
const { details } = record;
if (!details) return;
const deviceIdKeys = Object.keys(details);
const dataFormat = await handleAlarmDetailFormat(deviceIdKeys);
const mapDataFormat = deviceIdKeys.map((deviceKey: string) => {
const findDataFormat = dataFormat.find(
(dataItem: Recordable) => dataItem.tbDeviceId === deviceKey
);
const dataKeys = Object.keys(details[deviceKey]);
const data: any = dataKeys.map((dataItem: string) => {
if (dataItem !== 'triggerData' && dataItem !== 'conditionData') {
return findValue(findDataFormat, details[deviceKey]);
} else {
return {
[handleAlarmText(dataItem)]: findValue(
findDataFormat,
details[deviceKey][dataItem]
),
};
}
});
const objectDataFormat = data.reduce((acc: Recordable, curr: Recordable) => {
return {
...acc,
...curr,
};
});
return {
[findDataFormat.name]: objectDataFormat,
};
});
const objectDataFormats = mapDataFormat.reduce((acc: Recordable, curr: Recordable) => {
return {
...acc,
...curr,
};
});
Modal.info({
title: '告警详情',
width: 600,
centered: true,
maskClosable: true,
content: h(JsonPreview, { data: JSON.parse(JSON.stringify(objectDataFormats)), deep: 4 }),
});
};
const handleAlarmDetailFormat = async (keys: string[]) => {
const temp: any = [];
for (let item of keys) {
if (item === 'key' || item === 'data') return []; //旧数据则终止
const deviceDetailRes = await getDeviceDetail(item);
const { deviceProfileId } = deviceDetailRes;
if (!deviceProfileId) return [];
const attributeRes = await getAttribute(deviceProfileId);
const dataFormat: any = handleDataFormat(deviceDetailRes, attributeRes);
temp.push(dataFormat);
}
return temp;
};
const handleDataFormat = (deviceDetail: any, attributes: any) => {
const { name, alias, tbDeviceId } = deviceDetail;
const attribute = attributes.map((item) => ({
identifier: item.identifier,
name: item.name,
detail: item.detail,
}));
return {
name: alias || name,
tbDeviceId,
attribute,
};
};
return {
registerTable,
registerDetailModal,
handleDetail,
handleSuccess,
handleViewAlarmDetails,
};
},
});
</script>