Files
dashboard/src/views/Dashboard.vue
T
2026-04-23 22:46:15 +08:00

298 lines
8.8 KiB
Vue
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<template>
<div class="dashboard">
<HeaderBar />
<div class="main-content">
<!-- ===== 左侧面板 ===== -->
<div class="left-panel panel-box">
<StatusSummary
:devices="overviewDevices"
:weeklyLabels="weeklyLabels"
:weeklyProduce="weeklyProduceTotal"
:todayIdx="todayIdx"
/>
</div>
<!-- ===== 中央区域 ===== -->
<div class="center-panel">
<!-- A类 · 激光切割设备 -->
<div class="section">
<div class="section-header">
<dv-decoration-6 class="section-deco" :color="['#ff4444','#ff8a80']" />
<span class="section-title">A类 · 激光切割设备</span>
<dv-decoration-6 class="section-deco flip" :color="['#ff4444','#ff8a80']" />
</div>
<div class="laser-grid">
<LaserCard v-for="d in laserDevices" :key="d.id" :device="d" @click="goDetail(d.id)" />
</div>
</div>
<!-- B类 · CNC加工中心 -->
<div class="section section-grow">
<div class="section-header">
<dv-decoration-6 class="section-deco" :color="['#04b9ff','#00e676']" />
<span class="section-title">B类 · CNC加工中心</span>
<dv-decoration-6 class="section-deco flip" :color="['#04b9ff','#00e676']" />
</div>
<div class="cnc-grid">
<CncCard v-for="d in cncDevices" :key="d.id" :device="d" @click="goDetail(d.id)" />
</div>
</div>
<!-- C类 · 电力监测设备 -->
<div class="section">
<div class="section-header">
<dv-decoration-6 class="section-deco" :color="['#ffca28','#ff9800']" />
<span class="section-title">C类 · 电力监测设备</span>
<dv-decoration-6 class="section-deco flip" :color="['#ffca28','#ff9800']" />
</div>
<div class="power-grid">
<PowerCard v-for="d in powerDevices" :key="d.id" :device="d" @click="goDetail(d.id)" />
</div>
</div>
</div>
<!-- ===== 右侧面板 ===== -->
<div class="right-panel panel-box">
<AlarmPanel :alarms="alarms" />
</div>
</div>
<!-- 底部状态栏 -->
<div class="refresh-indicator">
<span class="refresh-dot" :class="{ pulse: mqtt.connected }"></span>
MQTT:{{ mqtt.connected ? '已连接' : '未连接' }}{{ mqtt.reconnecting ? '(重连中)' : '' }}
&nbsp;|&nbsp; 超时阈值:{{ mqtt.offlineThresholdMs / 1000 }}s
<template v-if="mqtt.lastMessageAt">
&nbsp;|&nbsp; 最近消息:{{ mqtt.lastMessageTopic }} @ {{ mqtt.lastMessageAt }}
</template>
<span v-if="mqtt.error" class="mqtt-err">&nbsp;|&nbsp; {{ mqtt.error }}</span>
<template v-if="laser.enabled">
&nbsp;|&nbsp; 激光魔盒:{{ laser.syncing ? '同步中' : '已同步' }}
<template v-if="laser.lastSyncAt">&nbsp;@ {{ laser.lastSyncAt }}</template>
<span v-if="laser.error" class="mqtt-err">&nbsp;|&nbsp; {{ laser.error }}</span>
</template>
</div>
<div v-if="mqtt.lastRouteHint" class="mqtt-hint">{{ mqtt.lastRouteHint }}</div>
</div>
</template>
<script setup>
import { computed } from 'vue'
import { useRouter } from 'vue-router'
import HeaderBar from '../components/HeaderBar.vue'
import LaserCard from '../components/LaserCard.vue'
import CncCard from '../components/CncCard.vue'
import PowerCard from '../components/PowerCard.vue'
import StatusSummary from '../components/StatusSummary.vue'
import AlarmPanel from '../components/AlarmPanel.vue'
import { mqttRuntime } from '../services/mqttDeviceStore.js'
import { laserBoxRuntime } from '../services/laserBoxSync.js'
import { getLast7DaysChartInfo } from '../utils/weekLabels.js'
import { getDeviceStatus } from '../composables/useDeviceStatus.js'
const router = useRouter()
const mqtt = mqttRuntime
const laser = laserBoxRuntime
/**
* A 类数据源:已配置魔盒 URL 时**只**用 HTTP 列表(即使暂时为空也不回退 MQTT)。
* 未配置魔盒时才展示 MQTT 中的 `type === 'laser'`。
*/
const laserListForUi = computed(() => {
if (laser.enabled) return laser.devices
return mqttRuntime.devices.filter((d) => d.type === 'laser')
})
const overviewDevices = computed(() => [
...laserListForUi.value,
...mqttRuntime.devices.filter((d) => d.type !== 'laser')
])
const laserDevices = computed(() => laserListForUi.value)
const cncDevices = computed(() => mqttRuntime.devices.filter(d => d.type === 'cnc'))
const powerDevices = computed(() => mqttRuntime.devices.filter(d => d.type === 'power'))
// 左侧柱图横轴:近 7 日(含今天);最后一根高亮为今天(todayIdx === 6)
const { labels: weeklyLabels, todayIdx } = getLast7DaysChartInfo()
/**
* 各设备 `weeklyStats.produce` 按索引 0…6 与上面对齐。
* 注意:B/C 若 MQTT 上游仍按「自然周」传 7 个数,与近 7 日日历可能错位,宜在采集端改为按日或滚动窗口。
*/
const weeklyProduceTotal = computed(() => {
const totals = [0, 0, 0, 0, 0, 0, 0]
overviewDevices.value.forEach((d) => {
if (d.weeklyStats?.produce) {
d.weeklyStats.produce.forEach((v, i) => {
if (i <= todayIdx) totals[i] += v
})
}
})
return totals
})
/**
* 报警汇总:
* - B/C:仍为「设备处于报警态」一条摘要
* - A 类:不再调用 alarm/v2,直接展示机台状态接口的 NcState.AlarmMsg(有内容则展示一条)
*/
const alarms = computed(() => {
const rows = []
const now = new Date()
overviewDevices.value.forEach((d) => {
if (!d.online || !d.machineData) return
if (d.type === 'laser') return
const status = getDeviceStatus(d)
if (status.key !== 'alarm') return
const a = d.machineData.alarm
const msg = a?.length ? a[0] : 'CNC系统报警'
rows.push({
time: now.toLocaleTimeString('zh-CN', { hour12: false }),
device: `CNC-${d.name}`,
message: msg
})
})
laserListForUi.value.forEach((d) => {
if (!d.online || d.type !== 'laser') return
const nc = d.machineData?.CutSystemState?.NcState
const msg = String(nc?.AlarmMsg || '').trim()
if (!msg) return
rows.push({
time: now.toLocaleTimeString('zh-CN', { hour12: false }),
device: d.name,
message: msg
})
})
return rows
})
function goDetail(id) {
router.push({ name: 'DeviceDetail', params: { id } })
}
</script>
<style scoped>
.dashboard {
width: 1920px;
height: 1080px;
background: #020c20;
display: flex;
flex-direction: column;
overflow: hidden;
font-family: 'PingFang SC', 'Microsoft YaHei', 'Noto Sans SC', sans-serif;
}
.main-content {
flex: 1;
display: flex;
gap: 10px;
padding: 8px 12px;
min-height: 0;
}
/* 左右面板 */
.left-panel { flex: 0 0 258px; }
.right-panel { flex: 0 0 258px; }
.panel-box {
background: rgba(4, 20, 55, 0.55);
border: 1px solid rgba(4, 185, 255, 0.15);
border-radius: 8px;
padding: 10px;
backdrop-filter: blur(4px);
height: 100%;
}
/* 中央区域 */
.center-panel {
flex: 1;
display: flex;
flex-direction: column;
gap: 6px;
min-height: 0;
}
.section { flex: 0 0 auto; }
.section-grow { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.section-header {
display: flex;
align-items: center;
justify-content: center;
gap: 8px;
margin-bottom: 6px;
}
.section-deco { width: 80px; height: 20px; }
.section-deco.flip { transform: scaleX(-1); }
.section-title {
font-size: 13px;
font-weight: 700;
color: #8eb8d8;
letter-spacing: 3px;
white-space: nowrap;
}
/* A类 激光网格:2列 */
.laser-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
/* B类 CNC网格:4列,自适应行 */
.cnc-grid {
flex: 1;
display: grid;
grid-template-columns: repeat(4, 1fr);
grid-template-rows: repeat(3, 1fr);
gap: 8px;
min-height: 0;
}
/* C类 电力网格:2列 */
.power-grid {
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
/* 底部刷新指示 */
.refresh-indicator {
height: 22px;
display: flex;
align-items: center;
justify-content: flex-end;
padding-right: 16px;
font-size: 11px;
color: #304a6a;
letter-spacing: 0.5px;
border-top: 1px solid rgba(4,185,255,0.08);
gap: 6px;
}
.refresh-dot {
width: 6px; height: 6px; border-radius: 50%;
background: #304a6a; display: inline-block; transition: background 0.3s;
}
.refresh-dot.pulse {
background: #00e676; box-shadow: 0 0 6px #00e676;
animation: pulse-dot 0.4s ease-in-out;
}
@keyframes pulse-dot { 0%{transform:scale(1)} 50%{transform:scale(1.6)} 100%{transform:scale(1)} }
.mqtt-err { color: #ff6b6b; }
.mqtt-hint {
flex-shrink: 0;
padding: 4px 16px 6px;
font-size: 10px;
line-height: 1.35;
color: #c9a227;
background: rgba(40, 30, 0, 0.35);
border-top: 1px solid rgba(201, 162, 39, 0.25);
word-break: break-all;
}
</style>