first commit
This commit is contained in:
14
app/src/modules/simulation/conveyor/conveyor.tsx
Normal file
14
app/src/modules/simulation/conveyor/conveyor.tsx
Normal file
@@ -0,0 +1,14 @@
|
||||
import React from 'react'
|
||||
import ConveyorInstances from './instances/conveyorInstances'
|
||||
|
||||
function Conveyor() {
|
||||
return (
|
||||
<>
|
||||
|
||||
<ConveyorInstances />
|
||||
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default Conveyor
|
||||
@@ -0,0 +1,76 @@
|
||||
import { useEffect, useRef } from 'react';
|
||||
import { useFrame } from '@react-three/fiber';
|
||||
import { usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../store/usePlayButtonStore';
|
||||
import { useSceneContext } from '../../../scene/sceneContext';
|
||||
|
||||
type ConveyorCallback = {
|
||||
conveyorId: string;
|
||||
callback: () => void;
|
||||
};
|
||||
|
||||
export function useConveyorEventManager() {
|
||||
const { conveyorStore } = useSceneContext();
|
||||
const { getConveyorById } = conveyorStore();
|
||||
const callbacksRef = useRef<ConveyorCallback[]>([]);
|
||||
const isMonitoringRef = useRef(false);
|
||||
const { isPlaying } = usePlayButtonStore();
|
||||
const { isPaused } = usePauseButtonStore();
|
||||
const { isReset } = useResetButtonStore();
|
||||
|
||||
useEffect(() => {
|
||||
if (isReset) {
|
||||
callbacksRef.current = [];
|
||||
}
|
||||
}, [isReset])
|
||||
|
||||
// Add a new conveyor to monitor
|
||||
const addConveyorToMonitor = (conveyorId: string, callback: () => void) => {
|
||||
// Avoid duplicates
|
||||
if (!callbacksRef.current.some((entry) => entry.conveyorId === conveyorId)) {
|
||||
callbacksRef.current.push({ conveyorId, callback });
|
||||
}
|
||||
|
||||
// Start monitoring if not already running
|
||||
if (!isMonitoringRef.current) {
|
||||
isMonitoringRef.current = true;
|
||||
}
|
||||
};
|
||||
|
||||
// Remove a conveyor from monitoring
|
||||
const removeConveyorFromMonitor = (conveyorId: string) => {
|
||||
callbacksRef.current = callbacksRef.current.filter(
|
||||
(entry) => entry.conveyorId !== conveyorId
|
||||
);
|
||||
|
||||
// Stop monitoring if no more conveyors to track
|
||||
if (callbacksRef.current.length === 0) {
|
||||
isMonitoringRef.current = false;
|
||||
}
|
||||
};
|
||||
|
||||
// Check conveyor states every frame
|
||||
useFrame(() => {
|
||||
if (!isMonitoringRef.current || callbacksRef.current.length === 0 || !isPlaying || isPaused) return;
|
||||
|
||||
callbacksRef.current.forEach(({ conveyorId, callback }) => {
|
||||
const conveyor = getConveyorById(conveyorId);
|
||||
if (conveyor?.isPaused === false) {
|
||||
callback();
|
||||
removeConveyorFromMonitor(conveyorId); // Remove after triggering
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// Cleanup on unmount
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
callbacksRef.current = [];
|
||||
isMonitoringRef.current = false;
|
||||
};
|
||||
}, []);
|
||||
|
||||
return {
|
||||
addConveyorToMonitor,
|
||||
removeConveyorFromMonitor,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { useEffect } from 'react'
|
||||
import { useResetButtonStore } from '../../../../../store/usePlayButtonStore';
|
||||
import { useProductStore } from '../../../../../store/simulation/useProductStore';
|
||||
import { useSceneContext } from '../../../../scene/sceneContext';
|
||||
import { useProductContext } from '../../../products/productContext';
|
||||
// import { findConveyorSubsequence } from '../../../simulator/functions/getConveyorSequencesInProduct';
|
||||
|
||||
function ConveyorInstance({ conveyor }: { readonly conveyor: ConveyorStatus }) {
|
||||
const { getProductById } = useProductStore();
|
||||
const { selectedProductStore } = useProductContext();
|
||||
const { selectedProduct } = selectedProductStore();
|
||||
const { materialStore, conveyorStore } = useSceneContext();
|
||||
const { getMaterialsByCurrentModelUuid, materials } = materialStore();
|
||||
const { isReset } = useResetButtonStore();
|
||||
const { setConveyorPaused } = conveyorStore();
|
||||
|
||||
useEffect(() => {
|
||||
const product = getProductById(selectedProduct.productUuid);
|
||||
if (!product) return;
|
||||
|
||||
const conveyorMaterials = getMaterialsByCurrentModelUuid(conveyor.modelUuid);
|
||||
if (conveyorMaterials && conveyorMaterials?.length > 0) {
|
||||
|
||||
const hasPausedMaterials = conveyorMaterials.some(material => material.isPaused);
|
||||
|
||||
if (hasPausedMaterials) {
|
||||
setConveyorPaused(conveyor.modelUuid, true);
|
||||
} else {
|
||||
setConveyorPaused(conveyor.modelUuid, false);
|
||||
}
|
||||
} else {
|
||||
setConveyorPaused(conveyor.modelUuid, false);
|
||||
}
|
||||
|
||||
// const conveyorSubsequence = findConveyorSubsequence(product, conveyor.modelUuid);
|
||||
|
||||
// if (!conveyorSubsequence || !conveyorSubsequence.currentSubSequence) {
|
||||
// setConveyorPaused(conveyor.modelUuid, false);
|
||||
// return;
|
||||
// }
|
||||
|
||||
// const { currentSubSequence } = conveyorSubsequence;
|
||||
|
||||
// const allMaterials = currentSubSequence.flatMap(event =>
|
||||
// getMaterialsByCurrentModelUuid(event.modelUuid)
|
||||
// );
|
||||
|
||||
// const hasPausedMaterials = allMaterials.some(mat => mat?.isPaused);
|
||||
|
||||
// currentSubSequence.forEach(event => {
|
||||
// if (event.type === 'transfer') {
|
||||
// setConveyorPaused(event.modelUuid, hasPausedMaterials);
|
||||
// }
|
||||
// });
|
||||
|
||||
}, [materials, conveyor.modelUuid, getMaterialsByCurrentModelUuid, setConveyorPaused, isReset, selectedProduct.productUuid, getProductById]);
|
||||
|
||||
useEffect(() => {
|
||||
// console.log('conveyor: ', conveyor);
|
||||
}, [conveyor])
|
||||
|
||||
return (
|
||||
<>
|
||||
</>
|
||||
)
|
||||
};
|
||||
|
||||
export default ConveyorInstance
|
||||
@@ -0,0 +1,21 @@
|
||||
import React from 'react'
|
||||
import ConveyorInstance from './conveyorInstance/conveyorInstance'
|
||||
import { useSceneContext } from '../../../scene/sceneContext';
|
||||
|
||||
function ConveyorInstances() {
|
||||
|
||||
const { conveyorStore } = useSceneContext();
|
||||
const { conveyors } = conveyorStore();
|
||||
|
||||
return (
|
||||
<>
|
||||
|
||||
{conveyors.map((conveyor: ConveyorStatus) =>
|
||||
<ConveyorInstance conveyor={conveyor} key={conveyor.modelUuid} />
|
||||
)}
|
||||
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default ConveyorInstances
|
||||
Reference in New Issue
Block a user