feat: Implement Zustand stores for machine, simulation, storage unit, vehicle, and visualization management
- Added `useMachineStore` for managing machine statuses, including actions for adding, removing, and updating machines. - Introduced `useSimulationStore` to handle product and event management with actions for adding, removing, and updating products and events. - Created `useStorageUnitStore` for managing storage unit statuses, including load tracking and state updates. - Developed `useVehicleStore` for vehicle management, including load and state updates. - Implemented `useChartStore` for managing measurement data and visualization settings. - Added `useDroppedObjectsStore` for handling dropped objects in visualization zones, including object manipulation actions. - Created `useZone3DWidgetStore` for managing 3D widget data in zones, including position and rotation updates. - Introduced `useZoneStore` for managing selected zone states and widget configurations.
This commit is contained in:
158
app/src/store/simulation/useStorageUnitStore.ts
Normal file
158
app/src/store/simulation/useStorageUnitStore.ts
Normal file
@@ -0,0 +1,158 @@
|
||||
import { create } from 'zustand';
|
||||
import { immer } from 'zustand/middleware/immer';
|
||||
|
||||
interface StorageUnitStatus extends StorageSchemaEvent {
|
||||
productId: string;
|
||||
isActive: boolean;
|
||||
idleTime: number;
|
||||
activeTime: number;
|
||||
currentLoad: number;
|
||||
}
|
||||
|
||||
interface StorageUnitStore {
|
||||
storageUnits: Record<string, StorageUnitStatus>;
|
||||
|
||||
// Actions
|
||||
addStorageUnit: (productId: string, storageUnit: StorageSchemaEvent) => void;
|
||||
removeStorageUnit: (modelUuid: string) => void;
|
||||
updateStorageUnit: (
|
||||
modelUuid: string,
|
||||
updates: Partial<Omit<StorageUnitStatus, 'modelUuid' | 'productId'>>
|
||||
) => void;
|
||||
|
||||
// Status updates
|
||||
setStorageUnitActive: (modelUuid: string, isActive: boolean) => void;
|
||||
setStorageUnitState: (modelUuid: string, newState: StorageUnitStatus['state']) => void;
|
||||
|
||||
// Load updates
|
||||
updateStorageUnitLoad: (modelUuid: string, load: number) => void;
|
||||
|
||||
// Time tracking
|
||||
incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
|
||||
incrementIdleTime: (modelUuid: string, incrementBy: number) => void;
|
||||
|
||||
// Helpers
|
||||
getStorageUnitById: (modelUuid: string) => StorageUnitStatus | undefined;
|
||||
getStorageUnitsByProduct: (productId: string) => StorageUnitStatus[];
|
||||
getActiveStorageUnits: () => StorageUnitStatus[];
|
||||
getIdleStorageUnits: () => StorageUnitStatus[];
|
||||
getFullStorageUnits: () => StorageUnitStatus[];
|
||||
getEmptyStorageUnits: () => StorageUnitStatus[];
|
||||
}
|
||||
|
||||
export const useStorageUnitStore = create<StorageUnitStore>()(
|
||||
immer((set, get) => ({
|
||||
storageUnits: {},
|
||||
|
||||
// Actions
|
||||
addStorageUnit: (productId, storageUnit) => {
|
||||
set((state) => {
|
||||
state.storageUnits[storageUnit.modelUuid] = {
|
||||
...storageUnit,
|
||||
productId,
|
||||
isActive: false,
|
||||
idleTime: 0,
|
||||
activeTime: 0,
|
||||
currentLoad: 0, // Initialize currentLoad to 0
|
||||
state: 'idle',
|
||||
};
|
||||
});
|
||||
},
|
||||
|
||||
removeStorageUnit: (modelUuid) => {
|
||||
set((state) => {
|
||||
delete state.storageUnits[modelUuid];
|
||||
});
|
||||
},
|
||||
|
||||
updateStorageUnit: (modelUuid, updates) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
Object.assign(storageUnit, updates);
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Status updates
|
||||
setStorageUnitActive: (modelUuid, isActive) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.isActive = isActive;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
setStorageUnitState: (modelUuid, newState) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.state = newState;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Load updates
|
||||
updateStorageUnitLoad: (modelUuid, load) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.currentLoad = load;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Time tracking
|
||||
incrementActiveTime: (modelUuid, incrementBy) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.activeTime += incrementBy;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
incrementIdleTime: (modelUuid, incrementBy) => {
|
||||
set((state) => {
|
||||
const storageUnit = state.storageUnits[modelUuid];
|
||||
if (storageUnit) {
|
||||
storageUnit.idleTime += incrementBy;
|
||||
}
|
||||
});
|
||||
},
|
||||
|
||||
// Helpers
|
||||
getStorageUnitById: (modelUuid) => {
|
||||
return get().storageUnits[modelUuid];
|
||||
},
|
||||
|
||||
getStorageUnitsByProduct: (productId) => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
s => s.productId === productId
|
||||
);
|
||||
},
|
||||
|
||||
getActiveStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(s => s.isActive);
|
||||
},
|
||||
|
||||
getIdleStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
s => !s.isActive && s.state === 'idle'
|
||||
);
|
||||
},
|
||||
|
||||
getFullStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
s => s.currentLoad >= s.point.action.storageCapacity
|
||||
);
|
||||
},
|
||||
|
||||
getEmptyStorageUnits: () => {
|
||||
return Object.values(get().storageUnits).filter(
|
||||
s => s.currentLoad === 0
|
||||
);
|
||||
},
|
||||
}))
|
||||
);
|
||||
Reference in New Issue
Block a user