Refactor action handling in simulation components for improved clarity and efficiency

This commit is contained in:
2025-07-28 12:23:22 +05:30
parent 06e4d516ba
commit 10e7f2f8c4
7 changed files with 92 additions and 118 deletions

View File

@@ -3,15 +3,17 @@ import { useFrame } from "@react-three/fiber";
import { usePlayButtonStore, usePauseButtonStore, useResetButtonStore, useAnimationPlaySpeed } from "../../../../../store/usePlayButtonStore";
import { useSceneContext } from "../../../../scene/sceneContext";
import { useProductContext } from "../../../products/productContext";
import { useHumanEventManager } from "../../../human/eventManager/useHumanEventManager";
export function useRetrieveHandler() {
const { materialStore, armBotStore, vehicleStore, storageUnitStore, productStore, humanStore, assetStore } = useSceneContext();
const { materialStore, armBotStore, vehicleStore, storageUnitStore, productStore, humanStore, assetStore, humanEventManagerRef } = useSceneContext();
const { selectedProductStore } = useProductContext();
const { addMaterial } = materialStore();
const { getModelUuidByActionUuid, getPointUuidByActionUuid, getEventByModelUuid, getActionByUuid } = productStore();
const { getStorageUnitById, getLastMaterial, updateCurrentLoad, removeLastMaterial } = storageUnitStore();
const { getVehicleById, incrementVehicleLoad, addCurrentMaterial } = vehicleStore();
const { getHumanById, incrementHumanLoad, addCurrentMaterial: addCurrentMaterialToHuman } = humanStore();
const { addHumanToMonitor } = useHumanEventManager();
const { getAssetById, setCurrentAnimation } = assetStore();
const { selectedProduct } = selectedProductStore();
const { getArmBotById, addCurrentAction } = armBotStore();
@@ -22,6 +24,7 @@ export function useRetrieveHandler() {
const [activeRetrievals, setActiveRetrievals] = useState<Map<string, { action: StorageAction, isProcessing: boolean, lastCheckTime: number }>>(new Map());
const retrievalTimeRef = useRef<Map<string, number>>(new Map());
const retrievalCountRef = useRef<Map<string, number>>(new Map());
const [initialDelayComplete, setInitialDelayComplete] = useState(false);
const delayTimerRef = useRef<NodeJS.Timeout | null>(null);
@@ -300,27 +303,46 @@ export function useRetrieveHandler() {
const humanAsset = getAssetById(triggeredModel.modelUuid);
const action = getActionByUuid(selectedProduct.productUuid, human?.currentAction?.actionUuid || '');
if (human && !human.isScheduled && human.state === 'idle' && human.currentLoad < (action as HumanAction).loadCapacity) {
if (humanAsset && humanAsset.animationState?.current === 'idle') {
if (!action || action.actionType !== 'worker' || !humanEventManagerRef.current) return;
let state = humanEventManagerRef.current.humanStates.find(h => h.humanId === triggeredModel.modelUuid);
console.log('state: ', state);
console.log('human: ', human);
const currentCount = retrievalCountRef.current.get(actionUuid) ?? 0;
let conditionMet = false;
if (state) {
console.log('state.actionQueue: ', state.actionQueue);
// state.actionQueue[0].count
}
if (currentCount >= action.loadCount) {
completedActions.push(actionUuid);
hasChanges = true;
return;
}
if (human && !human.isScheduled && human.state === 'idle' && human.currentLoad < action.loadCapacity) {
if (humanAsset?.animationState?.current === 'idle') {
setCurrentAnimation(human.modelUuid, 'pickup', true, false, false);
} else if (humanAsset && humanAsset.animationState?.current === 'pickup' && humanAsset.animationState.isCompleted) {
} else if (humanAsset?.animationState?.current === 'pickup' && humanAsset.animationState.isCompleted) {
const lastMaterial = getLastMaterial(storageUnit.modelUuid);
if (lastMaterial) {
if (action && human.currentLoad < (action as HumanAction).loadCapacity) {
const material = createNewMaterial(
lastMaterial.materialId,
lastMaterial.materialType,
storageUnit.point.action
);
if (material) {
removeLastMaterial(storageUnit.modelUuid);
updateCurrentLoad(storageUnit.modelUuid, -1);
incrementHumanLoad(human.modelUuid, 1);
const material = createNewMaterial(
lastMaterial.materialId,
lastMaterial.materialType,
storageUnit.point.action
);
if (material) {
removeLastMaterial(storageUnit.modelUuid);
updateCurrentLoad(storageUnit.modelUuid, -1);
incrementHumanLoad(human.modelUuid, 1);
addHumanToMonitor(human.modelUuid, () => {
addCurrentMaterialToHuman(human.modelUuid, material.materialType, material.materialId);
retrieveLogStatus(material.materialName, `is picked by ${human.modelName}`);
}
if (human.currentLoad + 1 < (action as HumanAction).loadCapacity) {
}
retrievalCountRef.current.set(actionUuid, currentCount + 1);
}, actionUuid)
}
}
}
@@ -352,11 +374,12 @@ export function useRetrieveHandler() {
}, []);
useEffect(() => {
if (isReset) {
if (isReset || !isPlaying) {
setActiveRetrievals(new Map());
retrievalCountRef.current.clear();
setInitialDelayComplete(false);
}
}, [isReset]);
}, [isReset, isPlaying]);
return {
handleRetrieve,

View File

@@ -714,10 +714,7 @@ export function useTriggerHandler() {
setIsVisible(materialId, false);
if (action && human) {
if (human.isActive === false && human.state === 'idle') {
// Handle current action from arm bot
addHumanToMonitor(human.modelUuid, () => {
const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
if (model?.type === 'transfer') {
const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
@@ -729,17 +726,14 @@ export function useTriggerHandler() {
handleAction(action, materialId)
} else {
setHumanScheduled(human.modelUuid, true);
addConveyorToMonitor(conveyor.modelUuid,
() => {
handleAction(action, materialId)
}
)
addConveyorToMonitor(conveyor.modelUuid, () => {
handleAction(action, materialId)
})
}
} else {
setHumanScheduled(human.modelUuid, true);
handleAction(action, materialId)
}
// handleAction(action, materialId)
}
} else if (model?.type === 'vehicle') {
const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
@@ -751,79 +745,20 @@ export function useTriggerHandler() {
handleAction(action, materialId);
} else {
// Handle current action using Event Manager
setIsPaused(materialId, true);
setHumanScheduled(human.modelUuid, true);
addVehicleToMonitor(vehicle.modelUuid,
() => {
handleAction(action, materialId);
}
)
addVehicleToMonitor(vehicle.modelUuid, () => {
handleAction(action, materialId);
})
}
}
} else {
setHumanScheduled(human.modelUuid, true);
handleAction(action, materialId)
}
} else {
// Handle current action using Event Manager
addHumanToMonitor(human.modelUuid, () => {
const model = getEventByModelUuid(selectedProduct.productUuid, action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
if (model?.type === 'transfer') {
const conveyor = getConveyorById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
if (conveyor) {
const previousModel = getEventByModelUuid(selectedProduct.productUuid, material.previous?.modelUuid || '');
if (previousModel) {
if (previousModel.type === 'transfer' && previousModel.modelUuid === model.modelUuid) {
setHumanScheduled(human.modelUuid, true);
handleAction(action, materialId)
} else {
setHumanScheduled(human.modelUuid, true);
addConveyorToMonitor(conveyor.modelUuid,
() => {
handleAction(action, materialId)
}
)
}
} else {
setHumanScheduled(human.modelUuid, true);
handleAction(action, materialId)
}
}
} else if (model?.type === 'vehicle') {
const vehicle = getVehicleById(action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '');
if (vehicle) {
if (vehicle.isActive === false && vehicle.state === 'idle' && vehicle.isPicking && vehicle.currentLoad < vehicle.point.action.loadCapacity) {
// Handle current action from vehicle
setIsPaused(materialId, true);
setHumanScheduled(human.modelUuid, true);
handleAction(action, materialId);
} else {
// Handle current action using Event Manager
setIsPaused(materialId, true);
setHumanScheduled(human.modelUuid, true);
addVehicleToMonitor(vehicle.modelUuid,
() => {
handleAction(action, materialId);
}
)
}
}
} else {
setHumanScheduled(human.modelUuid, true);
handleAction(action, materialId)
}
}, action.actionUuid);
}
}, action.actionUuid);
}
}
}