simulation #66
@@ -150,6 +150,7 @@ const ArmBotMechanics: React.FC = () => {
|
|||||||
modeluuid: updatedPath.modeluuid,
|
modeluuid: updatedPath.modeluuid,
|
||||||
eventData: { type: "ArmBot", points: updatedPath.points }
|
eventData: { type: "ArmBot", points: updatedPath.points }
|
||||||
}
|
}
|
||||||
|
console.log('data: ', data);
|
||||||
|
|
||||||
socket.emit('v2:model-asset:updateEventData', data);
|
socket.emit('v2:model-asset:updateEventData', data);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,7 +3,10 @@ import * as THREE from "three";
|
|||||||
import { useFrame, useThree } from "@react-three/fiber";
|
import { useFrame, useThree } from "@react-three/fiber";
|
||||||
import { NavMeshQuery } from "@recast-navigation/core";
|
import { NavMeshQuery } from "@recast-navigation/core";
|
||||||
import { Line } from "@react-three/drei";
|
import { Line } from "@react-three/drei";
|
||||||
import { useAnimationPlaySpeed, usePlayButtonStore } from "../../../store/usePlayButtonStore";
|
import {
|
||||||
|
useAnimationPlaySpeed,
|
||||||
|
usePlayButtonStore,
|
||||||
|
} from "../../../store/usePlayButtonStore";
|
||||||
import { usePlayAgv } from "../../../store/store";
|
import { usePlayAgv } from "../../../store/store";
|
||||||
|
|
||||||
interface PathNavigatorProps {
|
interface PathNavigatorProps {
|
||||||
@@ -11,7 +14,7 @@ interface PathNavigatorProps {
|
|||||||
pathPoints: any;
|
pathPoints: any;
|
||||||
id: string;
|
id: string;
|
||||||
speed: number;
|
speed: number;
|
||||||
globalSpeed: number,
|
globalSpeed: number;
|
||||||
bufferTime: number;
|
bufferTime: number;
|
||||||
hitCount: number;
|
hitCount: number;
|
||||||
processes: any[];
|
processes: any[];
|
||||||
@@ -40,11 +43,21 @@ export default function PathNavigator({
|
|||||||
}: PathNavigatorProps) {
|
}: PathNavigatorProps) {
|
||||||
const [currentPhase, setCurrentPhase] = useState<Phase>("initial");
|
const [currentPhase, setCurrentPhase] = useState<Phase>("initial");
|
||||||
const [path, setPath] = useState<[number, number, number][]>([]);
|
const [path, setPath] = useState<[number, number, number][]>([]);
|
||||||
const [toPickupPath, setToPickupPath] = useState<[number, number, number][]>([]);
|
const [toPickupPath, setToPickupPath] = useState<[number, number, number][]>(
|
||||||
const [pickupDropPath, setPickupDropPath] = useState<[number, number, number][]>([]);
|
[]
|
||||||
const [dropPickupPath, setDropPickupPath] = useState<[number, number, number][]>([]);
|
);
|
||||||
const [initialPosition, setInitialPosition] = useState<THREE.Vector3 | null>(null);
|
const [pickupDropPath, setPickupDropPath] = useState<
|
||||||
const [initialRotation, setInitialRotation] = useState<THREE.Euler | null>(null);
|
[number, number, number][]
|
||||||
|
>([]);
|
||||||
|
const [dropPickupPath, setDropPickupPath] = useState<
|
||||||
|
[number, number, number][]
|
||||||
|
>([]);
|
||||||
|
const [initialPosition, setInitialPosition] = useState<THREE.Vector3 | null>(
|
||||||
|
null
|
||||||
|
);
|
||||||
|
const [initialRotation, setInitialRotation] = useState<THREE.Euler | null>(
|
||||||
|
null
|
||||||
|
);
|
||||||
const [boxVisible, setBoxVisible] = useState(false);
|
const [boxVisible, setBoxVisible] = useState(false);
|
||||||
|
|
||||||
const distancesRef = useRef<number[]>([]);
|
const distancesRef = useRef<number[]>([]);
|
||||||
@@ -61,11 +74,14 @@ export default function PathNavigator({
|
|||||||
|
|
||||||
const boxRef = useRef<THREE.Mesh | null>(null);
|
const boxRef = useRef<THREE.Mesh | null>(null);
|
||||||
|
|
||||||
const baseMaterials = useMemo(() => ({
|
const baseMaterials = useMemo(
|
||||||
|
() => ({
|
||||||
Box: new THREE.MeshStandardMaterial({ color: 0x8b4513 }),
|
Box: new THREE.MeshStandardMaterial({ color: 0x8b4513 }),
|
||||||
Crate: new THREE.MeshStandardMaterial({ color: 0x00ff00 }),
|
Crate: new THREE.MeshStandardMaterial({ color: 0x00ff00 }),
|
||||||
Default: new THREE.MeshStandardMaterial({ color: 0xcccccc })
|
Default: new THREE.MeshStandardMaterial({ color: 0xcccccc }),
|
||||||
}), []);
|
}),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const object = scene.getObjectByProperty("uuid", id);
|
const object = scene.getObjectByProperty("uuid", id);
|
||||||
@@ -135,7 +151,11 @@ export default function PathNavigator({
|
|||||||
const pickupToDropPath = computePath(pickup, drop);
|
const pickupToDropPath = computePath(pickup, drop);
|
||||||
const dropToPickupPath = computePath(drop, pickup);
|
const dropToPickupPath = computePath(drop, pickup);
|
||||||
|
|
||||||
if (toPickupPath.length && pickupToDropPath.length && dropToPickupPath.length) {
|
if (
|
||||||
|
toPickupPath.length &&
|
||||||
|
pickupToDropPath.length &&
|
||||||
|
dropToPickupPath.length
|
||||||
|
) {
|
||||||
setPickupDropPath(pickupToDropPath);
|
setPickupDropPath(pickupToDropPath);
|
||||||
setDropPickupPath(dropToPickupPath);
|
setDropPickupPath(dropToPickupPath);
|
||||||
setToPickupPath(toPickupPath);
|
setToPickupPath(toPickupPath);
|
||||||
@@ -163,7 +183,10 @@ export default function PathNavigator({
|
|||||||
}, [path]);
|
}, [path]);
|
||||||
|
|
||||||
function logAgvStatus(id: string, status: string) {
|
function logAgvStatus(id: string, status: string) {
|
||||||
// console.log(`AGV ${id}: ${status}`);
|
// console.log(
|
||||||
|
// `AGV ${id}: ${status}`
|
||||||
|
|
||||||
|
// );
|
||||||
}
|
}
|
||||||
|
|
||||||
function findProcessByTargetModelUUID(processes: any, targetModelUUID: any) {
|
function findProcessByTargetModelUUID(processes: any, targetModelUUID: any) {
|
||||||
@@ -223,7 +246,9 @@ export default function PathNavigator({
|
|||||||
}, [processes, MaterialRef, boxVisible, scene, id, baseMaterials]);
|
}, [processes, MaterialRef, boxVisible, scene, id, baseMaterials]);
|
||||||
|
|
||||||
useFrame((_, delta) => {
|
useFrame((_, delta) => {
|
||||||
const currentAgv = (agvRef.current || []).find((agv: AGVData) => agv.vehicleId === id);
|
const currentAgv = (agvRef.current || []).find(
|
||||||
|
(agv: AGVData) => agv.vehicleId === id
|
||||||
|
);
|
||||||
|
|
||||||
if (!scene || !id || !isPlaying) return;
|
if (!scene || !id || !isPlaying) return;
|
||||||
|
|
||||||
@@ -243,6 +268,7 @@ export default function PathNavigator({
|
|||||||
const isAgvReady = () => {
|
const isAgvReady = () => {
|
||||||
if (!agvRef.current || agvRef.current.length === 0) return false;
|
if (!agvRef.current || agvRef.current.length === 0) return false;
|
||||||
if (!currentAgv) return false;
|
if (!currentAgv) return false;
|
||||||
|
|
||||||
return currentAgv.isActive && hitCount >= currentAgv.maxHitCount;
|
return currentAgv.isActive && hitCount >= currentAgv.maxHitCount;
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -266,12 +292,19 @@ export default function PathNavigator({
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isPlaying && currentPhase === "initial" && !hasReachedPickup.current) {
|
if (isPlaying && currentPhase === "initial" && !hasReachedPickup.current) {
|
||||||
const reached = moveAlongPath(object, path, distancesRef.current, speed, delta, progressRef);
|
const reached = moveAlongPath(
|
||||||
|
object,
|
||||||
|
path,
|
||||||
|
distancesRef.current,
|
||||||
|
speed,
|
||||||
|
delta,
|
||||||
|
progressRef
|
||||||
|
);
|
||||||
|
|
||||||
if (reached) {
|
if (reached) {
|
||||||
hasReachedPickup.current = true;
|
hasReachedPickup.current = true;
|
||||||
if (currentAgv) {
|
if (currentAgv) {
|
||||||
currentAgv.status = 'picking';
|
currentAgv.status = "picking";
|
||||||
}
|
}
|
||||||
logAgvStatus(id, "Reached pickup point, Waiting for material");
|
logAgvStatus(id, "Reached pickup point, Waiting for material");
|
||||||
}
|
}
|
||||||
@@ -287,20 +320,28 @@ export default function PathNavigator({
|
|||||||
progressRef.current = 0;
|
progressRef.current = 0;
|
||||||
logAgvStatus(id, "Started from pickup point, heading to drop point");
|
logAgvStatus(id, "Started from pickup point, heading to drop point");
|
||||||
if (currentAgv) {
|
if (currentAgv) {
|
||||||
currentAgv.status = 'toDrop';
|
currentAgv.status = "toDrop";
|
||||||
}
|
}
|
||||||
}, 0)
|
}, 0);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPlaying && currentPhase === "toDrop") {
|
if (isPlaying && currentPhase === "toDrop") {
|
||||||
const reached = moveAlongPath(object, path, distancesRef.current, speed, delta, progressRef);
|
const reached = moveAlongPath(
|
||||||
|
object,
|
||||||
|
path,
|
||||||
|
distancesRef.current,
|
||||||
|
speed,
|
||||||
|
delta,
|
||||||
|
progressRef
|
||||||
|
);
|
||||||
|
|
||||||
if (reached && !isWaiting.current) {
|
if (reached && !isWaiting.current) {
|
||||||
isWaiting.current = true;
|
isWaiting.current = true;
|
||||||
logAgvStatus(id, "Reached drop point");
|
logAgvStatus(id, "Reached drop point");
|
||||||
if (currentAgv) {
|
if (currentAgv) {
|
||||||
currentAgv.status = 'droping';
|
currentAgv.status = "droping";
|
||||||
|
currentAgv.hitCount = currentAgv.hitCount--;
|
||||||
}
|
}
|
||||||
timeoutRef.current = setTimeout(() => {
|
timeoutRef.current = setTimeout(() => {
|
||||||
setPath([...dropPickupPath]);
|
setPath([...dropPickupPath]);
|
||||||
@@ -309,16 +350,26 @@ export default function PathNavigator({
|
|||||||
isWaiting.current = false;
|
isWaiting.current = false;
|
||||||
setBoxVisible(false);
|
setBoxVisible(false);
|
||||||
if (currentAgv) {
|
if (currentAgv) {
|
||||||
currentAgv.status = 'toPickup';
|
currentAgv.status = "toPickup";
|
||||||
}
|
}
|
||||||
logAgvStatus(id, "Started from droping point, heading to pickup point");
|
logAgvStatus(
|
||||||
|
id,
|
||||||
|
"Started from droping point, heading to pickup point"
|
||||||
|
);
|
||||||
}, bufferTime * 1000);
|
}, bufferTime * 1000);
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isPlaying && currentPhase === "toPickup") {
|
if (isPlaying && currentPhase === "toPickup") {
|
||||||
const reached = moveAlongPath(object, path, distancesRef.current, speed, delta, progressRef);
|
const reached = moveAlongPath(
|
||||||
|
object,
|
||||||
|
path,
|
||||||
|
distancesRef.current,
|
||||||
|
speed,
|
||||||
|
delta,
|
||||||
|
progressRef
|
||||||
|
);
|
||||||
|
|
||||||
if (reached) {
|
if (reached) {
|
||||||
if (currentAgv) {
|
if (currentAgv) {
|
||||||
@@ -326,14 +377,21 @@ export default function PathNavigator({
|
|||||||
}
|
}
|
||||||
setCurrentPhase("initial");
|
setCurrentPhase("initial");
|
||||||
if (currentAgv) {
|
if (currentAgv) {
|
||||||
currentAgv.status = 'picking';
|
currentAgv.status = "picking";
|
||||||
}
|
}
|
||||||
logAgvStatus(id, "Reached pickup point again, cycle complete");
|
logAgvStatus(id, "Reached pickup point again, cycle complete");
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
moveAlongPath(object, path, distancesRef.current, speed, delta, progressRef);
|
moveAlongPath(
|
||||||
|
object,
|
||||||
|
path,
|
||||||
|
distancesRef.current,
|
||||||
|
speed,
|
||||||
|
delta,
|
||||||
|
progressRef
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
function moveAlongPath(
|
function moveAlongPath(
|
||||||
@@ -379,7 +437,11 @@ export default function PathNavigator({
|
|||||||
const targetRotationY = Math.atan2(targetDirection.x, targetDirection.z);
|
const targetRotationY = Math.atan2(targetDirection.x, targetDirection.z);
|
||||||
|
|
||||||
const rotationSpeed = Math.min(5 * delta, 1);
|
const rotationSpeed = Math.min(5 * delta, 1);
|
||||||
object.rotation.y = THREE.MathUtils.lerp(object.rotation.y, targetRotationY, rotationSpeed);
|
object.rotation.y = THREE.MathUtils.lerp(
|
||||||
|
object.rotation.y,
|
||||||
|
targetRotationY,
|
||||||
|
rotationSpeed
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -8,8 +8,7 @@ import crate from "../../../assets/gltf-glb/crate_box.glb";
|
|||||||
import { useProcessAnimation } from "./useProcessAnimations";
|
import { useProcessAnimation } from "./useProcessAnimations";
|
||||||
import ProcessObject from "./processObject";
|
import ProcessObject from "./processObject";
|
||||||
import { ProcessData } from "./types";
|
import { ProcessData } from "./types";
|
||||||
import { useSimulationStates } from "../../../store/store";
|
|
||||||
import { retrieveGLTF } from "../../../utils/indexDB/idbUtils";
|
|
||||||
|
|
||||||
interface ProcessContainerProps {
|
interface ProcessContainerProps {
|
||||||
processes: ProcessData[];
|
processes: ProcessData[];
|
||||||
@@ -44,19 +43,23 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
|
|||||||
checkAndCountTriggers,
|
checkAndCountTriggers,
|
||||||
} = useProcessAnimation(processes, setProcesses, agvRef);
|
} = useProcessAnimation(processes, setProcesses, agvRef);
|
||||||
|
|
||||||
const baseMaterials = useMemo(() => ({
|
const baseMaterials = useMemo(
|
||||||
|
() => ({
|
||||||
Box: new THREE.MeshStandardMaterial({ color: 0x8b4513 }),
|
Box: new THREE.MeshStandardMaterial({ color: 0x8b4513 }),
|
||||||
Crate: new THREE.MeshStandardMaterial({ color: 0x00ff00 }),
|
Crate: new THREE.MeshStandardMaterial({ color: 0x00ff00 }),
|
||||||
Default: new THREE.MeshStandardMaterial(),
|
Default: new THREE.MeshStandardMaterial(),
|
||||||
}), []);
|
}),
|
||||||
|
[]
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
// Update material references for all spawned objects
|
// Update material references for all spawned objects
|
||||||
Object.entries(animationStates).forEach(([processId, processState]) => {
|
Object.entries(animationStates).forEach(([processId, processState]) => {
|
||||||
Object.keys(processState.spawnedObjects).forEach((objectId) => {
|
Object.keys(processState.spawnedObjects).forEach((objectId) => {
|
||||||
const entry = { processId, objectId, };
|
const entry = { processId, objectId };
|
||||||
|
|
||||||
const materialType = processState.spawnedObjects[objectId]?.currentMaterialType;
|
const materialType =
|
||||||
|
processState.spawnedObjects[objectId]?.currentMaterialType;
|
||||||
|
|
||||||
if (!materialType) {
|
if (!materialType) {
|
||||||
return;
|
return;
|
||||||
@@ -65,13 +68,17 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
|
|||||||
const matRefArray = MaterialRef.current;
|
const matRefArray = MaterialRef.current;
|
||||||
|
|
||||||
// Find existing material group
|
// Find existing material group
|
||||||
const existing = matRefArray.find((entryGroup: { material: string; objects: any[] }) =>
|
const existing = matRefArray.find(
|
||||||
|
(entryGroup: { material: string; objects: any[] }) =>
|
||||||
entryGroup.material === materialType
|
entryGroup.material === materialType
|
||||||
);
|
);
|
||||||
|
|
||||||
if (existing) {
|
if (existing) {
|
||||||
// Check if this processId + objectId already exists
|
// Check if this processId + objectId already exists
|
||||||
const alreadyExists = existing.objects.some((o: any) => o.processId === entry.processId && o.objectId === entry.objectId);
|
const alreadyExists = existing.objects.some(
|
||||||
|
(o: any) =>
|
||||||
|
o.processId === entry.processId && o.objectId === entry.objectId
|
||||||
|
);
|
||||||
|
|
||||||
if (!alreadyExists) {
|
if (!alreadyExists) {
|
||||||
existing.objects.push(entry);
|
existing.objects.push(entry);
|
||||||
@@ -91,7 +98,8 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
|
|||||||
|
|
||||||
useFrame(() => {
|
useFrame(() => {
|
||||||
// Spawn logic frame
|
// Spawn logic frame
|
||||||
const currentTime = clockRef.current.getElapsedTime() - elapsedBeforePauseRef.current;
|
const currentTime =
|
||||||
|
clockRef.current.getElapsedTime() - elapsedBeforePauseRef.current;
|
||||||
|
|
||||||
setAnimationStates((prev) => {
|
setAnimationStates((prev) => {
|
||||||
const newStates = { ...prev };
|
const newStates = { ...prev };
|
||||||
@@ -119,7 +127,10 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
|
|||||||
: parseFloat(spawnAction.spawnInterval as string) || 0;
|
: parseFloat(spawnAction.spawnInterval as string) || 0;
|
||||||
|
|
||||||
// Check if this is a zero interval spawn and we already spawned an object
|
// Check if this is a zero interval spawn and we already spawned an object
|
||||||
if (spawnInterval === 0 && processState.hasSpawnedZeroIntervalObject === true) {
|
if (
|
||||||
|
spawnInterval === 0 &&
|
||||||
|
processState.hasSpawnedZeroIntervalObject === true
|
||||||
|
) {
|
||||||
return; // Don't spawn more objects for zero interval
|
return; // Don't spawn more objects for zero interval
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -324,10 +335,13 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
|
|||||||
|
|
||||||
if (isLastPoint) {
|
if (isLastPoint) {
|
||||||
const isAgvPicking = agvRef.current.some(
|
const isAgvPicking = agvRef.current.some(
|
||||||
(agv: any) => agv.processId === process.id && agv.status === "picking"
|
(agv: any) =>
|
||||||
|
agv.processId === process.id && agv.status === "picking"
|
||||||
);
|
);
|
||||||
|
|
||||||
const shouldHide = !currentPointData?.actions || !hasNonInheritActions(currentPointData.actions);
|
const shouldHide =
|
||||||
|
!currentPointData?.actions ||
|
||||||
|
!hasNonInheritActions(currentPointData.actions);
|
||||||
|
|
||||||
if (shouldHide) {
|
if (shouldHide) {
|
||||||
if (isAgvPicking) {
|
if (isAgvPicking) {
|
||||||
@@ -358,7 +372,8 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
|
|||||||
|
|
||||||
if (tempStackedObjectsRef.current[objectId]) {
|
if (tempStackedObjectsRef.current[objectId]) {
|
||||||
const isAgvPicking = agvRef.current.some(
|
const isAgvPicking = agvRef.current.some(
|
||||||
(agv: any) => agv.processId === process.id && agv.status === "picking"
|
(agv: any) =>
|
||||||
|
agv.processId === process.id && agv.status === "picking"
|
||||||
);
|
);
|
||||||
|
|
||||||
if (isAgvPicking) {
|
if (isAgvPicking) {
|
||||||
@@ -377,7 +392,8 @@ const ProcessAnimator: React.FC<ProcessContainerProps> = ({
|
|||||||
|
|
||||||
if (!isLastPoint) {
|
if (!isLastPoint) {
|
||||||
const nextPoint = path[nextPointIdx];
|
const nextPoint = path[nextPointIdx];
|
||||||
const distance = path[stateRef.currentIndex].distanceTo(nextPoint);
|
const distance =
|
||||||
|
path[stateRef.currentIndex].distanceTo(nextPoint);
|
||||||
const effectiveSpeed = stateRef.speed * speedRef.current;
|
const effectiveSpeed = stateRef.speed * speedRef.current;
|
||||||
const movement = effectiveSpeed * delta;
|
const movement = effectiveSpeed * delta;
|
||||||
|
|
||||||
|
|||||||
@@ -1,450 +1,3 @@
|
|||||||
// import React, {
|
|
||||||
// useEffect,
|
|
||||||
// useMemo,
|
|
||||||
// useState,
|
|
||||||
// useCallback,
|
|
||||||
// useRef,
|
|
||||||
// } from "react";
|
|
||||||
// import { useSimulationStates } from "../../../store/store";
|
|
||||||
// import * as THREE from "three";
|
|
||||||
// import { useThree } from "@react-three/fiber";
|
|
||||||
// import {
|
|
||||||
// ConveyorEventsSchema,
|
|
||||||
// VehicleEventsSchema,
|
|
||||||
// } from "../../../types/world/worldTypes";
|
|
||||||
|
|
||||||
// // Type definitions
|
|
||||||
// export interface PointAction {
|
|
||||||
// uuid: string;
|
|
||||||
// name: string;
|
|
||||||
// type: string;
|
|
||||||
// material: string;
|
|
||||||
// delay: number | string;
|
|
||||||
// spawnInterval: string | number;
|
|
||||||
// isUsed: boolean;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// export interface PointTrigger {
|
|
||||||
// uuid: string;
|
|
||||||
// bufferTime: number;
|
|
||||||
// name: string;
|
|
||||||
// type: string;
|
|
||||||
// isUsed: boolean;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// export interface PathPoint {
|
|
||||||
// uuid: string;
|
|
||||||
// position: [number, number, number];
|
|
||||||
// actions: PointAction[];
|
|
||||||
// triggers: PointTrigger[];
|
|
||||||
// connections: {
|
|
||||||
// targets: Array<{ modelUUID: string }>;
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// export interface SimulationPath {
|
|
||||||
// type: string;
|
|
||||||
// modeluuid: string;
|
|
||||||
// points: PathPoint[];
|
|
||||||
// pathPosition: [number, number, number];
|
|
||||||
// speed?: number;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// export interface Process {
|
|
||||||
// id: string;
|
|
||||||
// paths: SimulationPath[];
|
|
||||||
// animationPath: THREE.Vector3[];
|
|
||||||
// pointActions: PointAction[][];
|
|
||||||
// pointTriggers: PointTrigger[][];
|
|
||||||
// speed: number;
|
|
||||||
// isActive: boolean;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// interface ProcessCreatorProps {
|
|
||||||
// onProcessesCreated: (processes: Process[]) => void;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Convert event schemas to SimulationPath
|
|
||||||
// function convertToSimulationPath(
|
|
||||||
// path: ConveyorEventsSchema | VehicleEventsSchema
|
|
||||||
// ): SimulationPath {
|
|
||||||
// const { modeluuid } = path;
|
|
||||||
|
|
||||||
// // Normalized action handler
|
|
||||||
// const normalizeAction = (action: any): PointAction => {
|
|
||||||
// return { ...action }; // Return exact copy with no modifications
|
|
||||||
// };
|
|
||||||
|
|
||||||
// // Normalized trigger handler
|
|
||||||
// const normalizeTrigger = (trigger: any): PointTrigger => {
|
|
||||||
// return { ...trigger }; // Return exact copy with no modifications
|
|
||||||
// };
|
|
||||||
|
|
||||||
// if (path.type === "Conveyor") {
|
|
||||||
// return {
|
|
||||||
// type: path.type,
|
|
||||||
// modeluuid,
|
|
||||||
// points: path.points.map((point) => ({
|
|
||||||
// uuid: point.uuid,
|
|
||||||
// position: point.position,
|
|
||||||
// actions: Array.isArray(point.actions)
|
|
||||||
// ? point.actions.map(normalizeAction)
|
|
||||||
// : point.actions
|
|
||||||
// ? [normalizeAction(point.actions)]
|
|
||||||
// : [],
|
|
||||||
// triggers: Array.isArray(point.triggers)
|
|
||||||
// ? point.triggers.map(normalizeTrigger)
|
|
||||||
// : point.triggers
|
|
||||||
// ? [normalizeTrigger(point.triggers)]
|
|
||||||
// : [],
|
|
||||||
// connections: {
|
|
||||||
// targets: point.connections.targets.map((target) => ({
|
|
||||||
// modelUUID: target.modelUUID,
|
|
||||||
// })),
|
|
||||||
// },
|
|
||||||
// })),
|
|
||||||
// pathPosition: path.position,
|
|
||||||
// speed:
|
|
||||||
// typeof path.speed === "string"
|
|
||||||
// ? parseFloat(path.speed) || 1
|
|
||||||
// : path.speed || 1,
|
|
||||||
// };
|
|
||||||
// } else {
|
|
||||||
// // For vehicle paths, handle the case where triggers might not exist
|
|
||||||
// return {
|
|
||||||
// type: path.type,
|
|
||||||
// modeluuid,
|
|
||||||
// points: [
|
|
||||||
// {
|
|
||||||
// uuid: path.points.uuid,
|
|
||||||
// position: path.points.position,
|
|
||||||
// actions: Array.isArray(path.points.actions)
|
|
||||||
// ? path.points.actions.map(normalizeAction)
|
|
||||||
// : path.points.actions
|
|
||||||
// ? [normalizeAction(path.points.actions)]
|
|
||||||
// : [],
|
|
||||||
// // For vehicle paths, since triggers might not exist in the schema,
|
|
||||||
// // we always define default to an empty array
|
|
||||||
// triggers: [],
|
|
||||||
// connections: {
|
|
||||||
// targets: path.points.connections.targets.map((target) => ({
|
|
||||||
// modelUUID: target.modelUUID,
|
|
||||||
// })),
|
|
||||||
// },
|
|
||||||
// },
|
|
||||||
// ],
|
|
||||||
// pathPosition: path.position,
|
|
||||||
// speed: path.points.speed || 1,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Helper function to create an empty process
|
|
||||||
// const createEmptyProcess = (): Process => ({
|
|
||||||
// id: `process-${Math.random().toString(36).substring(2, 11)}`,
|
|
||||||
// paths: [],
|
|
||||||
// animationPath: [],
|
|
||||||
// pointActions: [],
|
|
||||||
// pointTriggers: [], // Added point triggers array
|
|
||||||
// speed: 1,
|
|
||||||
// isActive: false,
|
|
||||||
// });
|
|
||||||
|
|
||||||
// // Enhanced connection checking function
|
|
||||||
// function shouldReverseNextPath(
|
|
||||||
// currentPath: SimulationPath,
|
|
||||||
// nextPath: SimulationPath
|
|
||||||
// ): boolean {
|
|
||||||
// if (nextPath.points.length !== 3) return false;
|
|
||||||
|
|
||||||
// const currentLastPoint = currentPath.points[currentPath.points.length - 1];
|
|
||||||
// const nextFirstPoint = nextPath.points[0];
|
|
||||||
// const nextLastPoint = nextPath.points[nextPath.points.length - 1];
|
|
||||||
|
|
||||||
// // Check if current last connects to next last (requires reversal)
|
|
||||||
// const connectsToLast = currentLastPoint.connections.targets.some(
|
|
||||||
// (target) =>
|
|
||||||
// target.modelUUID === nextPath.modeluuid &&
|
|
||||||
// nextLastPoint.connections.targets.some(
|
|
||||||
// (t) => t.modelUUID === currentPath.modeluuid
|
|
||||||
// )
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // Check if current last connects to next first (no reversal needed)
|
|
||||||
// const connectsToFirst = currentLastPoint.connections.targets.some(
|
|
||||||
// (target) =>
|
|
||||||
// target.modelUUID === nextPath.modeluuid &&
|
|
||||||
// nextFirstPoint.connections.targets.some(
|
|
||||||
// (t) => t.modelUUID === currentPath.modeluuid
|
|
||||||
// )
|
|
||||||
// );
|
|
||||||
|
|
||||||
// // Only reverse if connected to last point and not to first point
|
|
||||||
// return connectsToLast && !connectsToFirst;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Check if a point has a spawn action
|
|
||||||
// function hasSpawnAction(point: PathPoint): boolean {
|
|
||||||
// return point.actions.some((action) => action.type.toLowerCase() === "spawn");
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Ensure spawn point is always at the beginning of the path
|
|
||||||
// function ensureSpawnPointIsFirst(path: SimulationPath): SimulationPath {
|
|
||||||
// if (path.points.length !== 3) return path;
|
|
||||||
|
|
||||||
// // If the third point has spawn action and first doesn't, reverse the array
|
|
||||||
// if (hasSpawnAction(path.points[2]) && !hasSpawnAction(path.points[0])) {
|
|
||||||
// return {
|
|
||||||
// ...path,
|
|
||||||
// points: [...path.points].reverse(),
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return path;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Updated path adjustment function
|
|
||||||
// function adjustPathPointsOrder(paths: SimulationPath[]): SimulationPath[] {
|
|
||||||
// if (paths.length < 1) return paths;
|
|
||||||
|
|
||||||
// const adjustedPaths = [...paths];
|
|
||||||
|
|
||||||
// // First ensure all paths have spawn points at the beginning
|
|
||||||
// for (let i = 0; i < adjustedPaths.length; i++) {
|
|
||||||
// adjustedPaths[i] = ensureSpawnPointIsFirst(adjustedPaths[i]);
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Then handle connections between paths
|
|
||||||
// for (let i = 0; i < adjustedPaths.length - 1; i++) {
|
|
||||||
// const currentPath = adjustedPaths[i];
|
|
||||||
// const nextPath = adjustedPaths[i + 1];
|
|
||||||
|
|
||||||
// if (shouldReverseNextPath(currentPath, nextPath)) {
|
|
||||||
// const reversedPoints = [
|
|
||||||
// nextPath.points[2],
|
|
||||||
// nextPath.points[1],
|
|
||||||
// nextPath.points[0],
|
|
||||||
// ];
|
|
||||||
|
|
||||||
// adjustedPaths[i + 1] = {
|
|
||||||
// ...nextPath,
|
|
||||||
// points: reversedPoints,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return adjustedPaths;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Main hook for process creation
|
|
||||||
// export function useProcessCreation() {
|
|
||||||
// const { scene } = useThree();
|
|
||||||
// const [processes, setProcesses] = useState<Process[]>([]);
|
|
||||||
|
|
||||||
// const hasSpawnAction = useCallback((path: SimulationPath): boolean => {
|
|
||||||
// if (path.type !== "Conveyor") return false;
|
|
||||||
// return path.points.some((point) =>
|
|
||||||
// point.actions.some((action) => action.type.toLowerCase() === "spawn")
|
|
||||||
// );
|
|
||||||
// }, []);
|
|
||||||
|
|
||||||
// const createProcess = useCallback(
|
|
||||||
// (paths: SimulationPath[]): Process => {
|
|
||||||
// if (!paths || paths.length === 0) {
|
|
||||||
// return createEmptyProcess();
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const animationPath: THREE.Vector3[] = [];
|
|
||||||
// const pointActions: PointAction[][] = [];
|
|
||||||
// const pointTriggers: PointTrigger[][] = []; // Added point triggers collection
|
|
||||||
// const processSpeed = paths[0]?.speed || 1;
|
|
||||||
|
|
||||||
// for (const path of paths) {
|
|
||||||
// for (const point of path.points) {
|
|
||||||
// if (path.type === "Conveyor") {
|
|
||||||
// const obj = scene.getObjectByProperty("uuid", point.uuid);
|
|
||||||
// if (!obj) {
|
|
||||||
// console.warn(`Object with UUID ${point.uuid} not found in scene`);
|
|
||||||
// continue;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const position = obj.getWorldPosition(new THREE.Vector3());
|
|
||||||
// animationPath.push(position.clone());
|
|
||||||
// pointActions.push(point.actions);
|
|
||||||
// pointTriggers.push(point.triggers); // Collect triggers for each point
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return {
|
|
||||||
// id: `process-${Math.random().toString(36).substring(2, 11)}`,
|
|
||||||
// paths,
|
|
||||||
// animationPath,
|
|
||||||
// pointActions,
|
|
||||||
// pointTriggers,
|
|
||||||
// speed: processSpeed,
|
|
||||||
// isActive: false,
|
|
||||||
// };
|
|
||||||
// },
|
|
||||||
// [scene]
|
|
||||||
// );
|
|
||||||
|
|
||||||
// const getAllConnectedPaths = useCallback(
|
|
||||||
// (
|
|
||||||
// initialPath: SimulationPath,
|
|
||||||
// allPaths: SimulationPath[],
|
|
||||||
// visited: Set<string> = new Set()
|
|
||||||
// ): SimulationPath[] => {
|
|
||||||
// const connectedPaths: SimulationPath[] = [];
|
|
||||||
// const queue: SimulationPath[] = [initialPath];
|
|
||||||
// visited.add(initialPath.modeluuid);
|
|
||||||
|
|
||||||
// const pathMap = new Map<string, SimulationPath>();
|
|
||||||
// allPaths.forEach((path) => pathMap.set(path.modeluuid, path));
|
|
||||||
|
|
||||||
// while (queue.length > 0) {
|
|
||||||
// const currentPath = queue.shift()!;
|
|
||||||
// connectedPaths.push(currentPath);
|
|
||||||
|
|
||||||
// // Process outgoing connections
|
|
||||||
// for (const point of currentPath.points) {
|
|
||||||
// for (const target of point.connections.targets) {
|
|
||||||
// if (!visited.has(target.modelUUID)) {
|
|
||||||
// const targetPath = pathMap.get(target.modelUUID);
|
|
||||||
// if (targetPath) {
|
|
||||||
// visited.add(target.modelUUID);
|
|
||||||
// queue.push(targetPath);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Process incoming connections
|
|
||||||
// for (const [uuid, path] of pathMap) {
|
|
||||||
// if (!visited.has(uuid)) {
|
|
||||||
// const hasConnectionToCurrent = path.points.some((point) =>
|
|
||||||
// point.connections.targets.some(
|
|
||||||
// (t) => t.modelUUID === currentPath.modeluuid
|
|
||||||
// )
|
|
||||||
// );
|
|
||||||
// if (hasConnectionToCurrent) {
|
|
||||||
// visited.add(uuid);
|
|
||||||
// queue.push(path);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return connectedPaths;
|
|
||||||
// },
|
|
||||||
// []
|
|
||||||
// );
|
|
||||||
|
|
||||||
// const createProcessesFromPaths = useCallback(
|
|
||||||
// (paths: SimulationPath[]): Process[] => {
|
|
||||||
// if (!paths || paths.length === 0) return [];
|
|
||||||
|
|
||||||
// const visited = new Set<string>();
|
|
||||||
// const processes: Process[] = [];
|
|
||||||
// const pathMap = new Map<string, SimulationPath>();
|
|
||||||
// paths.forEach((path) => pathMap.set(path.modeluuid, path));
|
|
||||||
|
|
||||||
// for (const path of paths) {
|
|
||||||
// if (!visited.has(path.modeluuid) && hasSpawnAction(path)) {
|
|
||||||
// const connectedPaths = getAllConnectedPaths(path, paths, visited);
|
|
||||||
// const adjustedPaths = adjustPathPointsOrder(connectedPaths);
|
|
||||||
// const process = createProcess(adjustedPaths);
|
|
||||||
// processes.push(process);
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return processes;
|
|
||||||
// },
|
|
||||||
// [createProcess, getAllConnectedPaths, hasSpawnAction]
|
|
||||||
// );
|
|
||||||
|
|
||||||
// return {
|
|
||||||
// processes,
|
|
||||||
// createProcessesFromPaths,
|
|
||||||
// setProcesses,
|
|
||||||
// };
|
|
||||||
// }
|
|
||||||
|
|
||||||
// const ProcessCreator: React.FC<ProcessCreatorProps> = React.memo(
|
|
||||||
// ({ onProcessesCreated }) => {
|
|
||||||
// const { simulationStates } = useSimulationStates();
|
|
||||||
// const { createProcessesFromPaths } = useProcessCreation();
|
|
||||||
// const prevPathsRef = useRef<SimulationPath[]>([]);
|
|
||||||
// const prevProcessesRef = useRef<Process[]>([]);
|
|
||||||
|
|
||||||
// const convertedPaths = useMemo((): SimulationPath[] => {
|
|
||||||
// if (!simulationStates) return [];
|
|
||||||
// return simulationStates.map((path) =>
|
|
||||||
// convertToSimulationPath(
|
|
||||||
// path as ConveyorEventsSchema | VehicleEventsSchema
|
|
||||||
// )
|
|
||||||
// );
|
|
||||||
// }, [simulationStates]);
|
|
||||||
|
|
||||||
// // Enhanced dependency tracking that includes action and trigger types
|
|
||||||
// const pathsDependency = useMemo(() => {
|
|
||||||
// if (!convertedPaths) return null;
|
|
||||||
// return convertedPaths.map((path) => ({
|
|
||||||
// id: path.modeluuid,
|
|
||||||
// // Track all action types for each point
|
|
||||||
// actionSignature: path.points
|
|
||||||
// .map((point, index) =>
|
|
||||||
// point.actions.map((action) => `${index}-${action.type}`).join("|")
|
|
||||||
// )
|
|
||||||
// .join(","),
|
|
||||||
// // Track all trigger types for each point
|
|
||||||
// triggerSignature: path.points
|
|
||||||
// .map((point, index) =>
|
|
||||||
// point.triggers
|
|
||||||
// .map((trigger) => `${index}-${trigger.type}`)
|
|
||||||
// .join("|")
|
|
||||||
// )
|
|
||||||
// .join(","),
|
|
||||||
// connections: path.points
|
|
||||||
// .flatMap((p: PathPoint) =>
|
|
||||||
// p.connections.targets.map((t: { modelUUID: string }) => t.modelUUID)
|
|
||||||
// )
|
|
||||||
// .join(","),
|
|
||||||
// isActive: false,
|
|
||||||
// }));
|
|
||||||
// }, [convertedPaths]);
|
|
||||||
|
|
||||||
// // Force process recreation when paths change
|
|
||||||
// useEffect(() => {
|
|
||||||
// if (!convertedPaths || convertedPaths.length === 0) {
|
|
||||||
// if (prevProcessesRef.current.length > 0) {
|
|
||||||
// onProcessesCreated([]);
|
|
||||||
// prevProcessesRef.current = [];
|
|
||||||
// }
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// // Always regenerate processes if the pathsDependency has changed
|
|
||||||
// // This ensures action and trigger type changes will be detected
|
|
||||||
// const newProcesses = createProcessesFromPaths(convertedPaths);
|
|
||||||
// prevPathsRef.current = convertedPaths;
|
|
||||||
|
|
||||||
// // Always update processes when action or trigger types change
|
|
||||||
// onProcessesCreated(newProcesses);
|
|
||||||
// prevProcessesRef.current = newProcesses;
|
|
||||||
// }, [
|
|
||||||
// pathsDependency, // This now includes action and trigger types
|
|
||||||
// onProcessesCreated,
|
|
||||||
// convertedPaths,
|
|
||||||
// createProcessesFromPaths,
|
|
||||||
// ]);
|
|
||||||
|
|
||||||
// return null;
|
|
||||||
// }
|
|
||||||
// );
|
|
||||||
|
|
||||||
// export default ProcessCreator;
|
|
||||||
|
|
||||||
import React, {
|
import React, {
|
||||||
useEffect,
|
useEffect,
|
||||||
useMemo,
|
useMemo,
|
||||||
@@ -456,6 +9,7 @@ import { useSimulationStates } from "../../../store/store";
|
|||||||
import * as THREE from "three";
|
import * as THREE from "three";
|
||||||
import { useThree } from "@react-three/fiber";
|
import { useThree } from "@react-three/fiber";
|
||||||
import {
|
import {
|
||||||
|
ArmBotEventsSchema,
|
||||||
ConveyorEventsSchema,
|
ConveyorEventsSchema,
|
||||||
VehicleEventsSchema,
|
VehicleEventsSchema,
|
||||||
} from "../../../types/simulation";
|
} from "../../../types/simulation";
|
||||||
@@ -480,13 +34,14 @@ export interface PointTrigger {
|
|||||||
isUsed: boolean;
|
isUsed: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Update the connections type in your interfaces
|
||||||
export interface PathPoint {
|
export interface PathPoint {
|
||||||
uuid: string;
|
uuid: string;
|
||||||
position: [number, number, number];
|
position: [number, number, number];
|
||||||
actions: PointAction[];
|
actions: PointAction[];
|
||||||
triggers: PointTrigger[];
|
triggers: PointTrigger[];
|
||||||
connections: {
|
connections: {
|
||||||
targets: Array<{ modelUUID: string }>;
|
targets: Array<{ modelUUID: string; pointUUID?: string }>;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -498,6 +53,14 @@ export interface SimulationPath {
|
|||||||
speed?: number;
|
speed?: number;
|
||||||
isActive: boolean;
|
isActive: boolean;
|
||||||
}
|
}
|
||||||
|
export interface ArmBot {
|
||||||
|
type: string;
|
||||||
|
modeluuid: string;
|
||||||
|
points: PathPoint[];
|
||||||
|
pathPosition: [number, number, number];
|
||||||
|
speed?: number;
|
||||||
|
isActive: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
export interface Process {
|
export interface Process {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -515,7 +78,7 @@ interface ProcessCreatorProps {
|
|||||||
|
|
||||||
// Convert event schemas to SimulationPath
|
// Convert event schemas to SimulationPath
|
||||||
function convertToSimulationPath(
|
function convertToSimulationPath(
|
||||||
path: ConveyorEventsSchema | VehicleEventsSchema
|
path: ConveyorEventsSchema | VehicleEventsSchema | ArmBotEventsSchema
|
||||||
): SimulationPath {
|
): SimulationPath {
|
||||||
const { modeluuid } = path;
|
const { modeluuid } = path;
|
||||||
|
|
||||||
@@ -559,6 +122,36 @@ function convertToSimulationPath(
|
|||||||
: path.speed || 1,
|
: path.speed || 1,
|
||||||
isActive: false, // Added missing property
|
isActive: false, // Added missing property
|
||||||
};
|
};
|
||||||
|
} else if (path.type === "ArmBot") {
|
||||||
|
return {
|
||||||
|
type: path.type,
|
||||||
|
modeluuid,
|
||||||
|
points: [
|
||||||
|
{
|
||||||
|
uuid: path.points.uuid,
|
||||||
|
position: path.points.position,
|
||||||
|
actions: Array.isArray(path.points.actions)
|
||||||
|
? path.points.actions.map(normalizeAction)
|
||||||
|
: path.points.actions
|
||||||
|
? [normalizeAction(path.points.actions)]
|
||||||
|
: [],
|
||||||
|
triggers: Array.isArray(path.points.triggers)
|
||||||
|
? path.points.triggers.map(normalizeTrigger)
|
||||||
|
: path.points.triggers
|
||||||
|
? [normalizeTrigger(path.points.triggers)]
|
||||||
|
: [],
|
||||||
|
connections: {
|
||||||
|
targets: path.points.connections.targets.map((target) => ({
|
||||||
|
modelUUID: target.modelUUID,
|
||||||
|
pointUUID: target.pointUUID, // Include if available
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
pathPosition: path.position,
|
||||||
|
speed: path.points.actions?.speed || 1,
|
||||||
|
isActive: false,
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
// For vehicle paths, handle the case where triggers might not exist
|
// For vehicle paths, handle the case where triggers might not exist
|
||||||
return {
|
return {
|
||||||
@@ -831,7 +424,10 @@ const ProcessCreator: React.FC<ProcessCreatorProps> = React.memo(
|
|||||||
if (!simulationStates) return [];
|
if (!simulationStates) return [];
|
||||||
return simulationStates.map((path) =>
|
return simulationStates.map((path) =>
|
||||||
convertToSimulationPath(
|
convertToSimulationPath(
|
||||||
path as ConveyorEventsSchema | VehicleEventsSchema
|
path as
|
||||||
|
| ConveyorEventsSchema
|
||||||
|
| VehicleEventsSchema
|
||||||
|
| ArmBotEventsSchema
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}, [simulationStates]);
|
}, [simulationStates]);
|
||||||
|
|||||||
@@ -38,8 +38,8 @@ export interface ProcessPath {
|
|||||||
pathPosition: number[];
|
pathPosition: number[];
|
||||||
pathRotation: number[];
|
pathRotation: number[];
|
||||||
speed: number;
|
speed: number;
|
||||||
type: "Conveyor" | "Vehicle";
|
type: "Conveyor" | "Vehicle" | "ArmBot";
|
||||||
isActive: boolean
|
isActive: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface ProcessData {
|
export interface ProcessData {
|
||||||
|
|||||||
@@ -17,6 +17,12 @@ import {
|
|||||||
} from "../../../store/usePlayButtonStore";
|
} from "../../../store/usePlayButtonStore";
|
||||||
import { usePlayAgv } from "../../../store/store";
|
import { usePlayAgv } from "../../../store/store";
|
||||||
|
|
||||||
|
interface ArmBotProcess {
|
||||||
|
triggerId: string;
|
||||||
|
startPoint: string;
|
||||||
|
endPoint: string;
|
||||||
|
}
|
||||||
|
|
||||||
// Enhanced ProcessAnimationState with trigger tracking
|
// Enhanced ProcessAnimationState with trigger tracking
|
||||||
interface EnhancedProcessAnimationState extends ProcessAnimationState {
|
interface EnhancedProcessAnimationState extends ProcessAnimationState {
|
||||||
triggerCounts: Record<string, number>;
|
triggerCounts: Record<string, number>;
|
||||||
@@ -52,13 +58,18 @@ export const useProcessAnimation = (
|
|||||||
const clockRef = useRef<THREE.Clock>(new THREE.Clock());
|
const clockRef = useRef<THREE.Clock>(new THREE.Clock());
|
||||||
const pauseTimeRef = useRef<number>(0);
|
const pauseTimeRef = useRef<number>(0);
|
||||||
const elapsedBeforePauseRef = useRef<number>(0);
|
const elapsedBeforePauseRef = useRef<number>(0);
|
||||||
const animationStatesRef = useRef<Record<string, EnhancedProcessAnimationState>>({});
|
const animationStatesRef = useRef<
|
||||||
|
Record<string, EnhancedProcessAnimationState>
|
||||||
|
>({});
|
||||||
const { speed } = useAnimationPlaySpeed();
|
const { speed } = useAnimationPlaySpeed();
|
||||||
const prevIsPlaying = useRef<boolean | null>(null);
|
const prevIsPlaying = useRef<boolean | null>(null);
|
||||||
const [internalResetFlag, setInternalResetFlag] = useState(false);
|
const [internalResetFlag, setInternalResetFlag] = useState(false);
|
||||||
const [animationStates, setAnimationStates] = useState<Record<string, EnhancedProcessAnimationState>>({});
|
const [animationStates, setAnimationStates] = useState<
|
||||||
|
Record<string, EnhancedProcessAnimationState>
|
||||||
|
>({});
|
||||||
const speedRef = useRef<number>(speed);
|
const speedRef = useRef<number>(speed);
|
||||||
const { PlayAgv, setPlayAgv } = usePlayAgv();
|
const armBotRef = useRef<any[]>([]);
|
||||||
|
|
||||||
|
|
||||||
// Effect hooks
|
// Effect hooks
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -100,7 +111,8 @@ export const useProcessAnimation = (
|
|||||||
if (isPaused) {
|
if (isPaused) {
|
||||||
pauseTimeRef.current = clockRef.current.getElapsedTime();
|
pauseTimeRef.current = clockRef.current.getElapsedTime();
|
||||||
} else if (pauseTimeRef.current > 0) {
|
} else if (pauseTimeRef.current > 0) {
|
||||||
const pausedDuration = clockRef.current.getElapsedTime() - pauseTimeRef.current;
|
const pausedDuration =
|
||||||
|
clockRef.current.getElapsedTime() - pauseTimeRef.current;
|
||||||
elapsedBeforePauseRef.current += pausedDuration;
|
elapsedBeforePauseRef.current += pausedDuration;
|
||||||
}
|
}
|
||||||
}, [isPaused]);
|
}, [isPaused]);
|
||||||
@@ -149,9 +161,8 @@ export const useProcessAnimation = (
|
|||||||
// Initialize AGVs for each process first
|
// Initialize AGVs for each process first
|
||||||
processes.forEach((process) => {
|
processes.forEach((process) => {
|
||||||
// Find all vehicle paths for this process
|
// Find all vehicle paths for this process
|
||||||
const vehiclePaths = process.paths?.filter(
|
const vehiclePaths =
|
||||||
(path) => path.type === "Vehicle"
|
process.paths?.filter((path) => path.type === "Vehicle") || [];
|
||||||
) || [];
|
|
||||||
|
|
||||||
// Initialize AGVs for each vehicle path
|
// Initialize AGVs for each vehicle path
|
||||||
vehiclePaths.forEach((vehiclePath) => {
|
vehiclePaths.forEach((vehiclePath) => {
|
||||||
@@ -176,8 +187,8 @@ export const useProcessAnimation = (
|
|||||||
maxHitCount: maxHitCount || 0,
|
maxHitCount: maxHitCount || 0,
|
||||||
isActive: false,
|
isActive: false,
|
||||||
hitCount: 0,
|
hitCount: 0,
|
||||||
status: 'stationed',
|
status: "stationed",
|
||||||
lastUpdated: 0
|
lastUpdated: 0,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -460,6 +471,7 @@ export const useProcessAnimation = (
|
|||||||
const onHitTriggers = point.triggers.filter(
|
const onHitTriggers = point.triggers.filter(
|
||||||
(t: Trigger) => t.type === "On-Hit" && t.isUsed
|
(t: Trigger) => t.type === "On-Hit" && t.isUsed
|
||||||
);
|
);
|
||||||
|
|
||||||
if (onHitTriggers.length === 0) return prev;
|
if (onHitTriggers.length === 0) return prev;
|
||||||
|
|
||||||
let newTriggerCounts = { ...processState.triggerCounts };
|
let newTriggerCounts = { ...processState.triggerCounts };
|
||||||
@@ -471,8 +483,13 @@ export const useProcessAnimation = (
|
|||||||
(path) => path.type === "Vehicle"
|
(path) => path.type === "Vehicle"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// Find all ArmBot paths for this process
|
||||||
|
const armBotPaths = process.paths.filter(
|
||||||
|
(path) => path.type === "ArmBot"
|
||||||
|
);
|
||||||
|
|
||||||
// Check if any vehicle is active for this process
|
// Check if any vehicle is active for this process
|
||||||
const activeVehicles = vehiclePaths.filter(path => {
|
const activeVehicles = vehiclePaths.filter((path) => {
|
||||||
const vehicleId = path.modeluuid;
|
const vehicleId = path.modeluuid;
|
||||||
const vehicleEntry = agvRef.current.find(
|
const vehicleEntry = agvRef.current.find(
|
||||||
(v: any) => v.vehicleId === vehicleId && v.processId === processId
|
(v: any) => v.vehicleId === vehicleId && v.processId === processId
|
||||||
@@ -480,11 +497,21 @@ export const useProcessAnimation = (
|
|||||||
return vehicleEntry?.isActive;
|
return vehicleEntry?.isActive;
|
||||||
});
|
});
|
||||||
|
|
||||||
// Only count triggers if no vehicles are active for this process
|
// Check if any ArmBot is active for this process
|
||||||
if (activeVehicles.length === 0) {
|
const activeArmBots = armBotPaths.filter((path) => {
|
||||||
|
const armBotId = path.modeluuid;
|
||||||
|
const armBotEntry = armBotRef.current.find(
|
||||||
|
(a: any) => a.armBotId === armBotId && a.processId === processId
|
||||||
|
);
|
||||||
|
return armBotEntry?.isActive;
|
||||||
|
});
|
||||||
|
|
||||||
|
// Only count triggers if no vehicles and no ArmBots are active for this process
|
||||||
|
if (activeVehicles.length === 0 && activeArmBots.length === 0) {
|
||||||
onHitTriggers.forEach((trigger: Trigger) => {
|
onHitTriggers.forEach((trigger: Trigger) => {
|
||||||
const triggerKey = `${point.uuid}-${trigger.uuid}`;
|
const triggerKey = `${point.uuid}-${trigger.uuid}`;
|
||||||
newTriggerCounts[triggerKey] = (newTriggerCounts[triggerKey] || 0) + 1;
|
newTriggerCounts[triggerKey] =
|
||||||
|
(newTriggerCounts[triggerKey] || 0) + 1;
|
||||||
shouldLog = true;
|
shouldLog = true;
|
||||||
newTriggerLogs.push({
|
newTriggerLogs.push({
|
||||||
timestamp: currentTime,
|
timestamp: currentTime,
|
||||||
@@ -495,9 +522,14 @@ export const useProcessAnimation = (
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
let processTotalHits = Object.values(newTriggerCounts).reduce((a, b) => a + b, 0);
|
let processTotalHits = Object.values(newTriggerCounts).reduce(
|
||||||
|
(a, b) => a + b,
|
||||||
|
0
|
||||||
|
);
|
||||||
|
|
||||||
|
// Handle logic for vehicles and ArmBots when a trigger is hit
|
||||||
if (shouldLog) {
|
if (shouldLog) {
|
||||||
|
// Handle vehicle logic (existing code)
|
||||||
vehiclePaths.forEach((vehiclePath) => {
|
vehiclePaths.forEach((vehiclePath) => {
|
||||||
if (vehiclePath.points?.length > 0) {
|
if (vehiclePath.points?.length > 0) {
|
||||||
const vehiclePoint = vehiclePath.points[0];
|
const vehiclePoint = vehiclePath.points[0];
|
||||||
@@ -506,7 +538,10 @@ export const useProcessAnimation = (
|
|||||||
|
|
||||||
if (maxHitCount !== undefined) {
|
if (maxHitCount !== undefined) {
|
||||||
const vehicleId = vehiclePath.modeluuid;
|
const vehicleId = vehiclePath.modeluuid;
|
||||||
let vehicleEntry = agvRef.current.find((v: any) => v.vehicleId === vehicleId && v.processId === processId);
|
let vehicleEntry = agvRef.current.find(
|
||||||
|
(v: any) =>
|
||||||
|
v.vehicleId === vehicleId && v.processId === processId
|
||||||
|
);
|
||||||
|
|
||||||
if (!vehicleEntry) {
|
if (!vehicleEntry) {
|
||||||
vehicleEntry = {
|
vehicleEntry = {
|
||||||
@@ -515,14 +550,13 @@ export const useProcessAnimation = (
|
|||||||
maxHitCount: maxHitCount,
|
maxHitCount: maxHitCount,
|
||||||
isActive: false,
|
isActive: false,
|
||||||
hitCount: 0,
|
hitCount: 0,
|
||||||
status: 'stationed'
|
status: "stationed",
|
||||||
};
|
};
|
||||||
agvRef.current.push(vehicleEntry);
|
agvRef.current.push(vehicleEntry);
|
||||||
}
|
}
|
||||||
|
|
||||||
// if (!vehicleEntry.isActive && vehicleEntry.status === 'picking') {
|
|
||||||
if (!vehicleEntry.isActive) {
|
if (!vehicleEntry.isActive) {
|
||||||
vehicleEntry.hitCount = processTotalHits;
|
vehicleEntry.hitCount++;
|
||||||
vehicleEntry.lastUpdated = currentTime;
|
vehicleEntry.lastUpdated = currentTime;
|
||||||
|
|
||||||
if (vehicleEntry.hitCount >= vehicleEntry.maxHitCount) {
|
if (vehicleEntry.hitCount >= vehicleEntry.maxHitCount) {
|
||||||
@@ -534,6 +568,48 @@ export const useProcessAnimation = (
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// Handle ArmBot logic (new code)
|
||||||
|
armBotPaths.forEach((armBotPath) => {
|
||||||
|
if (armBotPath.points?.length > 0) {
|
||||||
|
const armBotPoint = armBotPath.points[0];
|
||||||
|
const action = armBotPoint.actions?.[0];
|
||||||
|
const maxHitCount = action?.hitCount;
|
||||||
|
|
||||||
|
if (maxHitCount !== undefined) {
|
||||||
|
const armBotId = armBotPath.modeluuid;
|
||||||
|
let armBotEntry = armBotRef.current.find(
|
||||||
|
(a: any) =>
|
||||||
|
a.armBotId === armBotId && a.processId === processId
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!armBotEntry) {
|
||||||
|
armBotEntry = {
|
||||||
|
processId,
|
||||||
|
armBotId,
|
||||||
|
maxHitCount: maxHitCount,
|
||||||
|
isActive: false,
|
||||||
|
hitCount: 0,
|
||||||
|
status: "stationed",
|
||||||
|
};
|
||||||
|
armBotRef.current.push(armBotEntry);
|
||||||
|
console.log('armBotEntry: ', armBotEntry);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!armBotEntry.isActive) {
|
||||||
|
armBotEntry.hitCount++;
|
||||||
|
armBotEntry.lastUpdated = currentTime;
|
||||||
|
|
||||||
|
if (armBotEntry.hitCount >= armBotEntry.maxHitCount) {
|
||||||
|
armBotEntry.isActive = true;
|
||||||
|
// Reset trigger counts when ArmBot activates, similar to vehicle
|
||||||
|
newTriggerCounts = {};
|
||||||
|
processTotalHits = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -11,8 +11,10 @@ function Simulation() {
|
|||||||
const { activeModule } = useModuleStore();
|
const { activeModule } = useModuleStore();
|
||||||
const pathsGroupRef = useRef() as React.MutableRefObject<THREE.Group>;
|
const pathsGroupRef = useRef() as React.MutableRefObject<THREE.Group>;
|
||||||
const [processes, setProcesses] = useState<any[]>([]);
|
const [processes, setProcesses] = useState<any[]>([]);
|
||||||
|
|
||||||
const agvRef = useRef([]);
|
const agvRef = useRef([]);
|
||||||
const MaterialRef = useRef([]);
|
const MaterialRef = useRef([]);
|
||||||
|
const { simulationStates } = useSimulationStates();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|||||||
Reference in New Issue
Block a user