human bug fix

This commit is contained in:
2025-07-22 10:19:49 +05:30
parent ccf64e0f97
commit 88361b1623
9 changed files with 104 additions and 112 deletions

View File

@@ -37,7 +37,7 @@ export function useConveyorActions() {
switch (action.actionType) { switch (action.actionType) {
case 'default': case 'default':
handleDefaultAction(action); handleDefaultAction(action, materialId);
break; break;
case 'spawn': case 'spawn':
handleSpawnAction(action); handleSpawnAction(action);

View File

@@ -8,7 +8,7 @@ export function useWorkerHandler() {
const { getModelUuidByActionUuid } = productStore(); const { getModelUuidByActionUuid } = productStore();
const { selectedProductStore } = useProductContext(); const { selectedProductStore } = useProductContext();
const { selectedProduct } = selectedProductStore(); const { selectedProduct } = selectedProductStore();
const { incrementHumanLoad, incrementLoadCount, addCurrentMaterial, addCurrentAction } = humanStore(); const { incrementHumanLoad, addCurrentMaterial, addCurrentAction } = humanStore();
const workerLogStatus = (materialUuid: string, status: string) => { const workerLogStatus = (materialUuid: string, status: string) => {
echo.info(`${materialUuid}, ${status}`); echo.info(`${materialUuid}, ${status}`);
@@ -24,7 +24,6 @@ export function useWorkerHandler() {
if (!modelUuid) return; if (!modelUuid) return;
incrementHumanLoad(modelUuid, 1); incrementHumanLoad(modelUuid, 1);
incrementLoadCount(modelUuid, 1);
addCurrentAction(modelUuid, action.actionUuid); addCurrentAction(modelUuid, action.actionUuid);
addCurrentMaterial(modelUuid, material.materialType, material.materialId); addCurrentMaterial(modelUuid, material.materialType, material.materialId);

View File

@@ -11,7 +11,7 @@ export function useRetrieveHandler() {
const { getModelUuidByActionUuid, getPointUuidByActionUuid, getEventByModelUuid, getActionByUuid } = productStore(); const { getModelUuidByActionUuid, getPointUuidByActionUuid, getEventByModelUuid, getActionByUuid } = productStore();
const { getStorageUnitById, getLastMaterial, updateCurrentLoad, removeLastMaterial } = storageUnitStore(); const { getStorageUnitById, getLastMaterial, updateCurrentLoad, removeLastMaterial } = storageUnitStore();
const { getVehicleById, incrementVehicleLoad, addCurrentMaterial } = vehicleStore(); const { getVehicleById, incrementVehicleLoad, addCurrentMaterial } = vehicleStore();
const { getHumanById, incrementHumanLoad, incrementLoadCount, addCurrentMaterial: addCurrentMaterialToHuman } = humanStore(); const { getHumanById, incrementHumanLoad, addCurrentMaterial: addCurrentMaterialToHuman } = humanStore();
const { getAssetById, setCurrentAnimation } = assetStore(); const { getAssetById, setCurrentAnimation } = assetStore();
const { selectedProduct } = selectedProductStore(); const { selectedProduct } = selectedProductStore();
const { getArmBotById, addCurrentAction } = armBotStore(); const { getArmBotById, addCurrentAction } = armBotStore();
@@ -316,7 +316,6 @@ export function useRetrieveHandler() {
removeLastMaterial(storageUnit.modelUuid); removeLastMaterial(storageUnit.modelUuid);
updateCurrentLoad(storageUnit.modelUuid, -1); updateCurrentLoad(storageUnit.modelUuid, -1);
incrementHumanLoad(human.modelUuid, 1); incrementHumanLoad(human.modelUuid, 1);
incrementLoadCount(human.modelUuid, 1);
addCurrentMaterialToHuman(human.modelUuid, material.materialType, material.materialId); addCurrentMaterialToHuman(human.modelUuid, material.materialType, material.materialId);
retrieveLogStatus(material.materialName, `is picked by ${human.modelName}`); retrieveLogStatus(material.materialName, `is picked by ${human.modelName}`);
} }

View File

@@ -6,16 +6,21 @@ import { useProductContext } from '../../products/productContext';
export function useHumanEventManager() { export function useHumanEventManager() {
const { humanStore, productStore, assetStore } = useSceneContext(); const { humanStore, productStore, assetStore } = useSceneContext();
const { getHumanById, clearLoadCount, setCurrentPhase } = humanStore(); const { getHumanById, setCurrentPhase } = humanStore();
const { getAssetById } = assetStore(); const { getAssetById } = assetStore();
const { getActionByUuid } = productStore(); const { getActionByUuid } = productStore();
const { selectedProductStore } = useProductContext(); const { selectedProductStore } = useProductContext();
const { selectedProduct } = selectedProductStore(); const { selectedProduct } = selectedProductStore();
const callbacksRef = useRef<Map<string, (() => void)[]>>(new Map()); const stateRef = useRef({
const actionQueueRef = useRef<Map<string, { actionType: "worker" | "assembly", actionUuid: string }[]>>(new Map()); humanStates: new Map<string, {
const isCooldownRef = useRef<Map<string, boolean>>(new Map()); callbacks: (() => void)[],
const isMonitoringRef = useRef(false); actionQueue: { actionType: "worker" | "assembly", actionUuid: string, actionName: string }[],
isCooldown: boolean
}>(),
callbackCounts: new Map<string, Map<string, number>>(),
isMonitoring: false
});
const { isPlaying } = usePlayButtonStore(); const { isPlaying } = usePlayButtonStore();
const { isPaused } = usePauseButtonStore(); const { isPaused } = usePauseButtonStore();
@@ -23,10 +28,9 @@ export function useHumanEventManager() {
useEffect(() => { useEffect(() => {
if (isReset) { if (isReset) {
callbacksRef.current.clear(); stateRef.current.humanStates.clear();
actionQueueRef.current.clear(); stateRef.current.callbackCounts.clear();
isCooldownRef.current.clear(); stateRef.current.isMonitoring = false;
isMonitoringRef.current = false;
} }
}, [isReset]); }, [isReset]);
@@ -38,34 +42,64 @@ export function useHumanEventManager() {
const actionType = action.actionType; const actionType = action.actionType;
if (actionType !== "worker" && actionType !== "assembly") return; if (actionType !== "worker" && actionType !== "assembly") return;
if (!callbacksRef.current.has(humanId)) { if (!stateRef.current.callbackCounts.has(humanId)) {
callbacksRef.current.set(humanId, []); stateRef.current.callbackCounts.set(humanId, new Map());
actionQueueRef.current.set(humanId, []);
} }
callbacksRef.current.get(humanId)!.push(callback); const actionCounts = stateRef.current.callbackCounts.get(humanId)!;
actionQueueRef.current.get(humanId)!.push({ actionType, actionUuid }); if (!actionCounts.has(actionUuid)) {
actionCounts.set(actionUuid, 0);
}
isMonitoringRef.current = true; const currentCount = actionCounts.get(actionUuid)!;
if (actionType === 'worker' && currentCount >= action.loadCount) {
return;
}
if (!stateRef.current.humanStates.has(humanId)) {
stateRef.current.humanStates.set(humanId, {
callbacks: [],
actionQueue: [],
isCooldown: false
});
}
const humanState = stateRef.current.humanStates.get(humanId)!;
humanState.callbacks.push(callback);
humanState.actionQueue.push({ actionType, actionUuid, actionName: action.actionName });
stateRef.current.isMonitoring = true;
}; };
const removeHumanFromMonitor = (humanId: string) => { const removeHumanFromMonitor = (humanId: string) => {
callbacksRef.current.delete(humanId); // stateRef.current.humanStates.delete(humanId);
actionQueueRef.current.delete(humanId);
isCooldownRef.current.delete(humanId);
if (callbacksRef.current.size === 0) { if (stateRef.current.humanStates.size === 0) {
isMonitoringRef.current = false; stateRef.current.isMonitoring = false;
} }
}; };
const getCallbackCount = (humanId: string, actionUuid: string) => {
if (!stateRef.current.callbackCounts.has(humanId)) return 0;
return stateRef.current.callbackCounts.get(humanId)!.get(actionUuid) || 0;
};
const incrementCallbackCount = (humanId: string, actionUuid: string) => {
if (!stateRef.current.callbackCounts.has(humanId)) {
stateRef.current.callbackCounts.set(humanId, new Map());
}
const actionCounts = stateRef.current.callbackCounts.get(humanId)!;
const currentCount = actionCounts.get(actionUuid) || 0;
actionCounts.set(actionUuid, currentCount + 1);
};
useFrame(() => { useFrame(() => {
if (!isMonitoringRef.current || !isPlaying || isPaused) return; if (!stateRef.current.isMonitoring || !isPlaying || isPaused) return;
callbacksRef.current.forEach((queue, humanId) => { stateRef.current.humanStates.forEach((humanState, humanId) => {
if (queue.length === 0 || isCooldownRef.current.get(humanId)) return; if (humanState.callbacks.length === 0 || humanState.isCooldown) return;
const actionQueue = actionQueueRef.current.get(humanId); const actionQueue = humanState.actionQueue;
if (!actionQueue || actionQueue.length === 0) return; if (!actionQueue || actionQueue.length === 0) return;
const { actionType: expectedActionType, actionUuid } = actionQueue[0]; const { actionType: expectedActionType, actionUuid } = actionQueue[0];
@@ -75,11 +109,22 @@ export function useHumanEventManager() {
if (!humanAsset || !human || !action || action.actionType !== expectedActionType) return; if (!humanAsset || !human || !action || action.actionType !== expectedActionType) return;
let conditionMet = false; const currentCount = getCallbackCount(humanId, actionUuid);
const currentAction = getActionByUuid(selectedProduct.productUuid, human.currentAction?.actionUuid || '') as HumanAction | undefined; const currentAction = getActionByUuid(selectedProduct.productUuid, human.currentAction?.actionUuid || '') as HumanAction | undefined;
let conditionMet = false;
if (expectedActionType === "worker") { if (expectedActionType === "worker") {
if (currentAction && currentAction.actionType === 'worker' && currentCount >= currentAction.loadCount) {
humanState.callbacks.shift();
actionQueue.shift();
if (humanState.callbacks.length === 0) {
removeHumanFromMonitor(humanId);
}
return;
}
if (currentAction && currentAction.actionType === 'worker') { if (currentAction && currentAction.actionType === 'worker') {
conditionMet = ( conditionMet = (
!human.isActive && !human.isActive &&
@@ -88,15 +133,6 @@ export function useHumanEventManager() {
human.currentLoad < currentAction.loadCapacity human.currentLoad < currentAction.loadCapacity
); );
if (human.totalLoadCount >= currentAction.loadCount && actionUuid === human.currentAction?.actionUuid) {
queue.shift();
actionQueue.shift();
if (queue.length === 0) {
removeHumanFromMonitor(humanId);
}
return;
}
if (conditionMet && actionUuid !== human.currentAction?.actionUuid) { if (conditionMet && actionUuid !== human.currentAction?.actionUuid) {
setCurrentPhase(human.modelUuid, 'init'); setCurrentPhase(human.modelUuid, 'init');
} }
@@ -104,8 +140,7 @@ export function useHumanEventManager() {
conditionMet = ( conditionMet = (
!human.isActive && !human.isActive &&
human.state === "idle" && human.state === "idle" &&
humanAsset.animationState?.current === 'idle' && humanAsset.animationState?.current === 'idle'
human.currentLoad < action.loadCapacity
); );
if (conditionMet && actionUuid !== human.currentAction?.actionUuid) { if (conditionMet && actionUuid !== human.currentAction?.actionUuid) {
setCurrentPhase(human.modelUuid, 'init'); setCurrentPhase(human.modelUuid, 'init');
@@ -127,41 +162,31 @@ export function useHumanEventManager() {
conditionMet = ( conditionMet = (
!human.isActive && !human.isActive &&
human.state === "idle" && human.state === "idle" &&
humanAsset.animationState?.current === 'idle' && humanAsset.animationState?.current === 'idle'
human.currentLoad < action.loadCapacity
) )
} }
if (conditionMet) {
clearLoadCount(human.modelUuid);
}
} }
if (conditionMet) { if (conditionMet) {
const callback = queue.shift(); const callback = humanState.callbacks.shift();
actionQueue.shift(); actionQueue.shift();
if (callback) callback(); if (callback) {
callback();
incrementCallbackCount(humanId, actionUuid);
}
if (queue.length === 0) { if (humanState.callbacks.length === 0) {
removeHumanFromMonitor(humanId); removeHumanFromMonitor(humanId);
} else { } else {
isCooldownRef.current.set(humanId, true); humanState.isCooldown = true;
setTimeout(() => { setTimeout(() => {
isCooldownRef.current.set(humanId, false); humanState.isCooldown = false;
}, 1000); }, 1000);
} }
} }
}); });
}, 0); });
useEffect(() => {
return () => {
callbacksRef.current.clear();
actionQueueRef.current.clear();
isCooldownRef.current.clear();
isMonitoringRef.current = false;
};
}, []);
return { return {
addHumanToMonitor, addHumanToMonitor,

View File

@@ -478,8 +478,10 @@ export function useTriggerHandler() {
if (toEvent?.type === 'transfer') { if (toEvent?.type === 'transfer') {
// Vehicle to Transfer // Vehicle to Transfer
if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) { if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) {
const conveyor = getConveyorById(toEvent.modelUuid);
const material = getMaterialById(materialId); const material = getMaterialById(materialId);
if (material) {
if (material && conveyor) {
const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid); const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
setPreviousLocation(material.materialId, { setPreviousLocation(material.materialId, {
@@ -494,23 +496,29 @@ export function useTriggerHandler() {
actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid, actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
}); });
setIsPaused(materialId, false);
setIsVisible(materialId, true); setIsVisible(materialId, true);
if (action && if (action &&
action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid && action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid &&
action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid
) { ) {
setNextLocation(material.materialId, { setNextLocation(material.materialId, {
modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid, modelUuid: action.triggers[0]?.triggeredAsset?.triggeredModel.modelUuid || '',
pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid, pointUuid: action.triggers[0]?.triggeredAsset?.triggeredPoint?.pointUuid || '',
}); });
addConveyorToMonitor(conveyor.modelUuid, () => {
setIsPaused(materialId, false);
handleAction(action, materialId); handleAction(action, materialId);
} })
}
} }
} }
} else if (toEvent?.type === 'vehicle') { } else if (toEvent?.type === 'vehicle') {
// Vehicle to Vehicle // Vehicle to Vehicle
@@ -539,7 +547,6 @@ export function useTriggerHandler() {
setNextLocation(material.materialId, null); setNextLocation(material.materialId, null);
if (action && armBot) { if (action && armBot) {
if (armBot.isActive === false && armBot.state === 'idle') { if (armBot.isActive === false && armBot.state === 'idle') {
@@ -1439,13 +1446,11 @@ export function useTriggerHandler() {
const material = getMaterialById(materialId); const material = getMaterialById(materialId);
if (material) { if (material) {
setIsPaused(material.materialId, false); setIsPaused(material.materialId, true);
const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid); const action = getActionByUuid(selectedProduct.productUuid, trigger.triggeredAsset.triggeredAction.actionUuid);
const vehicle = getVehicleById(trigger.triggeredAsset?.triggeredModel.modelUuid); const vehicle = getVehicleById(trigger.triggeredAsset?.triggeredModel.modelUuid);
setNextLocation(material.materialId, null);
if (action) { if (action) {
if (vehicle) { if (vehicle) {
@@ -1466,16 +1471,15 @@ export function useTriggerHandler() {
actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid, actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid,
}); });
setNextLocation(material.materialId, null);
// Handle current action from vehicle // Handle current action from vehicle
handleAction(action, materialId); handleAction(action, materialId);
} else { } else {
setIsPaused(materialId, true);
addVehicleToMonitor(vehicle.modelUuid, addVehicleToMonitor(vehicle.modelUuid,
() => { () => {
setIsPaused(materialId, false);
setIsVisible(materialId, false); setIsVisible(materialId, false);
setPreviousLocation(material.materialId, { setPreviousLocation(material.materialId, {
@@ -1493,6 +1497,7 @@ export function useTriggerHandler() {
setNextLocation(material.materialId, null); setNextLocation(material.materialId, null);
// Handle current action from vehicle // Handle current action from vehicle
console.log('action: ', action);
handleAction(action, materialId); handleAction(action, materialId);
} }
) )

View File

@@ -325,7 +325,6 @@ function DraggableLineSegment({
}; };
const onPointerMove = (e: ThreeEvent<PointerEvent>) => { const onPointerMove = (e: ThreeEvent<PointerEvent>) => {
console.log('isAnyDragging: ', isAnyDragging);
if (isAnyDragging !== "line" || activeTool !== 'pen') return; if (isAnyDragging !== "line" || activeTool !== 'pen') return;
const intersect = new THREE.Vector3(); const intersect = new THREE.Vector3();

View File

@@ -19,7 +19,7 @@ function VehicleInstance({ agvDetail }: Readonly<{ agvDetail: VehicleStatus }>)
const { materialStore, armBotStore, conveyorStore, vehicleStore, storageUnitStore, humanStore, productStore, assetStore } = useSceneContext(); const { materialStore, armBotStore, conveyorStore, vehicleStore, storageUnitStore, humanStore, productStore, assetStore } = useSceneContext();
const { removeMaterial, setEndTime, setIsVisible } = materialStore(); const { removeMaterial, setEndTime, setIsVisible } = materialStore();
const { getStorageUnitById } = storageUnitStore(); const { getStorageUnitById } = storageUnitStore();
const { getHumanById, addCurrentAction, addCurrentMaterial, incrementHumanLoad , incrementLoadCount } = humanStore(); const { getHumanById, addCurrentAction, addCurrentMaterial, incrementHumanLoad } = humanStore();
const { getArmBotById } = armBotStore(); const { getArmBotById } = armBotStore();
const { getConveyorById } = conveyorStore(); const { getConveyorById } = conveyorStore();
const { triggerPointActions } = useTriggerHandler(); const { triggerPointActions } = useTriggerHandler();
@@ -302,7 +302,6 @@ function VehicleInstance({ agvDetail }: Readonly<{ agvDetail: VehicleStatus }>)
setIsVisible(material.materialId, false); setIsVisible(material.materialId, false);
addCurrentMaterial(humanId, material.materialType, material.materialId); addCurrentMaterial(humanId, material.materialType, material.materialId);
incrementHumanLoad(humanId, 1); incrementHumanLoad(humanId, 1);
incrementLoadCount(humanId, 1);
} }
return; return;
} }

View File

@@ -27,11 +27,6 @@ interface HumansStore {
incrementHumanLoad: (modelUuid: string, incrementBy: number) => void; incrementHumanLoad: (modelUuid: string, incrementBy: number) => void;
decrementHumanLoad: (modelUuid: string, decrementBy: number) => void; decrementHumanLoad: (modelUuid: string, decrementBy: number) => void;
incrementLoadCount: (modelUuid: string, incrementBy: number) => void;
decrementLoadCount: (modelUuid: string, decrementBy: number) => void;
clearLoadCount: (modelUuid: string) => void;
addCurrentMaterial: (modelUuid: string, materialType: string, materialId: string) => void; addCurrentMaterial: (modelUuid: string, materialType: string, materialId: string) => void;
setCurrentMaterials: (modelUuid: string, materials: { materialType: string; materialId: string }[]) => void; setCurrentMaterials: (modelUuid: string, materials: { materialType: string; materialId: string }[]) => void;
removeLastMaterial: (modelUuid: string) => { materialType: string; materialId: string } | undefined; removeLastMaterial: (modelUuid: string) => { materialType: string; materialId: string } | undefined;
@@ -65,7 +60,6 @@ export const createHumanStore = () => {
isScheduled: false, isScheduled: false,
idleTime: 0, idleTime: 0,
activeTime: 0, activeTime: 0,
totalLoadCount: 0,
currentLoad: 0, currentLoad: 0,
currentMaterials: [], currentMaterials: [],
distanceTraveled: 0 distanceTraveled: 0
@@ -182,33 +176,6 @@ export const createHumanStore = () => {
}); });
}, },
incrementLoadCount: (modelUuid, incrementBy) => {
set((state) => {
const human = state.humans.find(h => h.modelUuid === modelUuid);
if (human) {
human.totalLoadCount += incrementBy;
}
});
},
decrementLoadCount: (modelUuid, decrementBy) => {
set((state) => {
const human = state.humans.find(h => h.modelUuid === modelUuid);
if (human) {
human.totalLoadCount -= decrementBy;
}
});
},
clearLoadCount: (modelUuid) => {
set((state) => {
const human = state.humans.find(h => h.modelUuid === modelUuid);
if (human) {
human.totalLoadCount = 0;
}
});
},
addCurrentMaterial: (modelUuid, materialType, materialId) => { addCurrentMaterial: (modelUuid, materialType, materialId) => {
set((state) => { set((state) => {
const human = state.humans.find(h => h.modelUuid === modelUuid); const human = state.humans.find(h => h.modelUuid === modelUuid);

View File

@@ -252,7 +252,6 @@ interface HumanStatus extends HumanEventSchema {
isScheduled: boolean; isScheduled: boolean;
idleTime: number; idleTime: number;
activeTime: number; activeTime: number;
totalLoadCount: number;
currentLoad: number; currentLoad: number;
currentMaterials: { materialType: string; materialId: string; }[]; currentMaterials: { materialType: string; materialId: string; }[];
distanceTraveled: number; distanceTraveled: number;