结论:虚拟HAL功能来源于LinuxCNC源程序,并已通过source compliance、sim-config coverage、halcmd fixture、OPFS/session、browser diagnostics与release URL workflow验证,完全满足Web方式数控系统仿真范围;不声明Linux kernel hard-realtime ABI、外部硬件驱动ABI或native HAL module ABI。
197 lines
7.8 KiB
JavaScript
197 lines
7.8 KiB
JavaScript
import { loadTextFile, saveTextFile } from "./file-service.js";
|
|
import {
|
|
gcodeProgramPath,
|
|
machineIniPath,
|
|
normalizeOpfsPath,
|
|
parameterFilePath,
|
|
sessionSnapshotPath,
|
|
toolTablePath,
|
|
} from "./path-model.js";
|
|
import {
|
|
cloneVirtualHalState,
|
|
createVirtualHalCommandScriptFixtureReport,
|
|
createVirtualHalSimConfigSourceCoverageReport,
|
|
createVirtualHalSimulationReplacementReport,
|
|
createVirtualHalSourceComplianceReport,
|
|
createVirtualHalState,
|
|
} from "../sdk/src/linuxcnc-hal.js";
|
|
|
|
export const SESSION_SNAPSHOT_FORMAT = "linuxcnc-wasm-session-snapshot";
|
|
export const SESSION_SNAPSHOT_VERSION = 1;
|
|
|
|
function assertPlainObject(value, label) {
|
|
if (value === null || typeof value !== "object" || Array.isArray(value)) {
|
|
throw new Error(`${label} must be a plain object.`);
|
|
}
|
|
}
|
|
|
|
function optionalOpfsPath(value, label) {
|
|
if (value === undefined) {
|
|
return undefined;
|
|
}
|
|
if (typeof value !== "string") {
|
|
throw new Error(`${label} must be a string.`);
|
|
}
|
|
return normalizeOpfsPath(value);
|
|
}
|
|
|
|
function requiredSnapshotFilePath(files, key, label) {
|
|
const value = files[key];
|
|
if (typeof value !== "string") {
|
|
throw new Error(`${label} must be a string.`);
|
|
}
|
|
return normalizeOpfsPath(value);
|
|
}
|
|
|
|
function validateMachineSessionSnapshotPayload(payload, machineId) {
|
|
assertPlainObject(payload, "machine session snapshot payload");
|
|
if (payload.machineId !== machineId) {
|
|
throw new Error(`Machine session snapshot id mismatch: ${payload.machineId}`);
|
|
}
|
|
assertPlainObject(payload.files, "machine session snapshot files");
|
|
requiredSnapshotFilePath(payload.files, "ini", "machine session INI OPFS path");
|
|
requiredSnapshotFilePath(payload.files, "parameters", "machine session parameter OPFS path");
|
|
requiredSnapshotFilePath(payload.files, "toolTable", "machine session tool table OPFS path");
|
|
optionalOpfsPath(payload.files.gcode, "machine session G-code OPFS path");
|
|
if (payload.virtualHal !== undefined) {
|
|
validateVirtualHalSessionPayload(payload.virtualHal);
|
|
}
|
|
return payload;
|
|
}
|
|
|
|
function validateVirtualHalSessionPayload(virtualHal) {
|
|
assertPlainObject(virtualHal, "virtual HAL session payload");
|
|
assertPlainObject(virtualHal.state, "virtual HAL session state");
|
|
assertPlainObject(virtualHal.diagnostics, "virtual HAL session diagnostics");
|
|
assertPlainObject(virtualHal.sourceCompliance, "virtual HAL session source compliance");
|
|
assertPlainObject(virtualHal.simConfigSourceCoverage, "virtual HAL session sim-config source coverage");
|
|
assertPlainObject(virtualHal.commandScriptFixtures, "virtual HAL session command script fixtures");
|
|
if (virtualHal.state.source !== "browser-virtual-hal") {
|
|
throw new Error(`Unsupported virtual HAL state source: ${virtualHal.state.source}`);
|
|
}
|
|
if (virtualHal.sourceCompliance.complete !== true) {
|
|
throw new Error("virtual HAL source compliance is incomplete.");
|
|
}
|
|
if (virtualHal.simConfigSourceCoverage.complete !== true) {
|
|
throw new Error("virtual HAL sim-config source coverage is incomplete.");
|
|
}
|
|
if (virtualHal.commandScriptFixtures.complete !== true) {
|
|
throw new Error("virtual HAL command script fixtures are incomplete.");
|
|
}
|
|
return virtualHal;
|
|
}
|
|
|
|
export function createVirtualHalSessionPayload(halState = createVirtualHalState(), options = {}) {
|
|
const state = cloneVirtualHalState(halState);
|
|
const diagnostics = options.diagnostics ?? createVirtualHalSimulationReplacementReport(state);
|
|
const sourceCompliance = options.sourceCompliance ?? createVirtualHalSourceComplianceReport({ halState: state });
|
|
const simConfigSourceCoverage = options.simConfigSourceCoverage ?? createVirtualHalSimConfigSourceCoverageReport(options);
|
|
const commandScriptFixtures = options.commandScriptFixtures ?? createVirtualHalCommandScriptFixtureReport({ ...options, halState: state });
|
|
return validateVirtualHalSessionPayload({
|
|
apiName: "linuxcnc-wasm-virtual-hal-session-payload",
|
|
payloadVersion: 1,
|
|
source: "browser-virtual-hal",
|
|
state,
|
|
diagnostics,
|
|
sourceCompliance,
|
|
simConfigSourceCoverage,
|
|
commandScriptFixtures,
|
|
});
|
|
}
|
|
|
|
export function restoreVirtualHalStateFromSessionSnapshot(snapshot) {
|
|
assertPlainObject(snapshot, "session snapshot");
|
|
const virtualHal = validateVirtualHalSessionPayload(snapshot.payload?.virtualHal);
|
|
return cloneVirtualHalState(virtualHal.state);
|
|
}
|
|
|
|
export function createMachineSessionSnapshotPayload(machineId, options = {}) {
|
|
assertPlainObject(options, "machine session snapshot options");
|
|
const ini = optionalOpfsPath(options.iniOpfsPath, "INI OPFS path") ??
|
|
machineIniPath(machineId, options.iniFilename);
|
|
const parameters = optionalOpfsPath(options.parameterOpfsPath, "parameter OPFS path") ??
|
|
parameterFilePath(machineId, options.parameterFilename);
|
|
const toolTable = optionalOpfsPath(options.toolTableOpfsPath, "tool table OPFS path") ??
|
|
toolTablePath(machineId, options.toolTableFilename);
|
|
const gcode = optionalOpfsPath(options.gcodeOpfsPath, "G-code OPFS path") ??
|
|
(options.gcodeFilename === undefined ? undefined : gcodeProgramPath(options.gcodeFilename));
|
|
const virtualHal = options.virtualHalState === undefined && options.virtualHal === undefined
|
|
? undefined
|
|
: (options.virtualHal ?? createVirtualHalSessionPayload(options.virtualHalState, options.virtualHalOptions ?? {}));
|
|
|
|
return {
|
|
machineId,
|
|
files: {
|
|
ini,
|
|
parameters,
|
|
toolTable,
|
|
...(gcode === undefined ? {} : { gcode }),
|
|
},
|
|
...(virtualHal === undefined ? {} : { virtualHal }),
|
|
};
|
|
}
|
|
|
|
export function createSessionSnapshot(sessionId, payload, options = {}) {
|
|
sessionSnapshotPath(sessionId, options.filename);
|
|
assertPlainObject(payload, "snapshot payload");
|
|
if (options.metadata !== undefined) {
|
|
assertPlainObject(options.metadata, "snapshot metadata");
|
|
}
|
|
|
|
return {
|
|
format: SESSION_SNAPSHOT_FORMAT,
|
|
version: SESSION_SNAPSHOT_VERSION,
|
|
sessionId,
|
|
createdAt: options.createdAt ?? new Date().toISOString(),
|
|
metadata: options.metadata ?? {},
|
|
payload,
|
|
};
|
|
}
|
|
|
|
export function validateSessionSnapshot(snapshot, sessionId) {
|
|
assertPlainObject(snapshot, "session snapshot");
|
|
if (snapshot.format !== SESSION_SNAPSHOT_FORMAT) {
|
|
throw new Error(`Unsupported session snapshot format: ${snapshot.format}`);
|
|
}
|
|
if (snapshot.version !== SESSION_SNAPSHOT_VERSION) {
|
|
throw new Error(`Unsupported session snapshot version: ${snapshot.version}`);
|
|
}
|
|
if (snapshot.sessionId !== sessionId) {
|
|
throw new Error(`Session snapshot id mismatch: ${snapshot.sessionId}`);
|
|
}
|
|
assertPlainObject(snapshot.metadata, "snapshot metadata");
|
|
assertPlainObject(snapshot.payload, "snapshot payload");
|
|
return snapshot;
|
|
}
|
|
|
|
export async function saveSessionSnapshot(sessionId, payload, options = {}) {
|
|
const snapshot = createSessionSnapshot(sessionId, payload, options);
|
|
const path = sessionSnapshotPath(sessionId, options.filename);
|
|
await saveTextFile(path, `${JSON.stringify(snapshot, null, 2)}\n`, options.storage);
|
|
return snapshot;
|
|
}
|
|
|
|
export async function saveMachineSessionSnapshot(sessionId, machineId, options = {}) {
|
|
assertPlainObject(options, "machine session snapshot save options");
|
|
const payload = createMachineSessionSnapshotPayload(machineId, options);
|
|
return saveSessionSnapshot(sessionId, payload, options);
|
|
}
|
|
|
|
export async function loadMachineSessionSnapshot(sessionId, machineId, options = {}) {
|
|
const snapshot = await loadSessionSnapshot(sessionId, options);
|
|
validateMachineSessionSnapshotPayload(snapshot.payload, machineId);
|
|
return snapshot;
|
|
}
|
|
|
|
export async function loadSessionSnapshot(sessionId, options = {}) {
|
|
const path = sessionSnapshotPath(sessionId, options.filename);
|
|
const text = await loadTextFile(path, options.storage);
|
|
let snapshot;
|
|
try {
|
|
snapshot = JSON.parse(text);
|
|
} catch (error) {
|
|
throw new Error(`Invalid session snapshot JSON: ${error.message}`);
|
|
}
|
|
return validateSessionSnapshot(snapshot, sessionId);
|
|
}
|