refactor api-simulation for create and fetch
This commit is contained in:
@@ -6,9 +6,10 @@ import { usePlayButtonStore } from "../../../store/usePlayButtonStore";
|
||||
import { useSimulationManager } from "../../../store/rough/useSimulationManagerStore";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { useVersionContext } from "../../builder/version/versionContext";
|
||||
import { getSimulationData, saveSimulationData } from "../../../components/layout/scenes/functions/simulationStorage";
|
||||
import { updateSimulateData, saveSimulationData, getSimulationData } from "../../../components/layout/scenes/functions/simulationStorage";
|
||||
import { get } from "http";
|
||||
import { set } from "immer/dist/internal";
|
||||
import { useSimulateId } from "../../../store/builder/store";
|
||||
interface SimulationUsageRecord {
|
||||
activeTime: number;
|
||||
isActive: boolean;
|
||||
@@ -53,6 +54,7 @@ const SimulationHandler = () => {
|
||||
const { selectedVersionStore } = useVersionContext();
|
||||
const { selectedVersion } = selectedVersionStore();
|
||||
const [simulationEntry, setSimulationEntry] = useState<any>();
|
||||
const { setSimulateId } = useSimulateId();
|
||||
const COST_RATES: Record<SimulationUsageRecord["type"], number> = {
|
||||
roboticArm: 5,
|
||||
vehicle: 2,
|
||||
@@ -121,36 +123,55 @@ 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];
|
||||
// useEffect(() => {
|
||||
// const runSimulation = async () => {
|
||||
// console.log("simulationRecords: ", simulationRecords);
|
||||
// if (!projectId || !selectedVersion || !selectedProduct.productUuid || simulationRecords.length === 0) return;
|
||||
|
||||
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 project = simulationRecords[0];
|
||||
|
||||
const data = {
|
||||
projectId: projectId,
|
||||
versionId: selectedVersion.versionId,
|
||||
productUuid: selectedProduct.productUuid,
|
||||
simulateData: useSimulationManager.getState().getProductById(projectId, selectedVersion?.versionId, selectedProduct.productUuid)?.simulateData,
|
||||
};
|
||||
// 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 simulations = await saveSimulationData(data);
|
||||
if (simulations.message === "SimulatedData created Successfully") {
|
||||
setSimulationEntry(simulations.data);
|
||||
}
|
||||
console.log("Saved simulations:", simulations);
|
||||
};
|
||||
// const data = {
|
||||
// projectId: projectId,
|
||||
// versionId: selectedVersion.versionId,
|
||||
// productUuid: selectedProduct.productUuid,
|
||||
// simulateData: useSimulationManager.getState().getProductById(projectId, selectedVersion?.versionId, selectedProduct.productUuid)?.simulateData,
|
||||
// };
|
||||
|
||||
runSimulation();
|
||||
}, [simulationRecords, projectId, selectedVersion, selectedProduct, simulationEntry]);
|
||||
// const simulations = await saveSimulationData(data);
|
||||
// console.log("simulations: ", simulations);
|
||||
// const simulateId = localStorage.getItem("simulateId");
|
||||
// console.log("simulateId: ", simulateId);
|
||||
// if (simulations.message === "SimulatedData created Successfully") {
|
||||
// console.log("simulations.data: ", simulations.data);
|
||||
// setSimulationEntry(simulations.data);
|
||||
// localStorage.setItem("simulateId", simulations.data);
|
||||
// setSimulateId(simulations.data);
|
||||
// }
|
||||
|
||||
// // else {
|
||||
// // console.log("djh");
|
||||
// // const data = {
|
||||
// // projectId: projectId,
|
||||
// // versionId: selectedVersion.versionId,
|
||||
// // productUuid: selectedProduct.productUuid,
|
||||
// // simulateId: simulateId,
|
||||
// // simulateData: useSimulationManager.getState().getProductById(projectId, selectedVersion?.versionId, selectedProduct.productUuid)?.simulateData,
|
||||
// // };
|
||||
// // const update = await updateSimulateData(data);
|
||||
// // console.log("update: ", update);
|
||||
// // }
|
||||
// };
|
||||
|
||||
// runSimulation();
|
||||
// }, [simulationRecords, projectId, selectedVersion, selectedProduct]);
|
||||
|
||||
// useEffect(() => {
|
||||
// console.log('simulationRecords: ', simulationRecords);
|
||||
@@ -173,21 +194,6 @@ const SimulationHandler = () => {
|
||||
// // });
|
||||
// }, [simulationRecords]);
|
||||
|
||||
useEffect(() => {
|
||||
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>;
|
||||
if (!projectId) return;
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import { useEffect } from 'react';
|
||||
import { useActionHandler } from '../actions/useActionHandler';
|
||||
import { usePlayButtonStore, useResetButtonStore } from '../../../store/usePlayButtonStore';
|
||||
import { determineExecutionOrder } from './functions/determineExecutionOrder';
|
||||
import { useProductContext } from '../products/productContext';
|
||||
import { useSceneContext } from '../../scene/sceneContext';
|
||||
import SimulationHandler from './SimulationHandler';
|
||||
import { useEffect } from "react";
|
||||
import { useActionHandler } from "../actions/useActionHandler";
|
||||
import { usePlayButtonStore, useResetButtonStore } from "../../../store/usePlayButtonStore";
|
||||
import { determineExecutionOrder } from "./functions/determineExecutionOrder";
|
||||
import { useProductContext } from "../products/productContext";
|
||||
import { useSceneContext } from "../../scene/sceneContext";
|
||||
import SimulationHandler from "./SimulationHandler";
|
||||
import { getSimulationData, saveSimulationData } from "../../../components/layout/scenes/functions/simulationStorage";
|
||||
import { useParams } from "react-router-dom";
|
||||
import { useVersionContext } from "../../builder/version/versionContext";
|
||||
import { version } from "os";
|
||||
import { get } from "http";
|
||||
import { useSimulationManager } from "../../../store/rough/useSimulationManagerStore";
|
||||
|
||||
function Simulator() {
|
||||
const { selectedProductStore } = useProductContext();
|
||||
@@ -14,6 +20,9 @@ function Simulator() {
|
||||
const { selectedProduct } = selectedProductStore();
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
const { isReset } = useResetButtonStore();
|
||||
const { projectId } = useParams();
|
||||
const { selectedVersionStore } = useVersionContext();
|
||||
const { selectedVersion } = selectedVersionStore();
|
||||
|
||||
useEffect(() => {
|
||||
if (!isPlaying || isReset || !selectedProduct.productUuid) return;
|
||||
@@ -23,22 +32,41 @@ function Simulator() {
|
||||
|
||||
const executionOrder = determineExecutionOrder([product]);
|
||||
|
||||
executionOrder.forEach(action => {
|
||||
executionOrder.forEach((action) => {
|
||||
handleAction(action);
|
||||
});
|
||||
}, [products, isPlaying, isReset, selectedProduct]);
|
||||
|
||||
|
||||
useEffect(() => {
|
||||
if (!projectId || !selectedVersion || !selectedProduct?.productUuid) return;
|
||||
const fetchSimulateData = async () => {
|
||||
const getData = await getSimulationData(projectId, selectedVersion.versionId, selectedProduct?.productUuid);
|
||||
const product = getProductById(selectedProduct.productUuid);
|
||||
if (!product) return;
|
||||
const products: productsSchema = [product];
|
||||
const getSimulate = getData?.data;
|
||||
if (getData && getSimulate && getSimulate.productTimestamp === products[0]?.timestamp) {
|
||||
return;
|
||||
} else {
|
||||
//call create API
|
||||
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);
|
||||
console.log("simulations: ", simulations);
|
||||
echo.log("Simulation data saved successfully");
|
||||
}
|
||||
};
|
||||
fetchSimulateData();
|
||||
}, []);
|
||||
return (
|
||||
|
||||
<>
|
||||
|
||||
{/* <simulationHandler/> */}
|
||||
<SimulationHandler />
|
||||
|
||||
</>
|
||||
|
||||
);
|
||||
}
|
||||
|
||||
export default Simulator;
|
||||
export default Simulator;
|
||||
|
||||
Reference in New Issue
Block a user