layout worked with backend

This commit is contained in:
2025-09-06 09:24:10 +05:30
parent 901ecc0ec6
commit 0e8a004f70
11 changed files with 377 additions and 240 deletions

View File

@@ -1,4 +1,4 @@
import React, { useEffect } from "react";
import React, { useEffect, useState } from "react";
import { useSceneContext } from "../../scene/sceneContext";
import { useProductContext } from "../products/productContext";
import { determineExecutionMachineSequences } from "./functions/determineExecutionMachineSequences";
@@ -8,18 +8,13 @@ import { useParams } from "react-router-dom";
import { useVersionContext } from "../../builder/version/versionContext";
import { getSimulationData, saveSimulationData } from "../../../components/layout/scenes/functions/simulationStorage";
import { get } from "http";
import { set } from "immer/dist/internal";
interface SimulationUsageRecord {
activeTime: number;
isActive: boolean;
idleTime: number;
type:
| "roboticArm"
| "vehicle"
| "transfer"
| "storageUnit"
| "crane"
| "human"
| "machine";
type: "roboticArm" | "vehicle" | "transfer" | "storageUnit" | "crane" | "human" | "machine";
assetId: string;
}
// Product → holds multiple usage records
@@ -40,17 +35,7 @@ interface ProjectSimulation {
versions: VersionSimulation[];
}
const SimulationHandler = () => {
const {
materialStore,
armBotStore,
machineStore,
conveyorStore,
vehicleStore,
storageUnitStore,
productStore,
craneStore,
humanStore,
} = useSceneContext();
const { materialStore, armBotStore, machineStore, conveyorStore, vehicleStore, storageUnitStore, productStore, craneStore, humanStore } = useSceneContext();
const { armBots, getArmBotById } = armBotStore();
const { vehicles, getVehicleById } = vehicleStore();
const { getConveyorById } = conveyorStore();
@@ -67,6 +52,7 @@ const SimulationHandler = () => {
const { projectId } = useParams();
const { selectedVersionStore } = useVersionContext();
const { selectedVersion } = selectedVersionStore();
const [simulationEntry, setSimulationEntry] = useState<any>();
const COST_RATES: Record<SimulationUsageRecord["type"], number> = {
roboticArm: 5,
vehicle: 2,
@@ -135,31 +121,72 @@ const SimulationHandler = () => {
return { ...m, efficiencyScore };
});
}
useEffect(() => {
const runSimulation = async () => {
console.log("simulationRecords: ", simulationRecords);
if (!projectId || !selectedVersion || !selectedProduct.productUuid || simulationRecords.length === 0) return;
const project = simulationRecords[0];
if (project) {
// const scores = calculateEfficiencyScores(project.versions);
// console.log("Version Comparisons:", scores);
}
console.log("simulationEntry: ", simulationEntry);
console.log("simulationEntrysaddasd: ", useSimulationManager.getState().getProductById(projectId, selectedVersion?.versionId, selectedProduct.productUuid)?.simulateData);
const data = {
projectId: projectId,
versionId: selectedVersion.versionId,
productUuid: selectedProduct.productUuid,
simulateData: useSimulationManager.getState().getProductById(projectId, selectedVersion?.versionId, selectedProduct.productUuid)?.simulateData,
};
const simulations = await saveSimulationData(data);
if (simulations.message === "SimulatedData created Successfully") {
setSimulationEntry(simulations.data);
}
console.log("Saved simulations:", simulations);
};
runSimulation();
}, [simulationRecords, projectId, selectedVersion, selectedProduct, simulationEntry]);
// useEffect(() => {
// console.log('simulationRecords: ', simulationRecords);
// if (!projectId || !selectedVersion || !selectedProduct.productUuid || simulationRecords.length === 0) return;
// const project = simulationRecords[0];
// if (project) {
// const scores = calculateEfficiencyScores(project.versions);
// console.log("Version Comparisons:", scores);
// }
// saveSimulationData({
// key: selectedProduct.productUuid,
// data: simulationRecords,
// });
// // saveSimulationData({
// // key: selectedProduct.productUuid,
// // data: simulationRecords,
// // });
// }, [simulationRecords]);
useEffect(() => {
console.log('simulationRecords: ', simulationRecords);
if (!projectId || !selectedVersion || !selectedProduct.productUuid || simulationRecords.length === 0) return;
const project = simulationRecords[0];
if (project) {
const scores = calculateEfficiencyScores(project.versions);
console.log("Version Comparisons:", scores);
}
saveSimulationData({
key: selectedProduct.productUuid,
data: simulationRecords,
});
}, [simulationRecords]);
useEffect(() => {
const simData = getSimulationData({ key: selectedProduct.productUuid });
if (simData) {
useSimulationManager.getState().setSimulationRecords(JSON.parse(simData));
// Parse and set in the store
} else { }
}, [])
const fetchSimulationData = async () => {
if (!projectId || !selectedVersion || !selectedProduct.productUuid) return;
const data = {
projectId: projectId,
versionId: selectedVersion.versionId,
productUuid: selectedProduct.productUuid,
simulatedId: simulationEntry,
};
const datas = await getSimulationData(data);
console.log("datas: ", datas);
};
fetchSimulationData();
}, []);
useEffect(() => {
let checkTimer: ReturnType<typeof setTimeout>;
@@ -169,9 +196,7 @@ const SimulationHandler = () => {
let hasActiveEntity = false;
if (currentProduct) {
const executionSequences = await determineExecutionMachineSequences([
currentProduct,
]);
const executionSequences = await determineExecutionMachineSequences([currentProduct]);
if (executionSequences?.length > 0) {
executionSequences.forEach((sequence) => {
sequence.forEach((entity) => {
@@ -221,11 +246,7 @@ const SimulationHandler = () => {
});
}
if (
materials.length === 0 &&
materialHistory.length >= 0 &&
!hasActiveEntity
) {
if (materials.length === 0 && materialHistory.length >= 0 && !hasActiveEntity) {
if (executionSequences?.length > 0) {
executionSequences.forEach((sequence) => {
sequence.forEach((entity) => {
@@ -245,25 +266,13 @@ const SimulationHandler = () => {
const obj = getter(entity.modelUuid);
if (!obj) return; // skip if not found
addSimulationRecord(
projectId,
selectedVersion?.versionId || "",
selectedProduct?.productUuid,
{
activeTime: obj.activeTime ?? 0,
isActive: obj.isActive ?? false,
idleTime: obj.idleTime ?? 0,
type: entity.type as
| "roboticArm"
| "vehicle"
| "machine"
| "human"
| "crane"
| "storageUnit"
| "transfer",
assetId: entity.modelUuid,
}
);
addSimulationRecord(projectId, selectedVersion?.versionId || "", selectedProduct?.productUuid, {
activeTime: obj.activeTime ?? 0,
isActive: obj.isActive ?? false,
idleTime: obj.idleTime ?? 0,
type: entity.type as "roboticArm" | "vehicle" | "machine" | "human" | "crane" | "storageUnit" | "transfer",
assetId: entity.modelUuid,
});
});
});
}
@@ -281,16 +290,7 @@ const SimulationHandler = () => {
return () => {
if (checkTimer) clearTimeout(checkTimer);
};
}, [
materials,
materialHistory,
selectedVersion,
selectedProduct?.productUuid,
isPlaying,
armBots,
vehicles,
machines,
]);
}, [materials, materialHistory, selectedVersion, selectedProduct?.productUuid, isPlaying, armBots, vehicles, machines]);
return null;
};