新增 WMS 搬运规则与任务管理
支持搬运规则维护、候选预览、任务生成、预占、下发、取消和完成,并完善仓储管理前端交互。
This commit is contained in:
@@ -0,0 +1,227 @@
|
||||
/**
|
||||
* AGV 播放页外部 API:标准化请求/响应格式。
|
||||
*
|
||||
* 嵌入 iframe 时父页面:
|
||||
* await iframe.contentWindow.AgvPlaybackApi.getCatalog()
|
||||
* await iframe.contentWindow.AgvPlaybackApi.request({ version: 1, method: "playAction", params: { modelId: "英招", actionId: "dongzuo1" } })
|
||||
*
|
||||
* 跨窗口 postMessage(父 → 播放页):
|
||||
* iframe.contentWindow.postMessage({ version: 1, method: "playAction", params: { modelId: "英招", actionId: "dongzuo1" }, id: "req-1" }, "*")
|
||||
* 播放页回复:{ version: 1, id: "req-1", ok: true, data: { ... } }
|
||||
*/
|
||||
|
||||
export const API_VERSION = 1;
|
||||
|
||||
export const METHODS = {
|
||||
PLAY_ACTION: "playAction",
|
||||
STOP_ACTION: "stopAction",
|
||||
CLEAR_ACTIONS: "clearActions",
|
||||
};
|
||||
|
||||
const ALLOWED_METHODS = new Set(Object.values(METHODS));
|
||||
|
||||
const ERROR = {
|
||||
INVALID_REQUEST: "INVALID_REQUEST",
|
||||
UNKNOWN_METHOD: "UNKNOWN_METHOD",
|
||||
INTERNAL_ERROR: "INTERNAL_ERROR",
|
||||
};
|
||||
|
||||
function isPlainObject(v) {
|
||||
return v != null && typeof v === "object" && !Array.isArray(v);
|
||||
}
|
||||
|
||||
function asString(v) {
|
||||
return typeof v === "string" ? v.trim() : "";
|
||||
}
|
||||
|
||||
function asStringList(v) {
|
||||
if (Array.isArray(v)) return v.map(asString).filter(Boolean);
|
||||
if (typeof v === "string") return v.split(/[,+\s]+/).map((s) => s.trim()).filter(Boolean);
|
||||
return [];
|
||||
}
|
||||
|
||||
async function ensureModel(controller, params) {
|
||||
const modelId = asString(params.modelId ?? params.model);
|
||||
if (!modelId) return { ok: false, error: { code: ERROR.INVALID_REQUEST, message: "params.modelId 必填" } };
|
||||
const cur = controller.getState();
|
||||
if (cur.modelId === modelId && cur.ready) return { ok: true };
|
||||
await controller.loadModel(modelId);
|
||||
return { ok: true };
|
||||
}
|
||||
|
||||
/** @typedef {{ version: number, id?: string, method: string, params?: object }} AgvApiRequest */
|
||||
/** @typedef {{ version: number, id?: string, ok: boolean, data?: object|null, error?: { code: string, message: string }|null }} AgvApiResponse */
|
||||
|
||||
/**
|
||||
* 校验并规范化入站请求。
|
||||
* @param {unknown} raw
|
||||
* @returns {{ ok: true, request: AgvApiRequest } | { ok: false, error: { code: string, message: string } } }
|
||||
*/
|
||||
export function parseRequest(raw) {
|
||||
if (!isPlainObject(raw)) {
|
||||
return { ok: false, error: { code: ERROR.INVALID_REQUEST, message: "请求体须为 JSON 对象" } };
|
||||
}
|
||||
const version = Number(raw.version);
|
||||
if (version !== API_VERSION) {
|
||||
return { ok: false, error: { code: ERROR.INVALID_REQUEST, message: `不支持的 version,当前为 ${API_VERSION}` } };
|
||||
}
|
||||
const method = asString(raw.method);
|
||||
if (!method) {
|
||||
return { ok: false, error: { code: ERROR.INVALID_REQUEST, message: "缺少 method" } };
|
||||
}
|
||||
if (!ALLOWED_METHODS.has(method)) {
|
||||
return { ok: false, error: { code: ERROR.UNKNOWN_METHOD, message: "仅支持 playAction、stopAction、clearActions" } };
|
||||
}
|
||||
const params = isPlainObject(raw.params) ? raw.params : {};
|
||||
const id = raw.id != null ? String(raw.id) : undefined;
|
||||
return { ok: true, request: { version, method, params, id } };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string|undefined} id
|
||||
* @param {boolean} ok
|
||||
* @param {object|null} [data]
|
||||
* @param {{ code: string, message: string }|null} [error]
|
||||
* @returns {AgvApiResponse}
|
||||
*/
|
||||
export function formatResponse(id, ok, data = null, error = null) {
|
||||
return { version: API_VERSION, id, ok, data, error };
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {object} controller
|
||||
* @param {{ onStateChange?: (state: object) => void, allowedOrigins?: string[]|null }} [options]
|
||||
*/
|
||||
export function createPlaybackApi(controller, options = {}) {
|
||||
const allowedOrigins = options.allowedOrigins ?? null;
|
||||
let messageBound = false;
|
||||
|
||||
function emitState() {
|
||||
try {
|
||||
const state = controller.getState();
|
||||
options.onStateChange?.(state);
|
||||
dispatchEvent(new CustomEvent("agv-playback", { detail: { type: "state", state } }));
|
||||
} catch (_) { /* ignore */ }
|
||||
}
|
||||
|
||||
async function dispatch(req) {
|
||||
const { method, params = {} } = req;
|
||||
try {
|
||||
switch (method) {
|
||||
case METHODS.PLAY_ACTION: {
|
||||
const ensured = await ensureModel(controller, params);
|
||||
if (!ensured.ok) return formatResponse(req.id, false, null, ensured.error);
|
||||
const ids = asStringList(params.actionIds ?? params.actions).length
|
||||
? asStringList(params.actionIds ?? params.actions)
|
||||
: [asString(params.actionId ?? params.action)];
|
||||
const played = [];
|
||||
for (const id of ids) {
|
||||
if (!id) continue;
|
||||
const r = controller.playAction(id);
|
||||
if (!r.ok) return formatResponse(req.id, false, null, r.error);
|
||||
played.push(id);
|
||||
}
|
||||
if (!played.length) {
|
||||
return formatResponse(req.id, false, null, { code: ERROR.INVALID_REQUEST, message: "params.actionId 必填" });
|
||||
}
|
||||
emitState();
|
||||
return formatResponse(req.id, true, {
|
||||
modelId: controller.getState().modelId,
|
||||
activeActions: controller.getState().activeActions,
|
||||
played,
|
||||
});
|
||||
}
|
||||
case METHODS.STOP_ACTION: {
|
||||
const ensured = await ensureModel(controller, params);
|
||||
if (!ensured.ok) return formatResponse(req.id, false, null, ensured.error);
|
||||
const ids = asStringList(params.actionIds ?? params.actions).length
|
||||
? asStringList(params.actionIds ?? params.actions)
|
||||
: [asString(params.actionId ?? params.action)];
|
||||
const stopped = [];
|
||||
for (const id of ids) {
|
||||
if (!id) continue;
|
||||
const r = controller.stopAction(id);
|
||||
if (!r.ok) return formatResponse(req.id, false, null, r.error);
|
||||
stopped.push(id);
|
||||
}
|
||||
if (!stopped.length) {
|
||||
return formatResponse(req.id, false, null, { code: ERROR.INVALID_REQUEST, message: "params.actionId 必填" });
|
||||
}
|
||||
emitState();
|
||||
return formatResponse(req.id, true, {
|
||||
modelId: controller.getState().modelId,
|
||||
activeActions: controller.getState().activeActions,
|
||||
stopped,
|
||||
});
|
||||
}
|
||||
case METHODS.CLEAR_ACTIONS: {
|
||||
const ensured = await ensureModel(controller, params);
|
||||
if (!ensured.ok) return formatResponse(req.id, false, null, ensured.error);
|
||||
controller.clearActions();
|
||||
emitState();
|
||||
return formatResponse(req.id, true, { modelId: controller.getState().modelId, activeActions: [] });
|
||||
}
|
||||
default:
|
||||
return formatResponse(req.id, false, null, { code: ERROR.UNKNOWN_METHOD, message: "仅支持 playAction、stopAction、clearActions" });
|
||||
}
|
||||
} catch (e) {
|
||||
return formatResponse(req.id, false, null, {
|
||||
code: ERROR.INTERNAL_ERROR,
|
||||
message: e?.message || String(e),
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function isOriginAllowed(origin) {
|
||||
if (!allowedOrigins || allowedOrigins.includes("*")) return true;
|
||||
return allowedOrigins.includes(origin);
|
||||
}
|
||||
|
||||
function onMessage(event) {
|
||||
if (!isOriginAllowed(event.origin)) return;
|
||||
const parsed = parseRequest(event.data);
|
||||
if (!parsed.ok) {
|
||||
const res = formatResponse(undefined, false, null, parsed.error);
|
||||
event.source?.postMessage?.(res, event.origin);
|
||||
return;
|
||||
}
|
||||
if (!parsed.request.method) return;
|
||||
Promise.resolve(dispatch(parsed.request)).then((res) => {
|
||||
if (event.source && typeof event.source.postMessage === "function") {
|
||||
event.source.postMessage(res, event.origin);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function attach(target = window) {
|
||||
const api = {
|
||||
version: API_VERSION,
|
||||
METHODS,
|
||||
request: (raw) => {
|
||||
const parsed = parseRequest({ version: API_VERSION, ...raw });
|
||||
if (!parsed.ok) return Promise.resolve(formatResponse(raw?.id, false, null, parsed.error));
|
||||
return Promise.resolve(dispatch(parsed.request));
|
||||
},
|
||||
getCatalog: () => fetch("/api/catalog").then((r) => r.json()),
|
||||
getState: () => controller.getState(),
|
||||
parseRequest,
|
||||
formatResponse,
|
||||
};
|
||||
target.AgvPlaybackApi = api;
|
||||
if (!messageBound) {
|
||||
addEventListener("message", onMessage);
|
||||
messageBound = true;
|
||||
}
|
||||
return api;
|
||||
}
|
||||
|
||||
function detach() {
|
||||
if (messageBound) {
|
||||
removeEventListener("message", onMessage);
|
||||
messageBound = false;
|
||||
}
|
||||
delete window.AgvPlaybackApi;
|
||||
}
|
||||
|
||||
return { attach, detach, dispatch, parseRequest, formatResponse };
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,932 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>AGV 配置</title>
|
||||
<style>
|
||||
:root {
|
||||
--bg: #0d1117;
|
||||
--panel: #161b22;
|
||||
--panel2: #1f2633;
|
||||
--card: #1b2230;
|
||||
--primary: #3b82f6;
|
||||
--primary-hover: #2563eb;
|
||||
--success: #10b981;
|
||||
--warning: #f59e0b;
|
||||
--danger: #ef4444;
|
||||
--border: #30363d;
|
||||
--text: #e6edf3;
|
||||
--text-muted: #8b949e;
|
||||
--text-dim: #6e7681;
|
||||
--inspector-w: 360px;
|
||||
}
|
||||
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
height: 100vh;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#app {
|
||||
display: grid;
|
||||
grid-template-rows: auto 1fr auto;
|
||||
height: 100vh;
|
||||
min-height: 0;
|
||||
}
|
||||
|
||||
/* ── Top bar ── */
|
||||
#topbar {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 12px;
|
||||
padding: 8px 12px;
|
||||
background: var(--panel);
|
||||
border-bottom: 1px solid var(--border);
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.agv-info {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
#agvInfoName {
|
||||
font-size: 15px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.02em;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.agv-stats {
|
||||
display: flex;
|
||||
gap: 12px;
|
||||
margin-top: 2px;
|
||||
font-size: 11px;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.agv-stats span::before {
|
||||
content: "";
|
||||
display: inline-block;
|
||||
width: 6px;
|
||||
height: 6px;
|
||||
border-radius: 50%;
|
||||
background: var(--primary);
|
||||
margin-right: 5px;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
.agv-stats span:nth-child(2)::before {
|
||||
background: var(--success);
|
||||
}
|
||||
|
||||
.agv-stats span:nth-child(3)::before {
|
||||
background: var(--warning);
|
||||
}
|
||||
|
||||
.topbar-actions {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
#agvModel {
|
||||
background: var(--panel2);
|
||||
color: var(--text);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
padding: 5px 8px;
|
||||
font-size: 12px;
|
||||
min-width: 120px;
|
||||
}
|
||||
|
||||
.topbar-link {
|
||||
font-size: 11px;
|
||||
color: var(--primary);
|
||||
text-decoration: none;
|
||||
white-space: nowrap;
|
||||
padding: 5px 8px;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 6px;
|
||||
}
|
||||
|
||||
.topbar-link:hover {
|
||||
background: var(--panel2);
|
||||
}
|
||||
|
||||
#syncBaseline {
|
||||
padding: 5px 14px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: var(--primary);
|
||||
color: #fff;
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
#syncBaseline:hover {
|
||||
background: var(--primary-hover);
|
||||
}
|
||||
|
||||
/* ── Main layout ── */
|
||||
#layout {
|
||||
display: grid;
|
||||
grid-template-columns: minmax(0, 1fr) var(--inspector-w);
|
||||
min-height: 0;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#canvas-wrap {
|
||||
position: relative;
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
background: #080b10;
|
||||
border-right: 1px solid var(--border);
|
||||
}
|
||||
|
||||
#canvas-wrap::after {
|
||||
content: "点击选中 · 红X 绿Y 蓝Z";
|
||||
position: absolute;
|
||||
bottom: 8px;
|
||||
left: 10px;
|
||||
z-index: 2;
|
||||
font-size: 10px;
|
||||
color: var(--text-dim);
|
||||
pointer-events: none;
|
||||
}
|
||||
|
||||
#canvas-wrap canvas {
|
||||
display: block;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
}
|
||||
|
||||
/* ── Inspector ── */
|
||||
#inspector {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
min-width: 0;
|
||||
min-height: 0;
|
||||
background: var(--panel);
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.tabs {
|
||||
display: flex;
|
||||
flex-shrink: 0;
|
||||
border-bottom: 1px solid var(--border);
|
||||
background: var(--panel);
|
||||
}
|
||||
|
||||
.tab {
|
||||
flex: 1;
|
||||
padding: 9px 0;
|
||||
border: none;
|
||||
background: transparent;
|
||||
color: var(--text-muted);
|
||||
font-size: 12px;
|
||||
cursor: pointer;
|
||||
border-bottom: 2px solid transparent;
|
||||
transition: color 0.15s;
|
||||
}
|
||||
|
||||
.tab:hover {
|
||||
color: var(--text);
|
||||
background: var(--panel2);
|
||||
}
|
||||
|
||||
.tab.active {
|
||||
color: var(--text);
|
||||
font-weight: 600;
|
||||
border-bottom-color: var(--primary);
|
||||
}
|
||||
|
||||
.tab-panel {
|
||||
flex: 1;
|
||||
overflow-y: auto;
|
||||
overflow-x: hidden;
|
||||
padding: 10px;
|
||||
scrollbar-gutter: stable;
|
||||
}
|
||||
|
||||
/* ── Form elements ── */
|
||||
label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
font-size: 11px;
|
||||
margin-bottom: 5px;
|
||||
gap: 8px;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
label span {
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
select,
|
||||
input[type="text"],
|
||||
input[type="number"],
|
||||
input[type="search"] {
|
||||
flex: 1;
|
||||
max-width: 62%;
|
||||
background: var(--bg);
|
||||
color: var(--text);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 5px;
|
||||
padding: 4px 7px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
input[type="color"] {
|
||||
flex: none;
|
||||
width: 38px;
|
||||
height: 24px;
|
||||
max-width: 38px;
|
||||
padding: 1px;
|
||||
background: var(--bg);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 5px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
input[type="checkbox"] {
|
||||
accent-color: var(--primary);
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 6px 10px;
|
||||
border: none;
|
||||
border-radius: 6px;
|
||||
background: var(--primary);
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
margin-top: 6px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: var(--primary-hover);
|
||||
}
|
||||
|
||||
button.secondary {
|
||||
background: var(--panel2);
|
||||
border: 1px solid var(--border);
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
button.secondary:hover {
|
||||
background: var(--card);
|
||||
}
|
||||
|
||||
button.primary-green {
|
||||
background: #166534;
|
||||
}
|
||||
|
||||
button.primary-green:hover {
|
||||
background: #15803d;
|
||||
}
|
||||
|
||||
.hidden {
|
||||
display: none !important;
|
||||
}
|
||||
|
||||
.hint {
|
||||
color: var(--text-dim);
|
||||
font-size: 10px;
|
||||
line-height: 1.4;
|
||||
margin: 2px 0 6px;
|
||||
}
|
||||
|
||||
.ok {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.err {
|
||||
color: var(--danger);
|
||||
}
|
||||
|
||||
/* ── Setting groups ── */
|
||||
.setting-group {
|
||||
background: var(--panel2);
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 8px;
|
||||
padding: 10px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.setting-group-title {
|
||||
font-size: 10px;
|
||||
font-weight: 600;
|
||||
letter-spacing: 0.06em;
|
||||
text-transform: uppercase;
|
||||
color: var(--text-dim);
|
||||
margin-bottom: 8px;
|
||||
padding-bottom: 6px;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
/* ── Component cards ── */
|
||||
.component-scroll {
|
||||
max-height: 160px;
|
||||
overflow-y: auto;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 4px;
|
||||
margin-bottom: 8px;
|
||||
}
|
||||
|
||||
.component-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 8px 10px;
|
||||
border-radius: 7px;
|
||||
background: var(--card);
|
||||
cursor: pointer;
|
||||
border: 1px solid var(--border);
|
||||
transition: border-color 0.12s, box-shadow 0.12s;
|
||||
}
|
||||
|
||||
.component-card:hover {
|
||||
border-color: #484f58;
|
||||
background: #222b3a;
|
||||
}
|
||||
|
||||
.component-card.active {
|
||||
border-color: var(--primary);
|
||||
box-shadow: 0 0 0 1px var(--primary);
|
||||
background: #1a2744;
|
||||
}
|
||||
|
||||
.component-card.in-draft {
|
||||
border-color: #1d4ed8;
|
||||
opacity: 0.85;
|
||||
}
|
||||
|
||||
.component-card.compact {
|
||||
padding: 6px 8px;
|
||||
}
|
||||
|
||||
.component-card-icon {
|
||||
font-size: 14px;
|
||||
width: 22px;
|
||||
text-align: center;
|
||||
flex-shrink: 0;
|
||||
color: var(--text-muted);
|
||||
}
|
||||
|
||||
.component-card-body {
|
||||
min-width: 0;
|
||||
flex: 1;
|
||||
}
|
||||
|
||||
.component-card-name {
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.component-card-type {
|
||||
font-size: 10px;
|
||||
color: var(--text-dim);
|
||||
margin-top: 1px;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.comp-meta {
|
||||
font-size: 10px;
|
||||
color: var(--text-muted);
|
||||
margin-bottom: 6px;
|
||||
word-break: break-all;
|
||||
line-height: 1.45;
|
||||
}
|
||||
|
||||
.comp-meta strong {
|
||||
color: var(--text);
|
||||
}
|
||||
|
||||
.comp-meta code {
|
||||
font-size: 10px;
|
||||
color: #79c0ff;
|
||||
}
|
||||
|
||||
/* ── Workflow steps ── */
|
||||
.workflow-step {
|
||||
margin-bottom: 10px;
|
||||
}
|
||||
|
||||
.workflow-step-title {
|
||||
font-size: 10px;
|
||||
font-weight: 600;
|
||||
color: var(--primary);
|
||||
margin-bottom: 6px;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
}
|
||||
|
||||
.workflow-step-title::before {
|
||||
content: attr(data-step);
|
||||
display: inline-flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
width: 16px;
|
||||
height: 16px;
|
||||
border-radius: 50%;
|
||||
background: var(--primary);
|
||||
color: #fff;
|
||||
font-size: 9px;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.row-btns {
|
||||
display: flex;
|
||||
gap: 6px;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
.row-btns button {
|
||||
flex: 1;
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
/* ── Action cards ── */
|
||||
#actionList {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 5px;
|
||||
}
|
||||
|
||||
.action-card {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
padding: 9px 10px;
|
||||
border-radius: 7px;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--border);
|
||||
cursor: pointer;
|
||||
transition: border-color 0.12s;
|
||||
}
|
||||
|
||||
.action-card:hover {
|
||||
border-color: #484f58;
|
||||
}
|
||||
|
||||
.action-card.active {
|
||||
border-color: var(--success);
|
||||
box-shadow: 0 0 0 1px var(--success);
|
||||
}
|
||||
|
||||
.action-card-body {
|
||||
flex: 1;
|
||||
min-width: 0;
|
||||
}
|
||||
|
||||
.action-card-title {
|
||||
font-size: 12px;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.action-card-meta {
|
||||
font-size: 10px;
|
||||
color: var(--text-dim);
|
||||
margin-top: 1px;
|
||||
}
|
||||
|
||||
.action-card-del {
|
||||
width: 24px;
|
||||
height: 24px;
|
||||
flex-shrink: 0;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
background: transparent;
|
||||
border: 1px solid var(--border);
|
||||
border-radius: 5px;
|
||||
color: var(--danger);
|
||||
font-size: 14px;
|
||||
line-height: 1;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.action-card-del:hover {
|
||||
background: #3d1515;
|
||||
border-color: var(--danger);
|
||||
}
|
||||
|
||||
.action-comp-chip {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 6px;
|
||||
padding: 6px 8px;
|
||||
border-radius: 6px;
|
||||
background: var(--card);
|
||||
border: 1px solid var(--border);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.action-comp-chip.active {
|
||||
border-color: var(--success);
|
||||
box-shadow: 0 0 0 1px var(--success);
|
||||
}
|
||||
|
||||
.action-comp-chip .chip-del {
|
||||
margin-left: auto;
|
||||
border: none;
|
||||
background: transparent;
|
||||
color: var(--danger);
|
||||
cursor: pointer;
|
||||
font-size: 13px;
|
||||
padding: 0 2px;
|
||||
}
|
||||
|
||||
/* ── Status bar ── */
|
||||
#statusbar {
|
||||
padding: 4px 12px;
|
||||
font-size: 10px;
|
||||
color: var(--text-dim);
|
||||
background: var(--panel);
|
||||
border-top: 1px solid var(--border);
|
||||
flex-shrink: 0;
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
/* ── Toast ── */
|
||||
#toast-container {
|
||||
position: fixed;
|
||||
bottom: 28px;
|
||||
left: 12px;
|
||||
z-index: 9999;
|
||||
display: flex;
|
||||
flex-direction: column-reverse;
|
||||
align-items: flex-start;
|
||||
gap: 8px;
|
||||
pointer-events: none;
|
||||
max-width: min(360px, calc(100vw - var(--inspector-w) - 24px));
|
||||
}
|
||||
|
||||
.toast {
|
||||
display: flex;
|
||||
align-items: flex-start;
|
||||
gap: 8px;
|
||||
padding: 10px 14px;
|
||||
border-radius: 8px;
|
||||
background: var(--panel2);
|
||||
border: 1px solid var(--border);
|
||||
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
|
||||
font-size: 12px;
|
||||
max-width: 320px;
|
||||
opacity: 0;
|
||||
transform: translateY(8px);
|
||||
transition: opacity 0.25s, transform 0.25s;
|
||||
}
|
||||
|
||||
.toast.show {
|
||||
opacity: 1;
|
||||
transform: translateY(0);
|
||||
}
|
||||
|
||||
.toast-ok {
|
||||
border-left: 3px solid var(--success);
|
||||
}
|
||||
|
||||
.toast-warn {
|
||||
border-left: 3px solid var(--warning);
|
||||
}
|
||||
|
||||
.toast-err {
|
||||
border-left: 3px solid var(--danger);
|
||||
}
|
||||
|
||||
.toast-icon {
|
||||
font-weight: 700;
|
||||
flex-shrink: 0;
|
||||
}
|
||||
|
||||
.toast-ok .toast-icon {
|
||||
color: var(--success);
|
||||
}
|
||||
|
||||
.toast-warn .toast-icon {
|
||||
color: var(--warning);
|
||||
}
|
||||
|
||||
@media (max-width: 900px) {
|
||||
#layout {
|
||||
grid-template-columns: 1fr;
|
||||
grid-template-rows: 1fr var(--inspector-w);
|
||||
}
|
||||
|
||||
#canvas-wrap {
|
||||
border-right: none;
|
||||
border-bottom: 1px solid var(--border);
|
||||
}
|
||||
|
||||
#toast-container {
|
||||
max-width: calc(100vw - 24px);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="app">
|
||||
<header id="topbar">
|
||||
<div class="agv-info">
|
||||
<div id="agvInfoName">—</div>
|
||||
<div class="agv-stats">
|
||||
<span id="statComponents">组件 0</span>
|
||||
<span id="statActions">动作 0</span>
|
||||
<span id="statLights">灯光 0</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="topbar-actions">
|
||||
<select id="agvModel">
|
||||
<option value="">加载列表...</option>
|
||||
</select>
|
||||
<a class="topbar-link" href="./play.html">播放</a>
|
||||
<button type="button" id="syncBaseline">保存配置</button>
|
||||
</div>
|
||||
</header>
|
||||
|
||||
<div id="layout">
|
||||
<div id="canvas-wrap"></div>
|
||||
|
||||
<aside id="inspector">
|
||||
<div class="tabs">
|
||||
<button type="button" id="tabBaseline" class="tab active">基础配置</button>
|
||||
<button type="button" id="tabActions" class="tab">动作配置</button>
|
||||
</div>
|
||||
|
||||
<div id="tabPanelBaseline" class="tab-panel">
|
||||
<input type="search" id="componentFilter" placeholder="筛选组件 / GLB 节点 / 车壳"
|
||||
style="width:100%;max-width:100%;margin-bottom:8px;" />
|
||||
<div id="componentList" class="component-scroll"></div>
|
||||
<p id="compMeta" class="comp-meta">—</p>
|
||||
|
||||
<div class="setting-group">
|
||||
<div class="setting-group-title">组件类型</div>
|
||||
<label><span>类型</span>
|
||||
<select id="componentType">
|
||||
<option value="static">静态件</option>
|
||||
<option value="body">车体结构</option>
|
||||
<option value="light">灯光</option>
|
||||
<option value="wheel">轮子</option>
|
||||
<option value="actuator">可动机构</option>
|
||||
<option value="sensor">传感器</option>
|
||||
<option value="safety">安全件</option>
|
||||
</select>
|
||||
</label>
|
||||
</div>
|
||||
|
||||
<div class="setting-group" id="actionPanel">
|
||||
<div class="setting-group-title">参数配置</div>
|
||||
<div id="panelStatic" class="hint">静态件无动作参数,仅参与装配</div>
|
||||
<div id="panelLight" class="hidden">
|
||||
<label><span>颜色</span> <input id="compLightColor" type="color" value="#00ff55" /></label>
|
||||
<label><span>光效</span>
|
||||
<select id="compLightEffect">
|
||||
<option value="solid">常亮</option>
|
||||
<option value="breath">呼吸</option>
|
||||
<option value="blink">闪烁</option>
|
||||
<option value="strobe">爆闪</option>
|
||||
<option value="flow_forward">前向流光</option>
|
||||
<option value="flow_backward">反向流光</option>
|
||||
<option value="blink_segment">分段闪</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>亮度</span> <input id="compLightIntensity" type="number" step="any" value="100" /></label>
|
||||
<label id="compLightHzRow" class="hidden"><span>频率 Hz</span> <input id="compLightHz" type="number"
|
||||
step="any" value="4" /></label>
|
||||
<label id="compLightPeriodRow" class="hidden"><span>周期 s</span> <input id="compLightPeriod" type="number"
|
||||
step="any" value="2" /></label>
|
||||
</div>
|
||||
<div id="panelWheel" class="hidden">
|
||||
<label><span>旋转轴</span>
|
||||
<select id="wheelAxis">
|
||||
<option value="x">X</option>
|
||||
<option value="y">Y</option>
|
||||
<option value="z">Z</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>转速倍率</span> <input id="wheelSpeedFactor" type="number" step="any" value="1" /></label>
|
||||
<label><span>转向角 (°)</span> <input id="wheelSteerAngleDeg" type="number" step="any" value="0" /></label>
|
||||
</div>
|
||||
<div id="panelMotion" class="hidden">
|
||||
<label><span>机构角色</span>
|
||||
<select id="componentMotionRole">
|
||||
<option value="lift">顶升</option>
|
||||
<option value="lidar">雷达</option>
|
||||
<option value="camera">相机</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>运动方式</span>
|
||||
<select id="componentMotionKind">
|
||||
<option value="translate">平移</option>
|
||||
<option value="rotate">旋转</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>轴向</span>
|
||||
<select id="componentMotionAxis">
|
||||
<option value="y">Y</option>
|
||||
<option value="x">X</option>
|
||||
<option value="z">Z</option>
|
||||
</select>
|
||||
</label>
|
||||
<label id="liftStartRow" class="hidden"><span>起始 (m)</span> <input id="liftStart" type="number" step="any"
|
||||
value="0" /></label>
|
||||
<label id="liftEndRow" class="hidden"><span>终止 (m)</span> <input id="liftEnd" type="number" step="any"
|
||||
value="0.15" /></label>
|
||||
</div>
|
||||
<div id="panelCamera" class="hidden">
|
||||
<label><span>扫描摆动</span> <input type="checkbox" id="actionCamScan" checked /></label>
|
||||
<label><span>LED 呼吸</span> <input type="checkbox" id="actionCamLedBreath" checked /></label>
|
||||
<label><span>LED 颜色</span> <input id="actionCamLedColor" type="color" value="#00afff" /></label>
|
||||
</div>
|
||||
<div id="panelEmergency" class="hidden">
|
||||
<label><span>颜色</span> <input id="actionEmerColor" type="color" value="#ff0000" /></label>
|
||||
<label><span>效果</span>
|
||||
<select id="actionEmerEffect">
|
||||
<option value="blink">闪烁</option>
|
||||
<option value="strobe">爆闪</option>
|
||||
<option value="solid">常亮</option>
|
||||
<option value="off">熄灭</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>频率 Hz</span> <input id="actionEmerHz" type="number" step="any" value="4" /></label>
|
||||
</div>
|
||||
</div>
|
||||
<button type="button" id="applyComponent" class="primary-green">保存到基准</button>
|
||||
</div>
|
||||
|
||||
<div id="tabPanelActions" class="tab-panel hidden">
|
||||
<div class="workflow-step">
|
||||
<div class="workflow-step-title" data-step="1">选择组件</div>
|
||||
<div id="actionCompPickList" class="component-scroll" style="max-height:120px;"></div>
|
||||
<div class="row-btns">
|
||||
<button type="button" id="addAllActionComp" class="secondary">全部加入</button>
|
||||
</div>
|
||||
<p class="hint">点击组件加入动作 · 已选 <span id="actionSelectedCount">0</span> 个</p>
|
||||
<div id="actionSelectedList" class="component-scroll" style="max-height:80px;min-height:32px;"></div>
|
||||
</div>
|
||||
|
||||
<div class="workflow-step">
|
||||
<div class="workflow-step-title" data-step="2">配置参数</div>
|
||||
<div id="actionCfgBox" class="setting-group hidden">
|
||||
<div class="setting-group-title" id="actionCfgTitle">—</div>
|
||||
<label><span>类型</span>
|
||||
<select id="actComponentType">
|
||||
<option value="static">静态件</option>
|
||||
<option value="body">车体结构</option>
|
||||
<option value="light">灯光</option>
|
||||
<option value="wheel">轮子</option>
|
||||
<option value="actuator">可动机构</option>
|
||||
<option value="sensor">传感器</option>
|
||||
<option value="safety">安全件</option>
|
||||
</select>
|
||||
</label>
|
||||
<div id="actPanelStatic" class="hidden">
|
||||
<label><span>透明度</span> <input id="actOpacity" type="number" min="0" max="1" step="0.05"
|
||||
value="0.25" /></label>
|
||||
<button type="button" id="applyOpacityToAll" class="secondary">应用到全部静态件</button>
|
||||
</div>
|
||||
<div id="actPanelLight" class="hidden">
|
||||
<label><span>颜色</span> <input id="actLightColor" type="color" value="#00ff55" /></label>
|
||||
<label><span>光效</span>
|
||||
<select id="actLightEffect">
|
||||
<option value="solid">常亮</option>
|
||||
<option value="breath">呼吸</option>
|
||||
<option value="blink">闪烁</option>
|
||||
<option value="strobe">爆闪</option>
|
||||
<option value="flow_forward">前向流光</option>
|
||||
<option value="flow_backward">反向流光</option>
|
||||
<option value="blink_segment">分段闪</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>亮度</span> <input id="actLightIntensity" type="number" step="any" value="100" /></label>
|
||||
<label id="actLightHzRow" class="hidden"><span>频率 Hz</span> <input id="actLightHz" type="number"
|
||||
step="any" value="4" /></label>
|
||||
<label id="actLightPeriodRow" class="hidden"><span>周期 s</span> <input id="actLightPeriod" type="number"
|
||||
step="any" value="2" /></label>
|
||||
</div>
|
||||
<div id="actPanelWheel" class="hidden">
|
||||
<p class="hint">自转轴:<span id="actWheelAxisHint">—</span> · 红X 绿Y 蓝Z</p>
|
||||
<label><span>转速倍率</span> <input id="actWheelSpeedFactor" type="number" step="any" value="1" /></label>
|
||||
<label><span>转向角 (°)</span> <input id="actWheelSteerAngleDeg" type="number" step="any"
|
||||
value="0" /></label>
|
||||
</div>
|
||||
<div id="actPanelMotion" class="hidden">
|
||||
<label><span>机构角色</span>
|
||||
<select id="actMotionRole">
|
||||
<option value="lift">顶升</option>
|
||||
<option value="lidar">雷达</option>
|
||||
<option value="camera">相机</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>运动方式</span>
|
||||
<select id="actMotionKind">
|
||||
<option value="translate">平移</option>
|
||||
<option value="rotate">旋转</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>轴向</span>
|
||||
<select id="actMotionAxis">
|
||||
<option value="y">Y</option>
|
||||
<option value="x">X</option>
|
||||
<option value="z">Z</option>
|
||||
</select>
|
||||
</label>
|
||||
<label id="actLiftStartRow" class="hidden"><span>起始 (m)</span> <input id="actLiftStart" type="number"
|
||||
step="any" value="0" /></label>
|
||||
<label id="actLiftEndRow" class="hidden"><span>终止 (m)</span> <input id="actLiftEnd" type="number"
|
||||
step="any" value="0.15" /></label>
|
||||
</div>
|
||||
<div id="actPanelCamera" class="hidden">
|
||||
<label><span>扫描摆动</span> <input type="checkbox" id="actCamScan" checked /></label>
|
||||
<label><span>LED 呼吸</span> <input type="checkbox" id="actCamLedBreath" checked /></label>
|
||||
<label><span>LED 颜色</span> <input id="actCamLedColor" type="color" value="#00afff" /></label>
|
||||
</div>
|
||||
<div id="actPanelEmergency" class="hidden">
|
||||
<label><span>颜色</span> <input id="actEmerColor" type="color" value="#ff0000" /></label>
|
||||
<label><span>效果</span>
|
||||
<select id="actEmerEffect">
|
||||
<option value="blink">闪烁</option>
|
||||
<option value="strobe">爆闪</option>
|
||||
<option value="solid">常亮</option>
|
||||
<option value="off">熄灭</option>
|
||||
</select>
|
||||
</label>
|
||||
<label><span>频率 Hz</span> <input id="actEmerHz" type="number" step="any" value="4" /></label>
|
||||
</div>
|
||||
<button type="button" id="saveActionDraftComp" class="primary-green">保存该组件</button>
|
||||
</div>
|
||||
<p id="actionCfgPlaceholder" class="hint">请先在步骤 1 选择组件</p>
|
||||
</div>
|
||||
|
||||
<div class="workflow-step">
|
||||
<div class="workflow-step-title" data-step="3">保存动作</div>
|
||||
<label><span>显示名称</span>
|
||||
<input type="text" id="actionLabel" lang="zh-CN" autocomplete="off" spellcheck="false"
|
||||
placeholder="左转、举升" />
|
||||
</label>
|
||||
<div class="row-btns">
|
||||
<button type="button" id="saveAsAction">保存动作</button>
|
||||
<button type="button" id="clearActionDraft" class="secondary">清空</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="setting-group" style="margin-top:4px;">
|
||||
<div class="setting-group-title">已定义动作</div>
|
||||
<div id="actionList"></div>
|
||||
</div>
|
||||
</div>
|
||||
</aside>
|
||||
</div>
|
||||
|
||||
<footer id="statusbar">
|
||||
<div id="status">就绪</div>
|
||||
</footer>
|
||||
</div>
|
||||
|
||||
<div id="toast-container"></div>
|
||||
|
||||
<!-- hidden compat elements -->
|
||||
<select id="actionCompPick" class="hidden" aria-hidden="true">
|
||||
<option value=""></option>
|
||||
</select>
|
||||
<button type="button" id="addActionComp" class="hidden" aria-hidden="true"></button>
|
||||
|
||||
<script type="importmap">
|
||||
{ "imports": {
|
||||
"three": "https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js",
|
||||
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/",
|
||||
"pinyin-pro": "https://cdn.jsdelivr.net/npm/pinyin-pro@3.26.0/dist/index.mjs"
|
||||
}}
|
||||
</script>
|
||||
<script type="module" src="./app.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,194 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
|
||||
<title>AGV 动作播放</title>
|
||||
<style>
|
||||
* {
|
||||
box-sizing: border-box;
|
||||
margin: 0;
|
||||
padding: 0;
|
||||
}
|
||||
|
||||
body {
|
||||
font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
|
||||
background: #0f1117;
|
||||
color: #e8eaed;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
#canvas-wrap {
|
||||
width: 100vw;
|
||||
height: 100vh;
|
||||
}
|
||||
|
||||
#panel {
|
||||
position: fixed;
|
||||
top: 16px;
|
||||
left: 16px;
|
||||
width: 280px;
|
||||
max-height: calc(100vh - 32px);
|
||||
overflow-y: auto;
|
||||
background: rgba(22, 26, 36, 0.94);
|
||||
border: 1px solid #2a3142;
|
||||
border-radius: 12px;
|
||||
padding: 14px;
|
||||
backdrop-filter: blur(8px);
|
||||
z-index: 10;
|
||||
}
|
||||
|
||||
h1 {
|
||||
font-size: 15px;
|
||||
margin-bottom: 4px;
|
||||
}
|
||||
|
||||
.sub {
|
||||
font-size: 11px;
|
||||
color: #8b95a8;
|
||||
margin-bottom: 10px;
|
||||
line-height: 1.4;
|
||||
}
|
||||
|
||||
.sub a {
|
||||
color: #8eb4ff;
|
||||
}
|
||||
|
||||
label {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
font-size: 12px;
|
||||
margin-bottom: 8px;
|
||||
gap: 8px;
|
||||
}
|
||||
|
||||
select {
|
||||
flex: 1;
|
||||
background: #1a1f2b;
|
||||
color: #e8eaed;
|
||||
border: 1px solid #2a3142;
|
||||
border-radius: 6px;
|
||||
padding: 5px 8px;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
button {
|
||||
width: 100%;
|
||||
padding: 8px;
|
||||
border: none;
|
||||
border-radius: 8px;
|
||||
background: #2a3142;
|
||||
color: #fff;
|
||||
cursor: pointer;
|
||||
font-size: 12px;
|
||||
margin-top: 4px;
|
||||
}
|
||||
|
||||
button:hover {
|
||||
background: #3a4558;
|
||||
}
|
||||
|
||||
button.active {
|
||||
box-shadow: 0 0 0 1px #5dd39e;
|
||||
}
|
||||
|
||||
.mode-running {
|
||||
background: #1a3d28;
|
||||
}
|
||||
|
||||
.mode-lift {
|
||||
background: #3d3520;
|
||||
}
|
||||
|
||||
.mode-fault {
|
||||
background: #3d1a1a;
|
||||
}
|
||||
|
||||
.mode-idle {
|
||||
background: #1a3348;
|
||||
}
|
||||
|
||||
.tags {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
gap: 6px;
|
||||
margin-top: 8px;
|
||||
}
|
||||
|
||||
.tags button {
|
||||
width: auto;
|
||||
flex: 1 1 calc(50% - 6px);
|
||||
min-width: 72px;
|
||||
margin-top: 0;
|
||||
}
|
||||
|
||||
#activeTag {
|
||||
color: #c5d0e0;
|
||||
font-size: 12px;
|
||||
margin: 8px 0 4px;
|
||||
min-height: 18px;
|
||||
}
|
||||
|
||||
#remoteStatus {
|
||||
font-size: 11px;
|
||||
margin-top: 8px;
|
||||
line-height: 1.4;
|
||||
color: #8b95a8;
|
||||
}
|
||||
|
||||
#remoteStatus.ok {
|
||||
color: #5dd39e;
|
||||
}
|
||||
|
||||
#remoteStatus.warn {
|
||||
color: #e8c468;
|
||||
}
|
||||
|
||||
#remoteStatus.err {
|
||||
color: #f07178;
|
||||
}
|
||||
|
||||
#status {
|
||||
font-size: 11px;
|
||||
color: #7d899e;
|
||||
margin-top: 10px;
|
||||
line-height: 1.5;
|
||||
}
|
||||
|
||||
.ok {
|
||||
color: #5dd39e;
|
||||
}
|
||||
|
||||
.err {
|
||||
color: #f07178;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div id="canvas-wrap"></div>
|
||||
<div id="panel">
|
||||
<h1>AGV 动作播放</h1>
|
||||
<p class="sub">外部通过 <code>GET /api/catalog</code> 与 <code>POST /api/agv</code> 控制。<a href="./index.html">去配置页</a>
|
||||
</p>
|
||||
<label><span>模型</span><select id="modelSelect">
|
||||
<option>加载中...</option>
|
||||
</select></label>
|
||||
<button type="button" id="clearAll">全部关闭</button>
|
||||
<p id="activeTag">已开启:无</p>
|
||||
<div id="actionTags" class="tags"></div>
|
||||
<div id="remoteStatus">远程:连接中…</div>
|
||||
<div id="status">加载中...</div>
|
||||
</div>
|
||||
<script type="importmap">
|
||||
{ "imports": {
|
||||
"three": "https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js",
|
||||
"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/"
|
||||
}}
|
||||
</script>
|
||||
<script type="module" src="./player.js"></script>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,239 @@
|
||||
/**
|
||||
* 远程播放桥接:SSE 订阅 + 命令队列 + 本地状态回传。
|
||||
*/
|
||||
import { API_VERSION } from "./api.js";
|
||||
|
||||
const API_ROOT = "/api/agv";
|
||||
const REMOTE_METHODS = new Set(["playAction", "stopAction", "clearActions"]);
|
||||
const CLIENT_ID = globalThis.crypto?.randomUUID?.() || `client-${Date.now()}`;
|
||||
|
||||
export function connectRemotePlayback(playbackApi, { onStatus } = {}) {
|
||||
let lastRevision = 0;
|
||||
let connected = false;
|
||||
let es = null;
|
||||
const queue = [];
|
||||
let draining = false;
|
||||
let applyingRemote = 0;
|
||||
|
||||
function setStatus(text, kind = "idle") {
|
||||
onStatus?.({ text, kind, connected, revision: lastRevision });
|
||||
}
|
||||
|
||||
function shouldSkipEcho(msg) {
|
||||
return msg?.sourceClientId && msg.sourceClientId === CLIENT_ID;
|
||||
}
|
||||
|
||||
async function waitUntilReady(maxMs = 20000) {
|
||||
const start = Date.now();
|
||||
while (Date.now() - start < maxMs) {
|
||||
if (playbackApi.getState?.().ready) return true;
|
||||
await new Promise((r) => setTimeout(r, 150));
|
||||
}
|
||||
return playbackApi.getState?.().ready;
|
||||
}
|
||||
|
||||
async function withRemoteApply(fn) {
|
||||
applyingRemote += 1;
|
||||
try {
|
||||
return await fn();
|
||||
} finally {
|
||||
applyingRemote -= 1;
|
||||
}
|
||||
}
|
||||
|
||||
async function ensureModel(modelId) {
|
||||
if (!modelId) return true;
|
||||
const cur = playbackApi.getState?.() || {};
|
||||
if (cur.modelId === modelId && cur.ready) return true;
|
||||
const res = await playbackApi.request({
|
||||
version: API_VERSION,
|
||||
method: "clearActions",
|
||||
params: { modelId },
|
||||
});
|
||||
if (!res.ok) {
|
||||
setStatus(`远程加载失败: ${res.error?.message || "unknown"}`, "error");
|
||||
return false;
|
||||
}
|
||||
return waitUntilReady();
|
||||
}
|
||||
|
||||
async function applySnapshot(state) {
|
||||
if (!state) return false;
|
||||
const revision = Number(state.revision) || 0;
|
||||
if (revision <= lastRevision) return true;
|
||||
|
||||
return withRemoteApply(async () => {
|
||||
const modelId = state.modelId;
|
||||
const desired = state.activeActions || [];
|
||||
|
||||
if (!(await ensureModel(modelId))) return false;
|
||||
|
||||
const cur = playbackApi.getState?.() || {};
|
||||
const current = new Set(cur.activeActions || []);
|
||||
const want = new Set(desired);
|
||||
|
||||
for (const id of current) {
|
||||
if (!want.has(id)) {
|
||||
const res = await playbackApi.request({
|
||||
version: API_VERSION,
|
||||
method: "stopAction",
|
||||
params: { modelId: cur.modelId || modelId, actionId: id },
|
||||
});
|
||||
if (!res.ok) {
|
||||
setStatus(`远程动作同步失败: ${res.error?.message || "unknown"}`, "error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const id of want) {
|
||||
if (!current.has(id)) {
|
||||
const res = await playbackApi.request({
|
||||
version: API_VERSION,
|
||||
method: "playAction",
|
||||
params: { modelId: cur.modelId || modelId, actionId: id },
|
||||
});
|
||||
if (!res.ok) {
|
||||
setStatus(`远程动作同步失败: ${res.error?.message || "unknown"}`, "error");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
lastRevision = revision;
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
async function runCommand(msg) {
|
||||
if (msg.type === "connected") {
|
||||
connected = true;
|
||||
setStatus("远程已连接", "ok");
|
||||
if (msg.state) await applySnapshot(msg.state);
|
||||
return;
|
||||
}
|
||||
if (msg.type === "ping") return;
|
||||
if (!msg.method) return;
|
||||
|
||||
const revision = Number(msg.revision) || 0;
|
||||
if (revision && revision <= lastRevision) return;
|
||||
|
||||
if (shouldSkipEcho(msg)) {
|
||||
if (revision) lastRevision = Math.max(lastRevision, revision);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.method === "syncState") {
|
||||
await applySnapshot(msg.state);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!REMOTE_METHODS.has(msg.method)) return;
|
||||
|
||||
await withRemoteApply(async () => {
|
||||
const p = msg.params || {};
|
||||
if (!(await ensureModel(p.modelId || p.model))) return;
|
||||
await waitUntilReady();
|
||||
|
||||
const res = await playbackApi.request({
|
||||
version: API_VERSION,
|
||||
id: msg.id,
|
||||
method: msg.method,
|
||||
params: p,
|
||||
});
|
||||
if (!res.ok) {
|
||||
setStatus(`远程命令失败: ${res.error?.message || "unknown"}`, "error");
|
||||
return;
|
||||
}
|
||||
if (revision) lastRevision = Math.max(lastRevision, revision);
|
||||
});
|
||||
}
|
||||
|
||||
function enqueue(msg) {
|
||||
queue.push(msg);
|
||||
drain();
|
||||
}
|
||||
|
||||
async function drain() {
|
||||
if (draining) return;
|
||||
draining = true;
|
||||
while (queue.length) {
|
||||
const msg = queue.shift();
|
||||
try {
|
||||
await runCommand(msg);
|
||||
} catch (e) {
|
||||
console.warn("[remote-bridge]", e);
|
||||
setStatus(`远程执行异常: ${e.message}`, "error");
|
||||
}
|
||||
}
|
||||
draining = false;
|
||||
}
|
||||
|
||||
function connectSse() {
|
||||
if (es) es.close();
|
||||
es = new EventSource(`${API_ROOT}/events`);
|
||||
es.onmessage = (ev) => {
|
||||
try {
|
||||
enqueue(JSON.parse(ev.data));
|
||||
} catch (e) {
|
||||
console.warn("[remote-bridge] bad event", e);
|
||||
}
|
||||
};
|
||||
es.onerror = () => {
|
||||
connected = false;
|
||||
setStatus("远程连接断开,自动重连…", "warn");
|
||||
};
|
||||
}
|
||||
|
||||
async function fetchServerState() {
|
||||
const res = await fetch(`${API_ROOT}/state?t=${Date.now()}`);
|
||||
const body = await res.json();
|
||||
return body.ok ? body.data : null;
|
||||
}
|
||||
|
||||
async function syncFromServer() {
|
||||
try {
|
||||
const state = await fetchServerState();
|
||||
if (!state || (state.revision || 0) <= 0) return state;
|
||||
await applySnapshot(state);
|
||||
return state;
|
||||
} catch (e) {
|
||||
setStatus("远程状态同步失败", "warn");
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
let reportTimer = null;
|
||||
function reportLocalState() {
|
||||
if (applyingRemote > 0) return;
|
||||
clearTimeout(reportTimer);
|
||||
reportTimer = setTimeout(async () => {
|
||||
const s = playbackApi.getState?.();
|
||||
if (!s?.ready || applyingRemote > 0) return;
|
||||
try {
|
||||
await fetch(`${API_ROOT}/report`, {
|
||||
method: "POST",
|
||||
headers: { "Content-Type": "application/json" },
|
||||
body: JSON.stringify({
|
||||
clientId: CLIENT_ID,
|
||||
modelId: s.modelId,
|
||||
activeActions: s.activeActions,
|
||||
}),
|
||||
});
|
||||
} catch (_) { /* ignore */ }
|
||||
}, 250);
|
||||
}
|
||||
|
||||
connectSse();
|
||||
setStatus("等待远程连接…", "idle");
|
||||
|
||||
return {
|
||||
clientId: CLIENT_ID,
|
||||
fetchServerState,
|
||||
syncFromServer,
|
||||
reportLocalState,
|
||||
close: () => {
|
||||
clearTimeout(reportTimer);
|
||||
es?.close();
|
||||
},
|
||||
};
|
||||
}
|
||||
Reference in New Issue
Block a user