#include "stationdata.h" #include #include #include #include #include QStringList channelNames() { QStringList names; const char *cpus[2] = {"CPU0", "CPU1"}; const char *suffix = "ABCDEFGHIJKL"; for (int c = 0; c < 2; ++c) { for (int i = 0; i < 12; ++i) { names << QStringLiteral("%1_CH%2") .arg(QString::fromLatin1(cpus[c]), QChar(QLatin1Char(suffix[i]))); } } return names; } StationStore::StationStore(int stationCount, QObject *parent) : QObject(parent) , m_stations(qMax(0, stationCount)) { // 运行时间走表:1s 一拍,只对已点「开始测试」的工位发 infoChanged 让界面重画 m_runtimeTimer.setInterval(1000); connect(&m_runtimeTimer, &QTimer::timeout, this, &StationStore::runtimeTick); m_runtimeTimer.start(); } void StationStore::runtimeTick() { for (int i = 0; i < m_stations.size(); ++i) { const StationData &d = m_stations.at(i); // 已经冻结(ended_unix_ms > 0)或未起跑的工位不用再重画 if (d.backendStartedMs > 0 && d.backendEndedMs <= 0) touchRuntime(i); } } const StationData &StationStore::station(int index) const { static const StationData empty; if (index < 0 || index >= m_stations.size()) return empty; return m_stations.at(index); } void StationStore::initStation(int index, const StationData &data) { if (index < 0 || index >= m_stations.size()) return; m_stations[index] = data; emit channelsChanged(index); emit temperatureChanged(index); } void StationStore::appendLog(int index, const QString &line) { if (index < 0 || index >= m_stations.size() || line.isEmpty()) return; // 只追加,永不裁剪:详情页切走再进来仍是完整历史 m_stations[index].logs.append(line); emit logAppended(index, line); } void StationStore::clearLogs(int index) { if (index < 0 || index >= m_stations.size()) return; m_stations[index].logs.clear(); emit logsCleared(index); } // --------------------------------------------------------------------------- // 异常记录:后端 stage_errors / verdict=fail 落成一条条可读记录 // --------------------------------------------------------------------------- void StationStore::appendAlarm(int index, const QString &stage, const QString &text, const QString &rawKey) { const QString trimmed = text.trimmed(); if (index < 0 || index >= m_stations.size() || trimmed.isEmpty()) return; QVector &list = m_stations[index].alarms; const QString keyTrimmed = rawKey.trimmed(); // 去重 + 多行续行原地升级: // 1. 同一阶段 + 同一内容只留一条; // 2. 若后端 /GNR 坐标在下一拍续行追加到同一条 message(newRawKey 以 old.rawKey + " | " 开头), // 原地更新该条异常描述与时间,避免详情页出现「半条无坐标 + 完整带坐标」重复告警。 for (AlarmInfo &a : list) { if (a.stage != stage) continue; if (a.text == trimmed) return; if (!keyTrimmed.isEmpty() && !a.rawKey.isEmpty() && (keyTrimmed == a.rawKey || keyTrimmed.startsWith(a.rawKey + QLatin1String(" | ")) || keyTrimmed.startsWith(a.rawKey + QLatin1Char('\n')) || keyTrimmed.startsWith(a.rawKey + QLatin1String("\r\n")))) { a.text = trimmed; a.rawKey = keyTrimmed; a.time = QDateTime::currentDateTime(); emit alarmsChanged(index); return; } } list.append(AlarmInfo(QDateTime::currentDateTime(), stage, trimmed, keyTrimmed)); emit alarmsChanged(index); } void StationStore::clearAlarms(int index) { if (index < 0 || index >= m_stations.size()) return; if (m_stations[index].alarms.isEmpty()) return; m_stations[index].alarms.clear(); emit alarmsChanged(index); } // --------------------------------------------------------------------------- // 串口掉线:把这一工位的温度全部退回 -1(界面显示 --℃) // --------------------------------------------------------------------------- // 状态灯不动(PASS/FAIL/IDLE 只认后端),曲线历史也不清(掉线前那段仍要看得到); // 只把"当前温度"作废——否则卡片会一边写「未连接」一边挂着掉线前的 29.9℃。 // --------------------------------------------------------------------------- void StationStore::clearTemperatures(int index) { if (index < 0 || index >= m_stations.size()) return; StationData &d = m_stations[index]; bool tempChanged = false; bool chChanged = false; if (d.currentTemp >= 0.0) { d.currentTemp = -1.0; tempChanged = true; } for (ChannelInfo &ch : d.channels) { ch.mcuTempMs = 0; ch.mcuTempC = -1; if (ch.tempFromMcu) { ch.tempFromMcu = false; const double fallback = (ch.backendPresent && ch.hasBackendTemp) ? ch.backendTemp : -1.0; if (qAbs(ch.temperature - fallback) > 0.05 || (ch.temperature < 0.0) != (fallback < 0.0)) { ch.temperature = fallback; chChanged = true; } } } if (tempChanged) emit temperatureChanged(index); if (chChanged) emit channelsChanged(index); } void StationStore::setCurrentTemp(int index, double temperature) { if (index < 0 || index >= m_stations.size()) return; StationData &d = m_stations[index]; d.currentTemp = temperature; // 温控曲线:只保留最近 200 点,旧点从头部挤出去;start 记下队首的全局序号, // 曲线横轴因此跟着数据一起向前滚动(可见宽度恒为 200 点) d.tempHistory.append(temperature); while (d.tempHistory.size() > kTempHistorySize) d.tempHistory.removeFirst(); emit temperatureChanged(index); } void StationStore::setPort(int index, const QString &port) { if (index < 0 || index >= m_stations.size()) return; if (m_stations.at(index).port == port) return; m_stations[index].port = port; emit infoChanged(index); } static qint64 parseUnixMsValue(const QJsonValue &v) { if (v.isString()) { bool ok = false; const qint64 n = v.toString().toLongLong(&ok); return ok ? n : 0; } if (v.isDouble()) return static_cast(v.toDouble()); return 0; } void StationStore::resetBackendTiming(int index) { if (index < 0 || index >= m_stations.size()) return; StationData &d = m_stations[index]; const bool hadTiming = (d.backendStartedMs != 0 || d.backendEndedMs != 0 || d.backendElapsedMs != 0); d.backendStartedMs = 0; d.backendEndedMs = 0; d.backendElapsedMs = 0; d.syncClock.invalidate(); if (hadTiming) emit infoChanged(index); } void StationStore::applyBackendTiming(int index, const QJsonObject &obj) { if (index < 0 || index >= m_stations.size()) return; if (!obj.contains(QStringLiteral("started_unix_ms")) && !obj.contains(QStringLiteral("elapsed_ms"))) return; StationData &d = m_stations[index]; const qint64 started = parseUnixMsValue(obj.value(QStringLiteral("started_unix_ms"))); const qint64 ended = parseUnixMsValue(obj.value(QStringLiteral("ended_unix_ms"))); const qint64 elapsed = obj.contains(QStringLiteral("elapsed_ms")) ? qMax(0, parseUnixMsValue(obj.value(QStringLiteral("elapsed_ms")))) : (started > 0 && ended > 0 ? qMax(0, ended - started) : -1); if (started <= 0) { resetBackendTiming(index); return; } const qint64 prevShown = runtimeMs(index); const qint64 prevStarted = d.backendStartedMs; const qint64 prevEnded = d.backendEndedMs; if (ended > 0) { const qint64 exactMs = (elapsed >= 0) ? elapsed : qMax(0, ended - started); d.backendStartedMs = started; d.backendEndedMs = ended; d.backendElapsedMs = exactMs; d.syncClock.invalidate(); } else { qint64 newElapsed = (elapsed >= 0) ? elapsed : 0; if (prevStarted == started && prevEnded <= 0 && prevShown > newElapsed && (prevShown - newElapsed) < 2000) { newElapsed = prevShown; } d.backendStartedMs = started; d.backendEndedMs = 0; d.backendElapsedMs = newElapsed; d.syncClock.start(); } const qint64 nowShown = runtimeMs(index); if (d.backendStartedMs != prevStarted || d.backendEndedMs != prevEnded || (nowShown / 1000) != (prevShown / 1000)) { emit infoChanged(index); } } void StationStore::setTaskProgress(int index, int done, int current, int failed) { if (index < 0 || index >= m_stations.size()) return; if (m_stations.at(index).taskDone == done && m_stations.at(index).taskCurrent == current && m_stations.at(index).taskFailed == failed) return; m_stations[index].taskDone = done; m_stations[index].taskCurrent = current; m_stations[index].taskFailed = failed; emit infoChanged(index); } void StationStore::resetChannelResults(int index) { if (index < 0 || index >= m_stations.size()) return; QVector &channels = m_stations[index].channels; for (ChannelInfo &ch : channels) { ch.state = ChannelInfo::Idle; ch.temperature = -1.0; ch.hasBackendTemp = false; ch.backendTemp = -1.0; ch.mcuTempMs = 0; ch.mcuTempC = -1; ch.tempFromMcu = false; ch.history.clear(); ch.historyStart = 0; } StationData &d = m_stations[index]; d.stageCompletions = QJsonObject(); d.verdict.clear(); d.verdictScope.clear(); d.taskStatus.clear(); d.firmwareFamily.clear(); d.mtResult = QJsonObject(); d.awaitingDimmReplacement = false; // 主界面 24 格、详情页 24 格及详情曲线共用 channelsChanged,统一刷新。 emit channelsChanged(index); emit infoChanged(index); } qint64 StationStore::runtimeMs(int index) const { if (index < 0 || index >= m_stations.size()) return -1; const StationData &d = m_stations.at(index); if (d.backendStartedMs <= 0) return -1; // 空闲 / 尚未触发测试上电 if (d.backendEndedMs > 0) return d.backendElapsedMs; // 终态:停在后端冻结时长 return d.backendElapsedMs + (d.syncClock.isValid() ? qBound(0, d.syncClock.elapsed(), 1500) : 0); } void StationStore::touchRuntime(int index) { if (index < 0 || index >= m_stations.size()) return; emit infoChanged(index); } void StationStore::applyCtTemperatures(int index, const QByteArray &raw8) { if (index < 0 || index >= m_stations.size()) return; // 本板 CT7428 八路:跳过无效路(0xFF)后取算术平均,当作这一工位的「腔体温度」。 // 八路全无效(设备刚上电 / CPLD 失联时整组回 0xFF)→ 写 -1,界面显示 --℃。 double sum = 0.0; int valid = 0; for (int i = 0; i < raw8.size(); ++i) { const quint8 b = static_cast(raw8.at(i)); if (b == 0xFF) // slt::kTempInvalid continue; sum += b; ++valid; } setCurrentTemp(index, valid > 0 ? sum / valid : -1.0); } // --------------------------------------------------------------------------- // 24 路 BMC 温度:只写温度,**绝不碰状态** // --------------------------------------------------------------------------- // 本机串口(MCU)只提供两类温度:CT7428 的腔体温度 + 0x82 的 24 路温度。 // 24 通道的 PASS / FAIL / IDLE 一律由后端 state 推送决定(applyBackendState), // 读到一个有效温度字节只说明"这一路有读数",不代表测试通过——早先在这里 // 顺手把状态置成 Pass,导致"接了 MCU 就冒出几路 PASS",与后端结论打架。 // --------------------------------------------------------------------------- void StationStore::applyChannelTemperatures(int index, const QByteArray &raw24) { if (index < 0 || index >= m_stations.size()) return; StationData &d = m_stations[index]; QVector &channels = d.channels; if (raw24.size() != channels.size()) return; // 应答长度不对(比如还夹着别的 CMD)就整帧丢 const qint64 now = QDateTime::currentMSecsSinceEpoch(); bool changed = false; bool curveChanged = false; for (int i = 0; i < channels.size(); ++i) { const quint8 b = static_cast(raw24.at(i)); ChannelInfo &ch = channels[i]; if (b == 0xFF) { // MCU 该路无读数(0xFF):清除新鲜 MCU 标记,优先回落到后端 temperature_c(如有) ch.mcuTempMs = 0; ch.mcuTempC = -1; if (ch.tempFromMcu) { ch.tempFromMcu = false; const double fallback = (ch.backendPresent && ch.hasBackendTemp) ? ch.backendTemp : -1.0; if (qAbs(ch.temperature - fallback) > 0.05 || (ch.temperature < 0.0) != (fallback < 0.0)) { ch.temperature = fallback; changed = true; } } continue; } // 后端明确判定该槽位为空槽(present=false)时,即使 MCU 有读数也显示 --℃ if (d.backendHasChannels && !ch.backendPresent) { ch.mcuTempMs = 0; ch.mcuTempC = -1; ch.tempFromMcu = false; if (ch.temperature >= 0.0) { ch.temperature = -1.0; changed = true; } continue; } ch.mcuTempC = static_cast(b); ch.mcuTempMs = now; ch.tempFromMcu = true; const double t = static_cast(b); if (qAbs(ch.temperature - t) > 0.05 || ch.temperature < 0.0) { ch.temperature = t; changed = true; } // 每个轮询周期给这个通道落一个点:详情页曲线一路一段连续曲线靠它撑起来 ch.history.append(t); while (ch.history.size() > kTempHistorySize) { ch.history.removeFirst(); ++ch.historyStart; } curveChanged = true; } // 24 路逐格改会连发 24 次信号,这里合并成一次刷,避免界面被刷爆 if (changed || curveChanged) emit channelsChanged(index); } // --------------------------------------------------------------------------- // 后端状态落库:state 推送里单个工位对象 → 24 通道状态灯 / 本轮启动时间 // 24 通道内存条温度也从这里取(channels[i].temperature_c,float,单位 ℃), // 取代原本 MCU 串口 0x82 的 24 字节来源(见 MainWindow::setupCom 的注释)。 // 腔体温度(currentTemp)仍由本地 MCU 温控串口采集,后端不下发该字段。 // --------------------------------------------------------------------------- void StationStore::applyBackendState(int index, const QJsonObject &station) { if (index < 0 || index >= m_stations.size()) return; StationData &d = m_stations[index]; bool channelsChangedFlag = false; // 0.8.1.19:boot_id 是轮次边界。即使本机不是点击 StartTest 的客户端, // 只要收到新 boot_id,也必须先清掉上一轮 24 路状态、温度与曲线。 const QString incomingBootId = station.value(QStringLiteral("boot_id")).toString(); if (incomingBootId.isEmpty() && !d.bootId.isEmpty()) { d.bootId.clear(); } else if (!incomingBootId.isEmpty() && incomingBootId != d.bootId) { d.bootId = incomingBootId; for (ChannelInfo &ch : d.channels) { ch.state = ChannelInfo::Idle; ch.temperature = -1.0; ch.hasBackendTemp = false; ch.backendTemp = -1.0; ch.mcuTempMs = 0; ch.mcuTempC = -1; ch.tempFromMcu = false; ch.history.clear(); ch.historyStart = 0; } d.stageCompletions = QJsonObject(); d.verdict.clear(); d.verdictScope.clear(); d.firmwareFamily.clear(); d.taskStatus.clear(); d.mtResult = QJsonObject(); d.awaitingDimmReplacement = false; channelsChangedFlag = true; } bool infoDirty = false; // 后端整体阶段(boot / uip / training / rmt / amt / post / uefi / mt / powered_off): // 卡片标题右侧显示当前处于哪个阶段;空或 unknown 时界面不显示。 const QString phase = station.value(QStringLiteral("phase")).toString(); if (d.phase != phase) { d.phase = phase; infoDirty = true; } // 0.8.1.19:工位级结论、固件族与七阶段完成状态独立保存。 const QString verdict = station.value(QStringLiteral("verdict")).toString(); const QString verdictScope = station.value(QStringLiteral("verdict_scope")).toString(); const QString firmwareFamily = station.value(QStringLiteral("firmware_family")).toString(); const QJsonObject stageCompletions = station.value(QStringLiteral("stage_completions")).toObject(); const QJsonObject mtResult = station.value(QStringLiteral("mt_result")).toObject(); if (d.verdict != verdict) { d.verdict = verdict; infoDirty = true; } if (d.verdictScope != verdictScope) { d.verdictScope = verdictScope; infoDirty = true; } if (d.firmwareFamily != firmwareFamily) { d.firmwareFamily = firmwareFamily; infoDirty = true; } if (d.stageCompletions != stageCompletions) { d.stageCompletions = stageCompletions; infoDirty = true; } if (d.mtResult != mtResult) { d.mtResult = mtResult; infoDirty = true; } // 手册 9.2 新增字段:只在值变化时刷新界面,state 每秒一包,不做无谓重绘。 // 旧版后端没有这些字段:consoleOnline 留 -1(老判据),其余保持默认空值。 const QJsonValue consoleVal = station.value(QStringLiteral("console_online")); const int consoleOnline = consoleVal.isBool() ? (consoleVal.toBool() ? 1 : 0) : -1; if (d.consoleOnline != consoleOnline) { d.consoleOnline = consoleOnline; infoDirty = true; } const bool awaiting = station.value(QStringLiteral("awaiting_dimm_replacement")).toBool(); if (d.awaitingDimmReplacement != awaiting) { d.awaitingDimmReplacement = awaiting; infoDirty = true; } const QString taskStatus = station.value(QStringLiteral("task_status")).toString(); if (d.taskStatus != taskStatus) { d.taskStatus = taskStatus; infoDirty = true; } const QJsonObject taskBatch = station.value(QStringLiteral("task_batch")).toObject(); if (d.taskBatch != taskBatch) { d.taskBatch = taskBatch; infoDirty = true; } const QJsonArray taskHistory = station.value(QStringLiteral("task_history")).toArray(); if (d.taskHistory != taskHistory) { d.taskHistory = taskHistory; infoDirty = true; } // BIOS 控制台连接状态(online / connecting / offline / error): // 加 2 拍防抖并与 console_online 联动避免瞬时闪断误报「控制台离线」。 const QString rawConn = station.value(QStringLiteral("connection")).toString(); QString effectiveConn = rawConn; if (d.consoleOnline == 1 && rawConn == QLatin1String("offline")) { effectiveConn = QStringLiteral("online"); d.offlineDebounceTicks = 0; } else if (rawConn == QLatin1String("offline") && d.connection == QLatin1String("online") && d.consoleOnline != 0) { d.offlineDebounceTicks++; if (d.offlineDebounceTicks < 2) effectiveConn = d.connection; } else { d.offlineDebounceTicks = 0; } if (d.connection != effectiveConn) { d.connection = effectiveConn; infoDirty = true; } if (infoDirty) emit infoChanged(index); // 直接同步后端权威时间(started_unix_ms / ended_unix_ms / elapsed_ms); // 不在前端按阶段切换重置,也不以本机墙钟计算。 applyBackendTiming(index, station); const qint64 now = QDateTime::currentMSecsSinceEpoch(); const QJsonArray channels = station.value(QStringLiteral("channels")).toArray(); if (!channels.isEmpty()) d.backendHasChannels = true; for (const QJsonValue &v : channels) { const QJsonObject ch = v.toObject(); // 按 name(CPU0_CHA)匹配;匹配不到就退回 cpu*12 + channel 的位置 const QString name = ch.value(QStringLiteral("name")).toString(); int slot = -1; for (int i = 0; i < d.channels.size(); ++i) { if (d.channels.at(i).name == name) { slot = i; break; } } if (slot < 0) { const int cpu = ch.value(QStringLiteral("cpu")).toInt(0); bool okChan = false; const int chan = ch.value(QStringLiteral("channel")).toString().toInt(&okChan); if (okChan && chan >= 0) slot = cpu * 12 + chan; } if (slot < 0 || slot >= d.channels.size()) continue; // present 是后端提供的真实 DIMM 在位信息,协议要求必须是 JSON bool。 // 缺失、null 或类型错误一律按「未确认在位」处理,禁止猜测为 true。 const QJsonValue presentVal = ch.value(QStringLiteral("present")); const bool isPresent = presentVal.isBool() && presentVal.toBool(); const QString chVerdict = ch.value(QStringLiteral("state")).toString(); const ChannelInfo::State state = (chVerdict == QStringLiteral("fail")) ? ChannelInfo::Fail : (!isPresent ? ChannelInfo::Idle : ((chVerdict == QStringLiteral("pass")) ? ChannelInfo::Pass : ChannelInfo::Idle)); const QJsonValue tempVal = ch.value(QStringLiteral("temperature_c")); double temp = -1.0; bool tempValid = false; if (isPresent && tempVal.isDouble()) { temp = tempVal.toDouble(); tempValid = true; } ChannelInfo &info = d.channels[slot]; info.backendPresent = isPresent; info.hasBackendTemp = tempValid; info.backendTemp = tempValid ? temp : -1.0; if (info.state != state) { info.state = state; channelsChangedFlag = true; } if (!isPresent) { // 空槽不保留上一轮温度和曲线,避免插拔内存后仍显示旧数据。 info.mcuTempMs = 0; info.mcuTempC = -1; info.tempFromMcu = false; if (info.temperature >= 0.0 || !info.history.isEmpty() || info.historyStart != 0) { info.temperature = -1.0; info.history.clear(); info.historyStart = 0; channelsChangedFlag = true; } continue; } const bool mcuFresh = (info.mcuTempMs > 0 && info.mcuTempC >= 0 && (now - info.mcuTempMs) < 3000); if (mcuFresh) { // 3 秒内有有效 MCU 0x82 数据时由 MCU 驱动温度显示与 1Hz 曲线,不与后端重复落点或清零 info.tempFromMcu = true; } else if (tempValid) { info.tempFromMcu = false; // 值变化(或上一轮是 --℃)才改写,避免无谓刷新 if (qAbs(info.temperature - temp) > 0.05 || info.temperature < 0.0) { info.temperature = temp; channelsChangedFlag = true; } // 每个推送周期落一个点,详情页曲线靠这个 history 撑起来 info.history.append(temp); while (info.history.size() > kTempHistorySize) { info.history.removeFirst(); ++info.historyStart; } channelsChangedFlag = true; } else { info.tempFromMcu = false; if (info.temperature >= 0.0) { // 两路均无有效温度:退回 -1(显示 --℃),但不清历史曲线点 info.temperature = -1.0; channelsChangedFlag = true; } } } if (channelsChangedFlag) emit channelsChanged(index); } void StationStore::applyTaskComplete(int index, const QJsonObject &result) { if (index < 0 || index >= m_stations.size()) return; StationData &d = m_stations[index]; const QString eventBootId = result.value(QStringLiteral("boot_id")).toString(); // task_complete 的轮次归属已由 BackendLink 校验;这里接收已确认的终态事件。 bool changed = false; if (!eventBootId.isEmpty() && eventBootId != d.bootId) { // BackendLink 已按 boot_id 过滤旧事件;若终态推送先于 state 到达,先建立新轮次边界。 d.bootId = eventBootId; for (ChannelInfo &ch : d.channels) { ch.state = ChannelInfo::Idle; ch.temperature = -1.0; ch.hasBackendTemp = false; ch.backendTemp = -1.0; ch.mcuTempMs = 0; ch.mcuTempC = -1; ch.tempFromMcu = false; ch.history.clear(); ch.historyStart = 0; } d.stageCompletions = QJsonObject(); d.verdict.clear(); d.verdictScope.clear(); d.awaitingDimmReplacement = false; changed = true; emit channelsChanged(index); } const QString taskStatus = result.value(QStringLiteral("task_status")).toString(); const QString verdict = result.value(QStringLiteral("verdict")).toString(); const QString verdictScope = result.value(QStringLiteral("verdict_scope")).toString(); const QJsonObject completions = result.value(QStringLiteral("stage_completions")).toObject(); if (result.contains(QStringLiteral("awaiting_dimm_replacement"))) { const bool awaiting = result.value(QStringLiteral("awaiting_dimm_replacement")).toBool(); if (d.awaitingDimmReplacement != awaiting) { d.awaitingDimmReplacement = awaiting; changed = true; } } if (!taskStatus.isEmpty() && d.taskStatus != taskStatus) { d.taskStatus = taskStatus; changed = true; } if (!verdict.isEmpty() && d.verdict != verdict) { d.verdict = verdict; changed = true; } if (!verdictScope.isEmpty() && d.verdictScope != verdictScope) { d.verdictScope = verdictScope; changed = true; } if (!completions.isEmpty() && d.stageCompletions != completions) { d.stageCompletions = completions; changed = true; } applyBackendTiming(index, result); if (changed) emit infoChanged(index); }