diff --git a/app/src/modules/simulation/actions/conveyor/actionHandler/useSpawnHandler.ts b/app/src/modules/simulation/actions/conveyor/actionHandler/useSpawnHandler.ts index 8f639e6..da1626b 100644 --- a/app/src/modules/simulation/actions/conveyor/actionHandler/useSpawnHandler.ts +++ b/app/src/modules/simulation/actions/conveyor/actionHandler/useSpawnHandler.ts @@ -61,7 +61,7 @@ export function useSpawnHandler() { current: { modelUuid: modelUuid, pointUuid: pointUuid, - actionUuid: action?.actionUuid || '' + actionUuid: action.actionUuid }, weight: 1, cost: 1 @@ -74,7 +74,6 @@ export function useSpawnHandler() { newMaterial.next = { modelUuid: action.triggers[0].triggeredAsset?.triggeredModel.modelUuid, pointUuid: action.triggers[0].triggeredAsset?.triggeredPoint?.pointUuid, - actionUuid: action.triggers[0].triggeredAsset?.triggeredAction?.actionUuid } } diff --git a/app/src/modules/simulation/actions/conveyor/actionHandler/useSwapHandler.ts b/app/src/modules/simulation/actions/conveyor/actionHandler/useSwapHandler.ts new file mode 100644 index 0000000..46c9d02 --- /dev/null +++ b/app/src/modules/simulation/actions/conveyor/actionHandler/useSwapHandler.ts @@ -0,0 +1,38 @@ +import { useCallback } from "react"; +import { useMaterialStore } from "../../../../../store/simulation/useMaterialStore"; +import { useProductStore } from "../../../../../store/simulation/useProductStore"; +import { useSelectedProduct } from "../../../../../store/simulation/useSimulationStore"; +import { usePlayButtonStore } from "../../../../../store/usePlayButtonStore"; +import * as THREE from 'three'; + +export function useSwapHandler() { + const { addMaterial, getMaterialByCurrentPointUuid, setMaterial } = useMaterialStore(); + const { getPointUuidByActionUuid } = useProductStore(); + const { selectedProduct } = useSelectedProduct(); + const { isPlaying } = usePlayButtonStore(); + + const swapLogStatus = (materialUuid: string, status: string) => { + // console.log(`${materialUuid}, ${status}`); + } + + const handleSwap = useCallback((action: ConveyorAction) => { + if (!action || action.actionType !== 'swap' || !isPlaying) return; + + const { material: newMaterialType, actionUuid } = action; + const pointUuid = getPointUuidByActionUuid(selectedProduct.productId, actionUuid); + + if (!pointUuid) return; + + const currentMaterial = getMaterialByCurrentPointUuid(pointUuid); + + if (currentMaterial) { + setMaterial(currentMaterial.materialId, newMaterialType); + swapLogStatus(currentMaterial.materialId, `Swapped to ${newMaterialType}`); + } + + }, [addMaterial, getMaterialByCurrentPointUuid, getPointUuidByActionUuid, isPlaying, setMaterial, selectedProduct.productId]); + + return { + handleSwap, + }; +} \ No newline at end of file diff --git a/app/src/modules/simulation/actions/conveyor/useConveyorActions.ts b/app/src/modules/simulation/actions/conveyor/useConveyorActions.ts index f93ef4f..32d54c4 100644 --- a/app/src/modules/simulation/actions/conveyor/useConveyorActions.ts +++ b/app/src/modules/simulation/actions/conveyor/useConveyorActions.ts @@ -1,11 +1,12 @@ import { useEffect, useCallback, useRef } from "react"; import { useSpawnHandler } from "./actionHandler/useSpawnHandler"; +import { useSwapHandler } from "./actionHandler/useSwapHandler"; export function useConveyorActions() { const { handleSpawn, clearCurrentSpawn } = useSpawnHandler(); + const { handleSwap } = useSwapHandler(); const handleDefaultAction = useCallback((action: ConveyorAction) => { - console.log('action: ', action); console.log(`Default conveyor action ${action.actionUuid}`); }, []); @@ -14,8 +15,8 @@ export function useConveyorActions() { }, [handleSpawn]); const handleSwapAction = useCallback((action: ConveyorAction) => { - console.log(`Swapping to material ${action.material}`); - }, []); + handleSwap(action); + }, [handleSwap]); const handleDelayAction = useCallback((action: ConveyorAction) => { const delayMs = (action.delay || 0) * 1000; diff --git a/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx b/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx index 338a45c..52e4862 100644 --- a/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx +++ b/app/src/modules/simulation/materials/instances/instance/materialInstance.tsx @@ -11,7 +11,7 @@ import { useTriggerHandler } from '../../../triggers/triggerHandler/useTriggerHa function MaterialInstance({ material }: { material: MaterialSchema }) { const matRef: any = useRef(); const { scene } = useThree(); - const { getModelUuidByPointUuid, getPointByUuid, getEventByModelUuid, getActionByUuid } = useProductStore(); + const { getModelUuidByPointUuid, getPointByUuid, getEventByModelUuid, getActionByUuid, getTriggerByUuid, getActionByPointUuid } = useProductStore(); const { selectedProduct } = useSelectedProduct(); const { speed } = useAnimationPlaySpeed(); const { triggerPointActions } = useTriggerHandler(); @@ -84,13 +84,45 @@ function MaterialInstance({ material }: { material: MaterialSchema }) { useEffect(() => { // console.log('material: ', material); + if (material.current && material.next) { + // console.log('current: ', material.current.pointUuid); + // console.log('next: ', material.next.pointUuid); + } }, [material]) const callTrigger = () => { - const action = getActionByUuid(selectedProduct.productId, material.current.actionUuid) - if (action) { - triggerPointActions(action); + if (!material.next) return; + const fromModel = getEventByModelUuid(selectedProduct.productId, material.next.modelUuid); + if (!fromModel) return; + const fromPoint = getPointByUuid(selectedProduct.productId, fromModel.modelUuid, material.next.pointUuid); + if (!fromPoint) return; + + if (fromModel.type === 'transfer') { + const toModel = getEventByModelUuid(selectedProduct.productId, material.next.modelUuid); + if (!toModel) return; + if (toModel.type === 'transfer') { + const action = getActionByPointUuid(selectedProduct.productId, material.next.pointUuid); + if (action) { + triggerPointActions(action); + } + } else if (toModel?.type === 'vehicle') { + // Transfer to Vehicle + + } else if (toModel?.type === 'machine') { + // Transfer to Machine + + } else if (toModel?.type === 'roboticArm') { + // Transfer to Robotic Arm + + } else if (toModel?.type === 'storageUnit') { + // Transfer to Storage Unit + } + } else if (fromModel.type === 'vehicle') { + } else if (fromModel.type === 'machine') { + } else if (fromModel.type === 'roboticArm') { + } else if (fromModel.type === 'storageUnit') { } + } return ( diff --git a/app/src/modules/simulation/materials/instances/materialInstances.tsx b/app/src/modules/simulation/materials/instances/materialInstances.tsx index 199e604..1864f0f 100644 --- a/app/src/modules/simulation/materials/instances/materialInstances.tsx +++ b/app/src/modules/simulation/materials/instances/materialInstances.tsx @@ -6,7 +6,7 @@ function MaterialInstances() { const { materials } = useMaterialStore(); useEffect(() => { - console.log('materials: ', materials); + // console.log('materials: ', materials); }, [materials]) return ( diff --git a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx index 21bbc7e..8cb0d13 100644 --- a/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx +++ b/app/src/modules/simulation/roboticArm/instances/armInstance/roboticArmInstance.tsx @@ -138,7 +138,7 @@ function RoboticArmInstance({ armBot }: { armBot: ArmBotStatus }) { else if (armBot && !armBot.isActive && armBot.state === "idle" && currentPhase === "rest" && !armBot.currentAction) { logStatus(armBot.modelUuid, "Waiting to trigger CurrentAction") const timeoutId = setTimeout(() => { - addCurrentAction(armBot.modelUuid, armBot.point.actions[0].actionUuid); + addCurrentAction(armBot.modelUuid, armBot.point.actions[0].actionUuid, 'Default material'); }, 3000); return () => clearTimeout(timeoutId); } diff --git a/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts b/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts index 7541522..22d5cfd 100644 --- a/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts +++ b/app/src/modules/simulation/triggers/triggerHandler/useTriggerHandler.ts @@ -10,119 +10,116 @@ export function useTriggerHandler() { const { getMaterialByCurrentModelUuid, setCurrentLocation, setNextLocation } = useMaterialStore(); const { selectedProduct } = useSelectedProduct(); - const handleTrigger = (trigger: TriggerSchema, actionUuid: string) => { + const handleTrigger = (trigger: TriggerSchema, action: Action) => { - // const fromEvent = getEventByTriggerUuid(selectedProduct.productId, trigger.triggerUuid); - // console.log('fromEvent: ', fromEvent); + const fromEvent = getEventByTriggerUuid(selectedProduct.productId, trigger.triggerUuid); - // const toEvent = getEventByModelUuid(selectedProduct.productId, trigger.triggeredAsset?.triggeredModel.modelUuid || ''); - // console.log('toEvent: ', toEvent); + const toEvent = getEventByModelUuid(selectedProduct.productId, trigger.triggeredAsset?.triggeredModel.modelUuid || ''); - // if (fromEvent?.type === 'transfer') { - // if (toEvent?.type === 'transfer') { - // // console.log('toEvent: ', toEvent.type); - // // Transfer to Transfer - // const action = getActionByUuid(selectedProduct.productId, trigger.triggeredAsset?.triggeredAction?.actionUuid || ''); - // if (action && trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) { - // const material = getMaterialByCurrentModelUuid(fromEvent.modelUuid); - // if (material) { - // if (material.next && - // action.triggers[0].triggeredAsset?.triggeredAction?.actionUuid && - // action.triggers[0].triggeredAsset?.triggeredPoint?.pointUuid) { + if (fromEvent?.type === 'transfer') { + if (toEvent?.type === 'transfer') { + // Transfer to Transfer + if (trigger.triggeredAsset && trigger.triggeredAsset.triggeredPoint && trigger.triggeredAsset.triggeredAction) { + const material = getMaterialByCurrentModelUuid(fromEvent.modelUuid); + if (material) { + if (material.next) { - // setCurrentLocation(material.materialId, material.next); + setCurrentLocation(material.materialId, { + modelUuid: material.next.modelUuid, + pointUuid: material.next.pointUuid, + actionUuid: trigger.triggeredAsset?.triggeredAction?.actionUuid, + }); - // setNextLocation(material.materialId, { - // modelUuid: toEvent.modelUuid, - // pointUuid: action.triggers[0].triggeredAsset?.triggeredPoint?.pointUuid, - // actionUuid: action.triggers[0].triggeredAsset?.triggeredAction?.actionUuid - // }); - // } - // handleAction(action); - // } - // } - // } else if (toEvent?.type === 'vehicle') { - // // Transfer to Vehicle + setNextLocation(material.materialId, { + modelUuid: trigger.triggeredAsset.triggeredModel.modelUuid, + pointUuid: trigger.triggeredAsset.triggeredPoint.pointUuid, + }); + } + handleAction(action); + } + } + } else if (toEvent?.type === 'vehicle') { + // Transfer to Vehicle - // } else if (toEvent?.type === 'machine') { - // // Transfer to Machine + } else if (toEvent?.type === 'machine') { + // Transfer to Machine - // } else if (toEvent?.type === 'roboticArm') { - // // Transfer to Robotic Arm + } else if (toEvent?.type === 'roboticArm') { + // Transfer to Robotic Arm - // } else if (toEvent?.type === 'storageUnit') { - // // Transfer to Storage Unit + } else if (toEvent?.type === 'storageUnit') { + // Transfer to Storage Unit - // } - // } else if (fromEvent?.type === 'vehicle') { - // if (toEvent?.type === 'transfer') { - // // Vehicle to Transfer + } + } else if (fromEvent?.type === 'vehicle') { + if (toEvent?.type === 'transfer') { + // Vehicle to Transfer - // } else if (toEvent?.type === 'vehicle') { - // // Vehicle to Vehicle + } else if (toEvent?.type === 'vehicle') { + // Vehicle to Vehicle - // } else if (toEvent?.type === 'machine') { - // // Vehicle to Machine + } else if (toEvent?.type === 'machine') { + // Vehicle to Machine - // } else if (toEvent?.type === 'roboticArm') { - // // Vehicle to Robotic Arm + } else if (toEvent?.type === 'roboticArm') { + // Vehicle to Robotic Arm - // } else if (toEvent?.type === 'storageUnit') { - // // Vehicle to Storage Unit + } else if (toEvent?.type === 'storageUnit') { + // Vehicle to Storage Unit - // } - // } else if (fromEvent?.type === 'machine') { - // if (toEvent?.type === 'transfer') { - // // Machine to Transfer + } + } else if (fromEvent?.type === 'machine') { + if (toEvent?.type === 'transfer') { + // Machine to Transfer - // } else if (toEvent?.type === 'vehicle') { - // // Machine to Vehicle + } else if (toEvent?.type === 'vehicle') { + // Machine to Vehicle - // } else if (toEvent?.type === 'machine') { - // // Machine to Machine + } else if (toEvent?.type === 'machine') { + // Machine to Machine - // } else if (toEvent?.type === 'roboticArm') { - // // Machine to Robotic Arm + } else if (toEvent?.type === 'roboticArm') { + // Machine to Robotic Arm - // } else if (toEvent?.type === 'storageUnit') { - // // Machine to Storage Unit + } else if (toEvent?.type === 'storageUnit') { + // Machine to Storage Unit - // } - // } else if (fromEvent?.type === 'roboticArm') { - // if (toEvent?.type === 'transfer') { - // // Robotic Arm to Transfer + } + } else if (fromEvent?.type === 'roboticArm') { + if (toEvent?.type === 'transfer') { + // Robotic Arm to Transfer - // } else if (toEvent?.type === 'vehicle') { - // // Robotic Arm to Vehicle + } else if (toEvent?.type === 'vehicle') { + // Robotic Arm to Vehicle - // } else if (toEvent?.type === 'machine') { - // // Robotic Arm to Machine + } else if (toEvent?.type === 'machine') { + // Robotic Arm to Machine - // } else if (toEvent?.type === 'roboticArm') { - // // Robotic Arm to Robotic Arm + } else if (toEvent?.type === 'roboticArm') { + // Robotic Arm to Robotic Arm - // } else if (toEvent?.type === 'storageUnit') { - // // Robotic Arm to Storage Unit + } else if (toEvent?.type === 'storageUnit') { + // Robotic Arm to Storage Unit - // } - // } else if (fromEvent?.type === 'storageUnit') { - // if (toEvent?.type === 'transfer') { - // // Storage Unit to Transfer + } + } else if (fromEvent?.type === 'storageUnit') { + if (toEvent?.type === 'transfer') { + // Storage Unit to Transfer - // } else if (toEvent?.type === 'vehicle') { - // // Storage Unit to Vehicle + } else if (toEvent?.type === 'vehicle') { + // Storage Unit to Vehicle - // } else if (toEvent?.type === 'machine') { - // // Storage Unit to Machine + } else if (toEvent?.type === 'machine') { + // Storage Unit to Machine - // } else if (toEvent?.type === 'roboticArm') { - // // Storage Unit to Robotic Arm + } else if (toEvent?.type === 'roboticArm') { + // Storage Unit to Robotic Arm - // } else if (toEvent?.type === 'storageUnit') { - // // Storage Unit to Storage Unit + } else if (toEvent?.type === 'storageUnit') { + // Storage Unit to Storage Unit - // } - // } + } + } } const triggerPointActions = useCallback((action: Action) => { @@ -133,7 +130,7 @@ export function useTriggerHandler() { case 'onStart': break; case 'onComplete': - handleTrigger(trigger, action.actionUuid); + handleTrigger(trigger, action); break; case 'onStop': break; diff --git a/app/src/store/simulation/useArmBotStore.ts b/app/src/store/simulation/useArmBotStore.ts index 79ad835..bd81e61 100644 --- a/app/src/store/simulation/useArmBotStore.ts +++ b/app/src/store/simulation/useArmBotStore.ts @@ -12,7 +12,7 @@ interface ArmBotStore { ) => void; clearArmBots: () => void; - addCurrentAction: (modelUuid: string, actionUuid: string) => void; + addCurrentAction: (modelUuid: string, actionUuid: string, materialType: string) => void; removeCurrentAction: (modelUuid: string) => void; addAction: (modelUuid: string, action: RoboticArmPointSchema['actions'][number]) => void; @@ -75,7 +75,7 @@ export const useArmBotStore = create()( }); }, - addCurrentAction: (modelUuid, actionUuid) => { + addCurrentAction: (modelUuid, actionUuid, materialType) => { set((state) => { const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { @@ -84,7 +84,7 @@ export const useArmBotStore = create()( armBot.currentAction = { actionUuid: action.actionUuid, actionName: action.actionName, - materialType: null + materialType: materialType }; } } diff --git a/app/src/store/simulation/useMaterialStore.ts b/app/src/store/simulation/useMaterialStore.ts index ec63ff3..20476d0 100644 --- a/app/src/store/simulation/useMaterialStore.ts +++ b/app/src/store/simulation/useMaterialStore.ts @@ -23,10 +23,10 @@ type MaterialsStore = { location?: { modelUuid: string; pointUuid: string; - actionUuid: string; } | null ) => MaterialSchema | undefined; + setMaterial: (materialId: string, materialType: string) => MaterialSchema | undefined; setStartTime: (materialId: string, startTime: string) => MaterialSchema | undefined; setEndTime: (materialId: string, endTime: string) => MaterialSchema | undefined; setCost: (materialId: string, cost: number) => MaterialSchema | undefined; @@ -37,6 +37,7 @@ type MaterialsStore = { getMaterialById: (materialId: string) => MaterialSchema | undefined; getMaterialByCurrentModelUuid: (currentModelUuid: string) => MaterialSchema | undefined; + getMaterialByCurrentPointUuid: (currentPointUuid: string) => MaterialSchema | undefined; getMaterialsByPoint: (pointUuid: string) => MaterialSchema[]; getMaterialsByModel: (modelUuid: string) => MaterialSchema[]; }; @@ -107,6 +108,18 @@ export const useMaterialStore = create()( return updatedMaterial; }, + setMaterial: (materialId, materialType) => { + let updatedMaterial: MaterialSchema | undefined; + set((state) => { + const material = state.materials.find(m => m.materialId === materialId); + if (material) { + material.materialType = materialType; + updatedMaterial = JSON.parse(JSON.stringify(material)); + }; + }); + return updatedMaterial; + }, + setStartTime: (materialId, startTime) => { let updatedMaterial: MaterialSchema | undefined; set((state) => { @@ -198,6 +211,10 @@ export const useMaterialStore = create()( getMaterialByCurrentModelUuid: (currentModelUuid) => { return get().materials.find(m => m.current?.modelUuid === currentModelUuid); }, + + getMaterialByCurrentPointUuid: (currentPointlUuid) => { + return get().materials.find(m => m.current?.pointUuid === currentPointlUuid); + }, getMaterialsByPoint: (pointUuid) => { return get().materials.filter(m => diff --git a/app/src/store/simulation/useProductStore.ts b/app/src/store/simulation/useProductStore.ts index cdbc26c..b90fd7d 100644 --- a/app/src/store/simulation/useProductStore.ts +++ b/app/src/store/simulation/useProductStore.ts @@ -65,6 +65,7 @@ type ProductsStore = { getEventByPointUuid: (productId: string, pointUuid: string) => EventsSchema | undefined; getPointByUuid: (productId: string, modelUuid: string, pointUuid: string) => ConveyorPointSchema | VehiclePointSchema | RoboticArmPointSchema | MachinePointSchema | StoragePointSchema | undefined; getActionByUuid: (productId: string, actionUuid: string) => (ConveyorPointSchema['action'] | VehiclePointSchema['action'] | RoboticArmPointSchema['actions'][0] | MachinePointSchema['action'] | StoragePointSchema['action']) | undefined; + getActionByPointUuid: (productId: string, pointUuid: string) => (ConveyorPointSchema['action'] | VehiclePointSchema['action'] | RoboticArmPointSchema['actions'][0] | MachinePointSchema['action'] | StoragePointSchema['action']) | undefined; getModelUuidByPointUuid: (productId: string, actionUuid: string) => (string) | undefined; getModelUuidByActionUuid: (productId: string, actionUuid: string) => (string) | undefined; getPointUuidByActionUuid: (productId: string, actionUuid: string) => (string) | undefined; @@ -545,7 +546,7 @@ export const useProductStore = create()( getEventByTriggerUuid: (productId, triggerUuid) => { 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) { @@ -574,7 +575,7 @@ export const useProductStore = create()( getEventByPointUuid: (productId, pointUuid) => { const product = get().getProductById(productId); if (!product) return undefined; - + for (const event of product.eventDatas) { if ('points' in event) { if ((event as ConveyorEventSchema).points.some(p => p.uuid === pointUuid)) { @@ -625,6 +626,27 @@ export const useProductStore = create()( return undefined; }, + getActionByPointUuid: (productId, pointUuid) => { + const product = get().products.find(p => p.productId === 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.uuid === pointUuid) { + return point.action; + } + } + } else if ('point' in event) { + const point = (event as any).point; + if (point.uuid === pointUuid) { + return point.action; + } + } + } + return undefined; + }, + getModelUuidByPointUuid: (productId, pointUuid) => { const product = get().products.find(p => p.productId === productId); if (!product) return undefined; diff --git a/app/src/types/simulationTypes.d.ts b/app/src/types/simulationTypes.d.ts index f863c1e..c313db1 100644 --- a/app/src/types/simulationTypes.d.ts +++ b/app/src/types/simulationTypes.d.ts @@ -125,7 +125,6 @@ interface StorageAction { actionUuid: string; actionName: string; actionType: "store"; - materials: { materialName: string; materialId: string; }[]; storageCapacity: number; triggers: TriggerSchema[]; } @@ -190,6 +189,7 @@ interface StorageUnitStatus extends StorageEventSchema { idleTime: number; activeTime: number; currentLoad: number; + materials?: { materialName: string; materialId: string; }[]; } interface MaterialSchema { @@ -213,7 +213,6 @@ interface MaterialSchema { next?: { modelUuid: string; pointUuid: string; - actionUuid: string; }; }