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.
This commit is contained in:
@@ -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<string, ArmBotStatus>;
|
||||
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<Omit<ArmBotStatus, 'modelUuid' | 'productId'>>
|
||||
) => 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<ArmBotStore>()(
|
||||
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<ArmBotStore>()(
|
||||
|
||||
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<ArmBotStore>()(
|
||||
|
||||
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<ArmBotStore>()(
|
||||
});
|
||||
},
|
||||
|
||||
// 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<ArmBotStore>()(
|
||||
|
||||
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);
|
||||
}
|
||||
}))
|
||||
);
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user