Dwinzo_dev/app/src/modules/simulation/analysis/throughPut/throughPutData.tsx

152 lines
7.3 KiB
TypeScript
Raw Normal View History

2025-05-14 04:27:31 +00:00
import React, { useEffect, useRef, useState } from 'react';
import { useSelectedProduct } from '../../../../store/simulation/useSimulationStore';
import { useProductStore } from '../../../../store/simulation/useProductStore';
import { determineExecutionMachineSequences } from '../../simulator/functions/determineExecutionMachineSequences';
import { useArmBotStore } from '../../../../store/simulation/useArmBotStore';
import { useMachineCount, useMachineUptime, useMaterialCycle, useProcessBar, useThroughPutData } from '../../../../store/builder/store';
2025-05-14 04:27:31 +00:00
import { useVehicleStore } from '../../../../store/simulation/useVehicleStore';
import { useMachineStore } from '../../../../store/simulation/useMachineStore';
import { useConveyorStore } from '../../../../store/simulation/useConveyorStore';
import { useStorageUnitStore } from '../../../../store/simulation/useStorageUnitStore';
import { useMaterialStore } from '../../../../store/simulation/useMaterialStore';
import { usePauseButtonStore, usePlayButtonStore } from '../../../../store/usePlayButtonStore';
export default function ThroughPutData() {
const { selectedProduct } = useSelectedProduct();
const { products, getProductById } = useProductStore();
const { armBots, incrementActiveTime, incrementIdleTime } = useArmBotStore();
const { vehicles } = useVehicleStore();
const { machines } = useMachineStore();
const { conveyors } = useConveyorStore();
const { storageUnits } = useStorageUnitStore();
const { materialHistory } = useMaterialStore();
2025-05-14 04:27:31 +00:00
const { machineCount, setMachineCount } = useMachineCount();
const { machineActiveTime, setMachineActiveTime } = useMachineUptime();
const { materialCycleTime, setMaterialCycleTime } = useMaterialCycle();
const { processBar, setProcessBar } = useProcessBar();
2025-05-14 04:27:31 +00:00
// const [totalActiveTime, setTotalActiveTime] = useState(0);
const { setThroughputData } = useThroughPutData() // <=== ADD THIS
const { isPlaying } = usePlayButtonStore();
// Setting machine count
useEffect(() => {
if (materialCycleTime <= 0) return
let process: any = [];
2025-05-14 04:27:31 +00:00
const fetchProductSequenceData = async () => {
const productData = getProductById(selectedProduct.productId);
if (productData) {
const productSequenceData = await determineExecutionMachineSequences([productData])
2025-05-14 04:27:31 +00:00
if (productSequenceData?.length > 0) {
let totalItems = 0;
let totalActiveTime = 0;
productSequenceData.forEach((sequence) => {
sequence.forEach((item) => {
2025-05-14 04:27:31 +00:00
if (item.type === "roboticArm") {
armBots.filter(arm => arm.modelUuid === item.modelUuid)
.forEach(arm => {
if (arm.activeTime >= 0) {
process.push({ modelid: arm.modelUuid, modelName: arm.modelName, activeTime: arm?.activeTime })
2025-05-14 04:27:31 +00:00
totalActiveTime += arm.activeTime;
}
});
} else if (item.type === "vehicle") {
vehicles.filter(vehicle => vehicle.modelUuid === item.modelUuid)
.forEach(vehicle => {
if (vehicle.activeTime >= 0) {
process.push({ modelid: vehicle.modelUuid, modelName: vehicle.modelName, activeTime: vehicle?.activeTime })
totalActiveTime += vehicle.activeTime;
2025-05-14 04:27:31 +00:00
}
});
} else if (item.type === "machine") {
machines.filter(machine => machine.modelUuid === item.modelUuid)
.forEach(machine => {
if (machine.activeTime >= 0) {
process.push({ modelid: machine.modelUuid, modelName: machine.modelName, activeTime: machine?.activeTime })
totalActiveTime += machine.activeTime;
2025-05-14 04:27:31 +00:00
}
});
} else if (item.type === "transfer") {
conveyors.filter(conveyor => conveyor.modelUuid === item.modelUuid)
.forEach(conveyor => {
if (conveyor.activeTime >= 0) {
totalActiveTime += conveyor.activeTime;
2025-05-14 04:27:31 +00:00
}
});
} else if (item.type === "storageUnit") {
storageUnits.filter(storage => storage.modelUuid === item.modelUuid)
.forEach(storage => {
if (storage.activeTime >= 0) {
totalActiveTime += storage.activeTime;
2025-05-14 04:27:31 +00:00
}
});
}
});
totalItems += sequence.length;
});
setMachineCount(totalItems);
setMachineActiveTime(totalActiveTime);
let arr = process.map((item: any) => ({
name: item.modelName,
completed: Math.round((item.activeTime / totalActiveTime) * 100)
}));
setProcessBar(arr);
2025-05-14 04:27:31 +00:00
}
}
};
fetchProductSequenceData();
}, [products, selectedProduct, getProductById, setMachineCount, materialCycleTime, armBots, vehicles, machines]);
2025-05-14 04:27:31 +00:00
// Setting material cycle time
useEffect(() => {
materialHistory.forEach((material) => {
const start = material.material.startTime ?? 0;
const end = material.material.endTime ?? 0;
if (start === 0 || end === 0) return;
const totalCycleTime = (end - start) / 1000; // Convert milliseconds to seconds
setMaterialCycleTime(Number(totalCycleTime.toFixed(2))); // Set the material cycle time in the store
});
}, [materialHistory]);
2025-05-14 04:27:31 +00:00
useEffect(() => {
if (machineActiveTime > 0 && materialCycleTime > 0 && machineCount > 0) {
const avgProcessTime = (machineActiveTime / materialCycleTime) * 100;
const throughput = (3600 / materialCycleTime) * machineCount * (avgProcessTime / 100);
2025-05-14 04:27:31 +00:00
setThroughputData(throughput.toFixed(2)); // Set the throughput data in the store
console.log('---Throughput Results---');
console.log('Total Active Time:', machineActiveTime);
console.log('Material Cycle Time:', materialCycleTime);
console.log('Machine Count:', machineCount);
console.log('Average Process Time (%):', avgProcessTime);
console.log('Calculated Throughput:', throughput);
2025-05-14 04:27:31 +00:00
}
}, [machineActiveTime, materialCycleTime, machineCount]);
return (
<>
</>
);
}