stationdata.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729
  1. #include "stationdata.h"
  2. #include <QChar>
  3. #include <QJsonArray>
  4. #include <QJsonObject>
  5. #include <QJsonValue>
  6. #include <QtGlobal>
  7. QStringList channelNames()
  8. {
  9. QStringList names;
  10. const char *cpus[2] = {"CPU0", "CPU1"};
  11. const char *suffix = "ABCDEFGHIJKL";
  12. for (int c = 0; c < 2; ++c) {
  13. for (int i = 0; i < 12; ++i) {
  14. names << QStringLiteral("%1_CH%2")
  15. .arg(QString::fromLatin1(cpus[c]), QChar(QLatin1Char(suffix[i])));
  16. }
  17. }
  18. return names;
  19. }
  20. StationStore::StationStore(int stationCount, QObject *parent)
  21. : QObject(parent)
  22. , m_stations(qMax(0, stationCount))
  23. {
  24. // 运行时间走表:1s 一拍,只对已点「开始测试」的工位发 infoChanged 让界面重画
  25. m_runtimeTimer.setInterval(1000);
  26. connect(&m_runtimeTimer, &QTimer::timeout, this, &StationStore::runtimeTick);
  27. m_runtimeTimer.start();
  28. }
  29. void StationStore::runtimeTick()
  30. {
  31. for (int i = 0; i < m_stations.size(); ++i) {
  32. const StationData &d = m_stations.at(i);
  33. // 已经冻结(ended_unix_ms > 0)或未起跑的工位不用再重画
  34. if (d.backendStartedMs > 0 && d.backendEndedMs <= 0)
  35. touchRuntime(i);
  36. }
  37. }
  38. const StationData &StationStore::station(int index) const
  39. {
  40. static const StationData empty;
  41. if (index < 0 || index >= m_stations.size())
  42. return empty;
  43. return m_stations.at(index);
  44. }
  45. void StationStore::initStation(int index, const StationData &data)
  46. {
  47. if (index < 0 || index >= m_stations.size())
  48. return;
  49. m_stations[index] = data;
  50. emit channelsChanged(index);
  51. emit temperatureChanged(index);
  52. }
  53. void StationStore::appendLog(int index, const QString &line)
  54. {
  55. if (index < 0 || index >= m_stations.size() || line.isEmpty())
  56. return;
  57. // 只追加,永不裁剪:详情页切走再进来仍是完整历史
  58. m_stations[index].logs.append(line);
  59. emit logAppended(index, line);
  60. }
  61. void StationStore::clearLogs(int index)
  62. {
  63. if (index < 0 || index >= m_stations.size())
  64. return;
  65. m_stations[index].logs.clear();
  66. emit logsCleared(index);
  67. }
  68. // ---------------------------------------------------------------------------
  69. // 异常记录:后端 stage_errors / verdict=fail 落成一条条可读记录
  70. // ---------------------------------------------------------------------------
  71. void StationStore::appendAlarm(int index, const QString &stage, const QString &text,
  72. const QString &rawKey)
  73. {
  74. const QString trimmed = text.trimmed();
  75. if (index < 0 || index >= m_stations.size() || trimmed.isEmpty())
  76. return;
  77. QVector<AlarmInfo> &list = m_stations[index].alarms;
  78. const QString keyTrimmed = rawKey.trimmed();
  79. // 去重 + 多行续行原地升级:
  80. // 1. 同一阶段 + 同一内容只留一条;
  81. // 2. 若后端 /GNR 坐标在下一拍续行追加到同一条 message(newRawKey 以 old.rawKey + " | " 开头),
  82. // 原地更新该条异常描述与时间,避免详情页出现「半条无坐标 + 完整带坐标」重复告警。
  83. for (AlarmInfo &a : list) {
  84. if (a.stage != stage)
  85. continue;
  86. if (a.text == trimmed)
  87. return;
  88. if (!keyTrimmed.isEmpty() && !a.rawKey.isEmpty()
  89. && (keyTrimmed == a.rawKey
  90. || keyTrimmed.startsWith(a.rawKey + QLatin1String(" | "))
  91. || keyTrimmed.startsWith(a.rawKey + QLatin1Char('\n'))
  92. || keyTrimmed.startsWith(a.rawKey + QLatin1String("\r\n")))) {
  93. a.text = trimmed;
  94. a.rawKey = keyTrimmed;
  95. a.time = QDateTime::currentDateTime();
  96. emit alarmsChanged(index);
  97. return;
  98. }
  99. }
  100. list.append(AlarmInfo(QDateTime::currentDateTime(), stage, trimmed, keyTrimmed));
  101. emit alarmsChanged(index);
  102. }
  103. void StationStore::clearAlarms(int index)
  104. {
  105. if (index < 0 || index >= m_stations.size())
  106. return;
  107. if (m_stations[index].alarms.isEmpty())
  108. return;
  109. m_stations[index].alarms.clear();
  110. emit alarmsChanged(index);
  111. }
  112. // ---------------------------------------------------------------------------
  113. // 串口掉线:把这一工位的温度全部退回 -1(界面显示 --℃)
  114. // ---------------------------------------------------------------------------
  115. // 状态灯不动(PASS/FAIL/IDLE 只认后端),曲线历史也不清(掉线前那段仍要看得到);
  116. // 只把"当前温度"作废——否则卡片会一边写「未连接」一边挂着掉线前的 29.9℃。
  117. // ---------------------------------------------------------------------------
  118. void StationStore::clearTemperatures(int index)
  119. {
  120. if (index < 0 || index >= m_stations.size())
  121. return;
  122. StationData &d = m_stations[index];
  123. bool tempChanged = false;
  124. bool chChanged = false;
  125. if (d.currentTemp >= 0.0) {
  126. d.currentTemp = -1.0;
  127. tempChanged = true;
  128. }
  129. for (ChannelInfo &ch : d.channels) {
  130. ch.mcuTempMs = 0;
  131. ch.mcuTempC = -1;
  132. if (ch.tempFromMcu) {
  133. ch.tempFromMcu = false;
  134. const double fallback = (ch.backendPresent && ch.hasBackendTemp) ? ch.backendTemp : -1.0;
  135. if (qAbs(ch.temperature - fallback) > 0.05 || (ch.temperature < 0.0) != (fallback < 0.0)) {
  136. ch.temperature = fallback;
  137. chChanged = true;
  138. }
  139. }
  140. }
  141. if (tempChanged)
  142. emit temperatureChanged(index);
  143. if (chChanged)
  144. emit channelsChanged(index);
  145. }
  146. void StationStore::setCurrentTemp(int index, double temperature)
  147. {
  148. if (index < 0 || index >= m_stations.size())
  149. return;
  150. StationData &d = m_stations[index];
  151. d.currentTemp = temperature;
  152. // 温控曲线:只保留最近 200 点,旧点从头部挤出去;start 记下队首的全局序号,
  153. // 曲线横轴因此跟着数据一起向前滚动(可见宽度恒为 200 点)
  154. d.tempHistory.append(temperature);
  155. while (d.tempHistory.size() > kTempHistorySize)
  156. d.tempHistory.removeFirst();
  157. emit temperatureChanged(index);
  158. }
  159. void StationStore::setPort(int index, const QString &port)
  160. {
  161. if (index < 0 || index >= m_stations.size())
  162. return;
  163. if (m_stations.at(index).port == port)
  164. return;
  165. m_stations[index].port = port;
  166. emit infoChanged(index);
  167. }
  168. static qint64 parseUnixMsValue(const QJsonValue &v)
  169. {
  170. if (v.isString()) {
  171. bool ok = false;
  172. const qint64 n = v.toString().toLongLong(&ok);
  173. return ok ? n : 0;
  174. }
  175. if (v.isDouble())
  176. return static_cast<qint64>(v.toDouble());
  177. return 0;
  178. }
  179. void StationStore::resetBackendTiming(int index)
  180. {
  181. if (index < 0 || index >= m_stations.size())
  182. return;
  183. StationData &d = m_stations[index];
  184. const bool hadTiming = (d.backendStartedMs != 0 || d.backendEndedMs != 0 || d.backendElapsedMs != 0);
  185. d.backendStartedMs = 0;
  186. d.backendEndedMs = 0;
  187. d.backendElapsedMs = 0;
  188. d.syncClock.invalidate();
  189. if (hadTiming)
  190. emit infoChanged(index);
  191. }
  192. void StationStore::applyBackendTiming(int index, const QJsonObject &obj)
  193. {
  194. if (index < 0 || index >= m_stations.size())
  195. return;
  196. if (!obj.contains(QStringLiteral("started_unix_ms"))
  197. && !obj.contains(QStringLiteral("elapsed_ms")))
  198. return;
  199. StationData &d = m_stations[index];
  200. const qint64 started = parseUnixMsValue(obj.value(QStringLiteral("started_unix_ms")));
  201. const qint64 ended = parseUnixMsValue(obj.value(QStringLiteral("ended_unix_ms")));
  202. const qint64 elapsed = obj.contains(QStringLiteral("elapsed_ms"))
  203. ? qMax<qint64>(0, parseUnixMsValue(obj.value(QStringLiteral("elapsed_ms"))))
  204. : (started > 0 && ended > 0 ? qMax<qint64>(0, ended - started) : -1);
  205. if (started <= 0) {
  206. resetBackendTiming(index);
  207. return;
  208. }
  209. const qint64 prevShown = runtimeMs(index);
  210. const qint64 prevStarted = d.backendStartedMs;
  211. const qint64 prevEnded = d.backendEndedMs;
  212. if (ended > 0) {
  213. const qint64 exactMs = (elapsed >= 0) ? elapsed : qMax<qint64>(0, ended - started);
  214. d.backendStartedMs = started;
  215. d.backendEndedMs = ended;
  216. d.backendElapsedMs = exactMs;
  217. d.syncClock.invalidate();
  218. } else {
  219. qint64 newElapsed = (elapsed >= 0) ? elapsed : 0;
  220. if (prevStarted == started && prevEnded <= 0 && prevShown > newElapsed
  221. && (prevShown - newElapsed) < 2000) {
  222. newElapsed = prevShown;
  223. }
  224. d.backendStartedMs = started;
  225. d.backendEndedMs = 0;
  226. d.backendElapsedMs = newElapsed;
  227. d.syncClock.start();
  228. }
  229. const qint64 nowShown = runtimeMs(index);
  230. if (d.backendStartedMs != prevStarted
  231. || d.backendEndedMs != prevEnded
  232. || (nowShown / 1000) != (prevShown / 1000)) {
  233. emit infoChanged(index);
  234. }
  235. }
  236. void StationStore::setTaskProgress(int index, int done, int current, int failed)
  237. {
  238. if (index < 0 || index >= m_stations.size())
  239. return;
  240. if (m_stations.at(index).taskDone == done && m_stations.at(index).taskCurrent == current
  241. && m_stations.at(index).taskFailed == failed)
  242. return;
  243. m_stations[index].taskDone = done;
  244. m_stations[index].taskCurrent = current;
  245. m_stations[index].taskFailed = failed;
  246. emit infoChanged(index);
  247. }
  248. void StationStore::resetChannelResults(int index)
  249. {
  250. if (index < 0 || index >= m_stations.size())
  251. return;
  252. QVector<ChannelInfo> &channels = m_stations[index].channels;
  253. for (ChannelInfo &ch : channels) {
  254. ch.state = ChannelInfo::Idle;
  255. ch.temperature = -1.0;
  256. ch.hasBackendTemp = false;
  257. ch.backendTemp = -1.0;
  258. ch.mcuTempMs = 0;
  259. ch.mcuTempC = -1;
  260. ch.tempFromMcu = false;
  261. ch.history.clear();
  262. ch.historyStart = 0;
  263. }
  264. StationData &d = m_stations[index];
  265. d.stageCompletions = QJsonObject();
  266. d.verdict.clear();
  267. d.verdictScope.clear();
  268. d.taskStatus.clear();
  269. d.firmwareFamily.clear();
  270. d.mtResult = QJsonObject();
  271. d.awaitingDimmReplacement = false;
  272. // 主界面 24 格、详情页 24 格及详情曲线共用 channelsChanged,统一刷新。
  273. emit channelsChanged(index);
  274. emit infoChanged(index);
  275. }
  276. qint64 StationStore::runtimeMs(int index) const
  277. {
  278. if (index < 0 || index >= m_stations.size())
  279. return -1;
  280. const StationData &d = m_stations.at(index);
  281. if (d.backendStartedMs <= 0)
  282. return -1; // 空闲 / 尚未触发测试上电
  283. if (d.backendEndedMs > 0)
  284. return d.backendElapsedMs; // 终态:停在后端冻结时长
  285. return d.backendElapsedMs + (d.syncClock.isValid() ? qBound<qint64>(0, d.syncClock.elapsed(), 1500) : 0);
  286. }
  287. void StationStore::touchRuntime(int index)
  288. {
  289. if (index < 0 || index >= m_stations.size())
  290. return;
  291. emit infoChanged(index);
  292. }
  293. void StationStore::applyCtTemperatures(int index, const QByteArray &raw8)
  294. {
  295. if (index < 0 || index >= m_stations.size())
  296. return;
  297. // 本板 CT7428 八路:跳过无效路(0xFF)后取算术平均,当作这一工位的「腔体温度」。
  298. // 八路全无效(设备刚上电 / CPLD 失联时整组回 0xFF)→ 写 -1,界面显示 --℃。
  299. double sum = 0.0;
  300. int valid = 0;
  301. for (int i = 0; i < raw8.size(); ++i) {
  302. const quint8 b = static_cast<quint8>(raw8.at(i));
  303. if (b == 0xFF) // slt::kTempInvalid
  304. continue;
  305. sum += b;
  306. ++valid;
  307. }
  308. setCurrentTemp(index, valid > 0 ? sum / valid : -1.0);
  309. }
  310. // ---------------------------------------------------------------------------
  311. // 24 路 BMC 温度:只写温度,**绝不碰状态**
  312. // ---------------------------------------------------------------------------
  313. // 本机串口(MCU)只提供两类温度:CT7428 的腔体温度 + 0x82 的 24 路温度。
  314. // 24 通道的 PASS / FAIL / IDLE 一律由后端 state 推送决定(applyBackendState),
  315. // 读到一个有效温度字节只说明"这一路有读数",不代表测试通过——早先在这里
  316. // 顺手把状态置成 Pass,导致"接了 MCU 就冒出几路 PASS",与后端结论打架。
  317. // ---------------------------------------------------------------------------
  318. void StationStore::applyChannelTemperatures(int index, const QByteArray &raw24)
  319. {
  320. if (index < 0 || index >= m_stations.size())
  321. return;
  322. StationData &d = m_stations[index];
  323. QVector<ChannelInfo> &channels = d.channels;
  324. if (raw24.size() != channels.size())
  325. return; // 应答长度不对(比如还夹着别的 CMD)就整帧丢
  326. const qint64 now = QDateTime::currentMSecsSinceEpoch();
  327. bool changed = false;
  328. bool curveChanged = false;
  329. for (int i = 0; i < channels.size(); ++i) {
  330. const quint8 b = static_cast<quint8>(raw24.at(i));
  331. ChannelInfo &ch = channels[i];
  332. if (b == 0xFF) {
  333. // MCU 该路无读数(0xFF):清除新鲜 MCU 标记,优先回落到后端 temperature_c(如有)
  334. ch.mcuTempMs = 0;
  335. ch.mcuTempC = -1;
  336. if (ch.tempFromMcu) {
  337. ch.tempFromMcu = false;
  338. const double fallback = (ch.backendPresent && ch.hasBackendTemp) ? ch.backendTemp : -1.0;
  339. if (qAbs(ch.temperature - fallback) > 0.05 || (ch.temperature < 0.0) != (fallback < 0.0)) {
  340. ch.temperature = fallback;
  341. changed = true;
  342. }
  343. }
  344. continue;
  345. }
  346. // 后端明确判定该槽位为空槽(present=false)时,即使 MCU 有读数也显示 --℃
  347. if (d.backendHasChannels && !ch.backendPresent) {
  348. ch.mcuTempMs = 0;
  349. ch.mcuTempC = -1;
  350. ch.tempFromMcu = false;
  351. if (ch.temperature >= 0.0) {
  352. ch.temperature = -1.0;
  353. changed = true;
  354. }
  355. continue;
  356. }
  357. ch.mcuTempC = static_cast<int>(b);
  358. ch.mcuTempMs = now;
  359. ch.tempFromMcu = true;
  360. const double t = static_cast<double>(b);
  361. if (qAbs(ch.temperature - t) > 0.05 || ch.temperature < 0.0) {
  362. ch.temperature = t;
  363. changed = true;
  364. }
  365. // 每个轮询周期给这个通道落一个点:详情页曲线一路一段连续曲线靠它撑起来
  366. ch.history.append(t);
  367. while (ch.history.size() > kTempHistorySize) {
  368. ch.history.removeFirst();
  369. ++ch.historyStart;
  370. }
  371. curveChanged = true;
  372. }
  373. // 24 路逐格改会连发 24 次信号,这里合并成一次刷,避免界面被刷爆
  374. if (changed || curveChanged)
  375. emit channelsChanged(index);
  376. }
  377. // ---------------------------------------------------------------------------
  378. // 后端状态落库:state 推送里单个工位对象 → 24 通道状态灯 / 本轮启动时间
  379. // 24 通道内存条温度也从这里取(channels[i].temperature_c,float,单位 ℃),
  380. // 取代原本 MCU 串口 0x82 的 24 字节来源(见 MainWindow::setupCom 的注释)。
  381. // 腔体温度(currentTemp)仍由本地 MCU 温控串口采集,后端不下发该字段。
  382. // ---------------------------------------------------------------------------
  383. void StationStore::applyBackendState(int index, const QJsonObject &station)
  384. {
  385. if (index < 0 || index >= m_stations.size())
  386. return;
  387. StationData &d = m_stations[index];
  388. bool channelsChangedFlag = false;
  389. // 0.8.1.19:boot_id 是轮次边界。即使本机不是点击 StartTest 的客户端,
  390. // 只要收到新 boot_id,也必须先清掉上一轮 24 路状态、温度与曲线。
  391. const QString incomingBootId = station.value(QStringLiteral("boot_id")).toString();
  392. if (incomingBootId.isEmpty() && !d.bootId.isEmpty()) {
  393. d.bootId.clear();
  394. } else if (!incomingBootId.isEmpty() && incomingBootId != d.bootId) {
  395. d.bootId = incomingBootId;
  396. for (ChannelInfo &ch : d.channels) {
  397. ch.state = ChannelInfo::Idle;
  398. ch.temperature = -1.0;
  399. ch.hasBackendTemp = false;
  400. ch.backendTemp = -1.0;
  401. ch.mcuTempMs = 0;
  402. ch.mcuTempC = -1;
  403. ch.tempFromMcu = false;
  404. ch.history.clear();
  405. ch.historyStart = 0;
  406. }
  407. d.stageCompletions = QJsonObject();
  408. d.verdict.clear();
  409. d.verdictScope.clear();
  410. d.firmwareFamily.clear();
  411. d.taskStatus.clear();
  412. d.mtResult = QJsonObject();
  413. d.awaitingDimmReplacement = false;
  414. channelsChangedFlag = true;
  415. }
  416. bool infoDirty = false;
  417. // 后端整体阶段(boot / uip / training / rmt / amt / post / uefi / mt / powered_off):
  418. // 卡片标题右侧显示当前处于哪个阶段;空或 unknown 时界面不显示。
  419. const QString phase = station.value(QStringLiteral("phase")).toString();
  420. if (d.phase != phase) {
  421. d.phase = phase;
  422. infoDirty = true;
  423. }
  424. // 0.8.1.19:工位级结论、固件族与七阶段完成状态独立保存。
  425. const QString verdict = station.value(QStringLiteral("verdict")).toString();
  426. const QString verdictScope = station.value(QStringLiteral("verdict_scope")).toString();
  427. const QString firmwareFamily = station.value(QStringLiteral("firmware_family")).toString();
  428. const QJsonObject stageCompletions = station.value(QStringLiteral("stage_completions")).toObject();
  429. const QJsonObject mtResult = station.value(QStringLiteral("mt_result")).toObject();
  430. if (d.verdict != verdict) { d.verdict = verdict; infoDirty = true; }
  431. if (d.verdictScope != verdictScope) { d.verdictScope = verdictScope; infoDirty = true; }
  432. if (d.firmwareFamily != firmwareFamily) { d.firmwareFamily = firmwareFamily; infoDirty = true; }
  433. if (d.stageCompletions != stageCompletions) {
  434. d.stageCompletions = stageCompletions;
  435. infoDirty = true;
  436. }
  437. if (d.mtResult != mtResult) {
  438. d.mtResult = mtResult;
  439. infoDirty = true;
  440. }
  441. // 手册 9.2 新增字段:只在值变化时刷新界面,state 每秒一包,不做无谓重绘。
  442. // 旧版后端没有这些字段:consoleOnline 留 -1(老判据),其余保持默认空值。
  443. const QJsonValue consoleVal = station.value(QStringLiteral("console_online"));
  444. const int consoleOnline = consoleVal.isBool() ? (consoleVal.toBool() ? 1 : 0) : -1;
  445. if (d.consoleOnline != consoleOnline) {
  446. d.consoleOnline = consoleOnline;
  447. infoDirty = true;
  448. }
  449. const bool awaiting = station.value(QStringLiteral("awaiting_dimm_replacement")).toBool();
  450. if (d.awaitingDimmReplacement != awaiting) {
  451. d.awaitingDimmReplacement = awaiting;
  452. infoDirty = true;
  453. }
  454. const QString taskStatus = station.value(QStringLiteral("task_status")).toString();
  455. if (d.taskStatus != taskStatus) {
  456. d.taskStatus = taskStatus;
  457. infoDirty = true;
  458. }
  459. const QJsonObject taskBatch = station.value(QStringLiteral("task_batch")).toObject();
  460. if (d.taskBatch != taskBatch) {
  461. d.taskBatch = taskBatch;
  462. infoDirty = true;
  463. }
  464. const QJsonArray taskHistory = station.value(QStringLiteral("task_history")).toArray();
  465. if (d.taskHistory != taskHistory) {
  466. d.taskHistory = taskHistory;
  467. infoDirty = true;
  468. }
  469. // BIOS 控制台连接状态(online / connecting / offline / error):
  470. // 加 2 拍防抖并与 console_online 联动避免瞬时闪断误报「控制台离线」。
  471. const QString rawConn = station.value(QStringLiteral("connection")).toString();
  472. QString effectiveConn = rawConn;
  473. if (d.consoleOnline == 1 && rawConn == QLatin1String("offline")) {
  474. effectiveConn = QStringLiteral("online");
  475. d.offlineDebounceTicks = 0;
  476. } else if (rawConn == QLatin1String("offline")
  477. && d.connection == QLatin1String("online")
  478. && d.consoleOnline != 0) {
  479. d.offlineDebounceTicks++;
  480. if (d.offlineDebounceTicks < 2)
  481. effectiveConn = d.connection;
  482. } else {
  483. d.offlineDebounceTicks = 0;
  484. }
  485. if (d.connection != effectiveConn) {
  486. d.connection = effectiveConn;
  487. infoDirty = true;
  488. }
  489. if (infoDirty)
  490. emit infoChanged(index);
  491. // 直接同步后端权威时间(started_unix_ms / ended_unix_ms / elapsed_ms);
  492. // 不在前端按阶段切换重置,也不以本机墙钟计算。
  493. applyBackendTiming(index, station);
  494. const qint64 now = QDateTime::currentMSecsSinceEpoch();
  495. const QJsonArray channels = station.value(QStringLiteral("channels")).toArray();
  496. if (!channels.isEmpty())
  497. d.backendHasChannels = true;
  498. for (const QJsonValue &v : channels) {
  499. const QJsonObject ch = v.toObject();
  500. // 按 name(CPU0_CHA)匹配;匹配不到就退回 cpu*12 + channel 的位置
  501. const QString name = ch.value(QStringLiteral("name")).toString();
  502. int slot = -1;
  503. for (int i = 0; i < d.channels.size(); ++i) {
  504. if (d.channels.at(i).name == name) {
  505. slot = i;
  506. break;
  507. }
  508. }
  509. if (slot < 0) {
  510. const int cpu = ch.value(QStringLiteral("cpu")).toInt(0);
  511. bool okChan = false;
  512. const int chan = ch.value(QStringLiteral("channel")).toString().toInt(&okChan);
  513. if (okChan && chan >= 0)
  514. slot = cpu * 12 + chan;
  515. }
  516. if (slot < 0 || slot >= d.channels.size())
  517. continue;
  518. // present 是后端提供的真实 DIMM 在位信息,协议要求必须是 JSON bool。
  519. // 缺失、null 或类型错误一律按「未确认在位」处理,禁止猜测为 true。
  520. const QJsonValue presentVal = ch.value(QStringLiteral("present"));
  521. const bool isPresent = presentVal.isBool() && presentVal.toBool();
  522. const QString chVerdict = ch.value(QStringLiteral("state")).toString();
  523. const ChannelInfo::State state = (chVerdict == QStringLiteral("fail"))
  524. ? ChannelInfo::Fail
  525. : (!isPresent
  526. ? ChannelInfo::Idle
  527. : ((chVerdict == QStringLiteral("pass"))
  528. ? ChannelInfo::Pass
  529. : ChannelInfo::Idle));
  530. const QJsonValue tempVal = ch.value(QStringLiteral("temperature_c"));
  531. double temp = -1.0;
  532. bool tempValid = false;
  533. if (isPresent && tempVal.isDouble()) {
  534. temp = tempVal.toDouble();
  535. tempValid = true;
  536. }
  537. ChannelInfo &info = d.channels[slot];
  538. info.backendPresent = isPresent;
  539. info.hasBackendTemp = tempValid;
  540. info.backendTemp = tempValid ? temp : -1.0;
  541. if (info.state != state) {
  542. info.state = state;
  543. channelsChangedFlag = true;
  544. }
  545. if (!isPresent) {
  546. // 空槽不保留上一轮温度和曲线,避免插拔内存后仍显示旧数据。
  547. info.mcuTempMs = 0;
  548. info.mcuTempC = -1;
  549. info.tempFromMcu = false;
  550. if (info.temperature >= 0.0 || !info.history.isEmpty() || info.historyStart != 0) {
  551. info.temperature = -1.0;
  552. info.history.clear();
  553. info.historyStart = 0;
  554. channelsChangedFlag = true;
  555. }
  556. continue;
  557. }
  558. const bool mcuFresh = (info.mcuTempMs > 0 && info.mcuTempC >= 0
  559. && (now - info.mcuTempMs) < 3000);
  560. if (mcuFresh) {
  561. // 3 秒内有有效 MCU 0x82 数据时由 MCU 驱动温度显示与 1Hz 曲线,不与后端重复落点或清零
  562. info.tempFromMcu = true;
  563. } else if (tempValid) {
  564. info.tempFromMcu = false;
  565. // 值变化(或上一轮是 --℃)才改写,避免无谓刷新
  566. if (qAbs(info.temperature - temp) > 0.05 || info.temperature < 0.0) {
  567. info.temperature = temp;
  568. channelsChangedFlag = true;
  569. }
  570. // 每个推送周期落一个点,详情页曲线靠这个 history 撑起来
  571. info.history.append(temp);
  572. while (info.history.size() > kTempHistorySize) {
  573. info.history.removeFirst();
  574. ++info.historyStart;
  575. }
  576. channelsChangedFlag = true;
  577. } else {
  578. info.tempFromMcu = false;
  579. if (info.temperature >= 0.0) {
  580. // 两路均无有效温度:退回 -1(显示 --℃),但不清历史曲线点
  581. info.temperature = -1.0;
  582. channelsChangedFlag = true;
  583. }
  584. }
  585. }
  586. if (channelsChangedFlag)
  587. emit channelsChanged(index);
  588. }
  589. void StationStore::applyTaskComplete(int index, const QJsonObject &result)
  590. {
  591. if (index < 0 || index >= m_stations.size())
  592. return;
  593. StationData &d = m_stations[index];
  594. const QString eventBootId = result.value(QStringLiteral("boot_id")).toString();
  595. // task_complete 的轮次归属已由 BackendLink 校验;这里接收已确认的终态事件。
  596. bool changed = false;
  597. if (!eventBootId.isEmpty() && eventBootId != d.bootId) {
  598. // BackendLink 已按 boot_id 过滤旧事件;若终态推送先于 state 到达,先建立新轮次边界。
  599. d.bootId = eventBootId;
  600. for (ChannelInfo &ch : d.channels) {
  601. ch.state = ChannelInfo::Idle;
  602. ch.temperature = -1.0;
  603. ch.hasBackendTemp = false;
  604. ch.backendTemp = -1.0;
  605. ch.mcuTempMs = 0;
  606. ch.mcuTempC = -1;
  607. ch.tempFromMcu = false;
  608. ch.history.clear();
  609. ch.historyStart = 0;
  610. }
  611. d.stageCompletions = QJsonObject();
  612. d.verdict.clear();
  613. d.verdictScope.clear();
  614. d.awaitingDimmReplacement = false;
  615. changed = true;
  616. emit channelsChanged(index);
  617. }
  618. const QString taskStatus = result.value(QStringLiteral("task_status")).toString();
  619. const QString verdict = result.value(QStringLiteral("verdict")).toString();
  620. const QString verdictScope = result.value(QStringLiteral("verdict_scope")).toString();
  621. const QJsonObject completions = result.value(QStringLiteral("stage_completions")).toObject();
  622. if (result.contains(QStringLiteral("awaiting_dimm_replacement"))) {
  623. const bool awaiting = result.value(QStringLiteral("awaiting_dimm_replacement")).toBool();
  624. if (d.awaitingDimmReplacement != awaiting) {
  625. d.awaitingDimmReplacement = awaiting;
  626. changed = true;
  627. }
  628. }
  629. if (!taskStatus.isEmpty() && d.taskStatus != taskStatus) {
  630. d.taskStatus = taskStatus;
  631. changed = true;
  632. }
  633. if (!verdict.isEmpty() && d.verdict != verdict) {
  634. d.verdict = verdict;
  635. changed = true;
  636. }
  637. if (!verdictScope.isEmpty() && d.verdictScope != verdictScope) {
  638. d.verdictScope = verdictScope;
  639. changed = true;
  640. }
  641. if (!completions.isEmpty() && d.stageCompletions != completions) {
  642. d.stageCompletions = completions;
  643. changed = true;
  644. }
  645. applyBackendTiming(index, result);
  646. if (changed)
  647. emit infoChanged(index);
  648. }