From a37faf7adba7d9eac2b5e0ae87ec981dc094ae76 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Sat, 10 May 2025 13:50:36 +0530 Subject: [PATCH] Enhance RoboticArmInstance and useTriggerHandler: add action handling for pickup and drop triggers, improve event retrieval with getEventByActionUuid, and streamline material location management in storage units. --- .../armInstance/roboticArmInstance.tsx | 57 ++++++++++++------- .../triggerHandler/useTriggerHandler.ts | 36 ++++++++++++ app/src/store/simulation/useProductStore.ts | 27 +++++++++ 3 files changed, 99 insertions(+), 21 deletions(-) diff --git a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx index 2852bc7..f169ad2 100644 --- a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx +++ b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx @@ -30,7 +30,7 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { const { decrementVehicleLoad, removeLastMaterial } = useVehicleStore(); const { setIsVisible, getMaterialById } = useMaterialStore(); const { selectedProduct } = useSelectedProduct(); - const { getActionByUuid, getEventByModelUuid } = useProductStore(); + const { getActionByUuid, getEventByActionUuid, getEventByModelUuid } = useProductStore(); const { triggerPointActions } = useTriggerHandler(); const { isPlaying } = usePlayButtonStore(); const { isReset } = useResetButtonStore(); @@ -43,20 +43,20 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { const action = getActionByUuid(selectedProduct.productId, armBot.currentAction?.actionUuid || ''); - const handleTrigger = () => { + const handlePickUpTrigger = () => { if (armBot.currentAction && armBot.currentAction.materialId) { const material = getMaterialById(armBot.currentAction.materialId); if (material && material.previous && material.previous.modelUuid) { - const model = getEventByModelUuid(selectedProduct.productId, material.previous.modelUuid); - if (model) { - if (model.type === 'transfer') { + const previousModel = getEventByActionUuid(selectedProduct.productId, material.previous.actionUuid); + if (previousModel) { + if (previousModel.type === 'transfer') { setIsVisible(armBot.currentAction.materialId, false); - } else if (model.type === 'machine') { + } else if (previousModel.type === 'machine') { // machine specific logic - } else if (model.type === 'vehicle') { - decrementVehicleLoad(model.modelUuid, 1); - removeLastMaterial(model.modelUuid); - } else if (model.type === 'storageUnit') { + } else if (previousModel.type === 'vehicle') { + decrementVehicleLoad(previousModel.modelUuid, 1); + removeLastMaterial(previousModel.modelUuid); + } else if (previousModel.type === 'storageUnit') { // storage unit logic } } else { @@ -68,6 +68,30 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { } } + const handleDropTrigger = () => { + + if (armBot.currentAction) { + const action = getActionByUuid(selectedProduct.productId, armBot.currentAction.actionUuid); + if (action && action.triggers[0].triggeredAsset?.triggeredModel.modelUuid) { + const model = getEventByModelUuid(selectedProduct.productId, action?.triggers[0].triggeredAsset?.triggeredModel.modelUuid); + if (!model) return; + if (model.type === 'transfer') { + setIsVisible(armBot.currentAction.materialId || '', true); + } else if (model.type === 'machine') { + // + } else if (model.type === 'vehicle') { + // + } else if (model.type === 'storageUnit') { + // + } + } + if (action && armBot.currentAction.materialId) { + triggerPointActions(action, armBot.currentAction.materialId) + removeCurrentAction(armBot.modelUuid) + } + } + } + function step() { if (isPausedRef.current) { if (!pauseTimeRef.current) { @@ -103,7 +127,7 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { logStatus(armBot.modelUuid, "picking the object"); setPath(curve.points.map(point => [point.x, point.y, point.z])) - handleTrigger(); + handlePickUpTrigger(); } } @@ -122,17 +146,8 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { logStatus(armBot.modelUuid, "dropping the object"); setPath(curve.points.map(point => [point.x, point.y, point.z])); - if (armBot.currentAction) { - setIsVisible(armBot.currentAction.materialId || '', true); - } + handleDropTrigger(); - if (armBot.currentAction) { - const action = getActionByUuid(selectedProduct.productId, armBot.currentAction.actionUuid); - if (action && armBot.currentAction.materialId) { - triggerPointActions(action, armBot.currentAction.materialId) - removeCurrentAction(armBot.modelUuid) - } - } } } logStatus(armBot.modelUuid, "Moving armBot from end point to rest position.") diff --git a/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts b/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts index 6b974d5..7db5730 100644 --- a/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts +++ b/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts @@ -517,7 +517,43 @@ export function useTriggerHandler() { } else if (toEvent?.type === 'storageUnit') { // Robotic Arm to Storage Unit + if (materialId && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) { + const material = getMaterialById(materialId); + if (material) { + const action = getActionByUuid(selectedProduct.productId, trigger.triggeredAsset.triggeredAction.actionUuid); + const storageUnit = getStorageUnitById(trigger.triggeredAsset?.triggeredModel.modelUuid); + setPreviousLocation(material.materialId, { + modelUuid: material.current.modelUuid, + pointUuid: material.current.pointUuid, + actionUuid: material.current.actionUuid, + }) + + setCurrentLocation(material.materialId, { + modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid, + pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid, + actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid, + }); + + setNextLocation(material.materialId, null); + + setIsVisible(materialId, false); + + if (action && storageUnit) { + + if (storageUnit.currentLoad < storageUnit.point.action.storageCapacity) { + + // Handle current action from vehicle + handleAction(action, materialId); + + } else { + + // Event Manager Needed + + } + } + } + } } } else if (fromEvent?.type === 'storageUnit') { if (toEvent?.type === 'transfer') { diff --git a/app/src/store/simulation/useProductStore.ts b/app/src/store/simulation/useProductStore.ts index b90fd7d..e7d394a 100644 --- a/app/src/store/simulation/useProductStore.ts +++ b/app/src/store/simulation/useProductStore.ts @@ -61,6 +61,7 @@ type ProductsStore = { // Helper functions getProductById: (productId: string) => { productName: string; productId: string; eventDatas: EventsSchema[] } | undefined; getEventByModelUuid: (productId: string, modelUuid: string) => EventsSchema | undefined; + getEventByActionUuid: (productId: string, actionUuid: string) => EventsSchema | undefined; getEventByTriggerUuid: (productId: string, triggerUuid: string) => EventsSchema | undefined; getEventByPointUuid: (productId: string, pointUuid: string) => EventsSchema | undefined; getPointByUuid: (productId: string, modelUuid: string, pointUuid: string) => ConveyorPointSchema | VehiclePointSchema | RoboticArmPointSchema | MachinePointSchema | StoragePointSchema | undefined; @@ -543,6 +544,32 @@ export const useProductStore = create()( return product.eventDatas.find(e => 'modelUuid' in e && e.modelUuid === modelUuid); }, + getEventByActionUuid: (productId, actionUuid) => { + const product = get().getProductById(productId); + if (!product) return undefined; + + for (const event of product.eventDatas) { + if ('points' in event) { + for (const point of (event as ConveyorEventSchema).points) { + if (point.action?.actionUuid === actionUuid) { + return event; + } + } + } else if ('point' in event) { + const point = (event as any).point; + if ('action' in point && point.action?.actionUuid === actionUuid) { + return event; + } else if ('actions' in point) { + const action = point.actions.find((a: any) => a.actionUuid === actionUuid); + if (action) { + return event; + } + } + } + } + return undefined; + }, + getEventByTriggerUuid: (productId, triggerUuid) => { const product = get().getProductById(productId); if (!product) return undefined;