added actions for machines
This commit is contained in:
parent
14e3a60db3
commit
ad63a8d72b
|
@ -0,0 +1,102 @@
|
||||||
|
import { useFrame } from '@react-three/fiber';
|
||||||
|
import React, { useEffect, useRef } from 'react';
|
||||||
|
import { useMachineStore } from '../../../../../store/simulation/useMachineStore';
|
||||||
|
import { useAnimationPlaySpeed, usePauseButtonStore, usePlayButtonStore, useResetButtonStore } from '../../../../../store/usePlayButtonStore';
|
||||||
|
|
||||||
|
|
||||||
|
interface MachineAnimatorProps {
|
||||||
|
currentPhase: string;
|
||||||
|
handleCallBack: () => void;
|
||||||
|
reset: () => void;
|
||||||
|
machineStatus: (modelId: string, status: string) => void;
|
||||||
|
processingTime: number;
|
||||||
|
machineUuid: string
|
||||||
|
}
|
||||||
|
|
||||||
|
const MachineAnimator = ({ currentPhase, handleCallBack, processingTime, machineUuid, machineStatus, reset }: MachineAnimatorProps) => {
|
||||||
|
const animationStarted = useRef<boolean>(false);
|
||||||
|
const isPausedRef = useRef<boolean>(false);
|
||||||
|
const startTimeRef = useRef<number>(0);
|
||||||
|
const animationFrameId = useRef<number | null>(null);
|
||||||
|
const pauseTimeRef = useRef<number | null>(null);
|
||||||
|
const { isPaused } = usePauseButtonStore();
|
||||||
|
const { removeCurrentAction } = useMachineStore();
|
||||||
|
const { isReset, setReset } = useResetButtonStore();
|
||||||
|
const { isPlaying } = usePlayButtonStore();
|
||||||
|
const { speed } = useAnimationPlaySpeed();
|
||||||
|
const isPlayingRef = useRef<boolean>(false);
|
||||||
|
const isResetRef = useRef<boolean>(false)
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
isPausedRef.current = isPaused;
|
||||||
|
}, [isPaused]);
|
||||||
|
useEffect(() => {
|
||||||
|
isPlayingRef.current = isPlaying;
|
||||||
|
}, [isPlaying]);
|
||||||
|
useEffect(() => {
|
||||||
|
isResetRef.current = isReset;
|
||||||
|
}, [isReset]);
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
|
||||||
|
if (isReset || !isPlaying) {
|
||||||
|
reset();
|
||||||
|
setReset(false);
|
||||||
|
startTimeRef.current = 0;
|
||||||
|
isPausedRef.current = false;
|
||||||
|
pauseTimeRef.current = 0;
|
||||||
|
animationFrameId.current = null;
|
||||||
|
animationStarted.current = false;
|
||||||
|
removeCurrentAction(machineUuid)
|
||||||
|
}
|
||||||
|
}, [isReset, isPlaying])
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (currentPhase === 'processing' && !animationStarted.current && machineUuid) {
|
||||||
|
animationStarted.current = true;
|
||||||
|
startTimeRef.current = performance.now();
|
||||||
|
animationFrameId.current = requestAnimationFrame(step);
|
||||||
|
}
|
||||||
|
}, [currentPhase]);
|
||||||
|
|
||||||
|
function step(time: number) {
|
||||||
|
if (!isPausedRef.current || !isResetRef.current) {
|
||||||
|
if (animationFrameId.current) {
|
||||||
|
cancelAnimationFrame(animationFrameId.current);
|
||||||
|
animationFrameId.current = null;
|
||||||
|
}
|
||||||
|
if (isPausedRef.current) {
|
||||||
|
if (!pauseTimeRef.current) {
|
||||||
|
pauseTimeRef.current = performance.now();
|
||||||
|
}
|
||||||
|
animationFrameId.current = requestAnimationFrame(step);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pauseTimeRef.current) {
|
||||||
|
const pauseDuration = performance.now() - pauseTimeRef.current;
|
||||||
|
startTimeRef.current += pauseDuration;
|
||||||
|
pauseTimeRef.current = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
const elapsed = time - startTimeRef.current;
|
||||||
|
const processedTime = processingTime * 1000;
|
||||||
|
if (elapsed < processedTime) {
|
||||||
|
machineStatus(machineUuid, "Machine is currently processing the task");
|
||||||
|
animationFrameId.current = requestAnimationFrame(step);
|
||||||
|
} else {
|
||||||
|
removeCurrentAction(machineUuid);
|
||||||
|
animationStarted.current = false;
|
||||||
|
handleCallBack();
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MachineAnimator;
|
|
@ -1,8 +1,65 @@
|
||||||
import React from 'react'
|
import React, { useEffect, useRef, useState } from 'react'
|
||||||
|
import { useMachineStore } from '../../../../../store/simulation/useMachineStore';
|
||||||
|
import { usePlayButtonStore } from '../../../../../store/usePlayButtonStore';
|
||||||
|
import MachineAnimator from '../animator/machineAnimator';
|
||||||
|
|
||||||
|
function MachineInstance({ machineDetail }: any) {
|
||||||
|
const [currentPhase, setCurrentPhase] = useState<string>('idle');
|
||||||
|
let isIncrememtable = useRef<boolean>(true);
|
||||||
|
const { isPlaying } = usePlayButtonStore();
|
||||||
|
const { machines, addCurrentAction, setMachineState, setMachineActive } = useMachineStore();
|
||||||
|
|
||||||
|
const reset = () => {
|
||||||
|
setMachineState(machineDetail.modelUuid, 'idle');
|
||||||
|
setMachineActive(machineDetail.modelUuid, false);
|
||||||
|
isIncrememtable.current = true;
|
||||||
|
setCurrentPhase("idle");
|
||||||
|
}
|
||||||
|
const increment = () => {
|
||||||
|
if (isIncrememtable.current) {
|
||||||
|
addCurrentAction(machineDetail.modelUuid, "machine-action-2468-1357-8024")
|
||||||
|
isIncrememtable.current = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function machineStatus(modelId: string, status: string) {
|
||||||
|
// console.log(`${modelId} , ${status}`);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (isPlaying) {
|
||||||
|
if (!machineDetail.isActive && machineDetail.state === "idle" && currentPhase == "idle" && !machineDetail.currentAction) {
|
||||||
|
setTimeout(() => {
|
||||||
|
increment();
|
||||||
|
}, 2000);
|
||||||
|
machineStatus(machineDetail.modelUuid, 'Machine is idle and waiting for next instruction.')
|
||||||
|
} else if (!machineDetail.isActive && machineDetail.state === "idle" && currentPhase == "idle" && machineDetail.currentAction) {
|
||||||
|
setCurrentPhase("processing");
|
||||||
|
setMachineState(machineDetail.modelUuid, 'running');
|
||||||
|
setMachineActive(machineDetail.modelUuid, true);
|
||||||
|
machineStatus(machineDetail.modelUuid, "Machine started processing")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
reset();
|
||||||
|
}
|
||||||
|
}, [currentPhase, isPlaying, machines])
|
||||||
|
|
||||||
|
function handleCallBack() {
|
||||||
|
if (currentPhase == "processing") {
|
||||||
|
setMachineState(machineDetail.modelUuid, 'idle');
|
||||||
|
setMachineActive(machineDetail.modelUuid, false);
|
||||||
|
setCurrentPhase("idle")
|
||||||
|
isIncrememtable.current = true;
|
||||||
|
machineStatus(machineDetail.modelUuid, "Machine has completed the processing")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// console.log('currentPhase: ', currentPhase);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function MachineInstance() {
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
<MachineAnimator processingTime={machineDetail.point.action.processTime} handleCallBack={handleCallBack} currentPhase={currentPhase} machineUuid={machineDetail.modelUuid} machineStatus={machineStatus} reset={reset} />
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,14 @@
|
||||||
import React from 'react'
|
import React from 'react'
|
||||||
import MachineInstance from './machineInstance/machineInstance'
|
import MachineInstance from './machineInstance/machineInstance'
|
||||||
|
import { useMachineStore } from '../../../../store/simulation/useMachineStore';
|
||||||
|
|
||||||
function MachineInstances() {
|
function MachineInstances() {
|
||||||
|
const { machines } = useMachineStore();
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
{machines.map((val: MachineStatus) => (
|
||||||
<MachineInstance />
|
<MachineInstance key={val.modelUuid} machineDetail={val} />
|
||||||
|
))}
|
||||||
|
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
|
|
|
@ -4,7 +4,7 @@ import { useMachineStore } from '../../../store/simulation/useMachineStore'
|
||||||
import { useSelectedProduct } from '../../../store/simulation/useSimulationStore';
|
import { useSelectedProduct } from '../../../store/simulation/useSimulationStore';
|
||||||
|
|
||||||
function Machine() {
|
function Machine() {
|
||||||
const { addMachine, addCurrentAction, removeMachine } = useMachineStore();
|
const { addMachine, addCurrentAction, removeMachine, machines } = useMachineStore();
|
||||||
const { selectedProduct } = useSelectedProduct();
|
const { selectedProduct } = useSelectedProduct();
|
||||||
|
|
||||||
const machineSample: MachineEventSchema[] = [
|
const machineSample: MachineEventSchema[] = [
|
||||||
|
@ -38,6 +38,12 @@ function Machine() {
|
||||||
// addCurrentAction(machineSample[0].modelUuid, machineSample[0].point.action.actionUuid);
|
// addCurrentAction(machineSample[0].modelUuid, machineSample[0].point.action.actionUuid);
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
|
||||||
|
// console.log('machines: ', machines);
|
||||||
|
}, [machines])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
||||||
|
|
|
@ -7,7 +7,7 @@ function RoboticArmInstances() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{armBots?.map((robot: ArmBotStatus) => (
|
{armBots.map((robot: ArmBotStatus) => (
|
||||||
<RoboticArmInstance key={robot.modelUuid} robot={robot} />
|
<RoboticArmInstance key={robot.modelUuid} robot={robot} />
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
|
|
@ -25,9 +25,9 @@ function VehicleAnimator({ path, handleCallBack, currentPhase, agvUuid, agvDetai
|
||||||
const completedRef = useRef<boolean>(false);
|
const completedRef = useRef<boolean>(false);
|
||||||
const isPausedRef = useRef<boolean>(false);
|
const isPausedRef = useRef<boolean>(false);
|
||||||
const pauseTimeRef = useRef<number | null>(null);
|
const pauseTimeRef = useRef<number | null>(null);
|
||||||
|
const [progress, setProgress] = useState<number>(0);
|
||||||
const [restRotation, setRestingRotation] = useState<boolean>(true);
|
const [restRotation, setRestingRotation] = useState<boolean>(true);
|
||||||
const [currentPath, setCurrentPath] = useState<[number, number, number][]>([]);
|
const [currentPath, setCurrentPath] = useState<[number, number, number][]>([]);
|
||||||
const [progress, setProgress] = useState<number>(0);
|
|
||||||
const { scene } = useThree();
|
const { scene } = useThree();
|
||||||
let startTime: number;
|
let startTime: number;
|
||||||
let fixedInterval: number;
|
let fixedInterval: number;
|
||||||
|
@ -66,6 +66,8 @@ function VehicleAnimator({ path, handleCallBack, currentPhase, agvUuid, agvDetai
|
||||||
setReset(false);
|
setReset(false);
|
||||||
setRestingRotation(true);
|
setRestingRotation(true);
|
||||||
decrementVehicleLoad(agvDetail.modelUuid, 0);
|
decrementVehicleLoad(agvDetail.modelUuid, 0);
|
||||||
|
isPausedRef.current = false;
|
||||||
|
pauseTimeRef.current = 0;
|
||||||
const object = scene.getObjectByProperty('uuid', agvUuid);
|
const object = scene.getObjectByProperty('uuid', agvUuid);
|
||||||
if (object) {
|
if (object) {
|
||||||
object.position.set(agvDetail.position[0], agvDetail.position[1], agvDetail.position[2]);
|
object.position.set(agvDetail.position[0], agvDetail.position[1], agvDetail.position[2]);
|
||||||
|
|
|
@ -9,10 +9,8 @@ function VehicleInstances() {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
|
||||||
{vehicles.map((val: any, i: any) =>
|
{vehicles.map((val: VehicleStatus) =>
|
||||||
|
<VehicleInstance agvDetail={val} key={val.modelUuid} />
|
||||||
<VehicleInstance agvDetail={val} key={i} />
|
|
||||||
|
|
||||||
)}
|
)}
|
||||||
|
|
||||||
</>
|
</>
|
||||||
|
|
Loading…
Reference in New Issue