Files
pikasTech-HWLAB/internal/patchpanel/model.mjs
T
2026-05-21 14:40:31 +00:00

112 lines
2.9 KiB
JavaScript

import { ENVIRONMENT_DEV } from "../protocol/index.mjs";
import { DEFAULT_GATEWAY_SESSION_ID, DEFAULT_PROJECT_ID } from "../sim/model.mjs";
export function createWiringConfig({
wiringConfigId = "wir_mvp_patch_panel",
gatewaySessionId = DEFAULT_GATEWAY_SESSION_ID,
connections,
now = new Date().toISOString()
}) {
if (!Array.isArray(connections) || connections.length === 0) {
throw new Error("wiring config requires at least one connection");
}
return {
wiringConfigId,
projectId: DEFAULT_PROJECT_ID,
gatewaySessionId,
name: "MVP simulated patch panel wiring",
status: "active",
connections,
constraints: {
propagation: "patch-panel-only",
localLoopbackSubstituteAllowed: false
},
createdAt: now,
updatedAt: now
};
}
export function createPatchPanelState({
patchPanelStatusId = "pps_mvp_patch_panel",
gatewaySessionId = DEFAULT_GATEWAY_SESSION_ID,
wiringConfig,
now = new Date().toISOString()
}) {
return {
patchPanelStatusId,
projectId: DEFAULT_PROJECT_ID,
gatewaySessionId,
wiringConfigId: wiringConfig.wiringConfigId,
serviceId: "hwlab-patch-panel",
state: "active",
environment: ENVIRONMENT_DEV,
activeConnections: wiringConfig.connections.map((connection) => ({
fromResourceId: connection.from.resourceId,
fromPort: connection.from.port,
toResourceId: connection.to.resourceId,
toPort: connection.to.port
})),
observedAt: now,
metadata: {
propagation: "patch-panel-only",
connectionCount: wiringConfig.connections.length
}
};
}
export function createPatchPanel({ wiringConfig, now = () => new Date().toISOString() }) {
const signals = new Map();
function routeSignal({ fromResourceId, fromPort, value }) {
const observedAt = now();
const deliveries = [];
for (const connection of wiringConfig.connections) {
const forwardMatch =
connection.from.resourceId === fromResourceId && connection.from.port === fromPort;
const reverseMatch =
connection.to.resourceId === fromResourceId && connection.to.port === fromPort;
if (!forwardMatch && !reverseMatch) {
continue;
}
const target = forwardMatch ? connection.to : connection.from;
const key = `${target.resourceId}:${target.port}`;
const delivery = {
resourceId: target.resourceId,
port: target.port,
value,
sourceResourceId: fromResourceId,
sourcePort: fromPort,
observedAt
};
signals.set(key, delivery);
deliveries.push(delivery);
}
return {
accepted: true,
propagatedBy: "hwlab-patch-panel",
deliveryCount: deliveries.length,
deliveries,
observedAt
};
}
function status() {
return createPatchPanelState({
wiringConfig,
now: now()
});
}
return {
wiringConfig,
routeSignal,
status,
signals
};
}