423 更新7日曲线图,A类占位

This commit is contained in:
Mac mini git server 2026-04-23 23:42:09 +08:00
parent 96d6b8e8af
commit 7be712e7d5
6 changed files with 318 additions and 42 deletions

1
.gitignore vendored
View File

@ -12,3 +12,4 @@ npm-debug.log*
.env
.env.*
!.env.example
*.zip

View File

@ -21,11 +21,14 @@
<!-- 在线数据 -->
<template v-else>
<div class="produce-row">
<span class="produce-label">加工件数</span>
<span class="produce-val">{{ md.currentProduceCount?.toLocaleString() }}<small>件</small></span>
<span class="produce-label">累计总产量</span>
<span class="produce-val">{{ totalProduceCount.toLocaleString() }}<small>件</small></span>
</div>
<div class="daily-row">
<span class="produce-label">今日产量</span>
<span class="daily-val">{{ todayProduceCount.toLocaleString() }}<small>件</small></span>
</div>
<div class="time-rows">
<div class="time-item"><span class="t-label">切削</span><span class="t-val">{{ formatTime(md.cuttime) }}</span></div>
<div class="time-item"><span class="t-label">运行</span><span class="t-val">{{ formatTime(md.runtime) }}</span></div>
</div>
</template>
@ -50,6 +53,9 @@ const cncType = computed(() => {
if (props.device.online) return props.device.machineData?.sysInfo?.cncType || 'CNC'
return props.device.sysInfo?.cncType || 'CNC'
})
const totalProduceCount = computed(() => Number(md.value.currentProduceCount || 0))
/** 近 7 日窗口固定“最后一格=今天” */
const todayProduceCount = computed(() => Number(props.device.weeklyStats?.produce?.[6] || 0))
function formatTime(str) { return formatFanucTimeShort(str) }
</script>
@ -96,6 +102,9 @@ function formatTime(str) { return formatFanucTimeShort(str) }
.produce-label { font-size: 11px; color: #5a7a9a; font-weight: 600; }
.produce-val { font-size: 20px; font-weight: 800; color: #00e676; text-shadow: 0 0 8px rgba(0,230,118,0.3); }
.produce-val small { font-size: 11px; margin-left: 3px; opacity: 0.7; }
.daily-row { display: flex; justify-content: space-between; align-items: baseline; }
.daily-val { font-size: 14px; font-weight: 700; color: #8dd8ff; }
.daily-val small { font-size: 10px; margin-left: 3px; opacity: 0.75; }
.time-rows { display: flex; gap: 10px; margin-top: auto; }
.time-item { flex: 1; background: rgba(0,0,0,0.2); padding: 4px 8px; border-radius: 4px; display: flex; justify-content: space-between; }

View File

@ -41,10 +41,13 @@
</div>
</div>
<!-- 今日总产出 -->
<!-- CNC 累计/今日产量 -->
<div class="today-total">
<div class="total-title">CNC 总加工件数</div>
<div class="total-title">CNC 总产量(累计)</div>
<dv-digital-flop class="digital-flop" :config="flopConfig" />
<div class="daily-produce">
今日产量:<span class="daily-num">{{ dailyProduceCount.toLocaleString() }}</span> 件
</div>
</div>
<!-- 横轴为「近 7 日(含今天)」,由 Dashboard 传入 getLast7DaysChartInfo().labels -->
@ -102,7 +105,15 @@ const runningPct = computed(() => {
const totalProduceCount = computed(() => {
return props.devices
.filter(d => d.type === 'cnc' && d.online)
.reduce((s, d) => s + (d.machineData?.currentProduceCount || 0), 0)
.reduce((s, d) => s + Number(d.machineData?.currentProduceCount || 0), 0)
})
/** 今日 CNC 产量:使用 weeklyStats.produce[todayIdx](累计差分后的日增量) */
const dailyProduceCount = computed(() => {
const idx = Math.max(0, Math.min(6, props.todayIdx))
return props.devices
.filter((d) => d.type === 'cnc' && d.online)
.reduce((s, d) => s + Number(d.weeklyStats?.produce?.[idx] || 0), 0)
})
const flopConfig = computed(() => ({
@ -145,6 +156,8 @@ function barH(val) { return Math.max(6, (val / maxProduce.value) * 88) }
.today-total { text-align: center; padding: 8px; background: rgba(4,185,255,0.05); border: 1px solid rgba(4,185,255,0.15); border-radius: 6px; margin-bottom: 4px; }
.total-title { font-size: 11px; color: #5a7a9a; letter-spacing: 1px; margin-bottom: 2px; }
.digital-flop { height: 42px; }
.daily-produce { font-size: 11px; color: #7ea3c0; margin-top: 2px; }
.daily-num { color: #00e676; font-weight: 700; margin: 0 2px; }
.bar-chart { display: flex; gap: 3px; align-items: flex-end; height: 100px; flex: 1; min-height: 80px; }
.bar-group { flex: 1; display: flex; flex-direction: column; align-items: center; height: 100%; }

View File

@ -34,6 +34,37 @@ export function laserRouteId(ip, appName) {
return `laser:${ip}:${appName}`
}
/** 魔盒同步失败时占位:保证看板始终展示 2 台 A 类卡片(离线),避免整块空白 */
function offlineLaserPlaceholders(offlineReason) {
const reason = offlineReason || '魔盒同步失败,请检查网络与 VITE_LASER_BOX_URL'
return [
{
id: 'laser:offline:1',
name: '激光 #1',
type: 'laser',
online: false,
offlineReason: reason,
appName: '',
ip: '',
machineData: null,
weeklyStats: null,
_laser: { placeholder: true, slot: 1 }
},
{
id: 'laser:offline:2',
name: '激光 #2',
type: 'laser',
online: false,
offlineReason: reason,
appName: '',
ip: '',
machineData: null,
weeklyStats: null,
_laser: { placeholder: true, slot: 2 }
}
]
}
function pickCuttingAppName(appInfos) {
if (!Array.isArray(appInfos) || appInfos.length === 0) return ''
const skip = (n) => /datacenter/i.test(String(n || ''))
@ -211,6 +242,7 @@ async function syncOnce(baseUrl) {
}
} catch (e) {
laserBoxRuntime.error = e?.message || String(e)
laserBoxRuntime.devices = offlineLaserPlaceholders(laserBoxRuntime.error)
if (isLaserBoxHttpLog()) {
console.warn('[LaserBox] 同步异常', laserBoxRuntime.error)
}

View File

@ -11,11 +11,11 @@
*/
import { reactive } from 'vue'
import mqtt from 'mqtt'
import { getLast7DaysStatContext } from '../utils/weekLabels.js'
const NESTED_CNC = ['allCoors', 'statusInfo', 'programCurrent', 'spindleSpeedAndFeedRate', 'sysInfo']
const DEFAULT_MQTT_TOPICS = 'rbiot-c01-001,rbiot-b01-001'
/** 未单独配置模板/前缀时,短 id 默认展开为现场常见路径(避免只订 rbiot-xxx 与 topic/device/.../data 不一致) */
const DEFAULT_TOPIC_TEMPLATE = 'topic/device/{id}/data'
@ -111,6 +111,10 @@ function subscriptionVariants(canonical) {
function emptyWeeklyStats() {
return {
produce: [0, 0, 0, 0, 0, 0, 0],
/** 近 7 日每天的累计件数(用于差分与排障) */
produceCumulative: [0, 0, 0, 0, 0, 0, 0],
/** 与 produce/produceCumulative 对齐的 yyyy-MM-dd */
dateKeys: null,
cuttime: [0, 0, 0, 0, 0, 0, 0],
dates: null
}
@ -206,6 +210,143 @@ function apparentPowerKw(slave1) {
return (Math.sqrt(3) * 380 * iAvg) / 1000
}
function toFiniteNumber(v) {
const n = Number(v)
return Number.isFinite(n) ? n : null
}
function toYmdText(v) {
const s = String(v || '').trim()
if (!s) return ''
// 兼容 yyyy-MM-dd / yyyy-MM-dd HH:mm:ss / yyyyMMdd
const m = s.match(/^(\d{4})[-/]?(\d{2})[-/]?(\d{2})/)
if (!m) return ''
return `${m[1]}-${m[2]}-${m[3]}`
}
/**
* CNC 机床累计件数历史快照(基础数据)。
* 在 `normalizeCncWeeklyStats` 中最先写入;MQTT 若带同 `dt` 的 `weeklystatus` 或当日 `currentProduceCount` 会覆盖/更新。
*/
const CNC_PRODUCE_COUNT_BASELINE = [
{ dt: '2026-04-17', currentProduceCount: 519 },
{ dt: '2026-04-18', currentProduceCount: 533 },
{ dt: '2026-04-19', currentProduceCount: 538 },
{ dt: '2026-04-20', currentProduceCount: 551 },
{ dt: '2026-04-21', currentProduceCount: 568 },
{ dt: '2026-04-22', currentProduceCount: 568 },
{ dt: '2026-04-23', currentProduceCount: 568 }
]
/**
* 解析 CNC 周内累计快照数据:
* 支持 `weeklystatus` / `weeklyStatus`,以及数组字符串。
* 单项支持字段:
* - `dt` / `date` / `statDate`
* - `currentProduceCount` / `produceCount` / `count`
*/
function extractCncWeeklyRows(payload) {
if (!payload) return []
let src = payload
if (typeof src === 'string') {
try { src = JSON.parse(src) } catch { return [] }
}
if (!Array.isArray(src)) return []
return src.map((r) => {
const dt = toYmdText(r?.dt ?? r?.date ?? r?.statDate)
const count = toFiniteNumber(r?.currentProduceCount ?? r?.produceCount ?? r?.count)
return dt && count != null ? { dt, count } : null
}).filter(Boolean)
}
/**
* 根据“累计件数”计算近 7 日日增量:
* - 历史缺失日按前一天累计补齐(当日增量=0)
* - today 的累计优先用 MQTT 实时 `currentProduceCount`
*/
function normalizeCncWeeklyStats(prevStats, machineData, incomingData) {
const win = getLast7DaysStatContext()
const datesYmd = win.statInts.map((si) => {
const s = String(si)
return `${s.slice(0, 4)}-${s.slice(4, 6)}-${s.slice(6, 8)}`
})
const rowInputs = [
...(extractCncWeeklyRows(machineData?.weeklystatus)),
...(extractCncWeeklyRows(machineData?.weeklyStatus)),
...(extractCncWeeklyRows(incomingData?.weeklystatus)),
...(extractCncWeeklyRows(incomingData?.weeklyStatus))
]
const rowMap = new Map()
for (const row of CNC_PRODUCE_COUNT_BASELINE) {
const dt = toYmdText(row.dt)
const cnt = toFiniteNumber(row.currentProduceCount)
if (dt && cnt != null) rowMap.set(dt, cnt)
}
for (const r of rowInputs) rowMap.set(r.dt, r.count)
// 若没有外部 weeklystatus,沿用此前已算出的累计轨迹,保证图不跳 0
if (Array.isArray(prevStats?.dateKeys) && Array.isArray(prevStats?.produceCumulative)) {
const prevDates = prevStats.dateKeys
const prevCum = prevStats.produceCumulative
for (let i = 0; i < Math.min(prevDates.length, prevCum.length); i++) {
const ymd = toYmdText(prevDates[i])
const cnt = toFiniteNumber(prevCum[i])
if (ymd && cnt != null && !rowMap.has(ymd)) rowMap.set(ymd, cnt)
}
}
const cumulative = new Array(7).fill(0)
// 首日基准:如果没有更早数据,就以首日累计本身作为基准(首日产量=0)
let carry = null
for (const [dt, count] of rowMap.entries()) {
if (dt < datesYmd[0]) {
if (carry == null || count > carry) carry = count
}
}
// 若窗口内有数据但窗口前没有基准,用窗口内最早一条作为整窗起始,避免首个有值日出现“巨量日增”
if (carry == null) {
for (let i = 0; i < 7; i++) {
const cnt = rowMap.get(datesYmd[i])
if (cnt != null) {
carry = cnt
break
}
}
}
for (let i = 0; i < 7; i++) {
const cnt = rowMap.get(datesYmd[i])
if (cnt != null) carry = cnt
if (carry == null) carry = 0
cumulative[i] = carry
}
// 实时 currentProduceCount 作为今天累计
const liveCount = toFiniteNumber(machineData?.currentProduceCount ?? incomingData?.currentProduceCount)
if (liveCount != null) {
// 没有历史基准时,整窗填充为同一累计,避免“当天产量=累计总量”的误判
if (rowMap.size === 0 && cumulative.every((x) => x === 0)) {
for (let i = 0; i < 7; i++) cumulative[i] = liveCount
} else {
cumulative[6] = liveCount
}
}
const produce = new Array(7).fill(0)
for (let i = 1; i < 7; i++) {
produce[i] = Math.max(0, Math.round(cumulative[i] - cumulative[i - 1]))
}
return {
produce,
produceCumulative: cumulative,
dateKeys: datesYmd,
cuttime: Array.isArray(prevStats?.cuttime) ? prevStats.cuttime.slice(0, 7) : [0, 0, 0, 0, 0, 0, 0],
dates: win.labels
}
}
const DEBUG = import.meta.env.VITE_MQTT_DEBUG === 'true'
function log(...args) {
@ -247,6 +388,7 @@ function applyPayload(device, data) {
if (data.client_id) device.name = String(data.client_id)
} else if (device.type === 'cnc') {
device.machineData = mergeCnc(device.machineData, { ...data, timestamp: ts })
device.weeklyStats = normalizeCncWeeklyStats(device.weeklyStats, device.machineData, data)
} else if (device.type === 'laser') {
device.machineData = mergeLaser(device.machineData, { ...data, timestamp: ts })
if (data.appName) device.appName = String(data.appName)

View File

@ -48,7 +48,6 @@
<div class="metric"><span class="m-label">机床型号</span><span class="m-value">{{ md.sysInfo?.cncType }} · {{ md.sysInfo?.axes }}轴</span></div>
<div class="metric"><span class="m-label">开机时长</span><span class="m-value mono">{{ md.boottime }}</span></div>
<div class="metric"><span class="m-label">运行时长</span><span class="m-value mono">{{ md.runtime }}</span></div>
<div class="metric"><span class="m-label">切削时长</span><span class="m-value mono">{{ md.cuttime }}</span></div>
<div class="metric"><span class="m-label">循环时间</span><span class="m-value mono">{{ md.cycletime }}</span></div>
</div>
</div>
@ -57,7 +56,8 @@
<div class="detail-card">
<div class="card-title">生产与程序信息</div>
<div class="metric-list">
<div class="metric"><span class="m-label">加工件数</span><span class="m-value text-green" style="font-size:22px;">{{ md.currentProduceCount?.toLocaleString() }}<small> 件</small></span></div>
<div class="metric"><span class="m-label">累计总产量</span><span class="m-value text-green" style="font-size:22px;">{{ cncTotalProduce.toLocaleString() }}<small> 件</small></span></div>
<div class="metric"><span class="m-label">今日产量</span><span class="m-value text-cyan">{{ cncTodayProduce.toLocaleString() }}<small> 件</small></span></div>
<div class="metric"><span class="m-label">当前程序</span><span class="m-value mode-badge">{{ md.programCurrent?.name }}</span></div>
</div>
<div class="code-preview" v-if="md.peratorMessage?.[0]?.data">
@ -92,7 +92,11 @@
<!-- 趋势图 -->
<div class="detail-card chart-card">
<div class="card-title">近7日产量与切削时长趋势</div>
<div class="card-title">近7日产量与累计件数趋势</div>
<div class="chart-sub">
累计总产量:<span class="mono text-cyan">{{ cncTotalProduce.toLocaleString() }}</span> 件
<span class="chart-hint">(折线「累计件数」为近 7 日每日末累计快照)</span>
</div>
<div class="chart-container" ref="chartRef"></div>
</div>
</div>
@ -251,6 +255,9 @@ const md = computed(() => device.value?.machineData || {})
const nc = computed(() => md.value?.CutSystemState?.NcState)
const ds = computed(() => md.value?.CutSystemState?.DeviceState)
const lmd = computed(() => md.value || {})
const cncTotalProduce = computed(() => Number(md.value?.currentProduceCount || 0))
/** CNC 日增量已在 mqttDeviceStore 由累计差分得到:produce[6] 代表今天 */
const cncTodayProduce = computed(() => Number(device.value?.weeklyStats?.produce?.[6] || 0))
const powerPhases = computed(() => {
const s = md.value?.slave1
@ -369,39 +376,103 @@ function updatePowerChart() {
function updateChart() {
if (!chartInstance || !device.value?.weeklyStats) return
const stats = device.value.weeklyStats
const isLaser = device.value.type === 'laser'
const isCnc = device.value.type === 'cnc'
const xLabels = stats.dates || getLast7DaysChartInfo().labels
const cum = Array.isArray(stats.produceCumulative) ? stats.produceCumulative : new Array(7).fill(0)
const option = {
backgroundColor: 'transparent',
tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } },
legend: {
data: [isLaser ? '穿孔数 (次)' : '产量 (件)', isLaser ? '激光时长 (h)' : '切削时长 (h)'],
textStyle: { color: '#a0c0d0' }, top: 0
},
grid: { left: '3%', right: '4%', bottom: '3%', top: '40px', containLabel: true },
xAxis: {
type: 'category', data: stats.dates || getLast7DaysChartInfo().labels,
axisLabel: { color: '#6a8a9a' }, axisLine: { lineStyle: { color: '#1a3a6a' } }
},
yAxis: [
{ type: 'value', name: isLaser ? '次数' : '件', nameTextStyle: { color: '#6a8a9a' }, axisLabel: { color: '#6a8a9a' }, splitLine: { lineStyle: { color: 'rgba(26,58,106,0.3)' } } },
{ type: 'value', name: '小时', nameTextStyle: { color: '#6a8a9a' }, axisLabel: { color: '#6a8a9a' }, splitLine: { show: false } }
],
series: [
{
name: isLaser ? '穿孔数 (次)' : '产量 (件)', type: 'bar', barWidth: '30%',
itemStyle: { color: new echarts.graphic.LinearGradient(0,0,0,1,[{offset:0,color:'#00e5ff'},{offset:1,color:'#0070ff'}]), borderRadius: [4,4,0,0] },
data: stats.produce
let option
if (isCnc) {
option = {
backgroundColor: 'transparent',
tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } },
legend: {
data: ['日产量 (件)', '累计件数'],
textStyle: { color: '#a0c0d0' },
top: 0
},
{
name: isLaser ? '激光时长 (h)' : '切削时长 (h)', type: 'line', yAxisIndex: 1, smooth: true,
symbol: 'circle', symbolSize: 8,
itemStyle: { color: isLaser ? '#ff4444' : '#00e676' },
lineStyle: { width: 3, shadowColor: isLaser ? 'rgba(255,68,68,0.4)' : 'rgba(0,230,118,0.4)', shadowBlur: 10 },
areaStyle: { color: new echarts.graphic.LinearGradient(0,0,0,1,[{offset:0,color:isLaser?'rgba(255,68,68,0.25)':'rgba(0,230,118,0.25)'},{offset:1,color:'transparent'}]) },
data: stats.cuttime
}
]
grid: { left: '3%', right: '8%', bottom: '3%', top: '40px', containLabel: true },
xAxis: {
type: 'category',
data: xLabels,
axisLabel: { color: '#6a8a9a' },
axisLine: { lineStyle: { color: '#1a3a6a' } }
},
yAxis: [
{
type: 'value',
name: '日产量(件)',
nameTextStyle: { color: '#6a8a9a' },
axisLabel: { color: '#6a8a9a' },
splitLine: { lineStyle: { color: 'rgba(26,58,106,0.3)' } }
},
{
type: 'value',
name: '累计(件)',
position: 'right',
alignTicks: true,
nameTextStyle: { color: '#6a8a9a' },
axisLabel: { color: '#6a8a9a' },
splitLine: { show: false }
}
],
series: [
{
name: '日产量 (件)',
type: 'bar',
yAxisIndex: 0,
barWidth: '26%',
itemStyle: {
color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{ offset: 0, color: '#00e5ff' }, { offset: 1, color: '#0070ff' }]),
borderRadius: [4, 4, 0, 0]
},
data: stats.produce
},
{
name: '累计件数',
type: 'line',
yAxisIndex: 1,
smooth: true,
symbol: 'circle',
symbolSize: 7,
itemStyle: { color: '#ffca28' },
lineStyle: { width: 2, type: 'dashed' },
data: cum
}
]
}
} else {
option = {
backgroundColor: 'transparent',
tooltip: { trigger: 'axis', axisPointer: { type: 'cross' } },
legend: {
data: ['穿孔数 (次)', '激光时长 (h)'],
textStyle: { color: '#a0c0d0' }, top: 0
},
grid: { left: '3%', right: '4%', bottom: '3%', top: '40px', containLabel: true },
xAxis: {
type: 'category', data: xLabels,
axisLabel: { color: '#6a8a9a' }, axisLine: { lineStyle: { color: '#1a3a6a' } }
},
yAxis: [
{ type: 'value', name: '次数', nameTextStyle: { color: '#6a8a9a' }, axisLabel: { color: '#6a8a9a' }, splitLine: { lineStyle: { color: 'rgba(26,58,106,0.3)' } } },
{ type: 'value', name: '小时', nameTextStyle: { color: '#6a8a9a' }, axisLabel: { color: '#6a8a9a' }, splitLine: { show: false } }
],
series: [
{
name: '穿孔数 (次)', type: 'bar', barWidth: '30%',
itemStyle: { color: new echarts.graphic.LinearGradient(0,0,0,1,[{offset:0,color:'#00e5ff'},{offset:1,color:'#0070ff'}]), borderRadius: [4,4,0,0] },
data: stats.produce
},
{
name: '激光时长 (h)', type: 'line', yAxisIndex: 1, smooth: true,
symbol: 'circle', symbolSize: 8,
itemStyle: { color: '#ff4444' },
lineStyle: { width: 3, shadowColor: 'rgba(255,68,68,0.4)', shadowBlur: 10 },
areaStyle: { color: new echarts.graphic.LinearGradient(0,0,0,1,[{offset:0,color:'rgba(255,68,68,0.25)'},{offset:1,color:'transparent'}]) },
data: stats.cuttime
}
]
}
}
chartInstance.setOption(option, true)
}
@ -433,7 +504,8 @@ watch(
device.value?.type,
device.value?.machineData?.timestamp,
device.value?.weeklyStats?.produce?.join(','),
device.value?.weeklyStats?.cuttime?.join(',')
device.value?.weeklyStats?.cuttime?.join(','),
device.value?.weeklyStats?.produceCumulative?.join(',')
],
() => {
if (!device.value || device.value.type === 'power') return
@ -553,6 +625,13 @@ onUnmounted(() => {
/* Chart */
.chart-card { flex: 1; min-height: 220px; }
.chart-sub {
font-size: 12px;
color: #6a8a9a;
margin: -4px 0 8px;
padding-left: 8px;
}
.chart-hint { margin-left: 8px; font-size: 10px; color: #4a6a8a; }
.chart-container { width: 100%; height: 100%; min-height: 200px; }
/* Offline */