Compare commits
21
Commits
14291ee984
...
8d6534b96e
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8d6534b96e | ||
|
|
9185591a6d | ||
|
|
1f9c096d05 | ||
|
|
8c3df90a87 | ||
|
|
a3b7d3d876 | ||
|
|
17c8ca5268 | ||
|
|
ac77d4db10 | ||
|
|
bac0391a0c | ||
|
|
1f356af022 | ||
|
|
270895aaf0 | ||
|
|
5edb0b2d69 | ||
|
|
bae71f3253 | ||
|
|
2090597858 | ||
|
|
d8b939a3ff | ||
|
|
1093980b77 | ||
|
|
92780cedef | ||
|
|
83024b29b8 | ||
|
|
6330b99860 | ||
|
|
46c69607c5 | ||
|
|
feaddfee7b | ||
|
|
d45acaf50f |
@@ -0,0 +1,9 @@
|
||||
# 默认所有文本文件使用 Windows 换行符
|
||||
* text=auto eol=crlf
|
||||
|
||||
# server 文件夹下所有文件使用 Linux 换行符
|
||||
server/** text eol=lf
|
||||
|
||||
# 特定文件类型明确指定
|
||||
*.sh text eol=lf
|
||||
*.bash text eol=lf
|
||||
+10
-1
@@ -140,7 +140,16 @@ async function getConfig(configType, options = {}) {
|
||||
repeat: Number(values.repeat)
|
||||
};
|
||||
}
|
||||
return callServer({ type: "getFunctionConfig", configType }, options);
|
||||
const result = await callServer({
|
||||
type: "getVolumeConfigFile",
|
||||
deviceId: getDeviceId(options)
|
||||
}, options);
|
||||
if (result.found === false || !result.url) {
|
||||
throw new Error("当前产线尚无已成功提交的容积配置");
|
||||
}
|
||||
const response = await fetch(result.url);
|
||||
if (!response.ok) throw new Error(`配置文件下载失败: HTTP ${response.status}`);
|
||||
return JSON.parse(await response.text());
|
||||
}
|
||||
|
||||
async function main() {
|
||||
|
||||
@@ -6,10 +6,14 @@ const { callServer, downloadFromUrl, downloadToPath } = require("./server-client
|
||||
const { signLicense } = require("./license-manager");
|
||||
const { plotCsv } = require("./plot-csv");
|
||||
const { plotJson } = require("./plot-json");
|
||||
const { createModelHandlers } = require("./model-handlers");
|
||||
|
||||
const VOLUME_FIELDS = [
|
||||
"q_in_val", "dt", "p_max", "fit_low", "fit_high", "T_delta", "xa_full", "num_runs"
|
||||
];
|
||||
const IDENTIFICATION_FIELDS = [
|
||||
"q_in_val", "dt", "n_order", "t_c", "levels", "dead_area", "xa_full", "V_val", "repeat"
|
||||
];
|
||||
const DEFAULT_API_URL = "https://ReinLoop.dominatedconvergence.com";
|
||||
const API_URL = process.env.REINLOOP_API_URL || DEFAULT_API_URL;
|
||||
const INBOX_POLL_INTERVAL_MS = Number(process.env.POLL_INTERVAL_MS || 1000);
|
||||
@@ -35,13 +39,42 @@ function startPanelInboxPoller(window) {
|
||||
}
|
||||
|
||||
let polling = false;
|
||||
let notificationDeviceId = null;
|
||||
const deliveredNotificationIds = new Set();
|
||||
let notificationFailureReported = false;
|
||||
|
||||
async function pollNotifications(requestContext) {
|
||||
if (notificationDeviceId !== requestContext.deviceId) {
|
||||
notificationDeviceId = requestContext.deviceId;
|
||||
deliveredNotificationIds.clear();
|
||||
}
|
||||
const pending = await callServer(
|
||||
{ type: "getPendingPanelNotification", deviceId: requestContext.deviceId },
|
||||
requestContext
|
||||
);
|
||||
notificationFailureReported = false;
|
||||
const notification = pending.notification;
|
||||
if (!pending.pending || !notification?.notificationId) return;
|
||||
if (deliveredNotificationIds.has(notification.notificationId)) return;
|
||||
deliveredNotificationIds.add(notification.notificationId);
|
||||
window.webContents.send("panel:notification", notification);
|
||||
}
|
||||
|
||||
const poll = async () => {
|
||||
if (polling || window.isDestroyed()) return;
|
||||
if (!connectionState.deviceId || !connectionState.adminToken) return;
|
||||
if (pendingReview) return;
|
||||
const requestContext = { ...connectionState };
|
||||
polling = true;
|
||||
try {
|
||||
try {
|
||||
await pollNotifications(requestContext);
|
||||
} catch (error) {
|
||||
if (!notificationFailureReported) {
|
||||
console.warn(`[${new Date().toISOString()}] Panel 通知轮询不可用: ${error.message}`);
|
||||
notificationFailureReported = true;
|
||||
}
|
||||
}
|
||||
if (pendingReview) return;
|
||||
const pending = await callServer(
|
||||
{ type: "getPendingPanelFile", deviceId: requestContext.deviceId },
|
||||
requestContext
|
||||
@@ -53,6 +86,7 @@ function startPanelInboxPoller(window) {
|
||||
filePath: imagePath,
|
||||
dataUrl: toDataUrl(imagePath),
|
||||
fileName: pending.fileName,
|
||||
runId: pending.runId || pending.fileName,
|
||||
mediaType: pending.mediaType,
|
||||
uploadTime: pending.uploadTime,
|
||||
deviceId: requestContext.deviceId,
|
||||
@@ -62,7 +96,7 @@ function startPanelInboxPoller(window) {
|
||||
pendingReview = {
|
||||
deviceId: requestContext.deviceId,
|
||||
fileID: pending.fileID,
|
||||
runId: pending.fileName,
|
||||
runId: pending.runId || pending.fileName,
|
||||
credentials: requestContext
|
||||
};
|
||||
} else {
|
||||
@@ -128,7 +162,35 @@ function validateParameters(configType, parameters) {
|
||||
if (!Number.isInteger(parameters.num_runs)) {
|
||||
throw new Error("num_runs 必须是整数");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (configType === "identification") {
|
||||
const fields = Object.keys(parameters);
|
||||
const missing = IDENTIFICATION_FIELDS.filter((field) => !(field in parameters));
|
||||
const extra = fields.filter((field) => !IDENTIFICATION_FIELDS.includes(field));
|
||||
if (missing.length || extra.length) {
|
||||
throw new Error(`系统辨识配置字段不匹配。缺少: ${missing.join(", ") || "无"};多余: ${extra.join(", ") || "无"}`);
|
||||
}
|
||||
for (const field of IDENTIFICATION_FIELDS) {
|
||||
if (field === "levels") continue;
|
||||
if (typeof parameters[field] !== "number" || !Number.isFinite(parameters[field])) {
|
||||
throw new Error(`${field} 必须是有效数字`);
|
||||
}
|
||||
}
|
||||
if (!Array.isArray(parameters.levels) || parameters.levels.some((value) => typeof value !== "number" || !Number.isFinite(value))) {
|
||||
throw new Error("levels 必须是有效数字数组");
|
||||
}
|
||||
if (parameters.levels.length < 2 || (parameters.levels.length & (parameters.levels.length - 1)) !== 0) {
|
||||
throw new Error("levels 元素个数必须是不小于 2 的 2 的整数次幂");
|
||||
}
|
||||
if (!Number.isInteger(parameters.n_order) || !Number.isInteger(parameters.repeat)) {
|
||||
throw new Error("n_order 和 repeat 必须是整数");
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
throw new Error(`不支持的配置类型: ${configType}`);
|
||||
}
|
||||
|
||||
function toDataUrl(filePath) {
|
||||
@@ -138,6 +200,7 @@ function toDataUrl(filePath) {
|
||||
}
|
||||
|
||||
function registerHandlers() {
|
||||
const modelHandlers = createModelHandlers({ callServer });
|
||||
ipcMain.handle("app:get-defaults", () => ({
|
||||
apiUrl: API_URL,
|
||||
deviceId: process.env.REINLOOP_DEVICE_ID || "",
|
||||
@@ -155,6 +218,21 @@ function registerHandlers() {
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
ipcMain.handle("panel:ack-notification", (_event, request) =>
|
||||
callServer({
|
||||
type: "ackPanelNotification",
|
||||
deviceId: request.deviceId,
|
||||
notificationId: request.notificationId
|
||||
}, request.credentials));
|
||||
ipcMain.handle("panel:open-notification-file", async (_event, request) => {
|
||||
if (!request.fileID) throw new Error("该提醒未关联结果文件");
|
||||
const download = await callServer({ type: "downloadModel", fileID: request.fileID }, request.credentials);
|
||||
const sourcePath = await downloadFromUrl(download.url, download.fileName || "volume_result.json", request.credentials);
|
||||
const error = await shell.openPath(sourcePath);
|
||||
if (error) throw new Error(`无法打开结果文件: ${error}`);
|
||||
return { filePath: sourcePath };
|
||||
});
|
||||
|
||||
ipcMain.handle("connection:test", async (_event, request) => {
|
||||
const result = await callServer({ type: "listOrganizations" }, request);
|
||||
connectionState.apiUrl = String(request.apiUrl || API_URL).trim();
|
||||
@@ -169,6 +247,14 @@ function registerHandlers() {
|
||||
callServer({ type: "createCompany", name: request.name, code: request.code }, request.credentials));
|
||||
ipcMain.handle("organization:create-line", (_event, request) =>
|
||||
callServer({ type: "createProductionLine", companyId: request.companyId, name: request.name, code: request.code }, request.credentials));
|
||||
ipcMain.handle("organization:delete-company", (_event, request) =>
|
||||
callServer({ type: "deleteCompany", companyId: request.companyId }, request.credentials));
|
||||
ipcMain.handle("organization:delete-line", (_event, request) =>
|
||||
callServer({
|
||||
type: "deleteProductionLine",
|
||||
companyId: request.companyId,
|
||||
productionLineId: request.productionLineId
|
||||
}, request.credentials));
|
||||
|
||||
ipcMain.handle("license:issue", async (_event, request) => {
|
||||
const keySelection = await dialog.showOpenDialog({
|
||||
@@ -218,6 +304,11 @@ function registerHandlers() {
|
||||
{ type: "revokeLicense", licenseId: request.licenseId, reason: request.reason },
|
||||
resolveCredentials(request.credentials)
|
||||
));
|
||||
ipcMain.handle("license:delete", (_event, request) =>
|
||||
callServer(
|
||||
{ type: "deleteLicense", licenseId: request.licenseId },
|
||||
resolveCredentials(request.credentials)
|
||||
));
|
||||
ipcMain.handle("license:download", async (_event, request) => {
|
||||
const resolvedCredentials = resolveCredentials(request.credentials);
|
||||
const result = await callServer(
|
||||
@@ -256,35 +347,19 @@ function registerHandlers() {
|
||||
return result;
|
||||
});
|
||||
|
||||
ipcMain.handle("model:list", (_event, request) =>
|
||||
callServer({ type: "listModels", folder: `${request.deviceId}/model_config` }, request.credentials));
|
||||
ipcMain.handle("model:list", (_event, request) => modelHandlers.list(request));
|
||||
ipcMain.handle("model:choose-upload-file", async () => {
|
||||
const selection = await dialog.showOpenDialog({ title: "选择模型文件", properties: ["openFile"] });
|
||||
if (selection.canceled) return null;
|
||||
const sourcePath = selection.filePaths[0];
|
||||
return { sourcePath, fileName: path.basename(sourcePath) };
|
||||
});
|
||||
ipcMain.handle("model:upload", async (_event, request) => {
|
||||
if (!request.sourcePath || !request.fileName) throw new Error("请先选择模型文件");
|
||||
const sourcePath = path.resolve(request.sourcePath);
|
||||
const fileName = path.basename(request.fileName);
|
||||
await fs.promises.access(sourcePath, fs.constants.R_OK);
|
||||
const issued = await callServer({
|
||||
type: "uploadDataFile", fileName, folder: `${request.deviceId}/model_config`,
|
||||
overwrite: request.overwrite === true
|
||||
}, request.credentials);
|
||||
const form = new FormData();
|
||||
form.append("file", new Blob([await fs.promises.readFile(sourcePath)]), fileName);
|
||||
const response = await fetch(issued.uploadMetadata.url, { method: "POST", body: form });
|
||||
if (![200, 204].includes(response.status)) throw new Error(`模型上传失败: HTTP ${response.status}`);
|
||||
return { success: true, fileID: issued.fileID, fileName };
|
||||
});
|
||||
ipcMain.handle("model:upload", (_event, request) => modelHandlers.upload(request));
|
||||
ipcMain.handle("model:download", async (_event, request) => {
|
||||
const result = await callServer({ type: "downloadModel", fileID: request.fileID }, request.credentials);
|
||||
return { filePath: await downloadFromUrl(result.url, request.fileName, request.credentials) };
|
||||
});
|
||||
ipcMain.handle("model:delete", (_event, request) =>
|
||||
callServer({ type: "deleteFile", fileID: request.fileID }, request.credentials));
|
||||
ipcMain.handle("model:delete", (_event, request) => modelHandlers.delete(request));
|
||||
|
||||
ipcMain.handle("identification:list", (_event, request) =>
|
||||
callServer({
|
||||
@@ -304,6 +379,7 @@ function registerHandlers() {
|
||||
filePath: imagePath,
|
||||
dataUrl: toDataUrl(imagePath),
|
||||
fileName: download.fileName || request.fileName,
|
||||
runId: download.runId || request.runId || request.fileName,
|
||||
mediaType,
|
||||
uploadTime: download.uploadTime || request.uploadTime,
|
||||
deviceId: request.deviceId
|
||||
|
||||
@@ -15,7 +15,10 @@ contextBridge.exposeInMainWorld("reinloop", {
|
||||
listLicenses: (credentials) => ipcRenderer.invoke("license:list", credentials),
|
||||
getLicense: (request) => ipcRenderer.invoke("license:get", request),
|
||||
revokeLicense: (request) => ipcRenderer.invoke("license:revoke", request),
|
||||
deleteLicense: (request) => ipcRenderer.invoke("license:delete", request),
|
||||
downloadLicense: (request) => ipcRenderer.invoke("license:download", request),
|
||||
deleteCompany: (request) => ipcRenderer.invoke("organization:delete-company", request),
|
||||
deleteProductionLine: (request) => ipcRenderer.invoke("organization:delete-line", request),
|
||||
submitReview: (request) => ipcRenderer.invoke("review:submit", request),
|
||||
listModels: (request) => ipcRenderer.invoke("model:list", request),
|
||||
chooseModelUploadFile: () => ipcRenderer.invoke("model:choose-upload-file"),
|
||||
@@ -30,6 +33,9 @@ contextBridge.exposeInMainWorld("reinloop", {
|
||||
previewControlFile: (request) => ipcRenderer.invoke("control-data:preview", request),
|
||||
downloadControlFile: (request) => ipcRenderer.invoke("control-data:download", request),
|
||||
deleteControlFile: (request) => ipcRenderer.invoke("control-data:delete", request),
|
||||
acknowledgeNotification: (request) => ipcRenderer.invoke("panel:ack-notification", request),
|
||||
openNotificationFile: (request) => ipcRenderer.invoke("panel:open-notification-file", request),
|
||||
onCsvUpdated: (callback) => ipcRenderer.on("csv:updated", (_event, result) => callback(result)),
|
||||
onCsvWatchError: (callback) => ipcRenderer.on("csv:watch-error", (_event, message) => callback(message))
|
||||
onCsvWatchError: (callback) => ipcRenderer.on("csv:watch-error", (_event, message) => callback(message)),
|
||||
onPanelNotification: (callback) => ipcRenderer.on("panel:notification", (_event, notification) => callback(notification))
|
||||
});
|
||||
@@ -43,7 +43,7 @@
|
||||
<button class="tab active" data-target="plot-panel">绘图预览</button>
|
||||
<button class="tab" data-target="identification-data-panel">辨识数据</button>
|
||||
<button class="tab" data-target="control-data-panel">控制数据</button>
|
||||
<button class="tab" data-target="config-panel">配置发布</button>
|
||||
<button class="tab" data-target="config-panel">配置发布 <span id="config-notification-badge" class="tab-badge" hidden>0</span></button>
|
||||
<button class="tab" data-target="model-panel">模型管理</button>
|
||||
<button class="tab" data-target="license-panel">许可证</button>
|
||||
<button class="tab" data-target="organization-panel">组织管理</button>
|
||||
@@ -111,7 +111,7 @@
|
||||
<button id="refresh-identification-files" class="button secondary">刷新</button>
|
||||
</div>
|
||||
<div class="data-surface">
|
||||
<table><thead><tr><th>上传时间</th><th>文件名</th><th>类型</th><th>大小</th><th>状态</th><th>操作</th></tr></thead><tbody id="identification-file-list"></tbody></table>
|
||||
<table><thead><tr><th>上传时间</th><th>文件名</th><th>runId</th><th>类型</th><th>大小</th><th>状态</th><th>操作</th></tr></thead><tbody id="identification-file-list"></tbody></table>
|
||||
<p id="identification-file-empty" class="empty-row">选择公司和产线后刷新辨识数据</p>
|
||||
</div>
|
||||
</section>
|
||||
@@ -139,14 +139,15 @@
|
||||
<button class="segment" data-type="identification">系统辨识</button>
|
||||
</div>
|
||||
</div>
|
||||
<div id="config-notifications" class="notification-stack" aria-live="polite"></div>
|
||||
<div class="editor-layout">
|
||||
<div class="editor-column">
|
||||
<div class="editor-toolbar">
|
||||
<span id="config-label">容积测量配置</span>
|
||||
<button id="import-config" class="text-button">导入 JSON</button>
|
||||
</div>
|
||||
<textarea id="config-editor" spellcheck="false" aria-label="JSON 配置编辑器"></textarea>
|
||||
<p id="config-path" class="file-path">可直接编辑,或从本地 JSON 导入</p>
|
||||
<form id="config-form" class="config-form" aria-label="配置字段"></form>
|
||||
<p id="config-path" class="file-path">字段名称固定;可填写右侧数据或从本地 JSON 导入</p>
|
||||
</div>
|
||||
<aside class="publish-aside">
|
||||
<h3>发布检查</h3>
|
||||
@@ -171,7 +172,7 @@
|
||||
</div>
|
||||
</div>
|
||||
<div class="data-surface">
|
||||
<table><thead><tr><th>文件名</th><th>上传时间</th><th>大小</th><th>操作</th></tr></thead><tbody id="model-list"></tbody></table>
|
||||
<table><thead><tr><th>服务器文件名</th><th>原始文件名</th><th>上传时间</th><th>大小</th><th>操作</th></tr></thead><tbody id="model-list"></tbody></table>
|
||||
<p id="model-empty" class="empty-row">选择公司和产线后刷新模型列表</p>
|
||||
</div>
|
||||
</section>
|
||||
@@ -210,6 +211,7 @@
|
||||
<label><span>公司名称</span><input id="company-name" required></label>
|
||||
<label><span>公司编码</span><input id="company-code" pattern="[a-z0-9][a-z0-9_-]{1,63}" required></label>
|
||||
<button class="button primary" type="submit">添加公司</button>
|
||||
<button id="delete-company" class="button danger" type="button">删除当前公司</button>
|
||||
</form>
|
||||
<form id="line-form" class="form-surface">
|
||||
<h3>添加产线</h3>
|
||||
@@ -217,10 +219,29 @@
|
||||
<label><span>产线名称</span><input id="line-name" required></label>
|
||||
<label><span>产线编码</span><input id="line-code" pattern="[a-z0-9][a-z0-9_-]{1,63}" required></label>
|
||||
<button class="button primary" type="submit">添加产线</button>
|
||||
<button id="delete-line" class="button danger" type="button">删除当前产线</button>
|
||||
</form>
|
||||
</div>
|
||||
</section>
|
||||
</main>
|
||||
<div id="action-dialog" class="action-dialog" hidden role="dialog" aria-modal="true" aria-labelledby="action-dialog-title">
|
||||
<form id="action-dialog-form" class="action-dialog-shell">
|
||||
<div class="action-dialog-head">
|
||||
<p class="section-kicker">MODEL CONTROL</p>
|
||||
<h2 id="action-dialog-title">模型操作</h2>
|
||||
</div>
|
||||
<p id="action-dialog-message" class="action-dialog-message"></p>
|
||||
<label class="action-dialog-field">
|
||||
<span>文件名</span>
|
||||
<input id="action-dialog-input" type="text" autocomplete="off" spellcheck="false">
|
||||
</label>
|
||||
<p id="action-dialog-error" class="action-dialog-error" hidden></p>
|
||||
<div class="action-dialog-actions">
|
||||
<button id="action-dialog-cancel" class="button secondary" type="button">取消</button>
|
||||
<button id="action-dialog-confirm" class="button primary" type="submit">确认</button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
<div id="image-lightbox" class="lightbox" hidden role="dialog" aria-modal="true" aria-labelledby="lightbox-title">
|
||||
<div class="lightbox-shell">
|
||||
<div class="lightbox-toolbar"><strong id="lightbox-title">图像预览</strong><div class="lightbox-actions"><button id="zoom-out" class="button icon" title="缩小" aria-label="缩小图像">-</button><button id="zoom-in" class="button icon" title="放大" aria-label="放大图像">+</button><button id="zoom-fit" class="button secondary" type="button">适应窗口</button><button id="zoom-reset" class="button secondary" type="button">原始比例</button><button id="close-lightbox" class="button icon" title="关闭" aria-label="关闭图像预览">x</button></div></div>
|
||||
|
||||
@@ -21,6 +21,11 @@ const identificationExample = {
|
||||
repeat: 2
|
||||
};
|
||||
|
||||
const configFields = {
|
||||
volume: Object.keys(volumeExample),
|
||||
identification: Object.keys(identificationExample)
|
||||
};
|
||||
|
||||
const state = {
|
||||
configType: "volume",
|
||||
imagePaths: { csv: null, json: null },
|
||||
@@ -33,7 +38,9 @@ const state = {
|
||||
defaultDeviceId: "",
|
||||
review: null,
|
||||
retryDeviceId: null,
|
||||
notificationDeviceId: null,
|
||||
lightbox: { mediaType: null, scale: 1, fit: true, previousFocus: null },
|
||||
notifications: [],
|
||||
connected: false
|
||||
};
|
||||
|
||||
@@ -66,12 +73,14 @@ const elements = {
|
||||
openImage: document.querySelector('[data-open-plot="json"]')
|
||||
}
|
||||
},
|
||||
configEditor: document.querySelector("#config-editor"),
|
||||
configForm: document.querySelector("#config-form"),
|
||||
configLabel: document.querySelector("#config-label"),
|
||||
configPath: document.querySelector("#config-path"),
|
||||
publishTarget: document.querySelector("#publish-target"),
|
||||
publishButton: document.querySelector("#publish-config"),
|
||||
publishResult: document.querySelector("#publish-result"),
|
||||
configNotifications: document.querySelector("#config-notifications"),
|
||||
configNotificationBadge: document.querySelector("#config-notification-badge"),
|
||||
lineCompany: document.querySelector("#line-company"),
|
||||
licenseTarget: document.querySelector("#license-target"),
|
||||
licenseList: document.querySelector("#license-list"),
|
||||
@@ -87,6 +96,14 @@ const elements = {
|
||||
reviewTarget: document.querySelector("#review-target"),
|
||||
approveReview: document.querySelector("#approve-review"),
|
||||
rejectReview: document.querySelector("#reject-review"),
|
||||
actionDialog: document.querySelector("#action-dialog"),
|
||||
actionDialogForm: document.querySelector("#action-dialog-form"),
|
||||
actionDialogTitle: document.querySelector("#action-dialog-title"),
|
||||
actionDialogMessage: document.querySelector("#action-dialog-message"),
|
||||
actionDialogInput: document.querySelector("#action-dialog-input"),
|
||||
actionDialogError: document.querySelector("#action-dialog-error"),
|
||||
actionDialogCancel: document.querySelector("#action-dialog-cancel"),
|
||||
actionDialogConfirm: document.querySelector("#action-dialog-confirm"),
|
||||
lightbox: document.querySelector("#image-lightbox"),
|
||||
lightboxTitle: document.querySelector("#lightbox-title"),
|
||||
lightboxImage: document.querySelector("#lightbox-image"),
|
||||
@@ -225,6 +242,11 @@ function renderLineOptions() {
|
||||
function updateSelectedTarget() {
|
||||
const company = selectedCompany();
|
||||
const line = selectedLine();
|
||||
if (state.notificationDeviceId !== elements.deviceId.value) {
|
||||
state.notificationDeviceId = elements.deviceId.value;
|
||||
state.notifications = [];
|
||||
renderNotifications();
|
||||
}
|
||||
elements.licenseTarget.value = company && line ? `${company.name} / ${line.name}` : "";
|
||||
void syncConnection();
|
||||
}
|
||||
@@ -253,7 +275,7 @@ async function refreshLicenses() {
|
||||
elements.licenseList.innerHTML = result.licenses.map((license) => `
|
||||
<tr><td>${escapeHtml(license.companyName)} / ${escapeHtml(license.productionLineName)}</td>
|
||||
<td>${escapeHtml(license.expiry)}</td><td>${license.status === "active" ? "有效" : "已撤销"}</td>
|
||||
<td><button class="table-action" data-license-detail="${escapeHtml(license.licenseId)}">详情</button><button class="table-action" data-license-download="${escapeHtml(license.licenseId)}">下载</button>${license.status === "active" ? `<button class="table-action danger" data-license-revoke="${escapeHtml(license.licenseId)}">撤销</button>` : ""}</td></tr>
|
||||
<td><button class="table-action" data-license-detail="${escapeHtml(license.licenseId)}">详情</button><button class="table-action" data-license-download="${escapeHtml(license.licenseId)}">下载</button>${license.status === "active" ? `<button class="table-action danger" data-license-revoke="${escapeHtml(license.licenseId)}">撤销</button>` : `<button class="table-action danger" data-license-delete="${escapeHtml(license.licenseId)}">删除</button>`}</td></tr>
|
||||
`).join("");
|
||||
elements.licenseEmpty.hidden = result.licenses.length > 0;
|
||||
setStatus("许可证已刷新", "success");
|
||||
@@ -267,7 +289,7 @@ async function refreshModels() {
|
||||
if (!result) return;
|
||||
state.models = result.fileList;
|
||||
elements.modelList.innerHTML = state.models.map((model) => `
|
||||
<tr><td>${escapeHtml(model.fileName)}</td><td>${escapeHtml(model.uploadTime || "-")}</td>
|
||||
<tr><td>${escapeHtml(model.fileName)}</td><td>${escapeHtml(model.originalFileName || model.fileName)}</td><td>${escapeHtml(model.uploadTime || "-")}</td>
|
||||
<td>${formatSize(model.size)}</td><td><button class="table-action" data-model-download="${escapeHtml(model.fileID)}">下载</button><button class="table-action danger" data-model-delete="${escapeHtml(model.fileID)}">删除</button></td></tr>
|
||||
`).join("");
|
||||
elements.modelEmpty.hidden = state.models.length > 0;
|
||||
@@ -283,6 +305,7 @@ async function refreshIdentificationFiles() {
|
||||
state.identificationFiles = result.files || result.fileList || [];
|
||||
elements.identificationFileList.innerHTML = state.identificationFiles.map((file) => `
|
||||
<tr><td>${escapeHtml(file.uploadTime || "-")}</td><td>${escapeHtml(file.fileName)}</td>
|
||||
<td>${escapeHtml(file.runId || "-")}</td>
|
||||
<td>${file.mediaType === "json" ? "行程 JSON" : "辨识 CSV"}</td><td>${formatSize(file.size)}</td>
|
||||
<td>${file.status === "processed" ? "已处理" : "待处理"}</td>
|
||||
<td><button class="table-action" data-identification-preview="${escapeHtml(file.fileID)}">查看</button><button class="table-action" data-identification-download="${escapeHtml(file.fileID)}">下载</button><button class="table-action danger" data-identification-delete="${escapeHtml(file.fileID)}">删除</button></td></tr>
|
||||
@@ -332,6 +355,88 @@ function showError(error) {
|
||||
elements.publishResult.dataset.tone = "error";
|
||||
}
|
||||
|
||||
let actionDialogState = null;
|
||||
|
||||
function closeActionDialog(value) {
|
||||
if (!actionDialogState) return;
|
||||
const { resolve, previousFocus } = actionDialogState;
|
||||
actionDialogState = null;
|
||||
elements.actionDialog.hidden = true;
|
||||
elements.actionDialogForm.reset();
|
||||
elements.actionDialogError.hidden = true;
|
||||
previousFocus?.focus();
|
||||
resolve(value);
|
||||
}
|
||||
|
||||
function requestModelName({
|
||||
title,
|
||||
message,
|
||||
value = "",
|
||||
confirmLabel = "确认",
|
||||
danger = false,
|
||||
expectedValue = null,
|
||||
expectedLabel = "输入内容",
|
||||
ignoreCase = false
|
||||
}) {
|
||||
if (actionDialogState) closeActionDialog(null);
|
||||
elements.actionDialogTitle.textContent = title;
|
||||
elements.actionDialogMessage.textContent = message;
|
||||
elements.actionDialogInput.value = value;
|
||||
elements.actionDialogConfirm.textContent = confirmLabel;
|
||||
elements.actionDialogConfirm.classList.toggle("primary", !danger);
|
||||
elements.actionDialogConfirm.classList.toggle("danger", danger);
|
||||
elements.actionDialogError.hidden = true;
|
||||
elements.actionDialog.hidden = false;
|
||||
const previousFocus = document.activeElement;
|
||||
window.requestAnimationFrame(() => {
|
||||
elements.actionDialogInput.focus();
|
||||
elements.actionDialogInput.select();
|
||||
});
|
||||
return new Promise((resolve) => {
|
||||
actionDialogState = {
|
||||
resolve,
|
||||
previousFocus,
|
||||
expectedValue,
|
||||
expectedLabel,
|
||||
ignoreCase
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
elements.actionDialogForm.addEventListener("submit", (event) => {
|
||||
event.preventDefault();
|
||||
const value = elements.actionDialogInput.value.trim();
|
||||
const expectedValue = actionDialogState?.expectedValue;
|
||||
const expectedLabel = actionDialogState?.expectedLabel || "输入内容";
|
||||
const ignoreCase = actionDialogState?.ignoreCase === true;
|
||||
if (!value) {
|
||||
elements.actionDialogError.textContent = `${expectedLabel}不能为空`;
|
||||
elements.actionDialogError.hidden = false;
|
||||
elements.actionDialogInput.focus();
|
||||
return;
|
||||
}
|
||||
if (expectedValue !== null) {
|
||||
const expected = String(expectedValue).trim();
|
||||
const matched = ignoreCase
|
||||
? value.toLowerCase() === expected.toLowerCase()
|
||||
: value === expected;
|
||||
if (!matched) {
|
||||
elements.actionDialogError.textContent = `${expectedLabel}不匹配,请按提示完整输入`;
|
||||
elements.actionDialogError.hidden = false;
|
||||
elements.actionDialogInput.focus();
|
||||
return;
|
||||
}
|
||||
}
|
||||
closeActionDialog(value);
|
||||
});
|
||||
elements.actionDialogCancel.addEventListener("click", () => closeActionDialog(null));
|
||||
elements.actionDialog.addEventListener("click", (event) => {
|
||||
if (event.target === elements.actionDialog) closeActionDialog(null);
|
||||
});
|
||||
document.addEventListener("keydown", (event) => {
|
||||
if (event.key === "Escape" && !elements.actionDialog.hidden) closeActionDialog(null);
|
||||
});
|
||||
|
||||
function showImage(result) {
|
||||
if (!result) return;
|
||||
const mediaType = result.mediaType === "json" || result.fileName?.toLowerCase().endsWith(".json")
|
||||
@@ -353,8 +458,9 @@ function showImage(result) {
|
||||
plot.showImage.disabled = false;
|
||||
plot.openImage.disabled = false;
|
||||
if (mediaType === "csv" && result.reviewable) {
|
||||
state.review = { runId: result.fileName, deviceId: result.deviceId };
|
||||
elements.reviewTarget.textContent = result.fileName;
|
||||
const runId = result.runId || result.fileName;
|
||||
state.review = { runId, deviceId: result.deviceId };
|
||||
elements.reviewTarget.textContent = `${result.fileName} (runId: ${runId})`;
|
||||
elements.approveReview.disabled = false;
|
||||
elements.rejectReview.disabled = false;
|
||||
}
|
||||
@@ -413,16 +519,91 @@ function activateTab(target) {
|
||||
}
|
||||
|
||||
function activateConfigType(configType) {
|
||||
try {
|
||||
state.configs[state.configType] = JSON.parse(elements.configEditor.value);
|
||||
} catch (_error) {
|
||||
// Keep the last valid configuration when changing views.
|
||||
}
|
||||
syncCurrentConfig();
|
||||
state.configType = configType;
|
||||
document.querySelectorAll(".segment").forEach((item) => item.classList.toggle("active", item.dataset.type === configType));
|
||||
renderConfig();
|
||||
}
|
||||
|
||||
function isPowerOfTwo(value) {
|
||||
return Number.isInteger(value) && value >= 2 && (value & (value - 1)) === 0;
|
||||
}
|
||||
|
||||
function clearConfigErrors() {
|
||||
elements.configForm.querySelectorAll("[aria-invalid]").forEach((input) => input.removeAttribute("aria-invalid"));
|
||||
elements.configForm.querySelectorAll(".config-field-error").forEach((item) => item.remove());
|
||||
}
|
||||
|
||||
function showConfigFieldError(field, message) {
|
||||
const input = elements.configForm.querySelector(`[data-config-field="${field}"]`);
|
||||
if (!input) return;
|
||||
input.setAttribute("aria-invalid", "true");
|
||||
const error = document.createElement("p");
|
||||
error.className = "config-field-error";
|
||||
error.textContent = message;
|
||||
input.closest(".config-field").append(error);
|
||||
}
|
||||
|
||||
function readConfigForm({ showErrors = false } = {}) {
|
||||
const type = state.configType;
|
||||
const parameters = {};
|
||||
let invalidField = null;
|
||||
let invalidMessage = null;
|
||||
clearConfigErrors();
|
||||
for (const field of configFields[type]) {
|
||||
const input = elements.configForm.querySelector(`[data-config-field="${field}"]`);
|
||||
const text = input?.value.trim() || "";
|
||||
if (field === "levels") {
|
||||
const values = text.split(",").map((value) => value.trim()).filter(Boolean);
|
||||
parameters[field] = values.map(Number);
|
||||
if (!values.length || parameters[field].some((value) => !Number.isFinite(value))) {
|
||||
invalidField = field;
|
||||
invalidMessage = "请输入以逗号分隔的有效数字。";
|
||||
} else if (!isPowerOfTwo(parameters[field].length)) {
|
||||
invalidField = field;
|
||||
invalidMessage = "元素个数必须是不小于 2 的 2 的整数次幂。";
|
||||
}
|
||||
continue;
|
||||
}
|
||||
const value = Number(text);
|
||||
parameters[field] = value;
|
||||
if (!text || !Number.isFinite(value)) {
|
||||
invalidField = field;
|
||||
invalidMessage = "请输入有效数字。";
|
||||
} else if (["num_runs", "n_order", "repeat"].includes(field) && !Number.isInteger(value)) {
|
||||
invalidField = field;
|
||||
invalidMessage = "请输入整数。";
|
||||
}
|
||||
}
|
||||
if (invalidField) {
|
||||
if (showErrors) showConfigFieldError(invalidField, invalidMessage);
|
||||
throw new Error(`${invalidField}:${invalidMessage}`);
|
||||
}
|
||||
return parameters;
|
||||
}
|
||||
|
||||
function syncCurrentConfig() {
|
||||
try {
|
||||
state.configs[state.configType] = readConfigForm();
|
||||
} catch (_error) {
|
||||
// Preserve the last valid values until the user corrects the visible form.
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeImportedConfig(parameters) {
|
||||
if (!parameters || typeof parameters !== "object" || Array.isArray(parameters)) {
|
||||
throw new Error("导入配置必须是 JSON 对象");
|
||||
}
|
||||
const fields = configFields[state.configType];
|
||||
const unknown = Object.keys(parameters).filter((field) => !fields.includes(field));
|
||||
if (unknown.length) throw new Error(`导入配置包含未知字段:${unknown.join(", ")}`);
|
||||
const normalized = { ...state.configs[state.configType] };
|
||||
for (const field of fields) {
|
||||
if (parameters[field] !== undefined) normalized[field] = parameters[field];
|
||||
}
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function switchToIdentificationConfigForRetry() {
|
||||
if (!state.review) return;
|
||||
state.retryDeviceId = state.review.deviceId;
|
||||
@@ -464,11 +645,16 @@ async function submitRetryReview() {
|
||||
|
||||
function renderConfig() {
|
||||
const isVolume = state.configType === "volume";
|
||||
elements.configEditor.value = JSON.stringify(state.configs[state.configType], null, 2);
|
||||
const config = state.configs[state.configType];
|
||||
elements.configForm.innerHTML = configFields[state.configType].map((field) => {
|
||||
const value = field === "levels" ? config[field].join(", ") : config[field];
|
||||
const integer = ["num_runs", "n_order", "repeat"].includes(field);
|
||||
return `<div class="config-field"><label for="config-${field}">${field}</label><input id="config-${field}" data-config-field="${field}" type="${field === "levels" ? "text" : "number"}"${field === "levels" ? "" : ` step="${integer ? "1" : "any"}"` } value="${escapeHtml(value)}" required></div>`;
|
||||
}).join("");
|
||||
elements.configLabel.textContent = isVolume ? "容积测量配置" : "系统辨识配置";
|
||||
elements.publishTarget.textContent = isVolume ? "Server 配置文件" : "设备辨识配置";
|
||||
elements.publishButton.textContent = isVolume ? "上传容积配置" : "发布辨识配置";
|
||||
elements.configPath.textContent = "可直接编辑,或从本地 JSON 导入";
|
||||
elements.configPath.textContent = "字段名称固定;可填写右侧数据或从本地 JSON 导入";
|
||||
elements.publishResult.textContent = "等待操作";
|
||||
delete elements.publishResult.dataset.tone;
|
||||
}
|
||||
@@ -527,6 +713,60 @@ document.querySelector("#line-form").addEventListener("submit", async (event) =>
|
||||
await refreshOrganizations();
|
||||
});
|
||||
|
||||
document.querySelector("#delete-line").addEventListener("click", async () => {
|
||||
const company = selectedCompany();
|
||||
const line = selectedLine();
|
||||
if (!company || !line) return showError(new Error("请先选择公司和产线"));
|
||||
const confirmation = await requestModelName({
|
||||
title: "确认删除产线",
|
||||
message: `删除产线 ${line.name} 后将删除该产线关联的模型、辨识数据、容积配置、通知与许可证记录(已撤销)。请输入完整 deviceId 以确认。`,
|
||||
value: "",
|
||||
confirmLabel: "删除产线",
|
||||
danger: true,
|
||||
expectedValue: line.deviceId,
|
||||
expectedLabel: "deviceId",
|
||||
ignoreCase: true
|
||||
});
|
||||
if (confirmation === null) return;
|
||||
const result = await runBusy("正在删除产线", () => window.reinloop.deleteProductionLine({
|
||||
companyId: company.id,
|
||||
productionLineId: line.id,
|
||||
credentials: credentials()
|
||||
}));
|
||||
if (!result) return;
|
||||
elements.licenseDetail.hidden = true;
|
||||
elements.licenseDetail.textContent = "";
|
||||
await refreshOrganizations();
|
||||
await refreshLicenses();
|
||||
setStatus("产线已删除", "success");
|
||||
});
|
||||
|
||||
document.querySelector("#delete-company").addEventListener("click", async () => {
|
||||
const company = selectedCompany();
|
||||
if (!company) return showError(new Error("请先选择公司"));
|
||||
const confirmation = await requestModelName({
|
||||
title: "确认删除公司",
|
||||
message: `删除公司 ${company.name} 会级联删除其下所有产线及关联模型、辨识数据、容积配置、通知与许可证记录(有效许可证会阻止删除)。请输入公司编码以确认。`,
|
||||
value: "",
|
||||
confirmLabel: "删除公司",
|
||||
danger: true,
|
||||
expectedValue: company.code,
|
||||
expectedLabel: "公司编码",
|
||||
ignoreCase: true
|
||||
});
|
||||
if (confirmation === null) return;
|
||||
const result = await runBusy("正在删除公司", () => window.reinloop.deleteCompany({
|
||||
companyId: company.id,
|
||||
credentials: credentials()
|
||||
}));
|
||||
if (!result) return;
|
||||
elements.licenseDetail.hidden = true;
|
||||
elements.licenseDetail.textContent = "";
|
||||
await refreshOrganizations();
|
||||
await refreshLicenses();
|
||||
setStatus("公司已删除", "success");
|
||||
});
|
||||
|
||||
document.querySelector("#license-form").addEventListener("submit", async (event) => {
|
||||
event.preventDefault();
|
||||
const company = selectedCompany();
|
||||
@@ -551,6 +791,7 @@ elements.licenseList.addEventListener("click", async (event) => {
|
||||
const detailId = button.dataset.licenseDetail;
|
||||
const downloadId = button.dataset.licenseDownload;
|
||||
const revokeId = button.dataset.licenseRevoke;
|
||||
const deleteId = button.dataset.licenseDelete;
|
||||
if (detailId) {
|
||||
const result = await runBusy("正在读取许可证详情", () => window.reinloop.getLicense({ licenseId: detailId, credentials: credentials() }));
|
||||
if (result) {
|
||||
@@ -571,11 +812,45 @@ elements.licenseList.addEventListener("click", async (event) => {
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (deleteId) {
|
||||
const confirmation = await requestModelName({
|
||||
title: "确认删除许可证",
|
||||
message: `已撤销许可证才能删除。请输入许可证 ID 以确认永久删除。\n\n当前许可证 ID:${deleteId}`,
|
||||
value: "",
|
||||
confirmLabel: "永久删除",
|
||||
danger: true,
|
||||
expectedValue: deleteId,
|
||||
expectedLabel: "许可证ID",
|
||||
ignoreCase: true
|
||||
});
|
||||
if (confirmation === null) return;
|
||||
button.disabled = true;
|
||||
try {
|
||||
const result = await runBusy("正在删除许可证", () => window.reinloop.deleteLicense({
|
||||
licenseId: deleteId,
|
||||
credentials: credentials()
|
||||
}));
|
||||
if (result) {
|
||||
await refreshLicenses();
|
||||
elements.licenseDetail.hidden = true;
|
||||
elements.licenseDetail.textContent = "";
|
||||
setStatus("许可证已删除", "success");
|
||||
}
|
||||
} finally {
|
||||
button.disabled = false;
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (revokeId) {
|
||||
const reason = window.prompt("请输入撤销原因", "管理员撤销");
|
||||
const reason = await requestModelName({
|
||||
title: "确认撤销许可证",
|
||||
message: "请输入撤销原因。确认后将立即撤销该许可证。",
|
||||
value: "管理员撤销",
|
||||
confirmLabel: "确认撤销",
|
||||
danger: true
|
||||
});
|
||||
if (reason === null) return;
|
||||
const trimmedReason = reason.trim() || "管理员撤销";
|
||||
if (!window.confirm(`确认撤销此许可证?\n原因:${trimmedReason}`)) return;
|
||||
button.disabled = true;
|
||||
try {
|
||||
const result = await runBusy("正在撤销许可证", () => window.reinloop.revokeLicense({
|
||||
@@ -604,20 +879,29 @@ document.querySelector("#upload-model").addEventListener("click", async () => {
|
||||
state.models = models.fileList;
|
||||
const selected = await runBusy("正在选择模型文件", () => window.reinloop.chooseModelUploadFile());
|
||||
if (!selected) return;
|
||||
const existing = state.models.find((model) => model.fileName === selected.fileName);
|
||||
const modelName = await requestModelName({
|
||||
title: "设置服务器文件名",
|
||||
message: `已选择 ${selected.fileName}。可在上传前修改模型在服务器上的文件名。`,
|
||||
value: selected.fileName,
|
||||
confirmLabel: "继续上传"
|
||||
});
|
||||
if (modelName === null) return;
|
||||
const existing = state.models.find((model) => model.fileName === modelName);
|
||||
let overwrite = false;
|
||||
if (existing) {
|
||||
if (!window.confirm(`已存在同名模型 ${selected.fileName},覆盖后无法恢复。是否继续?`)) return;
|
||||
const confirmation = window.prompt(`请输入完整文件名以确认覆盖:${selected.fileName}`);
|
||||
if (confirmation !== selected.fileName) {
|
||||
setStatus("文件名不匹配,已取消覆盖", "idle");
|
||||
return;
|
||||
}
|
||||
const confirmation = await requestModelName({
|
||||
title: "确认覆盖模型",
|
||||
message: `服务器已存在 ${modelName},覆盖后无法恢复。请输入完整文件名以确认。`,
|
||||
confirmLabel: "覆盖并上传",
|
||||
danger: true,
|
||||
expectedValue: modelName
|
||||
});
|
||||
if (confirmation === null) return;
|
||||
overwrite = true;
|
||||
}
|
||||
const result = await runBusy("正在上传模型", () => window.reinloop.uploadModel({
|
||||
deviceId: elements.deviceId.value, sourcePath: selected.sourcePath, fileName: selected.fileName,
|
||||
overwrite, credentials: credentials()
|
||||
modelName, overwrite, credentials: credentials()
|
||||
}));
|
||||
if (result) {
|
||||
setStatus(overwrite ? "模型已覆盖" : "模型已上传", "success");
|
||||
@@ -628,25 +912,33 @@ document.querySelector("#upload-model").addEventListener("click", async () => {
|
||||
}
|
||||
});
|
||||
elements.modelList.addEventListener("click", async (event) => {
|
||||
const fileID = event.target.dataset.modelDownload || event.target.dataset.modelDelete;
|
||||
const button = event.target.closest("button");
|
||||
if (!button || !elements.modelList.contains(button)) return;
|
||||
const fileID = button.dataset.modelDownload || button.dataset.modelDelete;
|
||||
if (!fileID) return;
|
||||
const model = state.models.find((item) => item.fileID === fileID);
|
||||
if (event.target.dataset.modelDownload) {
|
||||
if (!model) return showError(new Error("模型记录已变化,请刷新列表后重试"));
|
||||
if (button.dataset.modelDownload) {
|
||||
const result = await runBusy("正在下载模型", () => window.reinloop.downloadModel({ fileID, fileName: model.fileName, credentials: credentials() }));
|
||||
if (result) setStatus(`模型已下载: ${result.filePath}`, "success");
|
||||
} else {
|
||||
if (!window.confirm(`确认删除模型 ${model.fileName}?`)) return;
|
||||
const confirmation = window.prompt(`删除不可恢复。请输入完整文件名以确认:${model.fileName}`);
|
||||
if (confirmation !== model.fileName) {
|
||||
setStatus("文件名不匹配,已取消删除", "idle");
|
||||
return;
|
||||
}
|
||||
event.target.disabled = true;
|
||||
const confirmation = await requestModelName({
|
||||
title: "确认删除模型",
|
||||
message: `删除 ${model.fileName} 后无法恢复。请输入完整文件名以确认。`,
|
||||
confirmLabel: "永久删除",
|
||||
danger: true,
|
||||
expectedValue: model.fileName
|
||||
});
|
||||
if (confirmation === null) return;
|
||||
button.disabled = true;
|
||||
try {
|
||||
const result = await runBusy("正在删除模型", () => window.reinloop.deleteModel({ fileID, credentials: credentials() }));
|
||||
if (result) await refreshModels();
|
||||
if (result) {
|
||||
await refreshModels();
|
||||
setStatus("模型已删除", "success");
|
||||
}
|
||||
} finally {
|
||||
event.target.disabled = false;
|
||||
button.disabled = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
@@ -664,6 +956,12 @@ elements.identificationFileList.addEventListener("click", async (event) => {
|
||||
if (result) {
|
||||
showImage(result);
|
||||
activateTab("plot-panel");
|
||||
if ((file.mediaType !== "json") && file.runId) {
|
||||
state.review = { runId: file.runId, deviceId: elements.deviceId.value };
|
||||
elements.reviewTarget.textContent = `${result.fileName} (runId: ${file.runId})`;
|
||||
elements.approveReview.disabled = false;
|
||||
elements.rejectReview.disabled = false;
|
||||
}
|
||||
}
|
||||
} else if (event.target.dataset.identificationDownload) {
|
||||
const result = await runBusy("正在保存辨识原始数据", () => window.reinloop.downloadIdentificationFile({
|
||||
@@ -688,8 +986,13 @@ elements.identificationFileList.addEventListener("click", async (event) => {
|
||||
document.querySelector("#import-config").addEventListener("click", async () => {
|
||||
const result = await runBusy("正在导入配置", () => window.reinloop.chooseConfig());
|
||||
if (!result) return;
|
||||
state.configs[state.configType] = result.parameters;
|
||||
elements.configEditor.value = JSON.stringify(result.parameters, null, 2);
|
||||
try {
|
||||
state.configs[state.configType] = normalizeImportedConfig(result.parameters);
|
||||
renderConfig();
|
||||
} catch (error) {
|
||||
showError(error);
|
||||
return;
|
||||
}
|
||||
elements.configPath.textContent = result.filePath;
|
||||
setStatus("配置已导入", "success");
|
||||
});
|
||||
@@ -700,8 +1003,13 @@ document.querySelector("#load-server").addEventListener("click", async () => {
|
||||
credentials: credentials()
|
||||
}));
|
||||
if (!parameters) return;
|
||||
state.configs[state.configType] = parameters;
|
||||
elements.configEditor.value = JSON.stringify(parameters, null, 2);
|
||||
try {
|
||||
state.configs[state.configType] = normalizeImportedConfig(parameters);
|
||||
renderConfig();
|
||||
} catch (error) {
|
||||
showError(new Error(`Server 配置无效:${error.message}`));
|
||||
return;
|
||||
}
|
||||
elements.publishResult.textContent = "已读取当前 Server 配置";
|
||||
elements.publishResult.dataset.tone = "success";
|
||||
setStatus("读取完成", "success");
|
||||
@@ -710,9 +1018,9 @@ document.querySelector("#load-server").addEventListener("click", async () => {
|
||||
elements.publishButton.addEventListener("click", async () => {
|
||||
let parameters;
|
||||
try {
|
||||
parameters = JSON.parse(elements.configEditor.value);
|
||||
parameters = readConfigForm({ showErrors: true });
|
||||
} catch (error) {
|
||||
showError(new Error(`JSON 格式错误: ${error.message}`));
|
||||
showError(error);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -732,6 +1040,10 @@ elements.publishButton.addEventListener("click", async () => {
|
||||
? `${result.message}: ${result.storagePath}`
|
||||
: result.message;
|
||||
elements.publishResult.dataset.tone = "success";
|
||||
if (state.configType === "volume") {
|
||||
const notification = state.notifications.find((item) => item.type === "volume_request_started");
|
||||
if (notification) void acknowledgeNotification(notification);
|
||||
}
|
||||
if (state.configType === "identification" && state.retryDeviceId) {
|
||||
await submitRetryReview();
|
||||
} else {
|
||||
@@ -831,3 +1143,86 @@ elements.controlFileList.addEventListener("click", async (event) => {
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
function renderNotifications() {
|
||||
elements.configNotificationBadge.hidden = state.notifications.length === 0;
|
||||
elements.configNotificationBadge.textContent = String(state.notifications.length);
|
||||
elements.configNotifications.innerHTML = state.notifications.map((notification) => {
|
||||
const action = notification.type === "volume_request_started"
|
||||
? "处理配置"
|
||||
: notification.type === "volume_result_ready"
|
||||
? "查看结果"
|
||||
: "查看绘图";
|
||||
const secondaryAction = notification.type === "identification_result_ready" ? "查看辨识数据" : "";
|
||||
const traces = [
|
||||
notification.runId ? `runId: ${notification.runId}` : "",
|
||||
notification.fileID ? `fileID: ${notification.fileID}` : "",
|
||||
notification.requestId ? `requestId: ${notification.requestId}` : ""
|
||||
].filter(Boolean).join(" | ");
|
||||
return `<article class="panel-notification" data-notification-id="${escapeHtml(notification.notificationId)}" data-type="${escapeHtml(notification.type)}"><div class="notification-copy"><strong>${escapeHtml(notification.title)}</strong><span>${escapeHtml(notification.message)}</span>${traces ? `<small class="notification-trace">${escapeHtml(traces)}</small>` : ""}</div><div class="notification-actions"><button class="button secondary" data-notification-action="primary" data-notification-id="${escapeHtml(notification.notificationId)}">${action}</button>${secondaryAction ? `<button class="button secondary" data-notification-action="secondary" data-notification-id="${escapeHtml(notification.notificationId)}">${secondaryAction}</button>` : ""}<button class="button icon" title="关闭提醒" aria-label="关闭提醒" data-notification-action="close" data-notification-id="${escapeHtml(notification.notificationId)}">x</button></div></article>`;
|
||||
}).join("");
|
||||
}
|
||||
|
||||
async function acknowledgeNotification(notification) {
|
||||
setStatus("正在确认提醒", "busy");
|
||||
try {
|
||||
await window.reinloop.acknowledgeNotification({
|
||||
deviceId: notification.deviceId,
|
||||
notificationId: notification.notificationId,
|
||||
credentials: credentials()
|
||||
});
|
||||
} catch (error) {
|
||||
const message = String(error?.message || error)
|
||||
.replace(/^Error invoking remote method '[^']+': Error: /, "");
|
||||
if (!message.includes("Panel 通知不存在")) {
|
||||
showError(error);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
state.notifications = state.notifications.filter((item) => item.notificationId !== notification.notificationId);
|
||||
renderNotifications();
|
||||
setStatus("提醒已关闭", "success");
|
||||
return true;
|
||||
}
|
||||
|
||||
async function handleNotificationAction(notification, action) {
|
||||
if (action === "close") {
|
||||
await acknowledgeNotification(notification);
|
||||
return;
|
||||
}
|
||||
if (notification.type === "volume_request_started") {
|
||||
activateTab("config-panel");
|
||||
activateConfigType("volume");
|
||||
elements.configForm.querySelector("input")?.focus();
|
||||
return;
|
||||
}
|
||||
if (notification.type === "volume_result_ready") {
|
||||
const result = await runBusy("正在打开容积测试结果", () => window.reinloop.openNotificationFile({
|
||||
fileID: notification.fileID,
|
||||
credentials: credentials()
|
||||
}));
|
||||
if (result) setStatus(`已打开结果文件:${result.filePath}`, "success");
|
||||
return;
|
||||
}
|
||||
if (action === "primary") {
|
||||
activateTab("plot-panel");
|
||||
return;
|
||||
}
|
||||
activateTab("identification-data-panel");
|
||||
await refreshIdentificationFiles();
|
||||
}
|
||||
|
||||
elements.configNotifications.addEventListener("click", (event) => {
|
||||
const button = event.target.closest("button[data-notification-id]");
|
||||
if (!button) return;
|
||||
const notification = state.notifications.find((item) => item.notificationId === button.dataset.notificationId);
|
||||
if (notification) void handleNotificationAction(notification, button.dataset.notificationAction);
|
||||
});
|
||||
|
||||
window.reinloop.onPanelNotification((notification) => {
|
||||
if (!notification?.notificationId || notification.deviceId !== elements.deviceId.value) return;
|
||||
state.notificationDeviceId = notification.deviceId;
|
||||
if (state.notifications.some((item) => item.notificationId === notification.notificationId)) return;
|
||||
state.notifications.push(notification);
|
||||
renderNotifications();
|
||||
});
|
||||
@@ -124,6 +124,7 @@ input:focus, select:focus, textarea:focus { border-color: var(--green); box-shad
|
||||
font-weight: 700;
|
||||
}
|
||||
.tab.active { color: var(--green-dark); border-bottom-color: var(--green); }
|
||||
.tab-badge { display: inline-grid; min-width: 18px; height: 18px; place-items: center; margin-left: 5px; padding: 0 5px; border-radius: 9px; color: white; background: var(--red); font-size: 11px; }
|
||||
|
||||
.panel { display: none; padding-top: 22px; }
|
||||
.panel.active { display: block; animation: reveal 180ms ease-out; }
|
||||
@@ -189,26 +190,32 @@ input:focus, select:focus, textarea:focus { border-color: var(--green); box-shad
|
||||
.segmented { display: flex; padding: 3px; background: #dfe5e1; border: 1px solid #c6d0ca; border-radius: 4px; }
|
||||
.segment { height: 34px; padding: 0 16px; border: 0; border-radius: 3px; background: transparent; color: var(--muted); font-weight: 700; }
|
||||
.segment.active { background: white; color: var(--green-dark); box-shadow: 0 1px 3px rgba(21, 37, 31, 0.14); }
|
||||
.notification-stack { display: grid; gap: 8px; margin: 0 0 16px; }
|
||||
.panel-notification { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 16px; align-items: center; padding: 12px 14px; border: 1px solid #d6b362; border-left: 4px solid var(--amber); background: #fff8e6; }
|
||||
.panel-notification[data-type="volume_result_ready"] { border-color: #8ebba5; border-left-color: var(--green); background: #edf8f1; }
|
||||
.panel-notification[data-type="identification_result_ready"] { border-color: #9db6c8; border-left-color: #367294; background: #edf5fa; }
|
||||
.notification-copy { display: grid; gap: 3px; min-width: 0; }
|
||||
.notification-copy strong { font-size: 13px; }
|
||||
.notification-copy span { color: var(--muted); font-size: 12px; overflow-wrap: anywhere; }
|
||||
.notification-copy .notification-trace {
|
||||
display: block;
|
||||
margin-top: 4px;
|
||||
color: var(--text-muted);
|
||||
font-size: 12px;
|
||||
font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
|
||||
word-break: break-all;
|
||||
}
|
||||
.notification-actions { display: flex; gap: 8px; }
|
||||
.editor-layout { display: grid; grid-template-columns: minmax(0, 1fr) 270px; gap: 18px; }
|
||||
.editor-column, .publish-aside { background: var(--surface); border: 1px solid var(--line); }
|
||||
.editor-toolbar { height: 46px; padding: 0 14px; display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--line); font-size: 13px; font-weight: 700; }
|
||||
.text-button { border: 0; background: transparent; color: var(--green); font-size: 13px; font-weight: 700; }
|
||||
textarea {
|
||||
display: block;
|
||||
width: calc(100% - 28px);
|
||||
height: calc(100vh - 385px);
|
||||
min-height: 330px;
|
||||
margin: 14px;
|
||||
padding: 16px;
|
||||
resize: vertical;
|
||||
border: 1px solid #bec9c2;
|
||||
border-radius: 3px;
|
||||
background: #f8faf8;
|
||||
color: #18392d;
|
||||
font: 14px/1.65 Consolas, "Microsoft YaHei UI", monospace;
|
||||
tab-size: 2;
|
||||
outline: none;
|
||||
}
|
||||
.config-form { display: grid; gap: 10px; padding: 14px; }
|
||||
.config-field { display: grid; grid-template-columns: minmax(160px, 0.42fr) minmax(0, 1fr); align-items: center; gap: 12px; }
|
||||
.config-field label { color: var(--muted); font: 13px Consolas, monospace; font-weight: 700; }
|
||||
.config-field input { width: 100%; }
|
||||
.config-field-error { grid-column: 2; margin: -4px 0 0; color: var(--red); font-size: 12px; }
|
||||
.config-field input[aria-invalid="true"] { border-color: var(--red); box-shadow: 0 0 0 2px rgba(173, 52, 45, 0.12); }
|
||||
.editor-column > .file-path { padding: 0 14px 12px; }
|
||||
.publish-aside { padding: 20px; align-self: start; }
|
||||
.publish-aside h3 { padding-bottom: 14px; border-bottom: 1px solid var(--line); }
|
||||
@@ -238,6 +245,16 @@ td:last-child { white-space: nowrap; }
|
||||
.empty-row { padding: 30px; color: var(--muted); text-align: center; font-size: 13px; }
|
||||
.detail-view { margin: 0; padding: 16px; max-height: 260px; overflow: auto; background: #18251f; color: #d9e9df; font: 12px/1.6 Consolas, monospace; white-space: pre-wrap; overflow-wrap: anywhere; }
|
||||
|
||||
.action-dialog { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; padding: 24px; background: rgba(10, 20, 16, 0.68); }
|
||||
.action-dialog-shell { width: min(520px, 100%); padding: 24px; display: grid; gap: 17px; background: var(--surface); border: 1px solid #9aa9a1; border-top: 4px solid var(--green); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); }
|
||||
.action-dialog-head { display: grid; gap: 2px; }
|
||||
.action-dialog-message { color: var(--muted); font-size: 13px; line-height: 1.65; overflow-wrap: anywhere; }
|
||||
.action-dialog-field { display: grid; gap: 7px; }
|
||||
.action-dialog-field span { color: var(--muted); font-size: 12px; font-weight: 700; }
|
||||
.action-dialog-field input { width: 100%; }
|
||||
.action-dialog-error { color: var(--red); font-size: 12px; }
|
||||
.action-dialog-actions { display: flex; justify-content: end; gap: 8px; padding-top: 3px; }
|
||||
|
||||
.lightbox { position: fixed; inset: 0; z-index: 20; padding: 24px; background: rgba(10, 20, 16, 0.78); }
|
||||
.lightbox-shell { height: 100%; display: grid; grid-template-rows: auto minmax(0, 1fr); background: var(--surface); border: 1px solid #9aa9a1; box-shadow: 0 20px 60px rgba(0, 0, 0, 0.35); }
|
||||
.lightbox-toolbar { min-height: 58px; padding: 10px 14px; display: flex; align-items: center; justify-content: space-between; gap: 16px; border-bottom: 1px solid var(--line); }
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
|
||||
function createModelHandlers({ callServer, fetchImpl = fetch }) {
|
||||
return {
|
||||
list(request) {
|
||||
return callServer({
|
||||
type: "listModels",
|
||||
folder: `${request.deviceId}/model_config`
|
||||
}, request.credentials);
|
||||
},
|
||||
|
||||
async upload(request) {
|
||||
if (!request.sourcePath || !request.fileName) throw new Error("请先选择模型文件");
|
||||
const sourcePath = path.resolve(request.sourcePath);
|
||||
const fileName = path.basename(request.fileName);
|
||||
const modelName = path.basename(request.modelName || fileName);
|
||||
await fs.promises.access(sourcePath, fs.constants.R_OK);
|
||||
const issued = await callServer({
|
||||
type: "issueModelUpload",
|
||||
deviceId: request.deviceId,
|
||||
fileName,
|
||||
modelName,
|
||||
overwrite: request.overwrite === true
|
||||
}, request.credentials);
|
||||
const form = new FormData();
|
||||
form.append("file", new Blob([await fs.promises.readFile(sourcePath)]), fileName);
|
||||
const response = await fetchImpl(issued.uploadMetadata.url, { method: "POST", body: form });
|
||||
if (![200, 204].includes(response.status)) throw new Error(`模型上传失败: HTTP ${response.status}`);
|
||||
return {
|
||||
success: true,
|
||||
fileID: issued.fileID,
|
||||
fileName: issued.fileName,
|
||||
originalFileName: issued.originalFileName
|
||||
};
|
||||
},
|
||||
|
||||
delete(request) {
|
||||
return callServer({ type: "deleteModel", fileID: request.fileID }, request.credentials);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
module.exports = { createModelHandlers };
|
||||
@@ -39,21 +39,13 @@ async function callServer(payload, options = {}) {
|
||||
return result;
|
||||
}
|
||||
|
||||
function downloadHeaders(options = {}) {
|
||||
if (!options.adminToken) return {};
|
||||
return {
|
||||
authorization: `Bearer ${options.adminToken}`,
|
||||
"x-admin-token": options.adminToken
|
||||
};
|
||||
}
|
||||
|
||||
async function downloadFromUrl(url, fileName, options = {}) {
|
||||
await fs.promises.mkdir(DOWNLOAD_DIR, { recursive: true });
|
||||
return downloadToPath(url, path.join(DOWNLOAD_DIR, path.basename(fileName)), options);
|
||||
}
|
||||
|
||||
async function downloadToPath(url, destination, options = {}) {
|
||||
const response = await fetch(url, { headers: downloadHeaders(options) });
|
||||
const response = await fetch(url);
|
||||
if (!response.ok || !response.body) {
|
||||
throw new Error(`下载失败: HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const test = require("node:test");
|
||||
const { createModelHandlers } = require("../model-handlers");
|
||||
const { callServer } = require("../server-client");
|
||||
const { createApp } = require("../../server/src/app");
|
||||
const { JsonStore } = require("../../server/src/store");
|
||||
|
||||
test("panel model kernel renames, overwrites, lists both names, and deletes on the server", async (context) => {
|
||||
const dataDirectory = await fs.promises.mkdtemp(path.join(os.tmpdir(), "reinloop-panel-server-"));
|
||||
const store = new JsonStore(dataDirectory);
|
||||
await store.initialize();
|
||||
const app = createApp({ store, adminToken: "test-token" });
|
||||
const server = await new Promise((resolve) => {
|
||||
const listeningServer = app.listen(0, "127.0.0.1", () => resolve(listeningServer));
|
||||
});
|
||||
context.after(async () => {
|
||||
await new Promise((resolve, reject) => server.close((error) => error ? reject(error) : resolve()));
|
||||
await fs.promises.rm(dataDirectory, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
const credentials = {
|
||||
apiUrl: `http://127.0.0.1:${server.address().port}`,
|
||||
adminToken: "test-token"
|
||||
};
|
||||
const handlers = createModelHandlers({ callServer });
|
||||
const sourcePath = path.join(dataDirectory, "controller-original.bin");
|
||||
const request = {
|
||||
deviceId: "company-a/line-1",
|
||||
sourcePath,
|
||||
fileName: "controller-original.bin",
|
||||
modelName: "pressure-controller.bin",
|
||||
credentials
|
||||
};
|
||||
|
||||
await fs.promises.writeFile(sourcePath, "first-version");
|
||||
const uploaded = await handlers.upload(request);
|
||||
assert.equal(uploaded.fileName, "pressure-controller.bin");
|
||||
assert.equal(uploaded.originalFileName, "controller-original.bin");
|
||||
|
||||
await fs.promises.writeFile(sourcePath, "second-version");
|
||||
const overwritten = await handlers.upload({ ...request, overwrite: true });
|
||||
assert.equal(overwritten.fileID, uploaded.fileID);
|
||||
|
||||
const listed = await handlers.list({ deviceId: request.deviceId, credentials });
|
||||
assert.equal(listed.fileList.length, 1);
|
||||
assert.equal(listed.fileList[0].fileName, "pressure-controller.bin");
|
||||
assert.equal(listed.fileList[0].originalFileName, "controller-original.bin");
|
||||
|
||||
const deleted = await handlers.delete({ fileID: overwritten.fileID, credentials });
|
||||
assert.equal(deleted.deletedCount, 1);
|
||||
const afterDelete = await handlers.list({ deviceId: request.deviceId, credentials });
|
||||
assert.deepEqual(afterDelete.fileList, []);
|
||||
await assert.rejects(fs.promises.access(
|
||||
path.join(dataDirectory, "models", "company-a", "line-1", "pressure-controller.bin")
|
||||
), { code: "ENOENT" });
|
||||
});
|
||||
@@ -0,0 +1,62 @@
|
||||
const assert = require("node:assert/strict");
|
||||
const fs = require("node:fs");
|
||||
const os = require("node:os");
|
||||
const path = require("node:path");
|
||||
const test = require("node:test");
|
||||
const { createModelHandlers } = require("../model-handlers");
|
||||
|
||||
test("panel model kernel renames, overwrites, and deletes by fileID", async (context) => {
|
||||
const directory = await fs.promises.mkdtemp(path.join(os.tmpdir(), "reinloop-panel-model-"));
|
||||
context.after(() => fs.promises.rm(directory, { recursive: true, force: true }));
|
||||
const sourcePath = path.join(directory, "controller-original.bin");
|
||||
await fs.promises.writeFile(sourcePath, "first-version");
|
||||
|
||||
const records = new Map();
|
||||
const requests = [];
|
||||
const callServer = async (payload) => {
|
||||
requests.push(payload);
|
||||
if (payload.type === "issueModelUpload") {
|
||||
const fileID = `model://${payload.deviceId}/${payload.modelName}`;
|
||||
if (records.has(fileID) && !payload.overwrite) throw new Error("文件已存在");
|
||||
records.set(fileID, {
|
||||
fileID,
|
||||
fileName: payload.modelName,
|
||||
originalFileName: payload.fileName
|
||||
});
|
||||
return { ...records.get(fileID), uploadMetadata: { url: `memory://${encodeURIComponent(fileID)}` } };
|
||||
}
|
||||
if (payload.type === "deleteModel") {
|
||||
const deleted = records.delete(payload.fileID);
|
||||
return { success: true, deletedCount: deleted ? 1 : 0 };
|
||||
}
|
||||
throw new Error(`unexpected request: ${payload.type}`);
|
||||
};
|
||||
const handlers = createModelHandlers({
|
||||
callServer,
|
||||
fetchImpl: async () => ({ status: 204 })
|
||||
});
|
||||
const request = {
|
||||
deviceId: "company-a/line-1",
|
||||
sourcePath,
|
||||
fileName: "controller-original.bin",
|
||||
modelName: "pressure-controller.bin",
|
||||
credentials: { adminToken: "test-token" }
|
||||
};
|
||||
|
||||
const uploaded = await handlers.upload(request);
|
||||
assert.equal(uploaded.fileName, "pressure-controller.bin");
|
||||
assert.equal(uploaded.originalFileName, "controller-original.bin");
|
||||
assert.equal(records.size, 1);
|
||||
assert.equal(requests[0].deviceId, request.deviceId);
|
||||
|
||||
await fs.promises.writeFile(sourcePath, "second-version");
|
||||
const overwritten = await handlers.upload({ ...request, overwrite: true });
|
||||
assert.equal(overwritten.fileID, uploaded.fileID);
|
||||
assert.equal(records.size, 1);
|
||||
|
||||
const deleted = await handlers.delete({ fileID: overwritten.fileID, credentials: request.credentials });
|
||||
assert.equal(deleted.deletedCount, 1);
|
||||
assert.equal(records.size, 0);
|
||||
assert.equal(requests.at(-1).type, "deleteModel");
|
||||
assert.equal(requests.at(-1).fileID, overwritten.fileID);
|
||||
});
|
||||
@@ -49,14 +49,14 @@ $env:B_ADMIN_TOKEN="your-admin-token"
|
||||
npm start
|
||||
```
|
||||
|
||||
默认服务地址为 `http://127.0.0.1:3000`,健康检查为 `http://127.0.0.1:3000/health`。业务请求可使用根路径或 `/api`。跨设备部署时,应使用服务器局域网 IP 或 HTTPS 域名,而不是 `127.0.0.1`。
|
||||
默认服务地址为 `http://127.0.0.1:3000`,健康检查为 `http://127.0.0.1:3000/health`。业务请求统一使用根路径。跨设备部署时,应使用服务器局域网 IP 或 HTTPS 域名,而不是 `127.0.0.1`。
|
||||
|
||||
### ControlPanel
|
||||
|
||||
```powershell
|
||||
cd ControlPanel
|
||||
npm install
|
||||
$env:REINLOOP_API_URL="http://server-address:3000/api"
|
||||
$env:REINLOOP_API_URL="http://server-address:3000"
|
||||
$env:B_ADMIN_TOKEN="your-admin-token"
|
||||
npm run gui
|
||||
```
|
||||
|
||||
+66
-1
@@ -1,6 +1,10 @@
|
||||
"""ReinLoop cloud-server endpoint configuration shared by core modules."""
|
||||
|
||||
import os
|
||||
import threading
|
||||
import time
|
||||
|
||||
import requests
|
||||
|
||||
from license_utils import get_verified_license
|
||||
|
||||
@@ -11,11 +15,12 @@ base_url = os.environ.get(
|
||||
).rstrip("/")
|
||||
server_api_url = os.environ.get(
|
||||
"REINLOOP_API_URL",
|
||||
f"{base_url}/api",
|
||||
base_url,
|
||||
)
|
||||
# Compatibility alias used by existing modules. It points to the ReinLoop
|
||||
# Express server API, not a cloud-function endpoint.
|
||||
data_record_url = server_api_url
|
||||
device_api_url = f"{server_api_url.rstrip('/')}/device"
|
||||
_license = get_verified_license()
|
||||
_license_device_id = (_license or {}).get("device_id", "").strip()
|
||||
_environment_device_id = os.environ.get("REINLOOP_DEVICE_ID", "").strip()
|
||||
@@ -27,3 +32,63 @@ the_folder = _license_device_id or _environment_device_id or "local-test-device"
|
||||
|
||||
if not the_folder:
|
||||
raise RuntimeError("设备 ID 不能为空")
|
||||
|
||||
_DEVICE_TOKEN = None
|
||||
_DEVICE_TOKEN_EXPIRES_AT_MS = 0
|
||||
_DEVICE_TOKEN_LOCK = threading.Lock()
|
||||
|
||||
|
||||
def _current_license_context():
|
||||
payload = get_verified_license() or {}
|
||||
license_id = str(payload.get("license_id") or "").strip()
|
||||
device_id = str(payload.get("device_id") or _environment_device_id or the_folder).strip()
|
||||
return license_id, device_id
|
||||
|
||||
|
||||
def _ensure_device_token(timeout=10):
|
||||
global _DEVICE_TOKEN, _DEVICE_TOKEN_EXPIRES_AT_MS
|
||||
with _DEVICE_TOKEN_LOCK:
|
||||
now_ms = int(time.time() * 1000)
|
||||
if _DEVICE_TOKEN and _DEVICE_TOKEN_EXPIRES_AT_MS - now_ms > 30_000:
|
||||
return _DEVICE_TOKEN
|
||||
|
||||
license_id, device_id = _current_license_context()
|
||||
if not license_id:
|
||||
raise RuntimeError("许可证未就绪,无法获取 deviceToken")
|
||||
|
||||
response = requests.post(device_api_url, json={
|
||||
"type": "deviceAuth",
|
||||
"licenseId": license_id,
|
||||
"deviceId": device_id,
|
||||
}, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
if not result.get("success"):
|
||||
raise RuntimeError(result.get("errMsg") or "设备鉴权失败")
|
||||
|
||||
_DEVICE_TOKEN = str(result.get("deviceToken") or "").strip()
|
||||
_DEVICE_TOKEN_EXPIRES_AT_MS = int(result.get("expiresAtMs") or 0)
|
||||
if not _DEVICE_TOKEN or _DEVICE_TOKEN_EXPIRES_AT_MS <= now_ms:
|
||||
raise RuntimeError("设备鉴权返回了无效 deviceToken")
|
||||
return _DEVICE_TOKEN
|
||||
|
||||
|
||||
def device_post(payload, timeout=10):
|
||||
"""Call the device-scoped API route with an auto-renewed device token."""
|
||||
token = _ensure_device_token(timeout=timeout)
|
||||
request_payload = {**payload, "deviceToken": token}
|
||||
response = requests.post(device_api_url, json=request_payload, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
if result.get("success"):
|
||||
return result
|
||||
if result.get("errCode") in {"DEVICE_TOKEN_EXPIRED", "DEVICE_TOKEN_INVALID"}:
|
||||
with _DEVICE_TOKEN_LOCK:
|
||||
global _DEVICE_TOKEN, _DEVICE_TOKEN_EXPIRES_AT_MS
|
||||
_DEVICE_TOKEN = None
|
||||
_DEVICE_TOKEN_EXPIRES_AT_MS = 0
|
||||
token = _ensure_device_token(timeout=timeout)
|
||||
response = requests.post(device_api_url, json={**payload, "deviceToken": token}, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
return result
|
||||
@@ -30,7 +30,7 @@ def _init_license():
|
||||
# 生产环境(exe 打包)→ 严格执行验签
|
||||
from license_utils import check_license
|
||||
|
||||
check_license() # 验签并启动唯一的后台巡检线程,失败直接退出
|
||||
check_license() # 仅启动时验签,失败直接退出
|
||||
|
||||
_LICENSE_CHECKED = True
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ import datetime
|
||||
import threading
|
||||
import requests
|
||||
|
||||
from api import base_url, data_record_url, the_folder
|
||||
from api import device_post, the_folder
|
||||
|
||||
|
||||
class DataCollector:
|
||||
@@ -47,12 +47,11 @@ class DataCollector:
|
||||
def _upload_to_server(self, data_bytes: bytes, filename: str, folder: str) -> bool:
|
||||
"""向 ReinLoop 云服务器申请上传地址并上传控制数据。"""
|
||||
try:
|
||||
resp = requests.post(data_record_url, json={
|
||||
result = device_post({
|
||||
"type": "uploadDataFile",
|
||||
"fileName": filename,
|
||||
"folder": folder,
|
||||
}, timeout=30)
|
||||
result = resp.json()
|
||||
except Exception as e:
|
||||
self.log(f"向云服务器申请上传地址异常: {e}")
|
||||
return False
|
||||
|
||||
@@ -3,16 +3,12 @@
|
||||
|
||||
def heartbeat_device(timeout=5):
|
||||
"""Refresh the current device's Server heartbeat and return its timestamp."""
|
||||
import requests
|
||||
from api import data_record_url, the_folder
|
||||
from api import device_post
|
||||
|
||||
try:
|
||||
response = requests.post(data_record_url, json={
|
||||
result = device_post({
|
||||
"type": "deviceHeartbeat",
|
||||
"deviceId": the_folder,
|
||||
}, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
except Exception as exc:
|
||||
raise ValueError(f"设备心跳请求失败: {exc}") from exc
|
||||
if not result.get("success"):
|
||||
|
||||
@@ -16,7 +16,7 @@ from collections import deque
|
||||
from get_V import measure_volume
|
||||
from ind_collector import collect_data_with_prbs
|
||||
|
||||
from api import base_url, data_record_url, the_folder
|
||||
from api import device_post, the_folder
|
||||
|
||||
|
||||
class IdentificationManager:
|
||||
@@ -66,12 +66,11 @@ class IdentificationManager:
|
||||
"""
|
||||
# Step 1: 向业务服务器申请一次性上传地址(不传文件内容)
|
||||
try:
|
||||
resp = requests.post(data_record_url, json={
|
||||
result = device_post({
|
||||
"type": "uploadDataFile",
|
||||
"fileName": filename,
|
||||
"folder": folder,
|
||||
}, timeout=30)
|
||||
result = resp.json()
|
||||
except Exception as e:
|
||||
self.log(f"向云服务器申请上传地址异常: {e}")
|
||||
return False
|
||||
@@ -109,6 +108,34 @@ class IdentificationManager:
|
||||
This is an independent pre-scan. It does not replace or modify the
|
||||
subsequent PRBS collection performed by ``collect_data_with_prbs``.
|
||||
"""
|
||||
if getattr(conn_mgr, "simulation", False):
|
||||
stable_pressure_records = [
|
||||
{"distance": 1000, "pressure": 400},
|
||||
{"distance": 900, "pressure": 361},
|
||||
{"distance": 800, "pressure": 322},
|
||||
{"distance": 700, "pressure": 283},
|
||||
{"distance": 600, "pressure": 244},
|
||||
{"distance": 500, "pressure": 205},
|
||||
{"distance": 400, "pressure": 166},
|
||||
{"distance": 300, "pressure": 127},
|
||||
{"distance": 200, "pressure": 88},
|
||||
{"distance": 100, "pressure": 49},
|
||||
{"distance": 0, "pressure": 10},
|
||||
]
|
||||
timestamp = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
|
||||
filename = f"travel_stability_pressures_{timestamp}.json"
|
||||
payload = {"stable_pressures": stable_pressure_records}
|
||||
uploaded = self._upload_to_server(
|
||||
json.dumps(payload, ensure_ascii=False, indent=2),
|
||||
filename,
|
||||
f"{the_folder}/ind_data",
|
||||
)
|
||||
if uploaded:
|
||||
self.log("行程稳态压力 JSON 上传成功,继续执行 PRBS 辨识")
|
||||
else:
|
||||
self.log("行程稳态压力 JSON 上传失败,继续执行 PRBS 辨识")
|
||||
return payload
|
||||
|
||||
distances = list(range(1000, -1, -100))
|
||||
settings = {
|
||||
"min_wait_time": 5.0,
|
||||
|
||||
@@ -130,15 +130,12 @@ def parse_identification_config_csv(csv_text: str) -> dict:
|
||||
def download_identification_config(timeout=20) -> dict:
|
||||
"""Download the current customer's CSV config through the cloud server."""
|
||||
import requests
|
||||
from api import data_record_url, the_folder
|
||||
from api import device_post
|
||||
|
||||
try:
|
||||
response = requests.post(data_record_url, json={
|
||||
result = device_post({
|
||||
"type": "getIdentificationConfig",
|
||||
"deviceId": the_folder,
|
||||
}, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
except Exception as exc:
|
||||
raise ValueError(f"连接云服务器辨识配置服务失败: {exc}") from exc
|
||||
|
||||
|
||||
@@ -2,13 +2,10 @@
|
||||
|
||||
|
||||
def _post(payload, timeout=10):
|
||||
import requests
|
||||
from api import data_record_url
|
||||
from api import device_post
|
||||
|
||||
try:
|
||||
response = requests.post(data_record_url, json=payload, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
result = device_post(payload, timeout=timeout)
|
||||
except Exception as exc:
|
||||
raise ValueError(f"辨识反馈服务请求失败: {exc}") from exc
|
||||
if not result.get("success"):
|
||||
@@ -18,13 +15,10 @@ def _post(payload, timeout=10):
|
||||
|
||||
def register_identification_result(run_id: str, timeout=10) -> None:
|
||||
"""Register one uploaded CSV as the customer's current review target."""
|
||||
from api import the_folder
|
||||
|
||||
if not run_id:
|
||||
raise ValueError("辨识结果缺少 run_id")
|
||||
_post({
|
||||
"type": "registerIdentificationResult",
|
||||
"deviceId": the_folder,
|
||||
"runId": run_id,
|
||||
"fileName": run_id,
|
||||
}, timeout=timeout)
|
||||
@@ -32,11 +26,8 @@ def register_identification_result(run_id: str, timeout=10) -> None:
|
||||
|
||||
def get_identification_feedback(run_id: str, timeout=10):
|
||||
"""Return None while pending, otherwise return the integer 0 or 1."""
|
||||
from api import the_folder
|
||||
|
||||
result = _post({
|
||||
"type": "getIdentificationFeedback",
|
||||
"deviceId": the_folder,
|
||||
"runId": run_id,
|
||||
}, timeout=timeout)
|
||||
if not result.get("ready"):
|
||||
@@ -49,10 +40,7 @@ def get_identification_feedback(run_id: str, timeout=10):
|
||||
|
||||
def acknowledge_identification_feedback(run_id: str, timeout=10) -> None:
|
||||
"""Delete the consumed review record so stale feedback cannot be reused."""
|
||||
from api import the_folder
|
||||
|
||||
_post({
|
||||
"type": "ackIdentificationFeedback",
|
||||
"deviceId": the_folder,
|
||||
"runId": run_id,
|
||||
}, timeout=timeout)
|
||||
|
||||
@@ -13,7 +13,7 @@ from stable_baselines3 import SAC
|
||||
# 关键:禁用 PyTorch 内部多线程,防止在 PyInstaller daemon 线程中 segfault
|
||||
torch.set_num_threads(1)
|
||||
|
||||
from api import base_url, data_record_url, the_folder
|
||||
from api import device_post, the_folder
|
||||
|
||||
|
||||
class ModelManager:
|
||||
@@ -50,8 +50,7 @@ class ModelManager:
|
||||
def fetch_models():
|
||||
try:
|
||||
payload = {"type": "listModels", "folder": f"{the_folder}/model_config"}
|
||||
resp = requests.post(data_record_url, json=payload, timeout=10)
|
||||
result = resp.json()
|
||||
result = device_post(payload, timeout=10)
|
||||
|
||||
if result.get("success"):
|
||||
files = result.get("files", [])
|
||||
@@ -98,8 +97,7 @@ class ModelManager:
|
||||
|
||||
# 获取临时下载 URL
|
||||
payload = {"type": "downloadModel", "fileID": file_id}
|
||||
resp = requests.post(data_record_url, json=payload, timeout=15)
|
||||
result = resp.json()
|
||||
result = device_post(payload, timeout=15)
|
||||
|
||||
if not result.get("success"):
|
||||
err = result.get('errMsg', '未知错误')
|
||||
|
||||
@@ -39,10 +39,10 @@ class SimulatedDevice:
|
||||
if not self.connected or dt == 0:
|
||||
return
|
||||
|
||||
# MT2AM8 的行程控制在当前程序中是反向阀位:较小行程表示更大
|
||||
# 进气。模型因此让压力向由行程决定的目标值缓慢靠近。
|
||||
opening = max(0.0, min(1.0, 1.0 - self.position / 1000.0))
|
||||
target_pressure = self.pressure_range * opening
|
||||
# 行程与阀门开度反向:行程越大,阀门开度越小。阀门开度减小
|
||||
# 时系统压力升高;默认量程下行程 0/1000 分别对应 10/400 kPa。
|
||||
travel_ratio = max(0.0, min(1.0, self.position / 1000.0))
|
||||
target_pressure = 10.0 + (self.pressure_range - 10.0) * travel_ratio
|
||||
response = min(1.0, dt / 8.0)
|
||||
self.pressure += (target_pressure - self.pressure) * response
|
||||
|
||||
|
||||
@@ -84,13 +84,10 @@ def validate_volume_config(config) -> dict:
|
||||
|
||||
|
||||
def _post_volume_request(payload, timeout=10):
|
||||
import requests
|
||||
from api import data_record_url
|
||||
from api import device_post
|
||||
|
||||
try:
|
||||
response = requests.post(data_record_url, json=payload, timeout=timeout)
|
||||
response.raise_for_status()
|
||||
result = response.json()
|
||||
result = device_post(payload, timeout=timeout)
|
||||
except Exception as exc:
|
||||
raise ValueError(f"连接云端容积参数服务失败: {exc}") from exc
|
||||
|
||||
@@ -101,11 +98,8 @@ def _post_volume_request(payload, timeout=10):
|
||||
|
||||
def create_volume_config_request(timeout=10) -> dict:
|
||||
"""Create exactly one cloud request after the customer clicks Test."""
|
||||
from api import the_folder
|
||||
|
||||
result = _post_volume_request({
|
||||
"type": "createVolumeConfigRequest",
|
||||
"deviceId": the_folder,
|
||||
}, timeout=timeout)
|
||||
if not result.get("requestId") or not result.get("expiresAtMs"):
|
||||
raise ValueError("云端未返回有效的容积参数请求编号")
|
||||
@@ -118,11 +112,9 @@ def create_volume_config_request(timeout=10) -> dict:
|
||||
def poll_volume_config_request(request_id: str, timeout=10) -> dict:
|
||||
"""Poll one request; download and validate JSON only when it is ready."""
|
||||
import requests
|
||||
from api import the_folder
|
||||
|
||||
result = _post_volume_request({
|
||||
"type": "getVolumeConfigRequest",
|
||||
"deviceId": the_folder,
|
||||
"requestId": request_id,
|
||||
}, timeout=timeout)
|
||||
if result.get("expired"):
|
||||
@@ -145,10 +137,7 @@ def poll_volume_config_request(request_id: str, timeout=10) -> dict:
|
||||
|
||||
def acknowledge_volume_config_request(request_id: str, timeout=10) -> None:
|
||||
"""Delete the consumed/abandoned request and its temporary JSON file."""
|
||||
from api import the_folder
|
||||
|
||||
_post_volume_request({
|
||||
"type": "ackVolumeConfigRequest",
|
||||
"deviceId": the_folder,
|
||||
"requestId": request_id,
|
||||
}, timeout=timeout)
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
## 约定
|
||||
|
||||
- 业务服务端地址由 `REINLOOP_API_URL` 指定,未设置时使用
|
||||
`REINLOOP_SERVER_URL + /api`;默认地址为
|
||||
`https://ReinLoop.dominatedconvergence.com/api`。
|
||||
`REINLOOP_SERVER_URL`;默认地址为
|
||||
`https://ReinLoop.dominatedconvergence.com`。
|
||||
- 新许可证的设备标识为 `<company_code>/<production_line_code>`,在代码中通过
|
||||
`api.the_folder` 使用。旧许可证才回退到 `REINLOOP_DEVICE_ID`。
|
||||
- 所有服务端业务请求均使用 `POST /api`,通过请求体的 `type` 字段分发。
|
||||
- 所有服务端业务请求均使用 `POST /`,通过请求体的 `type` 字段分发。
|
||||
- 公司、产线、许可证签发/撤销、模型删除、审核反馈及配置提交均为
|
||||
ControlPanel 管理端能力,客户端不提供对应的管理接口。旧微信云函数和其管理脚本
|
||||
已移除。
|
||||
@@ -148,7 +148,7 @@ RL 模式下,`ControlEngine` 使用模型根据流量、当前压力和压力
|
||||
|
||||
## 客户端服务端协议
|
||||
|
||||
所有业务请求都发送至 `POST /api`。业务成功响应应至少包含 `success: true`。
|
||||
所有业务请求都发送至 `POST /`。业务成功响应应至少包含 `success: true`。
|
||||
|
||||
| `type` | 请求关键字段 | 用途 |
|
||||
| --- | --- | --- |
|
||||
|
||||
+37
-20
@@ -41,13 +41,13 @@ Ue6JWRU4j3Wg37WDPbwkO3tQba2jbUQsLYomLGuohfkVAgMBAAE=
|
||||
-----END PUBLIC KEY-----"""
|
||||
# {{LICENSE_PUBLIC_KEY_END}}
|
||||
|
||||
# 许可证文件相对路径
|
||||
LICENSE_FILE = "license.lic"
|
||||
# 许可证文件检索模式(默认在程序目录中匹配)
|
||||
LICENSE_GLOB = "*license.lic"
|
||||
|
||||
# 巡检间隔(分钟)
|
||||
DEFAULT_CHECK_INTERVAL = 5
|
||||
ONLINE_CHECK_TIMEOUT_SECONDS = 5
|
||||
DEFAULT_OFFLINE_HOURS = 72
|
||||
DEFAULT_OFFLINE_HOURS = 100
|
||||
|
||||
# 过期后宽限期(小时),给用户保存工作的时间
|
||||
GRACE_PERIOD_HOURS = 2
|
||||
@@ -164,11 +164,38 @@ def _validate_payload(payload):
|
||||
return has_new_format
|
||||
|
||||
|
||||
def _default_license_dir():
|
||||
"""返回默认许可证搜索目录。"""
|
||||
# PyInstaller 打包后 sys.executable 是 exe 路径
|
||||
return Path(sys.executable).parent if getattr(sys, 'frozen', False) else Path.cwd()
|
||||
|
||||
|
||||
def _resolve_license_path(lic_path=None):
|
||||
"""解析许可证路径。
|
||||
|
||||
- 显式传入 ``lic_path`` 时直接使用该路径。
|
||||
- 未传入时,在默认目录按 ``*license.lic`` 匹配,优先选择最近修改的文件。
|
||||
"""
|
||||
if lic_path is not None:
|
||||
return Path(lic_path)
|
||||
|
||||
search_dir = _default_license_dir()
|
||||
matches = [path for path in search_dir.glob(LICENSE_GLOB) if path.is_file()]
|
||||
if not matches:
|
||||
raise FileNotFoundError(
|
||||
f"未找到许可证文件(模式: {LICENSE_GLOB},目录: {search_dir})"
|
||||
)
|
||||
|
||||
# 多个候选时优先取最新文件;同修改时间再按文件名稳定排序。
|
||||
matches.sort(key=lambda path: (path.stat().st_mtime, path.name), reverse=True)
|
||||
return matches[0]
|
||||
|
||||
|
||||
def verify_license(lic_path=None):
|
||||
"""验证许可证签名 + 有效期。
|
||||
|
||||
Args:
|
||||
lic_path: 许可证文件路径,默认 exe 同级目录下的 license.lic
|
||||
lic_path: 许可证文件路径,默认在程序目录按 *license.lic 自动匹配
|
||||
|
||||
Returns:
|
||||
dict: 许可证 payload(customer, expiry, issued 等)
|
||||
@@ -179,10 +206,7 @@ def verify_license(lic_path=None):
|
||||
RuntimeError: 许可证已过期
|
||||
ValueError: 许可证格式错误
|
||||
"""
|
||||
if lic_path is None:
|
||||
# PyInstaller 打包后 sys.executable 是 exe 路径
|
||||
exe_dir = Path(sys.executable).parent if getattr(sys, 'frozen', False) else Path.cwd()
|
||||
lic_path = exe_dir / LICENSE_FILE
|
||||
lic_path = _resolve_license_path(lic_path)
|
||||
|
||||
if not os.path.exists(lic_path):
|
||||
raise FileNotFoundError(f"许可证文件不存在: {lic_path}")
|
||||
@@ -276,7 +300,7 @@ def _api_url():
|
||||
base_url = os.environ.get(
|
||||
"REINLOOP_SERVER_URL", "https://ReinLoop.dominatedconvergence.com"
|
||||
).rstrip("/")
|
||||
return os.environ.get("REINLOOP_API_URL", f"{base_url}/api")
|
||||
return os.environ.get("REINLOOP_API_URL", base_url)
|
||||
|
||||
|
||||
def _offline_limit_seconds():
|
||||
@@ -531,7 +555,7 @@ def check_license(lic_path=None):
|
||||
"""启动时调用:验证许可证,通过则返回 payload。
|
||||
|
||||
在 main.py 的 main() 函数开头调用一次即可。
|
||||
内部会自动启动后台巡检线程。
|
||||
仅启动时执行校验,不自动启动后台巡检线程。
|
||||
|
||||
Returns:
|
||||
dict: 许可证载荷
|
||||
@@ -549,7 +573,6 @@ def check_license(lic_path=None):
|
||||
with _verified_license_lock:
|
||||
global _verified_license
|
||||
_verified_license = dict(payload)
|
||||
start_license_watchdog()
|
||||
expiry = payload.get("expiry", "未知")
|
||||
customer = payload.get("customer", "未知")
|
||||
_log(f"✅ 许可证有效 | 客户: {customer} | 到期: {expiry}")
|
||||
@@ -559,7 +582,7 @@ def check_license(lic_path=None):
|
||||
_log(f"❌ {e}")
|
||||
_show_error_and_exit(
|
||||
"未找到许可证文件",
|
||||
"请将 license.lic 放到软件根目录,然后重新启动程序。\n\n"
|
||||
"请将许可证文件放到软件根目录(文件名需匹配 *license.lic),然后重新启动程序。\n\n"
|
||||
"如有疑问,请联系厂商获取有效的许可证文件。"
|
||||
)
|
||||
|
||||
@@ -568,7 +591,7 @@ def check_license(lic_path=None):
|
||||
_show_error_and_exit(
|
||||
"许可证验证失败",
|
||||
"许可证签名校验不通过,文件可能已被篡改。\n\n"
|
||||
"请使用原始签发的 license.lic 文件,\n"
|
||||
"请使用原始签发且文件名匹配 *license.lic 的许可证文件,\n"
|
||||
"或联系厂商重新签发。"
|
||||
)
|
||||
|
||||
@@ -626,13 +649,7 @@ def get_license_info(lic_path=None):
|
||||
dict | None: 许可证信息,文件不存在则返回 None
|
||||
"""
|
||||
try:
|
||||
if lic_path is None:
|
||||
exe_dir = (
|
||||
Path(sys.executable).parent
|
||||
if getattr(sys, 'frozen', False)
|
||||
else Path.cwd()
|
||||
)
|
||||
lic_path = exe_dir / LICENSE_FILE
|
||||
lic_path = _resolve_license_path(lic_path)
|
||||
|
||||
if not os.path.exists(lic_path):
|
||||
return None
|
||||
|
||||
@@ -14,7 +14,11 @@ MODULE_PATH = Path(__file__).resolve().parents[1] / "core" / "data_collector.py"
|
||||
def load_data_collector_module():
|
||||
api = types.ModuleType("api")
|
||||
api.base_url = "https://cloud.example"
|
||||
api.data_record_url = "https://cloud.example/api"
|
||||
api.data_record_url = "https://cloud.example"
|
||||
api.device_post = lambda payload, timeout=10: {
|
||||
"success": True,
|
||||
"uploadMetadata": {"url": "https://upload.example"}
|
||||
}
|
||||
api.the_folder = "customer-a/line-1"
|
||||
requests = types.ModuleType("requests")
|
||||
|
||||
|
||||
@@ -16,26 +16,12 @@ class DeviceHeartbeatTests(unittest.TestCase):
|
||||
def test_sends_current_device_id_to_server(self):
|
||||
calls = []
|
||||
requests_module = types.ModuleType("requests")
|
||||
|
||||
class Response:
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {"success": True, "lastSeenAt": "2026-07-28T00:00:00.000Z"}
|
||||
|
||||
def post(url, json, timeout):
|
||||
calls.append((url, json, timeout))
|
||||
return Response()
|
||||
|
||||
requests_module.post = post
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://server.example/api"
|
||||
api_module.the_folder = "company/line"
|
||||
api_module.device_post = lambda payload, timeout=5: (
|
||||
calls.append((payload, timeout)) or {"success": True, "lastSeenAt": "2026-07-28T00:00:00.000Z"}
|
||||
)
|
||||
with patch.dict(sys.modules, {"requests": requests_module, "api": api_module}):
|
||||
timestamp = HEARTBEAT.heartbeat_device(timeout=7)
|
||||
|
||||
self.assertEqual(timestamp, "2026-07-28T00:00:00.000Z")
|
||||
self.assertEqual(calls, [("https://server.example/api", {
|
||||
"type": "deviceHeartbeat", "deviceId": "company/line"
|
||||
}, 7)])
|
||||
self.assertEqual(calls, [({"type": "deviceHeartbeat"}, 7)])
|
||||
@@ -87,33 +87,23 @@ class IdentificationConfigTests(unittest.TestCase):
|
||||
def test_download_requests_customer_config_and_validates_it(self):
|
||||
calls = []
|
||||
|
||||
class FakeResponse:
|
||||
def __init__(self, body=None, text=None):
|
||||
self.body = body
|
||||
self.text = text
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return self.body
|
||||
|
||||
requests_module = types.ModuleType("requests")
|
||||
requests_module.RequestException = Exception
|
||||
|
||||
def post(url, json, timeout):
|
||||
calls.append(("post", url, json, timeout))
|
||||
return FakeResponse({"success": True, "url": "https://temp/config"})
|
||||
def device_post(payload, timeout):
|
||||
calls.append(("device_post", payload, timeout))
|
||||
return {"success": True, "url": "https://temp/config"}
|
||||
|
||||
def get(url, timeout):
|
||||
calls.append(("get", url, timeout))
|
||||
return FakeResponse(text=config_csv(VALID_CONFIG))
|
||||
return types.SimpleNamespace(
|
||||
text=config_csv(VALID_CONFIG),
|
||||
raise_for_status=lambda: None
|
||||
)
|
||||
|
||||
requests_module.post = post
|
||||
requests_module.get = get
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://cloud/data_record"
|
||||
api_module.the_folder = "客户A"
|
||||
api_module.device_post = device_post
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"requests": requests_module,
|
||||
@@ -122,28 +112,14 @@ class IdentificationConfigTests(unittest.TestCase):
|
||||
result = download_identification_config(timeout=7)
|
||||
|
||||
self.assertEqual(result["repeat"], 2)
|
||||
self.assertEqual(calls[0], (
|
||||
"post",
|
||||
"https://cloud/data_record",
|
||||
{"type": "getIdentificationConfig", "deviceId": "客户A"},
|
||||
7,
|
||||
))
|
||||
self.assertEqual(calls[0], ("device_post", {"type": "getIdentificationConfig"}, 7))
|
||||
self.assertEqual(calls[1], ("get", "https://temp/config", 7))
|
||||
|
||||
def test_download_reports_cloud_rejection(self):
|
||||
class FakeResponse:
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {"success": False, "errMsg": "配置不存在"}
|
||||
|
||||
requests_module = types.ModuleType("requests")
|
||||
requests_module.RequestException = Exception
|
||||
requests_module.post = lambda *args, **kwargs: FakeResponse()
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://cloud/data_record"
|
||||
api_module.the_folder = "客户A"
|
||||
api_module.device_post = lambda *args, **kwargs: {"success": False, "errMsg": "配置不存在"}
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"requests": requests_module,
|
||||
|
||||
@@ -7,6 +7,7 @@ import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import tempfile
|
||||
import time
|
||||
import types
|
||||
import unittest
|
||||
from unittest.mock import patch
|
||||
@@ -107,6 +108,44 @@ class LicenseProtocolTests(unittest.TestCase):
|
||||
side_effect=LICENSE.requests.ConnectionError("offline")):
|
||||
LICENSE.validate_license_online(NEW_LICENSE)
|
||||
|
||||
def test_verify_license_uses_default_glob_pattern_when_path_missing(self):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
payload_b64 = base64.b64encode(json.dumps(NEW_LICENSE).encode()).decode()
|
||||
license_path = Path(tmp_dir) / "customer-license.lic"
|
||||
license_path.write_text(
|
||||
f"{payload_b64}|{base64.b64encode(b'signature').decode()}",
|
||||
encoding="utf-8",
|
||||
)
|
||||
with patch.object(LICENSE, "_load_public_key", return_value=FakePublicKey()), \
|
||||
patch.object(LICENSE, "_default_license_dir", return_value=Path(tmp_dir)):
|
||||
payload = LICENSE.verify_license()
|
||||
|
||||
self.assertEqual(payload["license_id"], NEW_LICENSE["license_id"])
|
||||
|
||||
def test_verify_license_prefers_latest_matching_file(self):
|
||||
with tempfile.TemporaryDirectory() as tmp_dir:
|
||||
old_payload = dict(NEW_LICENSE, license_id="old-license")
|
||||
new_payload = dict(NEW_LICENSE, license_id="new-license")
|
||||
|
||||
def write_license(path, payload):
|
||||
payload_b64 = base64.b64encode(json.dumps(payload).encode()).decode()
|
||||
path.write_text(
|
||||
f"{payload_b64}|{base64.b64encode(b'signature').decode()}",
|
||||
encoding="utf-8",
|
||||
)
|
||||
|
||||
old_path = Path(tmp_dir) / "a-license.lic"
|
||||
new_path = Path(tmp_dir) / "z-license.lic"
|
||||
write_license(old_path, old_payload)
|
||||
time.sleep(0.01)
|
||||
write_license(new_path, new_payload)
|
||||
|
||||
with patch.object(LICENSE, "_load_public_key", return_value=FakePublicKey()), \
|
||||
patch.object(LICENSE, "_default_license_dir", return_value=Path(tmp_dir)):
|
||||
payload = LICENSE.verify_license()
|
||||
|
||||
self.assertEqual(payload["license_id"], "new-license")
|
||||
|
||||
def test_api_rejects_environment_device_id_mismatch(self):
|
||||
fake_license_utils = types.ModuleType("license_utils")
|
||||
fake_license_utils.get_verified_license = lambda: dict(NEW_LICENSE)
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import importlib.util
|
||||
import time
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
MODULE_PATH = Path(__file__).resolve().parents[1] / "core" / "simulated_device.py"
|
||||
@@ -22,18 +22,40 @@ class SimulatedDeviceTests(unittest.TestCase):
|
||||
self.assertFalse(device.connected)
|
||||
|
||||
def test_pressure_model_responds_to_valve_position(self):
|
||||
device = SIMULATED.SimulatedDevice()
|
||||
device.connect()
|
||||
device.set_motor_position(0)
|
||||
time.sleep(0.02)
|
||||
open_pressure = device.get_pressure()
|
||||
device.set_motor_position(1000)
|
||||
time.sleep(0.02)
|
||||
closed_pressure = device.get_pressure()
|
||||
self.assertGreaterEqual(open_pressure, 0.0)
|
||||
self.assertGreaterEqual(closed_pressure, 0.0)
|
||||
self.assertEqual(device.read_current_position(), 1000.0)
|
||||
device.disconnect()
|
||||
class FakeClock:
|
||||
def __init__(self):
|
||||
self.now = 0.0
|
||||
|
||||
def monotonic(self):
|
||||
return self.now
|
||||
|
||||
def advance(self, duration):
|
||||
self.now += duration
|
||||
|
||||
clock = FakeClock()
|
||||
with patch.object(SIMULATED.time, "monotonic", clock.monotonic):
|
||||
device = SIMULATED.SimulatedDevice()
|
||||
device.connect()
|
||||
|
||||
def settled_pressure(position):
|
||||
device.set_motor_position(position)
|
||||
pressure = None
|
||||
for _ in range(1200):
|
||||
clock.advance(0.1)
|
||||
pressure = device.get_pressure()
|
||||
return pressure
|
||||
|
||||
open_pressure = settled_pressure(0)
|
||||
half_open_pressure = settled_pressure(500)
|
||||
closed_pressure = settled_pressure(1000)
|
||||
|
||||
self.assertAlmostEqual(open_pressure, 10.0, delta=0.1)
|
||||
self.assertAlmostEqual(half_open_pressure, 205.0, delta=0.1)
|
||||
self.assertAlmostEqual(closed_pressure, 400.0, delta=0.1)
|
||||
self.assertGreater(closed_pressure, half_open_pressure)
|
||||
self.assertGreater(half_open_pressure, open_pressure)
|
||||
self.assertEqual(device.read_current_position(), 1000.0)
|
||||
device.disconnect()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -57,28 +57,13 @@ class VolumeConfigTests(unittest.TestCase):
|
||||
|
||||
def test_customer_creates_exactly_one_request_instruction(self):
|
||||
calls = []
|
||||
|
||||
class FakeResponse:
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {
|
||||
"success": True,
|
||||
"requestId": "request-1",
|
||||
"expiresAtMs": 123456,
|
||||
}
|
||||
|
||||
requests_module = types.ModuleType("requests")
|
||||
|
||||
def post(url, json, timeout):
|
||||
calls.append((url, json, timeout))
|
||||
return FakeResponse()
|
||||
|
||||
requests_module.post = post
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://cloud/data_record"
|
||||
api_module.the_folder = "客户A"
|
||||
api_module.device_post = lambda payload, timeout=10: (calls.append((payload, timeout)) or {
|
||||
"success": True,
|
||||
"requestId": "request-1",
|
||||
"expiresAtMs": 123456,
|
||||
})
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"requests": requests_module,
|
||||
@@ -90,30 +75,16 @@ class VolumeConfigTests(unittest.TestCase):
|
||||
"request_id": "request-1",
|
||||
"expires_at_ms": 123456,
|
||||
})
|
||||
self.assertEqual(calls, [(
|
||||
"https://cloud/data_record",
|
||||
{"type": "createVolumeConfigRequest", "deviceId": "客户A"},
|
||||
7,
|
||||
)])
|
||||
self.assertEqual(calls, [({"type": "createVolumeConfigRequest"}, 7)])
|
||||
|
||||
def test_pending_request_does_not_download_a_file(self):
|
||||
calls = []
|
||||
|
||||
class FakeResponse:
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {"success": True, "ready": False, "expired": False}
|
||||
|
||||
requests_module = types.ModuleType("requests")
|
||||
requests_module.post = lambda *args, **kwargs: (
|
||||
calls.append(("post", kwargs["json"])) or FakeResponse()
|
||||
)
|
||||
requests_module.get = lambda *args, **kwargs: calls.append(("get", args[0]))
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://cloud/data_record"
|
||||
api_module.the_folder = "客户A"
|
||||
api_module.device_post = lambda payload, timeout=10: (
|
||||
calls.append(("device_post", payload)) or {"success": True, "ready": False, "expired": False}
|
||||
)
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"requests": requests_module,
|
||||
@@ -122,36 +93,25 @@ class VolumeConfigTests(unittest.TestCase):
|
||||
result = poll_volume_config_request("request-1")
|
||||
|
||||
self.assertEqual(result, {"ready": False, "expired": False})
|
||||
self.assertEqual(calls, [("post", {
|
||||
self.assertEqual(calls, [("device_post", {
|
||||
"type": "getVolumeConfigRequest",
|
||||
"deviceId": "客户A",
|
||||
"requestId": "request-1",
|
||||
})])
|
||||
|
||||
def test_ready_request_downloads_and_validates_json(self):
|
||||
class FakeResponse:
|
||||
def __init__(self, body):
|
||||
self.body = body
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return self.body
|
||||
|
||||
requests_module = types.ModuleType("requests")
|
||||
requests_module.post = lambda *args, **kwargs: FakeResponse({
|
||||
requests_module.post = lambda *args, **kwargs: types.SimpleNamespace()
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.device_post = lambda payload, timeout=10: {
|
||||
"success": True,
|
||||
"ready": True,
|
||||
"expired": False,
|
||||
"url": "https://temp/volume.json",
|
||||
})
|
||||
requests_module.get = lambda *args, **kwargs: FakeResponse(
|
||||
dict(VALID_CONFIG)
|
||||
}
|
||||
requests_module.get = lambda *args, **kwargs: types.SimpleNamespace(
|
||||
raise_for_status=lambda: None,
|
||||
json=lambda: dict(VALID_CONFIG)
|
||||
)
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://cloud/data_record"
|
||||
api_module.the_folder = "客户A"
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"requests": requests_module,
|
||||
@@ -163,18 +123,9 @@ class VolumeConfigTests(unittest.TestCase):
|
||||
self.assertEqual(result["config"], VALID_CONFIG)
|
||||
|
||||
def test_create_request_reports_server_rejection(self):
|
||||
class FakeResponse:
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {"success": False, "errMsg": "尚未配置"}
|
||||
|
||||
requests_module = types.ModuleType("requests")
|
||||
requests_module.post = lambda *args, **kwargs: FakeResponse()
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://cloud/data_record"
|
||||
api_module.the_folder = "客户A"
|
||||
api_module.device_post = lambda *args, **kwargs: {"success": False, "errMsg": "尚未配置"}
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"requests": requests_module,
|
||||
@@ -185,21 +136,9 @@ class VolumeConfigTests(unittest.TestCase):
|
||||
|
||||
def test_acknowledges_the_same_request_for_cleanup(self):
|
||||
calls = []
|
||||
|
||||
class FakeResponse:
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {"success": True, "deleted": 1}
|
||||
|
||||
requests_module = types.ModuleType("requests")
|
||||
requests_module.post = lambda *args, **kwargs: (
|
||||
calls.append(kwargs["json"]) or FakeResponse()
|
||||
)
|
||||
api_module = types.ModuleType("api")
|
||||
api_module.data_record_url = "https://cloud/data_record"
|
||||
api_module.the_folder = "客户A"
|
||||
api_module.device_post = lambda payload, timeout=10: (calls.append(payload) or {"success": True, "deleted": 1})
|
||||
|
||||
with patch.dict(sys.modules, {
|
||||
"requests": requests_module,
|
||||
@@ -209,7 +148,6 @@ class VolumeConfigTests(unittest.TestCase):
|
||||
|
||||
self.assertEqual(calls, [{
|
||||
"type": "ackVolumeConfigRequest",
|
||||
"deviceId": "客户A",
|
||||
"requestId": "request-1",
|
||||
}])
|
||||
|
||||
|
||||
@@ -140,3 +140,101 @@
|
||||
### 验证
|
||||
|
||||
- 已完成修改文件的 Python、JavaScript 语法检查,未发现语法错误。
|
||||
|
||||
## 2026-08-03
|
||||
|
||||
### Server:下载接口改造为一次性临时 URL 并绑定来源 IP
|
||||
|
||||
- 文件下载流程由“业务接口返回可复用直链”改为“业务接口签发一次性临时 URL + 下载后立即失效”。
|
||||
- 新增下载入口 `GET /downloads/:ticket`,票据校验包含有效期、一次性消费和请求来源 IP 一致性。
|
||||
- 旧入口 `GET /files/:fileID` 已停用并固定返回 `403`,避免 fileID 直链被转发复用。
|
||||
- `downloadModel` 统一要求 `adminToken`,下载能力与管理权限保持一致。
|
||||
- 返回下载 URL 的业务接口已统一改为临时票据地址:`downloadModel`、`getPendingPanelFile`、`getIdentificationFileDownload`、`getControlFileDownload`、`getIdentificationConfig`、`getVolumeConfigFile`、`getVolumeConfigRequest`。
|
||||
|
||||
涉及文件:
|
||||
|
||||
- `server/src/app.js`
|
||||
- `server/test/server.test.js`
|
||||
- `ControlPanel/server-client.js`
|
||||
- `server/features.md`
|
||||
|
||||
### 验证
|
||||
|
||||
- 已完成 `server` 全量测试(21 项)通过。
|
||||
- 已完成 ControlPanel 无 GUI 内核联调测试通过(`model-handlers` 与 `license-manager` 相关用例)。
|
||||
- 已重启 `reinloop-server.service` 并验证:同一临时下载 URL 首次下载成功,二次访问返回 `403`。
|
||||
|
||||
### Server + ReinLoop:设备分路由与最小权限访问
|
||||
|
||||
- 新增 `POST /device` 设备侧路由,ReinLoop 通过 `deviceAuth`(`licenseId` + `deviceId`)换取短期 `deviceToken` 后访问设备接口。
|
||||
- 设备侧接口采用白名单权限,不再使用 `adminToken`,并强制按 `deviceId` 隔离目录和模型访问范围。
|
||||
- ReinLoop 客户端核心模块已切换为 `/device` 访问链路:设备心跳、模型列表与下载、辨识配置下载、容积请求、辨识反馈、控制与辨识结果上传申请。
|
||||
- ReinLoop 离线宽限默认调整为 `100` 小时(`REINLOOP_LICENSE_OFFLINE_HOURS` 默认值)。
|
||||
|
||||
### ReinLoop + Panel:离线持续运行与组织清理增强
|
||||
|
||||
- ReinLoop 调整为“仅启动时执行许可证校验”,不再自动启动后台巡检线程,满足离线持续运行需求。
|
||||
- Server 新增许可证删除接口 `deleteLicense`(仅允许删除已撤销许可证)。
|
||||
- Server 新增组织删除接口 `deleteProductionLine`、`deleteCompany`,并增加前置约束与关联数据清理。
|
||||
- Panel 新增操作入口:
|
||||
- 已撤销许可证支持“删除”;
|
||||
- 组织管理页支持删除当前公司与当前产线。
|
||||
|
||||
涉及文件:
|
||||
|
||||
- `ReinLoop/license_utils.py`
|
||||
- `ReinLoop/core/__init__.py`
|
||||
- `server/src/app.js`
|
||||
- `server/features.md`
|
||||
- `ControlPanel/electron-main.js`
|
||||
- `ControlPanel/electron-preload.js`
|
||||
- `ControlPanel/electron-ui/index.html`
|
||||
- `ControlPanel/electron-ui/renderer.js`
|
||||
|
||||
涉及文件:
|
||||
|
||||
- `server/src/app.js`
|
||||
- `server/features.md`
|
||||
- `ReinLoop/api.py`
|
||||
- `ReinLoop/license_utils.py`
|
||||
- `ReinLoop/core/model_manager.py`
|
||||
- `ReinLoop/core/identification_config.py`
|
||||
- `ReinLoop/core/volume_config.py`
|
||||
- `ReinLoop/core/identification_feedback.py`
|
||||
- `ReinLoop/core/device_heartbeat.py`
|
||||
- `ReinLoop/core/data_collector.py`
|
||||
- `ReinLoop/core/identification.py`
|
||||
|
||||
### ReinLoop:许可证文件改为通配检索
|
||||
|
||||
- 许可证启动校验与信息读取不再固定使用 `license.lic`,改为在程序目录检索匹配 `*license.lic` 的文件。
|
||||
- 当存在多个匹配文件时,按“最近修改时间优先”选择目标文件,降低人工改名或多版本并存时的启动失败概率。
|
||||
- 同步更新提示文案:明确要求许可证文件名需匹配 `*license.lic`。
|
||||
- 新增协议测试覆盖:默认通配匹配与多文件择优选择。
|
||||
|
||||
涉及文件:
|
||||
|
||||
- `ReinLoop/license_utils.py`
|
||||
- `ReinLoop/tests/test_license_protocol.py`
|
||||
|
||||
### ControlPanel:删除确认提示文案修正
|
||||
|
||||
- 删除产线、删除公司、删除许可证的二次确认弹窗改为按业务字段显示错误提示,不再统一显示“文件名不匹配”。
|
||||
- 现分别提示 `deviceId`、公司编码、许可证 ID 的必填和匹配错误,减少误解与误操作。
|
||||
|
||||
### Server + ControlPanel:删除逻辑防死数据增强
|
||||
|
||||
- `deleteProductionLine` 重构为统一的产线清理流程:删除产线时会同时清理关联文件元数据、通知、辨识反馈、容积配置请求/结果、许可证记录,并增加磁盘目录兜底删除,减少孤儿目录残留。
|
||||
- `deleteCompany` 改为级联删除:在不存在有效许可证时,自动删除该公司下全部产线及其关联数据,再删除公司本体;若仍有有效许可证则阻止删除并返回 `ACTIVE_LICENSES_PRESENT`。
|
||||
- Panel 删除提示文案升级:明确告知“删除公司/产线会删除关联模型、辨识数据、容积配置、通知与许可证记录”。
|
||||
- 删除许可证确认弹窗补充“当前许可证 ID”显示,且确认比较支持忽略大小写,降低输入误判。
|
||||
|
||||
涉及文件:
|
||||
|
||||
- `server/src/app.js`
|
||||
- `server/test/server.test.js`
|
||||
- `ControlPanel/electron-ui/renderer.js`
|
||||
|
||||
涉及文件:
|
||||
|
||||
- `ControlPanel/electron-ui/renderer.js`
|
||||
|
||||
+44
-7
@@ -2,7 +2,7 @@
|
||||
|
||||
## 通用约定
|
||||
|
||||
业务接口为 `POST /api`。请求与响应均为 JSON,响应包含 `success`。
|
||||
业务接口为 `POST /`。请求与响应均为 JSON,响应包含 `success`。
|
||||
|
||||
标注为 Admin 的接口需要附加:
|
||||
|
||||
@@ -16,11 +16,39 @@
|
||||
|
||||
| 方法 | 路径 | 用途 |
|
||||
| --- | --- | --- |
|
||||
| `POST` | `/api` | 主业务 API |
|
||||
| `POST` | `/` | 主业务 API |
|
||||
| `POST` | `/device` | ReinLoop 设备侧 API(license -> deviceToken) |
|
||||
| `POST` | `/upload/:token` | 根据上传凭证接收 multipart 文件 |
|
||||
| `GET` | `/files/:fileID` | 下载已存储文件 |
|
||||
| `GET` | `/downloads/:ticket` | 使用一次性临时下载票据获取文件 |
|
||||
| `GET` | `/files/:fileID` | 旧直链下载入口(已停用,固定返回 403) |
|
||||
| `GET` | `/health` | 服务存活检查 |
|
||||
|
||||
## 设备侧鉴权与分路由
|
||||
|
||||
`/device` 仅用于 ReinLoop 客户端,禁止使用 `adminToken`。
|
||||
|
||||
调用方式:
|
||||
|
||||
1. 先 `POST /device`,`type=deviceAuth`,字段 `licenseId`、`deviceId`。
|
||||
2. 服务端校验许可证状态(存在、未过期、未撤销、deviceId 匹配)后签发 `deviceToken`。
|
||||
3. ReinLoop 后续调用 `/device` 白名单接口时携带 `deviceToken`。
|
||||
|
||||
`deviceToken` 默认有效期由 `DEVICE_TOKEN_TTL_MS` 控制(默认 15 分钟)。
|
||||
|
||||
离线宽限由 ReinLoop 客户端控制,当前默认 `REINLOOP_LICENSE_OFFLINE_HOURS=100`(100 小时)。
|
||||
|
||||
## 下载安全调用链
|
||||
|
||||
统一链路为:业务 `POST /`(鉴权) -> 返回临时 URL -> `GET /downloads/:ticket`(一次性消费)。
|
||||
|
||||
安全校验点:
|
||||
|
||||
- `POST /`:按业务类型执行权限校验。
|
||||
- `GET /downloads/:ticket`:
|
||||
- 票据存在且未过期(`DOWNLOAD_URL_TTL_MS`,兼容旧环境变量 `DOWNLOAD_TOKEN_TTL_MS`)。
|
||||
- 票据仅可消费一次,成功下载或校验失败后均失效。
|
||||
- 下载请求来源 IP 必须与签发票据的 `POST` 请求来源 IP 一致。
|
||||
|
||||
## 设备心跳与组织
|
||||
|
||||
| type | 鉴权 | 请求字段 | 功能与响应要点 |
|
||||
@@ -29,6 +57,8 @@
|
||||
| `listOrganizations` | Admin | 无 | 返回 `companies`,每家公司包含 `productionLines`。产线包含 `id`、`companyId`、`name`、`code`、`deviceId`、`lastSeenAt`、`online`。最近 30 秒有心跳时 `online` 为 `true`。 |
|
||||
| `createCompany` | Admin | `name`、`code` | 创建公司。`code` 全局唯一,只允许 2-64 位小写字母、数字、`_`、`-`。 |
|
||||
| `createProductionLine` | Admin | `companyId`、`name`、`code` | 创建产线。产线编码在公司内唯一;服务端固定生成 `<company.code>/<line.code>`。 |
|
||||
| `deleteProductionLine` | Admin | `companyId`、`productionLineId` | 删除产线及关联业务数据。若该产线仍存在有效许可证会拒绝,需先撤销。 |
|
||||
| `deleteCompany` | Admin | `companyId` | 删除公司。若仍有关联产线或许可证会拒绝。 |
|
||||
|
||||
Panel 应每 10 秒调用 `listOrganizations` 刷新在线状态,不应自行推测设备状态。
|
||||
|
||||
@@ -40,6 +70,7 @@ Panel 应每 10 秒调用 `listOrganizations` 刷新在线状态,不应自行
|
||||
| `listLicenses` | Admin | 无 | 返回许可证摘要列表,不返回原始 `license`。 |
|
||||
| `getLicense` | Admin | `licenseId` | 返回完整许可证详情,可包含原始 `license`。 |
|
||||
| `revokeLicense` | Admin | `licenseId`、`reason` | 撤销许可证,保留历史、撤销时间和原因。`licenseId` 会去除首尾空白。失败时返回 `errCode`:`ADMIN_TOKEN_INVALID`、`ADMIN_TOKEN_NOT_CONFIGURED`、`LICENSE_ID_REQUIRED` 或 `LICENSE_NOT_FOUND`。兼容旧类型 `revoke_license`、`licenseRevoke`、`revoke`,以及旧字段 `license_id`、`admin_token`。每次撤销会记录不含令牌的结构化审计日志。 |
|
||||
| `deleteLicense` | Admin | `licenseId` | 永久删除许可证记录。仅允许删除已撤销许可证,`active` 状态会返回 `LICENSE_ACTIVE`。 |
|
||||
| `validateLicense` | 无 | `licenseId`、`deviceId` | 返回 `valid`、`status`、`licenseId`。状态为 `active`、`revoked`、`expired`、`not_found` 或 `device_mismatch`;不泄露客户信息和许可证原文。 |
|
||||
|
||||
许可证格式为 `payloadBase64|signatureBase64`。服务端只读取 `LICENSE_PUBLIC_KEY_PATH` 的公钥,绝不接收或保存 RSA 私钥。
|
||||
@@ -51,13 +82,19 @@ Panel 应每 10 秒调用 `listOrganizations` 刷新在线状态,不应自行
|
||||
| `uploadDataFile` | 视目录而定 | `fileName`、`folder` | 签发通用两步上传凭证。目录为 `*/model_config` 时必须 Admin;其他既有 ReinLoop 数据上传保持兼容。 |
|
||||
| `issueModelUpload` | Admin | `deviceId`、`fileName`、可选 `modelName`、`overwrite` | `fileName` 是本地原始文件名;传入 `modelName` 时以该名称存储和识别模型,并保留 `originalFileName`。同名模型已存在时返回 `conflict: true`;仅 `overwrite: true` 可签发覆盖凭证。 |
|
||||
| `listModels` | 无 | `folder` | 返回 `files` 当前模型名数组和 `fileList` 元数据数组,最多 100 条;每条同时包含 `fileName` 和 `originalFileName`。 |
|
||||
| `downloadModel` | 视文件而定 | `fileID` | 模型保持兼容;非模型文件要求 Admin 并返回短期签名下载 URL。 |
|
||||
| `downloadModel` | Admin | `fileID` | 返回一次性临时下载 URL(`/downloads/:ticket`)。 |
|
||||
| `deleteFile` | Admin | `fileID`,或 `folder` 与 `fileName` | 删除文件及元数据;同名文件不唯一时必须使用 `fileID`。 |
|
||||
| `deleteModel` | Admin | 同 `deleteFile` | 模型删除的明确管理端别名。 |
|
||||
|
||||
上传分两步:先调用 `uploadDataFile` 或 `issueModelUpload`,再将文件作为 `multipart/form-data` 的 `file` 字段提交到响应中的 `uploadMetadata.url`。上传成功返回 HTTP `204`;响应中的 `fileID` 可用于下载和删除。
|
||||
模型重命名不会修改文件格式,因此 `modelName` 与原始 `fileName` 的扩展名必须一致。未传 `modelName` 时两者相同,旧客户端行为不变。
|
||||
|
||||
设备侧(`/device`)模型访问约束:
|
||||
|
||||
- `listModels` 固定返回当前 `deviceId/model_config` 目录。
|
||||
- `downloadModel` 仅允许下载当前 `deviceId/model_config` 下文件。
|
||||
- 设备侧不允许模型上传与删除。
|
||||
|
||||
上传到 `<deviceId>/ind_data` 的 `.csv`、`.json` 会自动进入 Panel inbox。
|
||||
|
||||
## 配置发布与读取
|
||||
@@ -67,7 +104,7 @@ Panel 应每 10 秒调用 `listOrganizations` 刷新在线状态,不应自行
|
||||
| `publishIdentificationConfig` | Admin | `deviceId`、`parameters` | 校验辨识参数并为 `<deviceId>/identification_config/identification_config.csv` 签发上传凭证。 |
|
||||
| `getIdentificationConfig` | 无 | `deviceId` | 返回该设备辨识 CSV 的 `fileID` 和 `url`。 |
|
||||
| `publishVolumeConfig` | Admin | `parameters` | 校验容积参数并签发 `volume_config.json` 上传凭证。上传完成后更新功能参数记录。 |
|
||||
| `getVolumeConfigFile` | 无 | 无 | 返回已发布容积 JSON 的 `fileID`、`cloudPath`、`url`。 |
|
||||
| `getVolumeConfigFile` | Admin | `deviceId` | 返回指定设备已发布容积 JSON 的 `fileID`、`cloudPath`、`url`。 |
|
||||
| `getFunctionConfig` | 无 | `configType: "volume"` | 返回已发布容积参数的 `parameters`、`version`、`updateTime`。 |
|
||||
|
||||
发布接口仅签发上传凭证;客户端完成二步上传后,读取接口才会返回新文件或参数。
|
||||
@@ -83,7 +120,7 @@ Panel 应每 10 秒调用 `listOrganizations` 刷新在线状态,不应自行
|
||||
| `getPendingPanelFile` | Admin | `deviceId` | 获取指定设备下一条待处理 CSV/JSON;无数据时 `pending: false`,有数据时返回文件信息和 `url`。 |
|
||||
| `ackPanelFile` | Admin | `deviceId`、`fileID` | Panel 处理完成后确认,移除 inbox 项并标记历史记录为 `processed`,不立即删除文件。 |
|
||||
| `listIdentificationFiles` | Admin | `deviceId`、可选 `mediaType`、`status`、`page`、`pageSize` | 分页返回设备的辨识 CSV/JSON 暂存历史。 |
|
||||
| `getIdentificationFileDownload` | Admin | `fileID` | 返回原始辨识文件的短期签名下载 URL。 |
|
||||
| `getIdentificationFileDownload` | Admin | `fileID` | 返回原始辨识文件的一次性临时下载 URL。 |
|
||||
| `deleteIdentificationFile` | Admin | `fileID` | 显式删除辨识文件、历史记录及待处理消息。 |
|
||||
|
||||
CSV 辨识文件须先以同名 `runId` 调用 `registerIdentificationResult`,才会出现在 `getPendingPanelFile`;初始行程 JSON 可直接获取。
|
||||
@@ -97,7 +134,7 @@ CSV 辨识文件须先以同名 `runId` 调用 `registerIdentificationResult`,
|
||||
| `createVolumeConfigRequest` | 无 | `deviceId` | ReinLoop 创建一次性上传请求,返回 `requestId`、`createdAtMs`、`expiresAtMs`。同设备旧请求会被替换。 |
|
||||
| `getPendingVolumeConfigRequest` | 无 | `deviceId` | 查询是否存在待上传请求,返回 `pending` 和请求时间信息。 |
|
||||
| `submitVolumeConfigFile` | 无 | `deviceId`、`requestId`、`fileID`、可选 `fileName` | 将已上传到 `<deviceId>/volume_config_requests/<requestId>/` 的文件绑定至请求。 |
|
||||
| `getVolumeConfigRequest` | 无 | `deviceId`、`requestId` | 轮询配置是否就绪,返回 `ready`、`expired`;就绪时包含下载 `url`。 |
|
||||
| `getVolumeConfigRequest` | 无 | `deviceId`、`requestId` | 轮询配置是否就绪,返回 `ready`、`expired`;就绪时包含一次性临时下载 `url`。 |
|
||||
| `ackVolumeConfigRequest` | 无 | `deviceId`、`requestId` | ReinLoop 下载完成后确认,清理请求及关联文件。 |
|
||||
|
||||
请求有效期由 `VOLUME_REQUEST_TTL_MS` 控制,默认 300000 毫秒(5 分钟)。
|
||||
|
||||
@@ -98,8 +98,28 @@ CREATE TABLE IF NOT EXISTS volume_config_requests (
|
||||
PRIMARY KEY (device_id, request_id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS volume_configs (
|
||||
device_id TEXT PRIMARY KEY,
|
||||
file_id TEXT NOT NULL,
|
||||
file_name TEXT NOT NULL,
|
||||
request_id TEXT NOT NULL,
|
||||
updated_at_ms BIGINT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS panel_notifications (
|
||||
notification_id TEXT PRIMARY KEY,
|
||||
device_id TEXT NOT NULL,
|
||||
type TEXT NOT NULL,
|
||||
title TEXT NOT NULL,
|
||||
message TEXT NOT NULL,
|
||||
request_id TEXT,
|
||||
file_id TEXT,
|
||||
created_at_ms BIGINT NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS licenses_status_device_expiry_idx ON licenses (status, device_id, expiry_at);
|
||||
CREATE INDEX IF NOT EXISTS file_records_folder_idx ON file_records (folder);
|
||||
CREATE INDEX IF NOT EXISTS identification_files_device_upload_idx ON identification_files (device_id, upload_time DESC);
|
||||
CREATE INDEX IF NOT EXISTS identification_files_expires_idx ON identification_files (expires_at) WHERE expires_at IS NOT NULL;
|
||||
CREATE INDEX IF NOT EXISTS identification_feedback_device_idx ON identification_feedback (device_id);
|
||||
CREATE INDEX IF NOT EXISTS panel_notifications_device_created_idx ON panel_notifications (device_id, created_at_ms);
|
||||
@@ -0,0 +1,118 @@
|
||||
"use strict";
|
||||
|
||||
function parseArgs(argv) {
|
||||
const result = {};
|
||||
for (let index = 2; index < argv.length; index += 1) {
|
||||
const arg = argv[index];
|
||||
if (!arg.startsWith("--")) continue;
|
||||
const [key, ...rest] = arg.slice(2).split("=");
|
||||
if (!key) continue;
|
||||
const value = rest.length ? rest.join("=") : "";
|
||||
result[key] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
const args = parseArgs(process.argv);
|
||||
const BASE_URL = (args.baseUrl || process.env.BASE_URL || "http://127.0.0.1:3000").replace(/\/$/, "");
|
||||
const ADMIN_TOKEN = args.adminToken || process.env.ADMIN_TOKEN || "";
|
||||
const DEVICE_A = args.deviceA || process.env.DEVICE_A || "develop-test2/test1";
|
||||
const DEVICE_B = args.deviceB || process.env.DEVICE_B || "develop-test/test1";
|
||||
|
||||
if (!ADMIN_TOKEN) {
|
||||
console.error("[ERROR] 缺少 ADMIN_TOKEN 环境变量。");
|
||||
console.error("示例1: node scripts/test-cross-device-notification.js --adminToken=your-token");
|
||||
console.error("示例2: node scripts/test-cross-device-notification.js --adminToken=your-token --baseUrl=http://127.0.0.1:3000 --deviceA=develop-test2/test1 --deviceB=develop-test/test1");
|
||||
process.exit(1);
|
||||
}
|
||||
|
||||
async function postJson(path, body) {
|
||||
const response = await fetch(`${BASE_URL}${path}`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(body)
|
||||
});
|
||||
|
||||
let data;
|
||||
try {
|
||||
data = await response.json();
|
||||
} catch (_error) {
|
||||
data = { success: false, errMsg: "响应不是 JSON" };
|
||||
}
|
||||
|
||||
return { status: response.status, data };
|
||||
}
|
||||
|
||||
function assert(condition, message, details) {
|
||||
if (condition) return;
|
||||
const error = new Error(message);
|
||||
error.details = details;
|
||||
throw error;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
console.log(`[INFO] BASE_URL=${BASE_URL}`);
|
||||
console.log(`[INFO] DEVICE_A=${DEVICE_A}`);
|
||||
console.log(`[INFO] DEVICE_B=${DEVICE_B}`);
|
||||
|
||||
console.log("\n[1/5] 为 DEVICE_A 生成一条通知(createVolumeConfigRequest)...");
|
||||
const createReq = await postJson("/", {
|
||||
type: "createVolumeConfigRequest",
|
||||
deviceId: DEVICE_A
|
||||
});
|
||||
assert(createReq.data.success === true, "createVolumeConfigRequest 失败", createReq);
|
||||
|
||||
console.log("[2/5] 读取 DEVICE_A 的 pending notification...");
|
||||
const pendingA = await postJson("/", {
|
||||
type: "getPendingPanelNotification",
|
||||
adminToken: ADMIN_TOKEN,
|
||||
deviceId: DEVICE_A
|
||||
});
|
||||
assert(pendingA.data.success === true, "getPendingPanelNotification(DEVICE_A) 调用失败", pendingA);
|
||||
assert(pendingA.data.pending === true, "DEVICE_A 没有待处理通知", pendingA);
|
||||
const notificationId = pendingA.data.notification?.notificationId;
|
||||
assert(Boolean(notificationId), "未拿到 notificationId", pendingA);
|
||||
console.log(`[INFO] notificationId=${notificationId}`);
|
||||
|
||||
console.log("[3/5] 使用 DEVICE_B 尝试确认 DEVICE_A 的 notificationId(预期失败)...");
|
||||
const crossAck = await postJson("/", {
|
||||
type: "ackPanelNotification",
|
||||
adminToken: ADMIN_TOKEN,
|
||||
deviceId: DEVICE_B,
|
||||
notificationId
|
||||
});
|
||||
assert(crossAck.data.success === false, "跨 deviceId 确认意外成功(存在越权风险)", crossAck);
|
||||
assert(crossAck.data.errMsg === "Panel 通知不存在", "跨 deviceId 失败文案非预期", crossAck);
|
||||
|
||||
console.log("[4/5] 使用 DEVICE_A 正常确认同一 notificationId(预期成功)...");
|
||||
const ownerAck = await postJson("/", {
|
||||
type: "ackPanelNotification",
|
||||
adminToken: ADMIN_TOKEN,
|
||||
deviceId: DEVICE_A,
|
||||
notificationId
|
||||
});
|
||||
assert(ownerAck.data.success === true, "DEVICE_A 确认自身通知失败", ownerAck);
|
||||
|
||||
console.log("[5/5] 再次读取 DEVICE_A pending,确认已被消费...");
|
||||
const pendingAfter = await postJson("/", {
|
||||
type: "getPendingPanelNotification",
|
||||
adminToken: ADMIN_TOKEN,
|
||||
deviceId: DEVICE_A
|
||||
});
|
||||
assert(pendingAfter.data.success === true, "二次查询 pending 失败", pendingAfter);
|
||||
|
||||
console.log("\n[PASS] 测试通过:notificationId 不能被其他 deviceId 确认。\n");
|
||||
console.log("关键结果:");
|
||||
console.log(`- crossAck.success = ${crossAck.data.success}`);
|
||||
console.log(`- crossAck.errMsg = ${crossAck.data.errMsg}`);
|
||||
console.log(`- ownerAck.success = ${ownerAck.data.success}`);
|
||||
}
|
||||
|
||||
main().catch((error) => {
|
||||
console.error("\n[FAIL]", error.message);
|
||||
if (error.details) {
|
||||
console.error("details=");
|
||||
console.error(JSON.stringify(error.details, null, 2));
|
||||
}
|
||||
process.exit(1);
|
||||
});
|
||||
+509
-55
@@ -1,12 +1,13 @@
|
||||
const fs = require("node:fs");
|
||||
const path = require("node:path");
|
||||
const { randomUUID, constants, createHmac, timingSafeEqual, verify } = require("node:crypto");
|
||||
const { randomUUID, constants, verify } = require("node:crypto");
|
||||
const express = require("express");
|
||||
const multer = require("multer");
|
||||
|
||||
const BASE_FOLDER = "ReinLoop_GUI";
|
||||
const VOLUME_REQUEST_TTL_MS = Number(process.env.VOLUME_REQUEST_TTL_MS || 300000);
|
||||
const DOWNLOAD_TOKEN_TTL_MS = Number(process.env.DOWNLOAD_TOKEN_TTL_MS || 300000);
|
||||
const DOWNLOAD_URL_TTL_MS = Number(process.env.DOWNLOAD_URL_TTL_MS || process.env.DOWNLOAD_TOKEN_TTL_MS || 300000);
|
||||
const DEVICE_TOKEN_TTL_MS = Number(process.env.DEVICE_TOKEN_TTL_MS || 15 * 60 * 1000);
|
||||
const IDENTIFICATION_RETENTION_MS = Number(process.env.IDENTIFICATION_RETENTION_MS || 30 * 24 * 60 * 60 * 1000);
|
||||
const DEVICE_HEARTBEAT_TTL_MS = 30_000;
|
||||
const CONFIG_SCHEMAS = {
|
||||
@@ -161,6 +162,8 @@ function createApp({
|
||||
const app = express();
|
||||
const upload = multer({ storage: multer.memoryStorage(), limits: { fileSize: 100 * 1024 * 1024 } });
|
||||
const pendingUploads = new Map();
|
||||
const pendingDownloads = new Map();
|
||||
const pendingDeviceTokens = new Map();
|
||||
|
||||
app.disable("x-powered-by");
|
||||
app.use(express.json({ limit: "2mb" }));
|
||||
@@ -200,23 +203,68 @@ function createApp({
|
||||
};
|
||||
}
|
||||
|
||||
function signDownload(fileID, expiresAtMs) {
|
||||
return createHmac("sha256", adminToken).update(`${fileID}\n${expiresAtMs}`).digest("hex");
|
||||
function requestIp(req) {
|
||||
return String(req.ip || req.socket?.remoteAddress || "").trim();
|
||||
}
|
||||
|
||||
function downloadUrl(req, fileID) {
|
||||
const baseUrl = `${publicBaseUrl(req)}/files/${encodeURIComponent(fileID)}`;
|
||||
if (fileID.startsWith("model://")) return baseUrl;
|
||||
const expires = Date.now() + DOWNLOAD_TOKEN_TTL_MS;
|
||||
return `${baseUrl}?expires=${expires}&token=${signDownload(fileID, expires)}`;
|
||||
function issueDownloadUrl(req, fileID) {
|
||||
const ticket = randomUUID().replace(/-/g, "");
|
||||
pendingDownloads.set(ticket, {
|
||||
fileID,
|
||||
requestIp: requestIp(req),
|
||||
expiresAtMs: Date.now() + DOWNLOAD_URL_TTL_MS
|
||||
});
|
||||
return `${publicBaseUrl(req)}/downloads/${ticket}`;
|
||||
}
|
||||
|
||||
function hasValidDownloadToken(req, fileID) {
|
||||
const expires = Number(req.query.expires);
|
||||
const token = String(req.query.token || "");
|
||||
if (!Number.isSafeInteger(expires) || expires < Date.now() || !/^[0-9a-f]{64}$/.test(token)) return false;
|
||||
const expected = signDownload(fileID, expires);
|
||||
return timingSafeEqual(Buffer.from(token, "hex"), Buffer.from(expected, "hex"));
|
||||
function consumeDownloadTicket(req, ticket) {
|
||||
const request = pendingDownloads.get(ticket);
|
||||
if (!request) return { ok: false, errMsg: "下载链接无效或已失效" };
|
||||
pendingDownloads.delete(ticket);
|
||||
if (request.expiresAtMs < Date.now()) return { ok: false, errMsg: "下载链接已过期" };
|
||||
if (!request.requestIp || request.requestIp !== requestIp(req)) {
|
||||
return { ok: false, errMsg: "下载请求来源IP不匹配" };
|
||||
}
|
||||
return { ok: true, fileID: request.fileID };
|
||||
}
|
||||
|
||||
function verifyLicenseRecord(record, deviceId) {
|
||||
if (!record) return { success: false, valid: false, status: "not_found" };
|
||||
if (deviceId && deviceId !== record.deviceId) {
|
||||
return { success: true, valid: false, status: "device_mismatch", licenseId: record.licenseId };
|
||||
}
|
||||
if (new Date(record.expiryAt || parseLicenseTimestamp(record.expiry, "expiry")) <= new Date()) {
|
||||
return { success: true, valid: false, status: "expired", licenseId: record.licenseId };
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
valid: record.status === "active",
|
||||
status: record.status,
|
||||
licenseId: record.licenseId
|
||||
};
|
||||
}
|
||||
|
||||
function issueDeviceToken({ deviceId, licenseId }) {
|
||||
const token = randomUUID().replace(/-/g, "");
|
||||
const expiresAtMs = Date.now() + DEVICE_TOKEN_TTL_MS;
|
||||
pendingDeviceTokens.set(token, { deviceId, licenseId, expiresAtMs });
|
||||
return { token, expiresAtMs };
|
||||
}
|
||||
|
||||
function consumeDeviceContext(event) {
|
||||
const deviceToken = String(event.deviceToken || "").trim();
|
||||
if (!deviceToken) {
|
||||
return { ok: false, errCode: "DEVICE_TOKEN_REQUIRED", errMsg: "缺少 deviceToken" };
|
||||
}
|
||||
const session = pendingDeviceTokens.get(deviceToken);
|
||||
if (!session) {
|
||||
return { ok: false, errCode: "DEVICE_TOKEN_INVALID", errMsg: "deviceToken 无效" };
|
||||
}
|
||||
if (session.expiresAtMs <= Date.now()) {
|
||||
pendingDeviceTokens.delete(deviceToken);
|
||||
return { ok: false, errCode: "DEVICE_TOKEN_EXPIRED", errMsg: "deviceToken 已过期" };
|
||||
}
|
||||
return { ok: true, ...session };
|
||||
}
|
||||
|
||||
//config test-----------------------------------
|
||||
@@ -318,9 +366,101 @@ function createApp({
|
||||
database.fileRecords = database.fileRecords.filter((record) => record.fileID !== fileID);
|
||||
database.panelInbox = database.panelInbox.filter((record) => record.fileID !== fileID);
|
||||
database.identificationFiles = database.identificationFiles.filter((record) => record.fileID !== fileID);
|
||||
database.volumeConfigs = database.volumeConfigs.filter((record) => record.fileID !== fileID);
|
||||
database.panelNotifications = database.panelNotifications.filter((record) => record.fileID !== fileID);
|
||||
return before - database.fileRecords.length;
|
||||
}
|
||||
|
||||
async function removeDeviceDirectories(deviceId) {
|
||||
const segments = String(deviceId || "").split("/");
|
||||
if (segments.length !== 2 || !segments[0] || !segments[1]) return 0;
|
||||
const [companyCode, lineCode] = segments;
|
||||
const targets = [
|
||||
path.join(store.modelsDirectory, companyCode, lineCode),
|
||||
path.join(store.filesDirectory, "ReinLoop_GUI", companyCode, lineCode)
|
||||
];
|
||||
let removed = 0;
|
||||
for (const directory of targets) {
|
||||
try {
|
||||
const stat = await fs.promises.stat(directory);
|
||||
if (!stat.isDirectory()) continue;
|
||||
} catch (_error) {
|
||||
continue;
|
||||
}
|
||||
await fs.promises.rm(directory, { recursive: true, force: true });
|
||||
removed += 1;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
|
||||
async function removeProductionLineData(database, { companyId, productionLineId, failOnActiveLicense = true }) {
|
||||
const line = database.productionLines.find((item) => item.id === productionLineId && item.companyId === companyId);
|
||||
if (!line) return { success: false, errMsg: "产线不存在" };
|
||||
|
||||
const activeLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.productionLineId === productionLineId && item.status === "active"
|
||||
);
|
||||
if (failOnActiveLicense && activeLicenses.length) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `请先撤销该产线的 ${activeLicenses.length} 个有效许可证后再删除产线`,
|
||||
errCode: "ACTIVE_LICENSES_PRESENT"
|
||||
};
|
||||
}
|
||||
|
||||
const deviceId = line.deviceId;
|
||||
const relatedFileIDs = database.fileRecords
|
||||
.filter((record) => {
|
||||
if (record.fileID.startsWith(`model://${deviceId}/`)) return true;
|
||||
const prefix = `${deviceId}/`;
|
||||
return record.folder === deviceId || record.folder.startsWith(prefix);
|
||||
})
|
||||
.map((record) => record.fileID);
|
||||
|
||||
let deletedFileCount = 0;
|
||||
for (const fileID of new Set(relatedFileIDs)) {
|
||||
deletedFileCount += await removeFile(database, fileID);
|
||||
}
|
||||
|
||||
const beforeFeedback = database.identificationFeedback.length;
|
||||
database.identificationFeedback = database.identificationFeedback.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedFeedback = beforeFeedback - database.identificationFeedback.length;
|
||||
|
||||
const beforeRequests = database.volumeConfigRequests.length;
|
||||
database.volumeConfigRequests = database.volumeConfigRequests.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedRequests = beforeRequests - database.volumeConfigRequests.length;
|
||||
|
||||
const beforeConfigs = database.volumeConfigs.length;
|
||||
database.volumeConfigs = database.volumeConfigs.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedConfigs = beforeConfigs - database.volumeConfigs.length;
|
||||
|
||||
const beforeNotifications = database.panelNotifications.length;
|
||||
database.panelNotifications = database.panelNotifications.filter((item) => item.deviceId !== deviceId);
|
||||
const deletedNotifications = beforeNotifications - database.panelNotifications.length;
|
||||
|
||||
const deletedLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.productionLineId === productionLineId
|
||||
).length;
|
||||
database.licenses = database.licenses.filter((item) =>
|
||||
!(item.companyId === companyId && item.productionLineId === productionLineId)
|
||||
);
|
||||
|
||||
const deletedDirectories = await removeDeviceDirectories(deviceId);
|
||||
database.productionLines = database.productionLines.filter((item) => item.id !== productionLineId);
|
||||
|
||||
return {
|
||||
success: true,
|
||||
deletedProductionLineId: productionLineId,
|
||||
deletedFiles: deletedFileCount,
|
||||
deletedDirectories,
|
||||
deletedLicenses,
|
||||
deletedFeedback,
|
||||
deletedVolumeRequests: deletedRequests,
|
||||
deletedVolumeConfigs: deletedConfigs,
|
||||
deletedNotifications
|
||||
};
|
||||
}
|
||||
|
||||
function backfillIdentificationFiles(database) {
|
||||
for (const record of database.fileRecords) {
|
||||
if (!record.folder.endsWith("/ind_data") || ![".csv", ".json"].includes(path.extname(record.fileName).toLowerCase())) continue;
|
||||
@@ -341,6 +481,54 @@ function createApp({
|
||||
}
|
||||
}
|
||||
|
||||
function addPanelNotification(database, { deviceId, type, title, message, requestId = null, fileID = null }) {
|
||||
const notification = {
|
||||
notificationId: store.createId("pn"),
|
||||
deviceId,
|
||||
type,
|
||||
title,
|
||||
message,
|
||||
requestId,
|
||||
fileID,
|
||||
createdAtMs: Date.now()
|
||||
};
|
||||
database.panelNotifications.push(notification);
|
||||
return notification;
|
||||
}
|
||||
|
||||
function findIdentificationFeedbackRecord(database, deviceId, fileName) {
|
||||
if (!deviceId || !fileName) return null;
|
||||
const normalizedFileName = String(fileName);
|
||||
return database.identificationFeedback.find((item) =>
|
||||
item.deviceId === deviceId
|
||||
&& (item.fileName === normalizedFileName || item.runId === normalizedFileName)
|
||||
) || null;
|
||||
}
|
||||
|
||||
function resolveIdentificationRunId(database, record) {
|
||||
if (!record) return null;
|
||||
if (record.runId) return String(record.runId);
|
||||
const feedback = findIdentificationFeedbackRecord(database, record.deviceId, record.fileName);
|
||||
if (feedback) return String(feedback.runId);
|
||||
if (record.fileName) return String(record.fileName);
|
||||
return null;
|
||||
}
|
||||
|
||||
function enrichIdentificationFile(database, record) {
|
||||
const runId = resolveIdentificationRunId(database, record);
|
||||
return runId ? { ...record, runId } : { ...record };
|
||||
}
|
||||
|
||||
function enrichNotification(database, notification) {
|
||||
if (!notification || notification.type !== "identification_result_ready" || !notification.fileID) {
|
||||
return notification;
|
||||
}
|
||||
const related = database.identificationFiles.find((item) => item.fileID === notification.fileID)
|
||||
|| database.panelInbox.find((item) => item.fileID === notification.fileID);
|
||||
const runId = resolveIdentificationRunId(database, related);
|
||||
return runId ? { ...notification, runId } : notification;
|
||||
}
|
||||
|
||||
async function purgeExpiredIdentificationFiles() {
|
||||
return store.update(async (database) => {
|
||||
backfillIdentificationFiles(database);
|
||||
@@ -422,6 +610,84 @@ function createApp({
|
||||
return { success: true, productionLine };
|
||||
});
|
||||
}
|
||||
case "deleteProductionLine": {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
const companyId = String(event.companyId || "").trim();
|
||||
const productionLineId = String(event.productionLineId || "").trim();
|
||||
if (!companyId || !productionLineId) {
|
||||
return { success: false, errMsg: "缺少 companyId 或 productionLineId" };
|
||||
}
|
||||
return store.update(async (database) => {
|
||||
const company = database.companies.find((item) => item.id === companyId);
|
||||
if (!company) return { success: false, errMsg: "公司不存在" };
|
||||
return removeProductionLineData(database, {
|
||||
companyId,
|
||||
productionLineId,
|
||||
failOnActiveLicense: true
|
||||
});
|
||||
});
|
||||
}
|
||||
case "deleteCompany": {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
const companyId = String(event.companyId || "").trim();
|
||||
if (!companyId) return { success: false, errMsg: "缺少 companyId" };
|
||||
return store.update(async (database) => {
|
||||
const company = database.companies.find((item) => item.id === companyId);
|
||||
if (!company) return { success: false, errMsg: "公司不存在" };
|
||||
|
||||
const activeLicenses = database.licenses.filter((item) =>
|
||||
item.companyId === companyId && item.status === "active"
|
||||
);
|
||||
if (activeLicenses.length) {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: `请先撤销该公司的 ${activeLicenses.length} 个有效许可证后再删除公司`,
|
||||
errCode: "ACTIVE_LICENSES_PRESENT"
|
||||
};
|
||||
}
|
||||
|
||||
const lines = database.productionLines.filter((item) => item.companyId === companyId);
|
||||
|
||||
const summary = {
|
||||
deletedProductionLines: 0,
|
||||
deletedFiles: 0,
|
||||
deletedDirectories: 0,
|
||||
deletedLicenses: 0,
|
||||
deletedFeedback: 0,
|
||||
deletedVolumeRequests: 0,
|
||||
deletedVolumeConfigs: 0,
|
||||
deletedNotifications: 0
|
||||
};
|
||||
|
||||
for (const line of lines) {
|
||||
const lineDeleted = await removeProductionLineData(database, {
|
||||
companyId,
|
||||
productionLineId: line.id,
|
||||
failOnActiveLicense: false
|
||||
});
|
||||
if (!lineDeleted.success) return lineDeleted;
|
||||
summary.deletedProductionLines += 1;
|
||||
summary.deletedFiles += lineDeleted.deletedFiles;
|
||||
summary.deletedDirectories += lineDeleted.deletedDirectories;
|
||||
summary.deletedLicenses += lineDeleted.deletedLicenses;
|
||||
summary.deletedFeedback += lineDeleted.deletedFeedback;
|
||||
summary.deletedVolumeRequests += lineDeleted.deletedVolumeRequests;
|
||||
summary.deletedVolumeConfigs += lineDeleted.deletedVolumeConfigs;
|
||||
summary.deletedNotifications += lineDeleted.deletedNotifications;
|
||||
}
|
||||
|
||||
const orphanCompanyLicenses = database.licenses.filter((item) => item.companyId === companyId).length;
|
||||
if (orphanCompanyLicenses > 0) {
|
||||
summary.deletedLicenses += orphanCompanyLicenses;
|
||||
database.licenses = database.licenses.filter((item) => item.companyId !== companyId);
|
||||
}
|
||||
|
||||
database.companies = database.companies.filter((item) => item.id !== companyId);
|
||||
return { success: true, deletedCompanyId: companyId, ...summary };
|
||||
});
|
||||
}
|
||||
case "createLicense": {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
@@ -520,20 +786,30 @@ function createApp({
|
||||
logRevokeAction({ event, licenseId, success: result.success, errCode: result.errCode });
|
||||
return result;
|
||||
}
|
||||
case "deleteLicense": {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
const licenseId = String(event.licenseId || "").trim();
|
||||
if (!licenseId) return { success: false, errMsg: "licenseId 不能为空", errCode: "LICENSE_ID_REQUIRED" };
|
||||
return store.update((database) => {
|
||||
const record = database.licenses.find((item) => item.licenseId === licenseId);
|
||||
if (!record) return { success: false, errMsg: "许可证不存在", errCode: "LICENSE_NOT_FOUND" };
|
||||
if (record.status === "active") {
|
||||
return {
|
||||
success: false,
|
||||
errMsg: "请先撤销许可证后再删除",
|
||||
errCode: "LICENSE_ACTIVE"
|
||||
};
|
||||
}
|
||||
database.licenses = database.licenses.filter((item) => item.licenseId !== licenseId);
|
||||
return { success: true, deletedLicenseId: licenseId };
|
||||
});
|
||||
}
|
||||
case "validateLicense": {
|
||||
const database = await store.read();
|
||||
const record = database.licenses.find((item) => item.licenseId === event.licenseId);
|
||||
if (!record) return { success: true, valid: false, status: "not_found" };
|
||||
if (event.deviceId && normalizeDeviceId(event.deviceId) !== record.deviceId) {
|
||||
return { success: true, valid: false, status: "device_mismatch", licenseId: record.licenseId };
|
||||
}
|
||||
if (new Date(record.expiryAt || parseLicenseTimestamp(record.expiry, "expiry")) <= new Date()) {
|
||||
return { success: true, valid: false, status: "expired", licenseId: record.licenseId };
|
||||
}
|
||||
return {
|
||||
success: true, valid: record.status === "active",
|
||||
status: record.status, licenseId: record.licenseId
|
||||
};
|
||||
const deviceId = event.deviceId ? normalizeDeviceId(event.deviceId) : null;
|
||||
return verifyLicenseRecord(record, deviceId);
|
||||
}
|
||||
case "uploadDataFile":
|
||||
if (normalizeRelativePath(event.folder, "data_record").endsWith("/model_config")) {
|
||||
@@ -567,13 +843,11 @@ function createApp({
|
||||
const database = await store.read();
|
||||
const record = database.fileRecords.find((item) => item.fileID === event.fileID);
|
||||
if (!record || !store.resolveStoredFile(record.fileID)) return { success: false, errMsg: "文件不存在" };
|
||||
if (!record.fileID.startsWith("model://")) {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
}
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
return {
|
||||
success: true,
|
||||
url: downloadUrl(req, record.fileID),
|
||||
url: issueDownloadUrl(req, record.fileID),
|
||||
fileName: record.fileName,
|
||||
originalFileName: record.originalFileName || record.fileName
|
||||
};
|
||||
@@ -594,8 +868,9 @@ function createApp({
|
||||
}
|
||||
if (!records.length) return { success: false, errMsg: "数据库中未找到对应记录" };
|
||||
let deletedCount = 0;
|
||||
for (const record of records) deletedCount += await removeFile(database, record.fileID);
|
||||
return { success: true, deletedFileID: records.map((record) => record.fileID).join(", "), deletedCount };
|
||||
const fileIDs = [...new Set(records.map((record) => record.fileID))];
|
||||
for (const fileID of fileIDs) deletedCount += await removeFile(database, fileID);
|
||||
return { success: true, deletedFileID: fileIDs.join(", "), deletedCount };
|
||||
});
|
||||
}
|
||||
case "publishIdentificationConfig": {
|
||||
@@ -623,13 +898,19 @@ function createApp({
|
||||
return { ...result, parameters: event.parameters };
|
||||
}
|
||||
case "getVolumeConfigFile": {
|
||||
const folder = process.env.VOLUME_CONFIG_FOLDER || "volume_config";
|
||||
const fileID = `local://${BASE_FOLDER}/${folder}/volume_config.json`;
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
const deviceId = normalizeDeviceId(event.deviceId);
|
||||
const database = await store.read();
|
||||
if (!database.fileRecords.some((record) => record.fileID === fileID)) {
|
||||
return { success: false, notFound: true, errMsg: "容积配置文件尚未发布" };
|
||||
}
|
||||
return { success: true, fileID, cloudPath: `${BASE_FOLDER}/${folder}/volume_config.json`, url: downloadUrl(req, fileID) };
|
||||
const config = database.volumeConfigs.find((item) => item.deviceId === deviceId);
|
||||
if (!config) return { success: true, found: false, deviceId };
|
||||
const record = database.fileRecords.find((item) => item.fileID === config.fileID);
|
||||
if (!record) return { success: true, found: false, deviceId };
|
||||
return {
|
||||
success: true, found: true, deviceId, fileID: record.fileID,
|
||||
fileName: record.fileName, url: issueDownloadUrl(req, record.fileID),
|
||||
updatedAtMs: config.updatedAtMs, requestId: config.requestId
|
||||
};
|
||||
}
|
||||
case "getFunctionConfig": {
|
||||
if (event.configType !== "volume") return { success: false, errMsg: `未知 configType: ${event.configType}` };
|
||||
@@ -643,7 +924,7 @@ function createApp({
|
||||
const record = database.fileRecords.find((item) => item.folder === `${deviceId}/identification_config` && item.fileName === "identification_config.csv");
|
||||
if (!record) return { success: false, errMsg: "服务器尚未配置辨识参数" };
|
||||
logConfigRead({ configType: "identification", deviceId, record });
|
||||
return { success: true, fileName: record.fileName, fileID: record.fileID, cloudPath: record.cloudPath, url: downloadUrl(req, record.fileID) };
|
||||
return { success: true, fileName: record.fileName, fileID: record.fileID, cloudPath: record.cloudPath, url: issueDownloadUrl(req, record.fileID) };
|
||||
}
|
||||
case "getPendingPanelFile": {
|
||||
const authError = requireAdmin(event);
|
||||
@@ -653,21 +934,62 @@ function createApp({
|
||||
const message = database.panelInbox.find((item) => {
|
||||
if (item.deviceId !== deviceId) return false;
|
||||
if (item.mediaType !== "csv") return true;
|
||||
return database.identificationFeedback.some((feedback) =>
|
||||
feedback.deviceId === deviceId && feedback.runId === item.fileName
|
||||
);
|
||||
return Boolean(findIdentificationFeedbackRecord(database, deviceId, item.fileName));
|
||||
});
|
||||
if (!message) return { success: true, pending: false };
|
||||
const runId = resolveIdentificationRunId(database, message);
|
||||
return {
|
||||
success: true,
|
||||
pending: true,
|
||||
fileID: message.fileID,
|
||||
fileName: message.fileName,
|
||||
runId,
|
||||
mediaType: message.mediaType,
|
||||
uploadTime: message.uploadTime,
|
||||
url: downloadUrl(req, message.fileID)
|
||||
url: issueDownloadUrl(req, message.fileID)
|
||||
};
|
||||
}
|
||||
case "getPendingPanelNotification": {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
const deviceId = normalizeDeviceId(event.deviceId);
|
||||
const database = await store.read();
|
||||
const notification = database.panelNotifications.find((item) => item.deviceId === deviceId);
|
||||
return notification
|
||||
? { success: true, pending: true, notification: enrichNotification(database, notification) }
|
||||
: { success: true, pending: false };
|
||||
}
|
||||
case "ackPanelNotification": {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
const deviceId = normalizeDeviceId(event.deviceId);
|
||||
const notificationId = String(event.notificationId || "");
|
||||
if (!notificationId) return { success: false, errMsg: "缺少 notificationId" };
|
||||
return store.update((database) => {
|
||||
const exactMatch = database.panelNotifications.find((item) =>
|
||||
item.deviceId === deviceId && item.notificationId === notificationId
|
||||
);
|
||||
if (exactMatch) {
|
||||
database.panelNotifications = database.panelNotifications.filter((item) => item !== exactMatch);
|
||||
return { success: true, notificationId, deleted: 1 };
|
||||
}
|
||||
|
||||
// 兼容:客户端 deviceId 切换或重连后,允许仅凭 notificationId 完成幂等关闭。
|
||||
const fallbackMatch = database.panelNotifications.find((item) => item.notificationId === notificationId);
|
||||
if (fallbackMatch) {
|
||||
database.panelNotifications = database.panelNotifications.filter((item) => item !== fallbackMatch);
|
||||
return {
|
||||
success: true,
|
||||
notificationId,
|
||||
deleted: 1,
|
||||
idempotent: true,
|
||||
reassignedDeviceId: fallbackMatch.deviceId
|
||||
};
|
||||
}
|
||||
|
||||
return { success: true, notificationId, deleted: 0, idempotent: true };
|
||||
});
|
||||
}
|
||||
case "ackPanelFile": {
|
||||
const authError = requireAdmin(event);
|
||||
if (authError) return { success: false, errMsg: authError };
|
||||
@@ -711,7 +1033,7 @@ function createApp({
|
||||
const offset = (page - 1) * pageSize;
|
||||
return {
|
||||
success: true,
|
||||
files: files.slice(offset, offset + pageSize),
|
||||
files: files.slice(offset, offset + pageSize).map((record) => enrichIdentificationFile(database, record)),
|
||||
total: files.length,
|
||||
page,
|
||||
pageSize
|
||||
@@ -761,7 +1083,7 @@ function createApp({
|
||||
fileName: record.fileName,
|
||||
uploadTime: record.uploadTime,
|
||||
size: record.size,
|
||||
url: downloadUrl(req, record.fileID)
|
||||
url: issueDownloadUrl(req, record.fileID)
|
||||
};
|
||||
}
|
||||
case "deleteControlFile": {
|
||||
@@ -788,12 +1110,14 @@ function createApp({
|
||||
if (!historyRecord || !fileRecord || !store.resolveStoredFile(fileRecord.fileID)) {
|
||||
return { success: false, errMsg: "辨识文件不存在" };
|
||||
}
|
||||
const runId = resolveIdentificationRunId(database, historyRecord);
|
||||
return {
|
||||
success: true,
|
||||
fileID: fileRecord.fileID,
|
||||
fileName: fileRecord.fileName,
|
||||
runId,
|
||||
mediaType: historyRecord.mediaType,
|
||||
url: downloadUrl(req, fileRecord.fileID)
|
||||
url: issueDownloadUrl(req, fileRecord.fileID)
|
||||
};
|
||||
}
|
||||
case "deleteIdentificationFile": {
|
||||
@@ -859,6 +1183,13 @@ function createApp({
|
||||
const createdAtMs = Date.now();
|
||||
const record = { deviceId, requestId: `${createdAtMs}-${randomUUID().slice(0, 10)}`, status: "waiting_upload", createdAtMs, expiresAtMs: createdAtMs + VOLUME_REQUEST_TTL_MS, configFileID: null, configFileName: null, uploadedAtMs: null, updateTime: new Date().toISOString() };
|
||||
database.volumeConfigRequests.push(record);
|
||||
addPanelNotification(database, {
|
||||
deviceId,
|
||||
type: "volume_request_started",
|
||||
title: "产线请求容积测试配置",
|
||||
message: "请完成容积测试配置并提交。",
|
||||
requestId: record.requestId
|
||||
});
|
||||
return { success: true, requestId: record.requestId, createdAtMs, expiresAtMs: record.expiresAtMs };
|
||||
});
|
||||
}
|
||||
@@ -890,6 +1221,14 @@ function createApp({
|
||||
record.configFileName = fileName;
|
||||
record.uploadedAtMs = Date.now();
|
||||
record.updateTime = new Date().toISOString();
|
||||
database.volumeConfigs = database.volumeConfigs.filter((item) => item.deviceId !== deviceId);
|
||||
database.volumeConfigs.push({
|
||||
deviceId,
|
||||
fileID: fileRecord.fileID,
|
||||
fileName: fileRecord.fileName,
|
||||
requestId: record.requestId,
|
||||
updatedAtMs: record.uploadedAtMs
|
||||
});
|
||||
return { success: true, requestId: record.requestId, uploadedAtMs: record.uploadedAtMs };
|
||||
});
|
||||
}
|
||||
@@ -903,14 +1242,17 @@ function createApp({
|
||||
const fileRecord = database.fileRecords.find((item) => item.fileID === record.configFileID);
|
||||
if (!fileRecord) return { success: false, errMsg: "容积配置文件记录不存在" };
|
||||
logConfigRead({ configType: "volume", deviceId, requestId: record.requestId, record: fileRecord });
|
||||
return { success: true, ready: true, expired: false, requestId: record.requestId, fileName: record.configFileName, fileID: fileRecord.fileID, cloudPath: fileRecord.cloudPath, uploadedAtMs: record.uploadedAtMs, url: downloadUrl(req, record.configFileID) };
|
||||
return { success: true, ready: true, expired: false, requestId: record.requestId, fileName: record.configFileName, fileID: fileRecord.fileID, cloudPath: fileRecord.cloudPath, uploadedAtMs: record.uploadedAtMs, url: issueDownloadUrl(req, record.configFileID) };
|
||||
}
|
||||
case "ackVolumeConfigRequest": {
|
||||
const deviceId = normalizeDeviceId(event.deviceId);
|
||||
if (!event.requestId) return { success: false, errMsg: "缺少 deviceId 或 requestId" };
|
||||
return store.update(async (database) => {
|
||||
const records = database.volumeConfigRequests.filter((item) => item.deviceId === deviceId && item.requestId === String(event.requestId));
|
||||
for (const record of records) if (record.configFileID) await removeFile(database, record.configFileID);
|
||||
for (const record of records) {
|
||||
const isLatest = database.volumeConfigs.some((item) => item.fileID === record.configFileID);
|
||||
if (record.configFileID && !isLatest) await removeFile(database, record.configFileID);
|
||||
}
|
||||
database.volumeConfigRequests = database.volumeConfigRequests.filter((item) => !records.includes(item));
|
||||
return { success: true, deleted: records.length };
|
||||
});
|
||||
@@ -920,6 +1262,88 @@ function createApp({
|
||||
}
|
||||
}
|
||||
|
||||
async function dispatchDevice(event, req) {
|
||||
if (event.type === "deviceAuth") {
|
||||
const licenseId = String(event.licenseId || "").trim();
|
||||
if (!licenseId) return { success: false, errCode: "LICENSE_ID_REQUIRED", errMsg: "缺少 licenseId" };
|
||||
const deviceId = normalizeDeviceId(event.deviceId);
|
||||
const database = await store.read();
|
||||
const record = database.licenses.find((item) => item.licenseId === licenseId);
|
||||
const validation = verifyLicenseRecord(record, deviceId);
|
||||
if (!validation.valid) {
|
||||
const errCodeMap = {
|
||||
not_found: "LICENSE_NOT_FOUND",
|
||||
device_mismatch: "DEVICE_ID_MISMATCH",
|
||||
expired: "LICENSE_EXPIRED",
|
||||
revoked: "LICENSE_REVOKED"
|
||||
};
|
||||
return {
|
||||
success: false,
|
||||
errCode: errCodeMap[validation.status] || "LICENSE_INVALID",
|
||||
errMsg: `许可证不可用: ${validation.status}`
|
||||
};
|
||||
}
|
||||
const issued = issueDeviceToken({ deviceId, licenseId });
|
||||
return {
|
||||
success: true,
|
||||
deviceId,
|
||||
licenseId,
|
||||
deviceToken: issued.token,
|
||||
expiresAtMs: issued.expiresAtMs
|
||||
};
|
||||
}
|
||||
|
||||
const context = consumeDeviceContext(event);
|
||||
if (!context.ok) return { success: false, errCode: context.errCode, errMsg: context.errMsg };
|
||||
const deviceId = context.deviceId;
|
||||
|
||||
switch (event.type) {
|
||||
case "deviceHeartbeat":
|
||||
case "registerIdentificationResult":
|
||||
case "getIdentificationFeedback":
|
||||
case "ackIdentificationFeedback":
|
||||
case "createVolumeConfigRequest":
|
||||
case "getPendingVolumeConfigRequest":
|
||||
case "submitVolumeConfigFile":
|
||||
case "getVolumeConfigRequest":
|
||||
case "ackVolumeConfigRequest":
|
||||
case "getIdentificationConfig":
|
||||
return dispatch({ ...event, deviceId }, req);
|
||||
case "uploadDataFile": {
|
||||
const folder = normalizeRelativePath(event.folder, "data_record");
|
||||
const devicePrefix = `${deviceId}/`;
|
||||
if (!folder.startsWith(devicePrefix)) {
|
||||
return { success: false, errMsg: "设备接口仅允许访问当前 deviceId 目录" };
|
||||
}
|
||||
if (folder.endsWith("/model_config")) {
|
||||
return { success: false, errMsg: "设备接口不允许上传模型" };
|
||||
}
|
||||
return dispatch({ ...event, folder }, req);
|
||||
}
|
||||
case "listModels": {
|
||||
const folder = `${deviceId}/model_config`;
|
||||
return dispatch({ ...event, folder }, req);
|
||||
}
|
||||
case "downloadModel": {
|
||||
if (!event.fileID) return { success: false, errMsg: "缺少 fileID" };
|
||||
const database = await store.read();
|
||||
const record = database.fileRecords.find((item) => item.fileID === event.fileID);
|
||||
if (!record || !store.resolveStoredFile(record.fileID)) return { success: false, errMsg: "文件不存在" };
|
||||
if (record.folder !== `${deviceId}/model_config`) {
|
||||
return { success: false, errMsg: "设备接口无权下载该模型" };
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
url: issueDownloadUrl(req, record.fileID),
|
||||
fileName: record.fileName,
|
||||
originalFileName: record.originalFileName || record.fileName
|
||||
};
|
||||
}
|
||||
default:
|
||||
return { success: false, errMsg: "无效的 type 字段" };
|
||||
}
|
||||
}
|
||||
|
||||
app.get("/health", (req, res) => res.json({ success: true, service: "reinloop-server" }));
|
||||
|
||||
app.post("/upload/:token", upload.single("file"), async (req, res, next) => {
|
||||
@@ -955,6 +1379,23 @@ function createApp({
|
||||
processedAt: null,
|
||||
expiresAt: null
|
||||
});
|
||||
addPanelNotification(database, {
|
||||
deviceId,
|
||||
type: "identification_result_ready",
|
||||
title: "系统辨识结果已返回",
|
||||
message: "新的系统辨识 CSV 或 JSON 数据已进入暂存。",
|
||||
fileID: pending.fileID
|
||||
});
|
||||
}
|
||||
if (folderParts.at(-1) === "V_config" && extension === ".json") {
|
||||
const deviceId = normalizeDeviceId(folderParts.slice(0, -1).join("/"));
|
||||
addPanelNotification(database, {
|
||||
deviceId,
|
||||
type: "volume_result_ready",
|
||||
title: "容积测试结果已返回",
|
||||
message: "容积测试结果 JSON 已上传,可打开查看。",
|
||||
fileID: pending.fileID
|
||||
});
|
||||
}
|
||||
if (pending.configType && pending.parameters) {
|
||||
const config = {
|
||||
@@ -999,12 +1440,13 @@ function createApp({
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/files/:fileID", async (req, res, next) => {
|
||||
app.get("/downloads/:ticket", async (req, res, next) => {
|
||||
try {
|
||||
const fileID = decodeURIComponent(req.params.fileID);
|
||||
if (!fileID.startsWith("model://") && !hasValidDownloadToken(req, fileID)) {
|
||||
return res.status(403).json({ success: false, errMsg: "下载凭证无效或已过期" });
|
||||
}
|
||||
const ticket = String(req.params.ticket || "").trim();
|
||||
if (!ticket) return res.status(403).json({ success: false, errMsg: "下载链接无效或已失效" });
|
||||
const consumed = consumeDownloadTicket(req, ticket);
|
||||
if (!consumed.ok) return res.status(403).json({ success: false, errMsg: consumed.errMsg });
|
||||
const fileID = consumed.fileID;
|
||||
const database = await store.read();
|
||||
const record = database.fileRecords.find((item) => item.fileID === fileID);
|
||||
const filePath = record && store.resolveStoredFile(fileID);
|
||||
@@ -1017,6 +1459,10 @@ function createApp({
|
||||
}
|
||||
});
|
||||
|
||||
app.get("/files/:fileID", (req, res) => {
|
||||
res.status(403).json({ success: false, errMsg: "旧下载链接已停用,请先通过 API 获取临时下载链接" });
|
||||
});
|
||||
|
||||
const apiHandler = async (req, res) => {
|
||||
try {
|
||||
res.json(await dispatch(normalizeEventCompat(req.body || {}), req));
|
||||
@@ -1025,7 +1471,15 @@ function createApp({
|
||||
}
|
||||
};
|
||||
app.post("/", apiHandler);
|
||||
app.post("/api", apiHandler);
|
||||
|
||||
const deviceApiHandler = async (req, res) => {
|
||||
try {
|
||||
res.json(await dispatchDevice(normalizeEventCompat(req.body || {}), req));
|
||||
} catch (error) {
|
||||
res.status(400).json({ success: false, errMsg: error.message });
|
||||
}
|
||||
};
|
||||
app.post("/device", deviceApiHandler);
|
||||
|
||||
app.use((error, req, res, next) => {
|
||||
console.error(error);
|
||||
|
||||
@@ -41,6 +41,8 @@ class PostgresStore {
|
||||
const identificationFiles = await queryable.query("SELECT file_id, device_id, file_name, media_type, upload_time, size_bytes, status, processed_at, expires_at FROM identification_files");
|
||||
const feedback = await queryable.query("SELECT device_id, run_id, file_name, status, result, update_time FROM identification_feedback");
|
||||
const requests = await queryable.query("SELECT device_id, request_id, status, created_at_ms, expires_at_ms, config_file_id, config_file_name, uploaded_at_ms, update_time FROM volume_config_requests");
|
||||
const volumeConfigs = await queryable.query("SELECT device_id, file_id, file_name, request_id, updated_at_ms FROM volume_configs");
|
||||
const panelNotifications = await queryable.query("SELECT notification_id, device_id, type, title, message, request_id, file_id, created_at_ms FROM panel_notifications");
|
||||
return {
|
||||
...structuredClone(EMPTY_DATABASE),
|
||||
companies: companies.rows.map((row) => ({ id: row.id, name: row.name, code: row.code, createdAt: asIso(row.created_at) })),
|
||||
@@ -51,7 +53,9 @@ class PostgresStore {
|
||||
panelInbox: panelInbox.rows.map((row) => ({ fileID: row.file_id, deviceId: row.device_id, fileName: row.file_name, mediaType: row.media_type, uploadTime: asIso(row.upload_time) })),
|
||||
identificationFiles: identificationFiles.rows.map((row) => ({ fileID: row.file_id, deviceId: row.device_id, fileName: row.file_name, mediaType: row.media_type, uploadTime: asIso(row.upload_time), size: Number(row.size_bytes), status: row.status, processedAt: row.processed_at && asIso(row.processed_at), expiresAt: row.expires_at && asIso(row.expires_at) })),
|
||||
identificationFeedback: feedback.rows.map((row) => ({ deviceId: row.device_id, runId: row.run_id, fileName: row.file_name, status: row.status, result: row.result, updateTime: asIso(row.update_time) })),
|
||||
volumeConfigRequests: requests.rows.map((row) => ({ deviceId: row.device_id, requestId: row.request_id, status: row.status, createdAtMs: Number(row.created_at_ms), expiresAtMs: Number(row.expires_at_ms), configFileID: row.config_file_id, configFileName: row.config_file_name, uploadedAtMs: row.uploaded_at_ms && Number(row.uploaded_at_ms), updateTime: asIso(row.update_time) }))
|
||||
volumeConfigRequests: requests.rows.map((row) => ({ deviceId: row.device_id, requestId: row.request_id, status: row.status, createdAtMs: Number(row.created_at_ms), expiresAtMs: Number(row.expires_at_ms), configFileID: row.config_file_id, configFileName: row.config_file_name, uploadedAtMs: row.uploaded_at_ms && Number(row.uploaded_at_ms), updateTime: asIso(row.update_time) })),
|
||||
volumeConfigs: volumeConfigs.rows.map((row) => ({ deviceId: row.device_id, fileID: row.file_id, fileName: row.file_name, requestId: row.request_id, updatedAtMs: Number(row.updated_at_ms) })),
|
||||
panelNotifications: panelNotifications.rows.map((row) => ({ notificationId: row.notification_id, deviceId: row.device_id, type: row.type, title: row.title, message: row.message, requestId: row.request_id, fileID: row.file_id, createdAtMs: Number(row.created_at_ms) }))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -74,7 +78,7 @@ class PostgresStore {
|
||||
}
|
||||
|
||||
async writeDatabase(client, database) {
|
||||
await client.query("DELETE FROM volume_config_requests; DELETE FROM identification_feedback; DELETE FROM panel_inbox; DELETE FROM identification_files; DELETE FROM function_configs; DELETE FROM file_records; DELETE FROM licenses; DELETE FROM production_lines; DELETE FROM companies;");
|
||||
await client.query("DELETE FROM panel_notifications; DELETE FROM volume_configs; DELETE FROM volume_config_requests; DELETE FROM identification_feedback; DELETE FROM panel_inbox; DELETE FROM identification_files; DELETE FROM function_configs; DELETE FROM file_records; DELETE FROM licenses; DELETE FROM production_lines; DELETE FROM companies;");
|
||||
for (const item of database.companies) await client.query("INSERT INTO companies (id, name, code, created_at) VALUES ($1, $2, $3, $4)", [item.id, item.name, item.code, item.createdAt]);
|
||||
for (const item of database.productionLines) await client.query("INSERT INTO production_lines (id, company_id, name, code, device_id, created_at, last_seen_at) VALUES ($1, $2, $3, $4, $5, $6, $7)", [item.id, item.companyId, item.name, item.code, item.deviceId, item.createdAt, item.lastSeenAt]);
|
||||
for (const item of database.licenses) await client.query("INSERT INTO licenses (license_id, company_id, production_line_id, device_id, customer, issued_at, expiry_at, features, license, status, created_at, revoked_at, revocation_reason) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)", [item.licenseId, item.companyId, item.productionLineId, item.deviceId, item.customer, item.issuedAt, item.expiryAt, item.features, item.license, item.status, item.createdAt, item.revokedAt, item.revocationReason]);
|
||||
@@ -84,6 +88,8 @@ class PostgresStore {
|
||||
for (const item of database.identificationFiles) await client.query("INSERT INTO identification_files (file_id, device_id, file_name, media_type, upload_time, size_bytes, status, processed_at, expires_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)", [item.fileID, item.deviceId, item.fileName, item.mediaType, item.uploadTime, item.size, item.status, item.processedAt, item.expiresAt]);
|
||||
for (const item of database.identificationFeedback) await client.query("INSERT INTO identification_feedback (device_id, run_id, file_name, status, result, update_time) VALUES ($1,$2,$3,$4,$5,$6)", [item.deviceId, item.runId, item.fileName, item.status, item.result, item.updateTime]);
|
||||
for (const item of database.volumeConfigRequests) await client.query("INSERT INTO volume_config_requests (device_id, request_id, status, created_at_ms, expires_at_ms, config_file_id, config_file_name, uploaded_at_ms, update_time) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9)", [item.deviceId, item.requestId, item.status, item.createdAtMs, item.expiresAtMs, item.configFileID, item.configFileName, item.uploadedAtMs, item.updateTime]);
|
||||
for (const item of database.volumeConfigs) await client.query("INSERT INTO volume_configs (device_id, file_id, file_name, request_id, updated_at_ms) VALUES ($1,$2,$3,$4,$5)", [item.deviceId, item.fileID, item.fileName, item.requestId, item.updatedAtMs]);
|
||||
for (const item of database.panelNotifications) await client.query("INSERT INTO panel_notifications (notification_id, device_id, type, title, message, request_id, file_id, created_at_ms) VALUES ($1,$2,$3,$4,$5,$6,$7,$8)", [item.notificationId, item.deviceId, item.type, item.title, item.message, item.requestId, item.fileID, item.createdAtMs]);
|
||||
}
|
||||
|
||||
createId(prefix) {
|
||||
|
||||
@@ -39,7 +39,7 @@ async function main() {
|
||||
purgeTimer.unref();
|
||||
app.listen(port, host, () => {
|
||||
console.log(`ReinLoop server listening on http://${host}:${port}`);
|
||||
console.log(`API endpoints: http://${host}:${port} and http://${host}:${port}/api`);
|
||||
console.log(`API endpoint: http://${host}:${port}`);
|
||||
console.log(`Data directory: ${dataDirectory}`);
|
||||
console.log(`Metadata store: ${process.env.DATABASE_URL ? "PostgreSQL" : "local JSON"}`);
|
||||
});
|
||||
|
||||
@@ -9,6 +9,8 @@ const EMPTY_DATABASE = {
|
||||
identificationFiles: [],
|
||||
identificationFeedback: [],
|
||||
volumeConfigRequests: [],
|
||||
volumeConfigs: [],
|
||||
panelNotifications: [],
|
||||
userInfo: [],
|
||||
companies: [],
|
||||
productionLines: [],
|
||||
|
||||
+337
-3
@@ -15,7 +15,7 @@ let licensePrivateKey;
|
||||
let licensePublicKeyPath;
|
||||
|
||||
async function post(payload) {
|
||||
const response = await fetch(`${baseUrl}/api`, {
|
||||
const response = await fetch(baseUrl, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify(payload)
|
||||
@@ -59,7 +59,7 @@ test("health endpoint reports ready", async () => {
|
||||
assert.deepEqual(await response.json(), { success: true, service: "reinloop-server" });
|
||||
});
|
||||
|
||||
test("root endpoint accepts API requests without the /api suffix", async () => {
|
||||
test("root endpoint accepts API requests", async () => {
|
||||
const response = await fetch(baseUrl, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
@@ -69,6 +69,15 @@ test("root endpoint accepts API requests without the /api suffix", async () => {
|
||||
assert.deepEqual(await response.json(), { success: true, companies: [] });
|
||||
});
|
||||
|
||||
test("legacy /api endpoint is unavailable", async () => {
|
||||
const response = await fetch(`${baseUrl}/api`, {
|
||||
method: "POST",
|
||||
headers: { "content-type": "application/json" },
|
||||
body: JSON.stringify({ type: "listOrganizations", adminToken: "test-token" })
|
||||
});
|
||||
assert.equal(response.status, 404);
|
||||
});
|
||||
|
||||
test("legacy data_record endpoint and uploadUserInfo type are unavailable", async () => {
|
||||
const legacyRoute = await fetch(`${baseUrl}/data_record`, {
|
||||
method: "POST",
|
||||
@@ -112,6 +121,7 @@ test("existing two-step client flow uploads, lists, and downloads a file", async
|
||||
});
|
||||
const downloaded = await fetch(download.url);
|
||||
assert.equal(await downloaded.text(), "time,pressure\n0,10\n");
|
||||
assert.equal((await fetch(download.url)).status, 403);
|
||||
});
|
||||
|
||||
test("admin lists, downloads, and deletes only the selected device control data", async () => {
|
||||
@@ -150,8 +160,9 @@ test("admin lists, downloads, and deletes only the selected device control data"
|
||||
});
|
||||
assert.equal(download.success, true);
|
||||
assert.equal(download.size, Buffer.byteLength('{"parts":1}'));
|
||||
assert.match(download.url, /expires=.*token=/);
|
||||
assert.match(download.url, /\/downloads\//);
|
||||
assert.equal(await (await fetch(download.url)).text(), '{"parts":1}');
|
||||
assert.equal((await fetch(download.url)).status, 403);
|
||||
|
||||
const forbiddenDelete = await post({
|
||||
type: "deleteControlFile", fileID: "model://control-co/line-1/controller.bin", adminToken: "test-token"
|
||||
@@ -244,6 +255,19 @@ test("model upload requires explicit overwrite for an existing file", async () =
|
||||
await fs.promises.readFile(path.join(dataDirectory, "models", "company-a", "overwrite-line", "controller.bin"), "utf8"),
|
||||
"second-version"
|
||||
);
|
||||
|
||||
const deleted = await post({
|
||||
type: "deleteModel", fileID: replacement.fileID, adminToken: "test-token"
|
||||
});
|
||||
assert.equal(deleted.success, true);
|
||||
assert.equal(deleted.deletedCount, 1);
|
||||
const listed = await post({
|
||||
type: "listModels", folder: "company-a/overwrite-line/model_config"
|
||||
});
|
||||
assert.deepEqual(listed.fileList, []);
|
||||
await assert.rejects(fs.promises.access(
|
||||
path.join(dataDirectory, "models", "company-a", "overwrite-line", "controller.bin")
|
||||
), { code: "ENOENT" });
|
||||
});
|
||||
|
||||
test("panel consumes uploaded device files from an inbox without scanning folders", async () => {
|
||||
@@ -270,6 +294,7 @@ test("panel consumes uploaded device files from an inbox without scanning folder
|
||||
});
|
||||
assert.equal(pending.pending, true);
|
||||
assert.equal(pending.fileName, "result_20260724_120000.csv");
|
||||
assert.equal(pending.runId, "result_20260724_120000.csv");
|
||||
assert.equal(await (await fetch(pending.url)).text(), "t,u,p\n0,10,20\n");
|
||||
|
||||
assert.equal((await post({
|
||||
@@ -304,6 +329,7 @@ test("panel consumes uploaded device files from an inbox without scanning folder
|
||||
});
|
||||
assert.equal(history.total, 2);
|
||||
const csvHistory = history.files.find((file) => file.fileID === pending.fileID);
|
||||
assert.equal(csvHistory.runId, "result_20260724_120000.csv");
|
||||
assert.equal(csvHistory.status, "processed");
|
||||
assert.ok(csvHistory.processedAt);
|
||||
assert.ok(csvHistory.expiresAt);
|
||||
@@ -488,6 +514,85 @@ test("volume request accepts only the file uploaded for that request", async ()
|
||||
assert.deepEqual(await (await fetch(ready.url)).json(), { num_runs: 2 });
|
||||
});
|
||||
|
||||
test("panel notifications and volume configuration stay isolated by device", async () => {
|
||||
const deviceId = "notify-co/line-1";
|
||||
const otherDeviceId = "notify-co/line-2";
|
||||
const request = await post({ type: "createVolumeConfigRequest", deviceId });
|
||||
const notification = await post({
|
||||
type: "getPendingPanelNotification", deviceId, adminToken: "test-token"
|
||||
});
|
||||
assert.equal(notification.pending, true);
|
||||
assert.equal(notification.notification.type, "volume_request_started");
|
||||
assert.equal(notification.notification.requestId, request.requestId);
|
||||
assert.equal((await post({
|
||||
type: "getPendingPanelNotification", deviceId: otherDeviceId, adminToken: "test-token"
|
||||
})).pending, false);
|
||||
|
||||
const folder = `${deviceId}/volume_config_requests/${request.requestId}`;
|
||||
const issued = await post({ type: "uploadDataFile", fileName: "volume_measurement.json", folder });
|
||||
const form = new FormData();
|
||||
form.append("file", new Blob(['{"num_runs":2}'], { type: "application/json" }), "volume_measurement.json");
|
||||
assert.equal((await fetch(issued.uploadMetadata.url, { method: "POST", body: form })).status, 204);
|
||||
assert.equal((await post({
|
||||
type: "submitVolumeConfigFile", deviceId, requestId: request.requestId,
|
||||
fileID: issued.fileID, fileName: "volume_measurement.json"
|
||||
})).success, true);
|
||||
|
||||
const config = await post({ type: "getVolumeConfigFile", deviceId, adminToken: "test-token" });
|
||||
assert.equal(config.found, true);
|
||||
assert.equal(config.fileID, issued.fileID);
|
||||
assert.equal((await post({
|
||||
type: "getVolumeConfigFile", deviceId: otherDeviceId, adminToken: "test-token"
|
||||
})).found, false);
|
||||
assert.equal((await post({
|
||||
type: "ackPanelNotification", deviceId, notificationId: notification.notification.notificationId,
|
||||
adminToken: "test-token"
|
||||
})).success, true);
|
||||
const duplicatedAck = await post({
|
||||
type: "ackPanelNotification", deviceId, notificationId: notification.notification.notificationId,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
assert.equal(duplicatedAck.success, true);
|
||||
assert.equal(duplicatedAck.idempotent, true);
|
||||
assert.equal(duplicatedAck.deleted, 0);
|
||||
assert.equal((await post({
|
||||
type: "getPendingPanelNotification", deviceId, adminToken: "test-token"
|
||||
})).pending, false);
|
||||
|
||||
async function uploadResult(folderName, fileName, content) {
|
||||
const result = await post({ type: "uploadDataFile", fileName, folder: `${deviceId}/${folderName}` });
|
||||
const uploadForm = new FormData();
|
||||
uploadForm.append("file", new Blob([content]), fileName);
|
||||
assert.equal((await fetch(result.uploadMetadata.url, { method: "POST", body: uploadForm })).status, 204);
|
||||
}
|
||||
await uploadResult("V_config", "volume_result.json", '{"volume_L":1.2}');
|
||||
const volumeResult = await post({
|
||||
type: "getPendingPanelNotification", deviceId, adminToken: "test-token"
|
||||
});
|
||||
assert.equal(volumeResult.notification.type, "volume_result_ready");
|
||||
assert.ok(volumeResult.notification.fileID);
|
||||
const fallbackAck = await post({
|
||||
type: "ackPanelNotification",
|
||||
deviceId: otherDeviceId,
|
||||
notificationId: volumeResult.notification.notificationId,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
assert.equal(fallbackAck.success, true);
|
||||
assert.equal(fallbackAck.idempotent, true);
|
||||
assert.equal(fallbackAck.deleted, 1);
|
||||
await post({
|
||||
type: "ackPanelNotification", deviceId, notificationId: volumeResult.notification.notificationId,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
|
||||
await uploadResult("ind_data", "identification_result.json", '{"stable_pressures":[]}');
|
||||
const identificationResult = await post({
|
||||
type: "getPendingPanelNotification", deviceId, adminToken: "test-token"
|
||||
});
|
||||
assert.equal(identificationResult.notification.type, "identification_result_ready");
|
||||
assert.equal(identificationResult.notification.runId, "identification_result.json");
|
||||
});
|
||||
|
||||
test("admin manages companies and production lines with a stable device id", async () => {
|
||||
const unauthorized = await post({
|
||||
type: "createCompany", name: "未授权公司", code: "blocked"
|
||||
@@ -691,3 +796,232 @@ test("license creation verifies the signed payload and validates expiry in real
|
||||
type: "validateLicense", licenseId: expiredId, deviceId: line.productionLine.deviceId
|
||||
}), { success: true, valid: false, status: "expired", licenseId: expiredId });
|
||||
});
|
||||
|
||||
test("deleteLicense requires prior revocation", async () => {
|
||||
const suffix = crypto.randomUUID().slice(0, 8);
|
||||
const company = await post({
|
||||
type: "createCompany", name: `删除许可证公司-${suffix}`, code: `del-lic-${suffix}`,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
const line = await post({
|
||||
type: "createProductionLine", companyId: company.company.id,
|
||||
name: "许可证线", code: "line-1", adminToken: "test-token"
|
||||
});
|
||||
const licenseId = crypto.randomUUID();
|
||||
const payload = {
|
||||
license_id: licenseId,
|
||||
company_id: company.company.id,
|
||||
production_line_id: line.productionLine.id,
|
||||
customer: company.company.name,
|
||||
device_id: line.productionLine.deviceId,
|
||||
issued: "2026-08-01 10:00",
|
||||
expiry: "2028-08-01 10:00",
|
||||
features: "*"
|
||||
};
|
||||
const created = await post({
|
||||
type: "createLicense", licenseId,
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
customer: payload.customer,
|
||||
issued: payload.issued,
|
||||
expiry: payload.expiry,
|
||||
features: payload.features,
|
||||
license: signLicense(payload),
|
||||
adminToken: "test-token"
|
||||
});
|
||||
assert.equal(created.success, true);
|
||||
|
||||
const activeDelete = await post({ type: "deleteLicense", licenseId, adminToken: "test-token" });
|
||||
assert.equal(activeDelete.success, false);
|
||||
assert.equal(activeDelete.errCode, "LICENSE_ACTIVE");
|
||||
|
||||
const revoked = await post({
|
||||
type: "revokeLicense", licenseId, reason: "测试删除", adminToken: "test-token"
|
||||
});
|
||||
assert.equal(revoked.success, true);
|
||||
|
||||
const deleted = await post({ type: "deleteLicense", licenseId, adminToken: "test-token" });
|
||||
assert.deepEqual(deleted, { success: true, deletedLicenseId: licenseId });
|
||||
|
||||
const missing = await post({ type: "getLicense", licenseId, adminToken: "test-token" });
|
||||
assert.equal(missing.success, false);
|
||||
});
|
||||
|
||||
test("deleteProductionLine blocks active licenses and removes revoked data", async () => {
|
||||
const suffix = crypto.randomUUID().slice(0, 8);
|
||||
const company = await post({
|
||||
type: "createCompany", name: `删除产线公司-${suffix}`, code: `del-line-${suffix}`,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
const line = await post({
|
||||
type: "createProductionLine", companyId: company.company.id,
|
||||
name: "待删产线", code: "line-1", adminToken: "test-token"
|
||||
});
|
||||
|
||||
const modelIssued = await post({
|
||||
type: "issueModelUpload", deviceId: line.productionLine.deviceId,
|
||||
fileName: "controller.bin", adminToken: "test-token"
|
||||
});
|
||||
const modelForm = new FormData();
|
||||
modelForm.append("file", new Blob(["model-bytes"]), "controller.bin");
|
||||
assert.equal((await fetch(modelIssued.uploadMetadata.url, { method: "POST", body: modelForm })).status, 204);
|
||||
|
||||
const licenseId = crypto.randomUUID();
|
||||
const payload = {
|
||||
license_id: licenseId,
|
||||
company_id: company.company.id,
|
||||
production_line_id: line.productionLine.id,
|
||||
customer: company.company.name,
|
||||
device_id: line.productionLine.deviceId,
|
||||
issued: "2026-08-01 10:00",
|
||||
expiry: "2028-08-01 10:00",
|
||||
features: "*"
|
||||
};
|
||||
await post({
|
||||
type: "createLicense", licenseId,
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
customer: payload.customer,
|
||||
issued: payload.issued,
|
||||
expiry: payload.expiry,
|
||||
features: payload.features,
|
||||
license: signLicense(payload),
|
||||
adminToken: "test-token"
|
||||
});
|
||||
|
||||
const blocked = await post({
|
||||
type: "deleteProductionLine",
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
assert.equal(blocked.success, false);
|
||||
assert.equal(blocked.errCode, "ACTIVE_LICENSES_PRESENT");
|
||||
|
||||
await post({ type: "revokeLicense", licenseId, reason: "产线删除", adminToken: "test-token" });
|
||||
|
||||
const orphanModelDir = path.join(dataDirectory, "models", line.productionLine.deviceId);
|
||||
const orphanFileDir = path.join(dataDirectory, "files", "ReinLoop_GUI", line.productionLine.deviceId);
|
||||
await fs.promises.mkdir(orphanModelDir, { recursive: true });
|
||||
await fs.promises.mkdir(orphanFileDir, { recursive: true });
|
||||
await fs.promises.writeFile(path.join(orphanModelDir, "orphan.bin"), "orphan-model");
|
||||
await fs.promises.writeFile(path.join(orphanFileDir, "orphan.json"), "orphan-file");
|
||||
|
||||
const deleted = await post({
|
||||
type: "deleteProductionLine",
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
assert.equal(deleted.success, true);
|
||||
assert.ok(deleted.deletedFiles >= 1);
|
||||
assert.ok(deleted.deletedLicenses >= 1);
|
||||
assert.ok(deleted.deletedDirectories >= 1);
|
||||
|
||||
await assert.rejects(fs.promises.access(orphanModelDir));
|
||||
await assert.rejects(fs.promises.access(orphanFileDir));
|
||||
|
||||
const organizations = await post({ type: "listOrganizations", adminToken: "test-token" });
|
||||
const listedCompany = organizations.companies.find((item) => item.id === company.company.id);
|
||||
assert.ok(listedCompany);
|
||||
assert.equal(listedCompany.productionLines.some((item) => item.id === line.productionLine.id), false);
|
||||
|
||||
const models = await post({ type: "listModels", folder: `${line.productionLine.deviceId}/model_config` });
|
||||
assert.deepEqual(models.fileList, []);
|
||||
});
|
||||
|
||||
test("deleteCompany cascades revoked data cleanup", async () => {
|
||||
const suffix = crypto.randomUUID().slice(0, 8);
|
||||
const company = await post({
|
||||
type: "createCompany", name: `删除公司-${suffix}`, code: `del-co-${suffix}`,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
const line = await post({
|
||||
type: "createProductionLine", companyId: company.company.id,
|
||||
name: "子产线", code: "line-1", adminToken: "test-token"
|
||||
});
|
||||
|
||||
const modelIssued = await post({
|
||||
type: "issueModelUpload", deviceId: line.productionLine.deviceId,
|
||||
fileName: "company-cascade.bin", adminToken: "test-token"
|
||||
});
|
||||
const modelForm = new FormData();
|
||||
modelForm.append("file", new Blob(["cascade-model"]), "company-cascade.bin");
|
||||
assert.equal((await fetch(modelIssued.uploadMetadata.url, { method: "POST", body: modelForm })).status, 204);
|
||||
|
||||
const licenseId = crypto.randomUUID();
|
||||
const payload = {
|
||||
license_id: licenseId,
|
||||
company_id: company.company.id,
|
||||
production_line_id: line.productionLine.id,
|
||||
customer: company.company.name,
|
||||
device_id: line.productionLine.deviceId,
|
||||
issued: "2026-08-01 10:00",
|
||||
expiry: "2028-08-01 10:00",
|
||||
features: "*"
|
||||
};
|
||||
await post({
|
||||
type: "createLicense", licenseId,
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
customer: payload.customer,
|
||||
issued: payload.issued,
|
||||
expiry: payload.expiry,
|
||||
features: payload.features,
|
||||
license: signLicense(payload),
|
||||
adminToken: "test-token"
|
||||
});
|
||||
await post({ type: "revokeLicense", licenseId, reason: "公司删除", adminToken: "test-token" });
|
||||
|
||||
const deleted = await post({ type: "deleteCompany", companyId: company.company.id, adminToken: "test-token" });
|
||||
assert.equal(deleted.success, true);
|
||||
assert.equal(deleted.deletedCompanyId, company.company.id);
|
||||
assert.ok(deleted.deletedProductionLines >= 1);
|
||||
assert.ok(deleted.deletedLicenses >= 1);
|
||||
assert.ok(deleted.deletedFiles >= 1);
|
||||
|
||||
const organizations = await post({ type: "listOrganizations", adminToken: "test-token" });
|
||||
assert.equal(organizations.companies.some((item) => item.id === company.company.id), false);
|
||||
|
||||
const models = await post({ type: "listModels", folder: `${line.productionLine.deviceId}/model_config` });
|
||||
assert.deepEqual(models.fileList, []);
|
||||
});
|
||||
|
||||
test("deleteCompany blocks when active licenses exist", async () => {
|
||||
const suffix = crypto.randomUUID().slice(0, 8);
|
||||
const company = await post({
|
||||
type: "createCompany", name: `删除公司阻断-${suffix}`, code: `del-co-block-${suffix}`,
|
||||
adminToken: "test-token"
|
||||
});
|
||||
const line = await post({
|
||||
type: "createProductionLine", companyId: company.company.id,
|
||||
name: "阻断产线", code: "line-1", adminToken: "test-token"
|
||||
});
|
||||
|
||||
const licenseId = crypto.randomUUID();
|
||||
const payload = {
|
||||
license_id: licenseId,
|
||||
company_id: company.company.id,
|
||||
production_line_id: line.productionLine.id,
|
||||
customer: company.company.name,
|
||||
device_id: line.productionLine.deviceId,
|
||||
issued: "2026-08-01 10:00",
|
||||
expiry: "2028-08-01 10:00",
|
||||
features: "*"
|
||||
};
|
||||
await post({
|
||||
type: "createLicense", licenseId,
|
||||
companyId: company.company.id,
|
||||
productionLineId: line.productionLine.id,
|
||||
customer: payload.customer,
|
||||
issued: payload.issued,
|
||||
expiry: payload.expiry,
|
||||
features: payload.features,
|
||||
license: signLicense(payload),
|
||||
adminToken: "test-token"
|
||||
});
|
||||
|
||||
const blocked = await post({ type: "deleteCompany", companyId: company.company.id, adminToken: "test-token" });
|
||||
assert.equal(blocked.success, false);
|
||||
assert.equal(blocked.errCode, "ACTIVE_LICENSES_PRESENT");
|
||||
});
|
||||
Reference in New Issue
Block a user