gaoluyang
2025-10-23 4d5e975ce21256b1ba64206c5deb79b94bceb0ba
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<template>
  <div class="app-container">
    <el-row :gutter="12" class="mb12">
      <el-col :span="18">
        <el-card class="compact-card">
          <template #header>
            <div class="card-header">
              <span>设备实时监测</span>
              <div class="header-actions">
                <span class="mr8">告警通道:</span>
                <el-switch v-model="alarmChannels.platform" active-text="平台" @change="onSaveChannels" />
                <el-switch v-model="alarmChannels.sms" class="ml8" active-text="短信" @change="onSaveChannels" />
                <el-switch v-model="alarmChannels.voice" class="ml8" active-text="语音" @change="onSaveChannels" />
              </div>
            </div>
          </template>
 
          <el-table :data="equipmentRows" size="small" :border="true" :height="tableHeight">
            <el-table-column prop="name" label="设备" min-width="240" />
            <el-table-column prop="location" label="位置" min-width="200" />
            <el-table-column label="状态" width="110">
              <template #default="scope">
                <el-tag :type="statusTagType(scope.row.status)">{{ renderStatus(scope.row.status) }}</el-tag>
              </template>
            </el-table-column>
            <el-table-column label="温度(℃)" min-width="140">
              <template #default="scope">
                <span :class="overClass(scope.row, 'temperatureC')">{{ fmt(scope.row.metrics.temperatureC) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="压力(bar)" min-width="140">
              <template #default="scope">
                <span :class="overClass(scope.row, 'pressureBar')">{{ fmt(scope.row.metrics.pressureBar) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="电流(A)" min-width="140">
              <template #default="scope">
                <span :class="overClass(scope.row, 'currentA')">{{ fmt(scope.row.metrics.currentA) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="电压(V)" min-width="140">
              <template #default="scope">
                <span :class="overClass(scope.row, 'voltageV')">{{ fmt(scope.row.metrics.voltageV) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="功率因数" min-width="160">
              <template #default="scope">
                <span :class="overClass(scope.row, 'powerFactor', true)">{{ fmt(scope.row.metrics.powerFactor) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="控制" width="220">
              <template #default="scope">
                <el-button
                  type="success"
                  size="small"
                  plain
                  icon="VideoPlay"
                  @click="onControl(scope.row, 'START')"
                  v-hasPermi="['monitor:equipment:control']"
                >启用</el-button>
                <el-button
                  type="danger"
                  size="small"
                  plain
                  icon="VideoPause"
                  @click="onControl(scope.row, 'STOP')"
                  v-hasPermi="['monitor:equipment:control']"
                >停机</el-button>
              </template>
            </el-table-column>
          </el-table>
        </el-card>
      </el-col>
 
      <el-col :span="6">
        <el-card class="mb12 compact-card">
          <template #header>
            <span>阈值设置</span>
          </template>
          <el-form label-width="100px" :inline="false" size="small">
            <el-form-item label="温度上限(℃)">
              <el-input-number v-model="thresholds.temperatureC" :min="20" :max="150" :step="1" />
            </el-form-item>
            <el-form-item label="压力上限(bar)">
              <el-input-number v-model="thresholds.pressureBar" :min="0" :max="40" :step="0.5" />
            </el-form-item>
            <el-form-item label="电流上限(A)">
              <el-input-number v-model="thresholds.currentA" :min="1" :max="500" :step="1" />
            </el-form-item>
            <el-form-item label="电压上限(V)">
              <el-input-number v-model="thresholds.voltageV" :min="360" :max="500" :step="1" />
            </el-form-item>
            <el-form-item label="功率因数下限">
              <el-input-number v-model="thresholds.powerFactor" :min="0.5" :max="1" :step="0.01" :precision="2" />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Check" @click="onSaveThresholds">保存</el-button>
              <el-button type="warning" icon="Refresh" @click="onResetThresholds">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
 
        <el-card class="compact-card">
          <template #header>
            <span>告警事件</span>
          </template>
          <el-scrollbar max-height="480px">
            <el-empty v-if="alarmEvents.length === 0" description="暂无告警" />
            <el-timeline v-else>
              <el-timeline-item
                v-for="(ev, idx) in alarmEvents"
                :key="idx"
                :timestamp="formatTs(ev.ts)"
                :type="ev.level === 'CRITICAL' ? 'danger' : 'warning'"
              >
                <div class="alarm-entry">
                  <div class="alarm-title">{{ ev.name }}({{ ev.location }})</div>
                  <div class="alarm-body">
                    <div>指标:{{ renderField(ev.field) }} = {{ ev.value }},阈值 {{ ev.comparator }} {{ ev.threshold }}</div>
                    <div>通道:{{ renderChannels(ev.channels) }}</div>
                  </div>
                </div>
              </el-timeline-item>
            </el-timeline>
          </el-scrollbar>
        </el-card>
      </el-col>
    </el-row>
  </div>
  
</template>
 
<script setup>
import { ElNotification } from 'element-plus'
import { onMounted, onBeforeUnmount, reactive, ref, computed } from 'vue'
import {
  subscribeEquipmentData,
  detectAlarms,
  getThresholds,
  saveThresholds,
  getAlarmChannels,
  saveAlarmChannels,
  listEquipments,
  sendControlCommand
} from '@/api/equipment/monitoring/equipment.js'
 
const thresholds = reactive(getThresholds())
const alarmChannels = reactive(getAlarmChannels())
 
const equipmentDataMap = reactive(new Map())
const equipmentRows = computed(() => Array.from(equipmentDataMap.values()))
 
const alarmEvents = ref([])
let unsubscribe = null
const tableHeight = ref(520)
 
function updateHeights() {
  // 预留顶部导航、卡片头、内边距等空间,避免出现外部滚动条
  const reserve = 260
  const h = window.innerHeight - reserve
  tableHeight.value = Math.max(420, h)
}
 
function pushAlarm(equipmentRow, overItem) {
  const channelUsed = { ...alarmChannels }
  const event = {
    ts: Date.now(),
    equipmentId: equipmentRow.id,
    name: equipmentRow.name,
    location: equipmentRow.location,
    field: overItem.field,
    value: +overItem.value.toFixed ? +overItem.value.toFixed(2) : overItem.value,
    threshold: overItem.threshold,
    comparator: overItem.field === 'powerFactor' ? '<' : '>',
    level: 'CRITICAL',
    channels: channelUsed
  }
  alarmEvents.value.unshift(event)
  if (alarmEvents.value.length > 100) alarmEvents.value.pop()
  if (channelUsed.platform) {
    ElNotification({ title: '设备告警', message: `${event.name} ${renderField(event.field)} 超限`, type: 'error', duration: 3000 })
  }
  if (channelUsed.sms) {
    /* eslint-disable no-console */
    console.log(`[SMS] 发送至值班员:${event.name} ${renderField(event.field)} 超限`) 
  }
  if (channelUsed.voice) {
    /* eslint-disable no-console */
    console.log(`[VOICE] IVR外呼:${event.name} ${renderField(event.field)} 超限`) 
  }
}
 
function startStream() {
  const baseList = listEquipments()
  baseList.forEach(e => {
    equipmentDataMap.set(e.id, {
      id: e.id,
      name: e.name,
      location: e.location,
      status: 'RUNNING',
      metrics: { temperatureC: 0, pressureBar: 0, currentA: 0, voltageV: 0, powerFactor: 0 }
    })
  })
  unsubscribe = subscribeEquipmentData((msg) => {
    const row = equipmentDataMap.get(msg.equipmentId)
    if (!row) return
    row.status = msg.status
    row.metrics = msg.metrics
    // 告警检测
    const overs = detectAlarms(row.metrics, thresholds)
    overs.forEach(item => pushAlarm(row, item))
  }, { streaming: false })
}
 
function onSaveThresholds() {
  saveThresholds({ ...thresholds })
  ElNotification({ title: '保存成功', message: '阈值已更新', type: 'success' })
}
 
function onResetThresholds() {
  const latest = getThresholds()
  Object.assign(thresholds, latest)
}
 
function onSaveChannels() {
  saveAlarmChannels({ ...alarmChannels })
}
 
async function onControl(row, action) {
  const res = await sendControlCommand(row.id, action)
  if (res && res.code === 200) {
    ElNotification({ title: '控制指令下发', message: `${row.name} ${action === 'START' ? '启用' : '停机'} 指令已受理`, type: 'success' })
  }
}
 
function statusTagType(status) {
  if (status === 'RUNNING') return 'success'
  if (status === 'STOPPED') return 'info'
  return 'warning'
}
 
function renderStatus(status) {
  if (status === 'RUNNING') return '运行'
  if (status === 'STOPPED') return '停机'
  return '异常'
}
 
function fmt(v) {
  if (v === null || v === undefined) return '-'
  return typeof v === 'number' ? v.toFixed(2) : v
}
 
function overClass(row, field, reverse = false) {
  const v = row.metrics[field]
  const t = thresholds[field]
  if (v === undefined || t === undefined) return ''
  const over = reverse ? v < t : v > t
  return over ? 'text-danger' : ''
}
 
function renderField(field) {
  switch (field) {
    case 'temperatureC': return '温度'
    case 'pressureBar': return '压力'
    case 'currentA': return '电流'
    case 'voltageV': return '电压'
    case 'powerFactor': return '功率因数'
    default: return field
  }
}
 
function formatTs(ts) {
  const d = new Date(ts)
  const p2 = (n) => n.toString().padStart(2, '0')
  return `${d.getFullYear()}-${p2(d.getMonth()+1)}-${p2(d.getDate())} ${p2(d.getHours())}:${p2(d.getMinutes())}:${p2(d.getSeconds())}`
}
 
onMounted(() => {
  startStream()
  updateHeights()
  window.addEventListener('resize', updateHeights)
})
 
onBeforeUnmount(() => {
  if (typeof unsubscribe === 'function') unsubscribe()
  window.removeEventListener('resize', updateHeights)
})
</script>
 
<style scoped>
.mb12 { margin-bottom: 12px; }
.mr8 { margin-right: 8px; }
.ml8 { margin-left: 8px; }
.card-header { display: flex; align-items: center; justify-content: space-between; }
.header-actions { display: flex; align-items: center; }
.alarm-entry { line-height: 1.6; }
.alarm-title { font-weight: 600; margin-bottom: 2px; }
.text-danger { color: #f56c6c; font-weight: 600; }
.compact-card :deep(.el-card__body) { padding: 12px; }
:deep(.el-table__cell) { padding: 6px 8px; }
:deep(.el-form-item) { margin-bottom: 8px; }
</style>