129 lines
3.8 KiB
TypeScript
129 lines
3.8 KiB
TypeScript
|
import { create } from 'zustand';
|
||
|
import { immer } from 'zustand/middleware/immer';
|
||
|
|
||
|
interface ConveyorStatus extends ConveyorEventSchema {
|
||
|
productId: string;
|
||
|
isActive: boolean;
|
||
|
idleTime: number;
|
||
|
activeTime: number;
|
||
|
}
|
||
|
|
||
|
interface ConveyorStore {
|
||
|
conveyors: Record<string, ConveyorStatus>;
|
||
|
|
||
|
// Actions
|
||
|
addConveyor: (productId: string, event: ConveyorEventSchema) => void;
|
||
|
removeConveyor: (modelUuid: string) => void;
|
||
|
updateConveyor: (
|
||
|
modelUuid: string,
|
||
|
updates: Partial<Omit<ConveyorStatus, 'modelUuid' | 'productId'>>
|
||
|
) => 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[];
|
||
|
getIdleConveyors: () => ConveyorStatus[];
|
||
|
}
|
||
|
|
||
|
export const useConveyorStore = create<ConveyorStore>()(
|
||
|
immer((set, get) => ({
|
||
|
conveyors: {},
|
||
|
|
||
|
// Actions
|
||
|
addConveyor: (productId, event) => {
|
||
|
set((state) => {
|
||
|
state.conveyors[event.modelUuid] = {
|
||
|
...event,
|
||
|
productId,
|
||
|
isActive: false,
|
||
|
idleTime: 0,
|
||
|
activeTime: 0,
|
||
|
state: 'idle',
|
||
|
};
|
||
|
});
|
||
|
},
|
||
|
|
||
|
removeConveyor: (modelUuid) => {
|
||
|
set((state) => {
|
||
|
delete state.conveyors[modelUuid];
|
||
|
});
|
||
|
},
|
||
|
|
||
|
updateConveyor: (modelUuid, updates) => {
|
||
|
set((state) => {
|
||
|
const conveyor = state.conveyors[modelUuid];
|
||
|
if (conveyor) {
|
||
|
Object.assign(conveyor, updates);
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Status updates
|
||
|
setConveyorActive: (modelUuid, isActive) => {
|
||
|
set((state) => {
|
||
|
const conveyor = state.conveyors[modelUuid];
|
||
|
if (conveyor) {
|
||
|
conveyor.isActive = isActive;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
setConveyorState: (modelUuid, newState) => {
|
||
|
set((state) => {
|
||
|
const conveyor = state.conveyors[modelUuid];
|
||
|
if (conveyor) {
|
||
|
conveyor.state = newState;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Time tracking
|
||
|
incrementActiveTime: (modelUuid, incrementBy) => {
|
||
|
set((state) => {
|
||
|
const conveyor = state.conveyors[modelUuid];
|
||
|
if (conveyor) {
|
||
|
conveyor.activeTime += incrementBy;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
incrementIdleTime: (modelUuid, incrementBy) => {
|
||
|
set((state) => {
|
||
|
const conveyor = state.conveyors[modelUuid];
|
||
|
if (conveyor) {
|
||
|
conveyor.idleTime += incrementBy;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Helper functions
|
||
|
getConveyorById: (modelUuid) => {
|
||
|
return get().conveyors[modelUuid];
|
||
|
},
|
||
|
|
||
|
getConveyorsByProduct: (productId) => {
|
||
|
return Object.values(get().conveyors).filter(
|
||
|
c => c.productId === productId
|
||
|
);
|
||
|
},
|
||
|
|
||
|
getActiveConveyors: () => {
|
||
|
return Object.values(get().conveyors).filter(c => c.isActive);
|
||
|
},
|
||
|
|
||
|
getIdleConveyors: () => {
|
||
|
return Object.values(get().conveyors).filter(
|
||
|
c => !c.isActive && c.state === 'idle'
|
||
|
);
|
||
|
},
|
||
|
}))
|
||
|
);
|