From 7907bbab0ae8e3edeccfc0cf90b7746e0b8a5418 Mon Sep 17 00:00:00 2001 From: Jerald-Golden-B Date: Tue, 22 Apr 2025 14:44:09 +0530 Subject: [PATCH] Refactor simulation store structures to use arrays instead of records for conveyors, machines, storage units, vehicles, and products; remove useSimulationStore; enhance state management and helper functions for better performance and readability. --- app/src/modules/simulation/simulation.tsx | 2 +- app/src/store/simulation/useArmBotStore.ts | 60 ++++++---------- app/src/store/simulation/useConveyorStore.ts | 42 ++++------- app/src/store/simulation/useEventsStore.ts | 2 +- app/src/store/simulation/useMachineStore.ts | 38 +++++----- ...eSimulationStore.ts => useProductStore.ts} | 4 +- .../store/simulation/useStorageUnitStore.ts | 72 +++++++++---------- app/src/store/simulation/useVehicleStore.ts | 58 +++++---------- app/src/types/simulationTypes.d.ts | 10 +-- 9 files changed, 115 insertions(+), 173 deletions(-) rename app/src/store/simulation/{useSimulationStore.ts => useProductStore.ts} (98%) diff --git a/app/src/modules/simulation/simulation.tsx b/app/src/modules/simulation/simulation.tsx index 387bfa8..3599803 100644 --- a/app/src/modules/simulation/simulation.tsx +++ b/app/src/modules/simulation/simulation.tsx @@ -1,6 +1,6 @@ import React, { useEffect } from 'react'; import { useEventsStore } from '../../store/simulation/useEventsStore'; -import { useProductStore } from '../../store/simulation/useSimulationStore'; +import { useProductStore } from '../../store/simulation/useProductStore'; function Simulation() { const { events } = useEventsStore(); diff --git a/app/src/store/simulation/useArmBotStore.ts b/app/src/store/simulation/useArmBotStore.ts index 4733161..7795290 100644 --- a/app/src/store/simulation/useArmBotStore.ts +++ b/app/src/store/simulation/useArmBotStore.ts @@ -1,7 +1,7 @@ import { create } from 'zustand'; import { immer } from 'zustand/middleware/immer'; -interface ArmBotStatus extends RoboticArmSchemaEvent { +interface ArmBotStatus extends RoboticArmEventSchema { productId: string; isActive: boolean; idleTime: number; @@ -13,29 +13,24 @@ interface ArmBotStatus extends RoboticArmSchemaEvent { } interface ArmBotStore { - armBots: Record; + armBots: ArmBotStatus[]; - // ArmBot actions - addArmBot: (productId: string, event: RoboticArmSchemaEvent) => void; + addArmBot: (productId: string, event: RoboticArmEventSchema) => void; removeArmBot: (modelUuid: string) => void; updateArmBot: ( modelUuid: string, updates: Partial> ) => void; - // Action management startAction: (modelUuid: string, actionUuid: string) => void; completeAction: (modelUuid: string) => void; cancelAction: (modelUuid: string) => void; - // Status updates setArmBotActive: (modelUuid: string, isActive: boolean) => void; - // Time tracking incrementActiveTime: (modelUuid: string, incrementBy: number) => void; incrementIdleTime: (modelUuid: string, incrementBy: number) => void; - // Helper functions getArmBotById: (modelUuid: string) => ArmBotStatus | undefined; getArmBotsByProduct: (productId: string) => ArmBotStatus[]; getActiveArmBots: () => ArmBotStatus[]; @@ -45,47 +40,45 @@ interface ArmBotStore { export const useArmBotStore = create()( immer((set, get) => ({ - armBots: {}, + armBots: [], - // ArmBot actions addArmBot: (productId, event) => { set((state) => { - state.armBots[event.modelUuid] = { + state.armBots.push({ ...event, productId, isActive: false, idleTime: 0, activeTime: 0, - state: 'idle' - }; + state: 'idle', + }); }); }, removeArmBot: (modelUuid) => { set((state) => { - delete state.armBots[modelUuid]; + state.armBots = state.armBots.filter(a => a.modelUuid !== modelUuid); }); }, updateArmBot: (modelUuid, updates) => { set((state) => { - const armBot = state.armBots[modelUuid]; + const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { Object.assign(armBot, updates); } }); }, - // Action management startAction: (modelUuid, actionUuid) => { set((state) => { - const armBot = state.armBots[modelUuid]; + const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { const action = armBot.point.actions.find(a => a.actionUuid === actionUuid); if (action) { armBot.currentAction = { actionUuid: action.actionUuid, - actionName: action.actionName + actionName: action.actionName, }; armBot.isActive = true; } @@ -95,7 +88,7 @@ export const useArmBotStore = create()( completeAction: (modelUuid) => { set((state) => { - const armBot = state.armBots[modelUuid]; + const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot && armBot.currentAction) { armBot.currentAction = undefined; armBot.isActive = false; @@ -105,7 +98,7 @@ export const useArmBotStore = create()( cancelAction: (modelUuid) => { set((state) => { - const armBot = state.armBots[modelUuid]; + const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.currentAction = undefined; armBot.isActive = false; @@ -113,20 +106,18 @@ export const useArmBotStore = create()( }); }, - // Status updates setArmBotActive: (modelUuid, isActive) => { set((state) => { - const armBot = state.armBots[modelUuid]; + const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.isActive = isActive; } }); }, - // Time tracking incrementActiveTime: (modelUuid, incrementBy) => { set((state) => { - const armBot = state.armBots[modelUuid]; + const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.activeTime += incrementBy; } @@ -135,38 +126,31 @@ export const useArmBotStore = create()( incrementIdleTime: (modelUuid, incrementBy) => { set((state) => { - const armBot = state.armBots[modelUuid]; + const armBot = state.armBots.find(a => a.modelUuid === modelUuid); if (armBot) { armBot.idleTime += incrementBy; } }); }, - // Helper functions getArmBotById: (modelUuid) => { - return get().armBots[modelUuid]; + return get().armBots.find(a => a.modelUuid === modelUuid); }, getArmBotsByProduct: (productId) => { - return Object.values(get().armBots).filter( - a => a.productId === productId - ); + return get().armBots.filter(a => a.productId === productId); }, getActiveArmBots: () => { - return Object.values(get().armBots).filter(a => a.isActive); + return get().armBots.filter(a => a.isActive); }, getIdleArmBots: () => { - return Object.values(get().armBots).filter( - a => !a.isActive && a.state === 'idle' - ); + return get().armBots.filter(a => !a.isActive && a.state === 'idle'); }, getArmBotsByCurrentAction: (actionUuid) => { - return Object.values(get().armBots).filter( - a => a.currentAction?.actionUuid === actionUuid - ); + return get().armBots.filter(a => a.currentAction?.actionUuid === actionUuid); } })) -); \ No newline at end of file +); diff --git a/app/src/store/simulation/useConveyorStore.ts b/app/src/store/simulation/useConveyorStore.ts index f49d5e0..72f8400 100644 --- a/app/src/store/simulation/useConveyorStore.ts +++ b/app/src/store/simulation/useConveyorStore.ts @@ -9,9 +9,8 @@ interface ConveyorStatus extends ConveyorEventSchema { } interface ConveyorStore { - conveyors: Record; + conveyors: ConveyorStatus[]; - // Actions addConveyor: (productId: string, event: ConveyorEventSchema) => void; removeConveyor: (modelUuid: string) => void; updateConveyor: ( @@ -19,15 +18,12 @@ interface ConveyorStore { updates: Partial> ) => void; - // Status updates setConveyorActive: (modelUuid: string, isActive: boolean) => void; setConveyorState: (modelUuid: string, newState: ConveyorStatus['state']) => void; - // Time tracking incrementActiveTime: (modelUuid: string, incrementBy: number) => void; incrementIdleTime: (modelUuid: string, incrementBy: number) => void; - // Helper functions getConveyorById: (modelUuid: string) => ConveyorStatus | undefined; getConveyorsByProduct: (productId: string) => ConveyorStatus[]; getActiveConveyors: () => ConveyorStatus[]; @@ -36,41 +32,39 @@ interface ConveyorStore { export const useConveyorStore = create()( immer((set, get) => ({ - conveyors: {}, + conveyors: [], - // Actions addConveyor: (productId, event) => { set((state) => { - state.conveyors[event.modelUuid] = { + state.conveyors.push({ ...event, productId, isActive: false, idleTime: 0, activeTime: 0, state: 'idle', - }; + }); }); }, removeConveyor: (modelUuid) => { set((state) => { - delete state.conveyors[modelUuid]; + state.conveyors = state.conveyors.filter(c => c.modelUuid !== modelUuid); }); }, updateConveyor: (modelUuid, updates) => { set((state) => { - const conveyor = state.conveyors[modelUuid]; + const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid); if (conveyor) { Object.assign(conveyor, updates); } }); }, - // Status updates setConveyorActive: (modelUuid, isActive) => { set((state) => { - const conveyor = state.conveyors[modelUuid]; + const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid); if (conveyor) { conveyor.isActive = isActive; } @@ -79,17 +73,16 @@ export const useConveyorStore = create()( setConveyorState: (modelUuid, newState) => { set((state) => { - const conveyor = state.conveyors[modelUuid]; + const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid); if (conveyor) { conveyor.state = newState; } }); }, - // Time tracking incrementActiveTime: (modelUuid, incrementBy) => { set((state) => { - const conveyor = state.conveyors[modelUuid]; + const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid); if (conveyor) { conveyor.activeTime += incrementBy; } @@ -98,32 +91,27 @@ export const useConveyorStore = create()( incrementIdleTime: (modelUuid, incrementBy) => { set((state) => { - const conveyor = state.conveyors[modelUuid]; + const conveyor = state.conveyors.find(c => c.modelUuid === modelUuid); if (conveyor) { conveyor.idleTime += incrementBy; } }); }, - // Helper functions getConveyorById: (modelUuid) => { - return get().conveyors[modelUuid]; + return get().conveyors.find(c => c.modelUuid === modelUuid); }, getConveyorsByProduct: (productId) => { - return Object.values(get().conveyors).filter( - c => c.productId === productId - ); + return get().conveyors.filter(c => c.productId === productId); }, getActiveConveyors: () => { - return Object.values(get().conveyors).filter(c => c.isActive); + return get().conveyors.filter(c => c.isActive); }, getIdleConveyors: () => { - return Object.values(get().conveyors).filter( - c => !c.isActive && c.state === 'idle' - ); + return get().conveyors.filter(c => !c.isActive && c.state === 'idle'); }, })) -); \ No newline at end of file +); diff --git a/app/src/store/simulation/useEventsStore.ts b/app/src/store/simulation/useEventsStore.ts index f13ac5b..2d92fc2 100644 --- a/app/src/store/simulation/useEventsStore.ts +++ b/app/src/store/simulation/useEventsStore.ts @@ -75,7 +75,7 @@ export const useEventsStore = create()( if (event && 'points' in event) { (event as ConveyorEventSchema).points.push(point as ConveyorPointSchema); } else if (event && 'point' in event) { - (event as VehicleSchemaEvent | RoboticArmSchemaEvent | MachineSchemaEvent | StorageSchemaEvent).point = point as any; + (event as VehicleEventSchema | RoboticArmEventSchema | MachineEventSchema | StorageEventSchema).point = point as any; } }); }, diff --git a/app/src/store/simulation/useMachineStore.ts b/app/src/store/simulation/useMachineStore.ts index 64f2dcc..913b374 100644 --- a/app/src/store/simulation/useMachineStore.ts +++ b/app/src/store/simulation/useMachineStore.ts @@ -1,7 +1,7 @@ import { create } from 'zustand'; import { immer } from 'zustand/middleware/immer'; -interface MachineStatus extends MachineSchemaEvent { +interface MachineStatus extends MachineEventSchema { productId: string; isActive: boolean; idleTime: number; @@ -9,10 +9,10 @@ interface MachineStatus extends MachineSchemaEvent { } interface MachineStore { - machines: Record; + machines: MachineStatus[]; // Actions - addMachine: (productId: string, machine: MachineSchemaEvent) => void; + addMachine: (productId: string, machine: MachineEventSchema) => void; removeMachine: (modelUuid: string) => void; updateMachine: ( modelUuid: string, @@ -36,31 +36,31 @@ interface MachineStore { export const useMachineStore = create()( immer((set, get) => ({ - machines: {}, + machines: [], // Actions addMachine: (productId, machine) => { set((state) => { - state.machines[machine.modelUuid] = { + state.machines.push({ ...machine, productId, isActive: false, idleTime: 0, activeTime: 0, state: 'idle', - }; + }); }); }, removeMachine: (modelUuid) => { set((state) => { - delete state.machines[modelUuid]; + state.machines = state.machines.filter(m => m.modelUuid !== modelUuid); }); }, updateMachine: (modelUuid, updates) => { set((state) => { - const machine = state.machines[modelUuid]; + const machine = state.machines.find(m => m.modelUuid === modelUuid); if (machine) { Object.assign(machine, updates); } @@ -70,7 +70,7 @@ export const useMachineStore = create()( // Status updates setMachineActive: (modelUuid, isActive) => { set((state) => { - const machine = state.machines[modelUuid]; + const machine = state.machines.find(m => m.modelUuid === modelUuid); if (machine) { machine.isActive = isActive; } @@ -79,7 +79,7 @@ export const useMachineStore = create()( setMachineState: (modelUuid, newState) => { set((state) => { - const machine = state.machines[modelUuid]; + const machine = state.machines.find(m => m.modelUuid === modelUuid); if (machine) { machine.state = newState; } @@ -89,7 +89,7 @@ export const useMachineStore = create()( // Time tracking incrementActiveTime: (modelUuid, incrementBy) => { set((state) => { - const machine = state.machines[modelUuid]; + const machine = state.machines.find(m => m.modelUuid === modelUuid); if (machine) { machine.activeTime += incrementBy; } @@ -98,7 +98,7 @@ export const useMachineStore = create()( incrementIdleTime: (modelUuid, incrementBy) => { set((state) => { - const machine = state.machines[modelUuid]; + const machine = state.machines.find(m => m.modelUuid === modelUuid); if (machine) { machine.idleTime += incrementBy; } @@ -107,23 +107,19 @@ export const useMachineStore = create()( // Helpers getMachineById: (modelUuid) => { - return get().machines[modelUuid]; + return get().machines.find(m => m.modelUuid === modelUuid); }, getMachinesByProduct: (productId) => { - return Object.values(get().machines).filter( - m => m.productId === productId - ); + return get().machines.filter(m => m.productId === productId); }, getActiveMachines: () => { - return Object.values(get().machines).filter(m => m.isActive); + return get().machines.filter(m => m.isActive); }, getIdleMachines: () => { - return Object.values(get().machines).filter( - m => !m.isActive && m.state === 'idle' - ); + return get().machines.filter(m => !m.isActive && m.state === 'idle'); }, })) -); \ No newline at end of file +); diff --git a/app/src/store/simulation/useSimulationStore.ts b/app/src/store/simulation/useProductStore.ts similarity index 98% rename from app/src/store/simulation/useSimulationStore.ts rename to app/src/store/simulation/useProductStore.ts index dfd8991..41a13f6 100644 --- a/app/src/store/simulation/useSimulationStore.ts +++ b/app/src/store/simulation/useProductStore.ts @@ -123,7 +123,7 @@ export const useProductStore = create()( if (event && 'points' in event) { (event as ConveyorEventSchema).points.push(point as ConveyorPointSchema); } else if (event && 'point' in event) { - (event as VehicleSchemaEvent | RoboticArmSchemaEvent | MachineSchemaEvent | StorageSchemaEvent).point = point as any; + (event as VehicleEventSchema | RoboticArmEventSchema | MachineEventSchema | StorageEventSchema).point = point as any; } } }); @@ -193,7 +193,7 @@ export const useProductStore = create()( } else if ('point' in event) { const point = (event as any).point; if (event.type === "roboticArm") { - // Handle RoboticArmSchemaEvent + // Handle RoboticArmEventSchema if ('actions' in point) { point.actions = point.actions.filter((a: any) => a.actionUuid !== actionUuid); } diff --git a/app/src/store/simulation/useStorageUnitStore.ts b/app/src/store/simulation/useStorageUnitStore.ts index db9d3c2..6cf87e4 100644 --- a/app/src/store/simulation/useStorageUnitStore.ts +++ b/app/src/store/simulation/useStorageUnitStore.ts @@ -1,7 +1,7 @@ import { create } from 'zustand'; import { immer } from 'zustand/middleware/immer'; -interface StorageUnitStatus extends StorageSchemaEvent { +interface StorageUnitStatus extends StorageEventSchema { productId: string; isActive: boolean; idleTime: number; @@ -10,10 +10,10 @@ interface StorageUnitStatus extends StorageSchemaEvent { } interface StorageUnitStore { - storageUnits: Record; + storageUnits: StorageUnitStatus[]; // Actions - addStorageUnit: (productId: string, storageUnit: StorageSchemaEvent) => void; + addStorageUnit: (productId: string, storageUnit: StorageEventSchema) => void; removeStorageUnit: (modelUuid: string) => void; updateStorageUnit: ( modelUuid: string, @@ -42,34 +42,34 @@ interface StorageUnitStore { export const useStorageUnitStore = create()( immer((set, get) => ({ - storageUnits: {}, + storageUnits: [], // Actions addStorageUnit: (productId, storageUnit) => { set((state) => { - state.storageUnits[storageUnit.modelUuid] = { + state.storageUnits.push({ ...storageUnit, productId, isActive: false, idleTime: 0, activeTime: 0, - currentLoad: 0, // Initialize currentLoad to 0 + currentLoad: 0, state: 'idle', - }; + }); }); }, removeStorageUnit: (modelUuid) => { set((state) => { - delete state.storageUnits[modelUuid]; + state.storageUnits = state.storageUnits.filter(s => s.modelUuid !== modelUuid); }); }, updateStorageUnit: (modelUuid, updates) => { set((state) => { - const storageUnit = state.storageUnits[modelUuid]; - if (storageUnit) { - Object.assign(storageUnit, updates); + const unit = state.storageUnits.find(s => s.modelUuid === modelUuid); + if (unit) { + Object.assign(unit, updates); } }); }, @@ -77,18 +77,18 @@ export const useStorageUnitStore = create()( // Status updates setStorageUnitActive: (modelUuid, isActive) => { set((state) => { - const storageUnit = state.storageUnits[modelUuid]; - if (storageUnit) { - storageUnit.isActive = isActive; + const unit = state.storageUnits.find(s => s.modelUuid === modelUuid); + if (unit) { + unit.isActive = isActive; } }); }, setStorageUnitState: (modelUuid, newState) => { set((state) => { - const storageUnit = state.storageUnits[modelUuid]; - if (storageUnit) { - storageUnit.state = newState; + const unit = state.storageUnits.find(s => s.modelUuid === modelUuid); + if (unit) { + unit.state = newState; } }); }, @@ -96,9 +96,9 @@ export const useStorageUnitStore = create()( // Load updates updateStorageUnitLoad: (modelUuid, load) => { set((state) => { - const storageUnit = state.storageUnits[modelUuid]; - if (storageUnit) { - storageUnit.currentLoad = load; + const unit = state.storageUnits.find(s => s.modelUuid === modelUuid); + if (unit) { + unit.currentLoad = load; } }); }, @@ -106,53 +106,47 @@ export const useStorageUnitStore = create()( // Time tracking incrementActiveTime: (modelUuid, incrementBy) => { set((state) => { - const storageUnit = state.storageUnits[modelUuid]; - if (storageUnit) { - storageUnit.activeTime += incrementBy; + const unit = state.storageUnits.find(s => s.modelUuid === modelUuid); + if (unit) { + unit.activeTime += incrementBy; } }); }, incrementIdleTime: (modelUuid, incrementBy) => { set((state) => { - const storageUnit = state.storageUnits[modelUuid]; - if (storageUnit) { - storageUnit.idleTime += incrementBy; + const unit = state.storageUnits.find(s => s.modelUuid === modelUuid); + if (unit) { + unit.idleTime += incrementBy; } }); }, // Helpers getStorageUnitById: (modelUuid) => { - return get().storageUnits[modelUuid]; + return get().storageUnits.find(s => s.modelUuid === modelUuid); }, getStorageUnitsByProduct: (productId) => { - return Object.values(get().storageUnits).filter( - s => s.productId === productId - ); + return get().storageUnits.filter(s => s.productId === productId); }, getActiveStorageUnits: () => { - return Object.values(get().storageUnits).filter(s => s.isActive); + return get().storageUnits.filter(s => s.isActive); }, getIdleStorageUnits: () => { - return Object.values(get().storageUnits).filter( - s => !s.isActive && s.state === 'idle' - ); + return get().storageUnits.filter(s => !s.isActive && s.state === 'idle'); }, getFullStorageUnits: () => { - return Object.values(get().storageUnits).filter( + return get().storageUnits.filter( s => s.currentLoad >= s.point.action.storageCapacity ); }, getEmptyStorageUnits: () => { - return Object.values(get().storageUnits).filter( - s => s.currentLoad === 0 - ); + return get().storageUnits.filter(s => s.currentLoad === 0); }, })) -); \ No newline at end of file +); diff --git a/app/src/store/simulation/useVehicleStore.ts b/app/src/store/simulation/useVehicleStore.ts index da093ca..c12fcd9 100644 --- a/app/src/store/simulation/useVehicleStore.ts +++ b/app/src/store/simulation/useVehicleStore.ts @@ -1,7 +1,7 @@ import { create } from 'zustand'; import { immer } from 'zustand/middleware/immer'; -interface VehicleStatus extends VehicleSchemaEvent { +interface VehicleStatus extends VehicleEventSchema { productId: string; isActive: boolean; idleTime: number; @@ -11,28 +11,21 @@ interface VehicleStatus extends VehicleSchemaEvent { } interface VehiclesStore { - vehicles: Record; + vehicles: VehicleStatus[]; - // Vehicle actions - addVehicle: (productId: string, event: VehicleSchemaEvent) => void; + addVehicle: (productId: string, event: VehicleEventSchema) => void; removeVehicle: (modelUuid: string) => void; updateVehicle: ( modelUuid: string, updates: Partial> ) => void; - // Status updates setVehicleActive: (modelUuid: string, isActive: boolean) => void; updateVehicleLoad: (modelUuid: string, load: number) => void; - - // State management setVehicleState: (modelUuid: string, newState: VehicleStatus['state']) => void; - - // Time tracking incrementActiveTime: (modelUuid: string, incrementBy: number) => void; incrementIdleTime: (modelUuid: string, incrementBy: number) => void; - // Helper functions getVehicleById: (modelUuid: string) => VehicleStatus | undefined; getVehiclesByProduct: (productId: string) => VehicleStatus[]; getActiveVehicles: () => VehicleStatus[]; @@ -41,75 +34,67 @@ interface VehiclesStore { export const useVehicleStore = create()( immer((set, get) => ({ - vehicles: {}, + vehicles: [], addVehicle: (productId, event) => { set((state) => { - state.vehicles[event.modelUuid] = { + state.vehicles.push({ ...event, - isActive: false, productId, + isActive: false, idleTime: 0, activeTime: 0, currentLoad: 0, distanceTraveled: 0, - }; + }); }); }, removeVehicle: (modelUuid) => { set((state) => { - delete state.vehicles[modelUuid]; + state.vehicles = state.vehicles.filter(v => v.modelUuid !== modelUuid); }); }, updateVehicle: (modelUuid, updates) => { set((state) => { - const vehicle = state.vehicles[modelUuid]; + const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid); if (vehicle) { Object.assign(vehicle, updates); } }); }, - // Status updates setVehicleActive: (modelUuid, isActive) => { set((state) => { - const vehicle = state.vehicles[modelUuid]; + const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid); if (vehicle) { vehicle.isActive = isActive; - if (isActive) { - vehicle.state = 'running'; - } else { - vehicle.state = vehicle.currentLoad > 0 ? 'idle' : 'stopped'; - } } }); }, updateVehicleLoad: (modelUuid, load) => { set((state) => { - const vehicle = state.vehicles[modelUuid]; + const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid); if (vehicle) { vehicle.currentLoad = load; } }); }, - // State management setVehicleState: (modelUuid, newState) => { set((state) => { - const vehicle = state.vehicles[modelUuid]; + const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid); if (vehicle) { vehicle.state = newState; } }); }, - // Time tracking incrementActiveTime: (modelUuid, incrementBy) => { set((state) => { - const vehicle = state.vehicles[modelUuid]; + const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid); if (vehicle) { vehicle.activeTime += incrementBy; } @@ -118,32 +103,27 @@ export const useVehicleStore = create()( incrementIdleTime: (modelUuid, incrementBy) => { set((state) => { - const vehicle = state.vehicles[modelUuid]; + const vehicle = state.vehicles.find(v => v.modelUuid === modelUuid); if (vehicle) { vehicle.idleTime += incrementBy; } }); }, - // Getters getVehicleById: (modelUuid) => { - return get().vehicles[modelUuid]; + return get().vehicles.find(v => v.modelUuid === modelUuid); }, getVehiclesByProduct: (productId) => { - return Object.values(get().vehicles).filter( - v => v.productId === productId - ); + return get().vehicles.filter(v => v.productId === productId); }, getActiveVehicles: () => { - return Object.values(get().vehicles).filter(v => v.isActive); + return get().vehicles.filter(v => v.isActive); }, getIdleVehicles: () => { - return Object.values(get().vehicles).filter( - v => !v.isActive && v.currentLoad > 0 - ); + return get().vehicles.filter(v => !v.isActive && v.currentLoad > 0); } })) -); \ No newline at end of file +); diff --git a/app/src/types/simulationTypes.d.ts b/app/src/types/simulationTypes.d.ts index 07a04fa..8f0c180 100644 --- a/app/src/types/simulationTypes.d.ts +++ b/app/src/types/simulationTypes.d.ts @@ -97,29 +97,29 @@ interface ConveyorEventSchema extends AssetEventSchema { points: ConveyorPointSchema[]; } -interface VehicleSchemaEvent extends AssetEventSchema { +interface VehicleEventSchema extends AssetEventSchema { type: "vehicle"; speed: number; point: VehiclePointSchema; } -interface RoboticArmSchemaEvent extends AssetEventSchema { +interface RoboticArmEventSchema extends AssetEventSchema { type: "roboticArm"; speed: number; point: RoboticArmPointSchema; } -interface MachineSchemaEvent extends AssetEventSchema { +interface MachineEventSchema extends AssetEventSchema { type: "machine"; point: MachinePointSchema; } -interface StorageSchemaEvent extends AssetEventSchema { +interface StorageEventSchema extends AssetEventSchema { type: "storageUnit"; point: StoragePointSchema; } -type EventsSchema = ConveyorEventSchema | VehicleSchemaEvent | RoboticArmSchemaEvent | MachineSchemaEvent | StorageSchemaEvent | []; +type EventsSchema = ConveyorEventSchema | VehicleEventSchema | RoboticArmEventSchema | MachineEventSchema | StorageEventSchema | []; type productsSchema = { productName: string;