129 lines
3.7 KiB
TypeScript
129 lines
3.7 KiB
TypeScript
|
import { create } from 'zustand';
|
||
|
import { immer } from 'zustand/middleware/immer';
|
||
|
|
||
|
interface MachineStatus extends MachineSchemaEvent {
|
||
|
productId: string;
|
||
|
isActive: boolean;
|
||
|
idleTime: number;
|
||
|
activeTime: number;
|
||
|
}
|
||
|
|
||
|
interface MachineStore {
|
||
|
machines: Record<string, MachineStatus>;
|
||
|
|
||
|
// Actions
|
||
|
addMachine: (productId: string, machine: MachineSchemaEvent) => void;
|
||
|
removeMachine: (modelUuid: string) => void;
|
||
|
updateMachine: (
|
||
|
modelUuid: string,
|
||
|
updates: Partial<Omit<MachineStatus, 'modelUuid' | 'productId'>>
|
||
|
) => void;
|
||
|
|
||
|
// Status updates
|
||
|
setMachineActive: (modelUuid: string, isActive: boolean) => void;
|
||
|
setMachineState: (modelUuid: string, newState: MachineStatus['state']) => void;
|
||
|
|
||
|
// Time tracking
|
||
|
incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
|
||
|
incrementIdleTime: (modelUuid: string, incrementBy: number) => void;
|
||
|
|
||
|
// Helpers
|
||
|
getMachineById: (modelUuid: string) => MachineStatus | undefined;
|
||
|
getMachinesByProduct: (productId: string) => MachineStatus[];
|
||
|
getActiveMachines: () => MachineStatus[];
|
||
|
getIdleMachines: () => MachineStatus[];
|
||
|
}
|
||
|
|
||
|
export const useMachineStore = create<MachineStore>()(
|
||
|
immer((set, get) => ({
|
||
|
machines: {},
|
||
|
|
||
|
// Actions
|
||
|
addMachine: (productId, machine) => {
|
||
|
set((state) => {
|
||
|
state.machines[machine.modelUuid] = {
|
||
|
...machine,
|
||
|
productId,
|
||
|
isActive: false,
|
||
|
idleTime: 0,
|
||
|
activeTime: 0,
|
||
|
state: 'idle',
|
||
|
};
|
||
|
});
|
||
|
},
|
||
|
|
||
|
removeMachine: (modelUuid) => {
|
||
|
set((state) => {
|
||
|
delete state.machines[modelUuid];
|
||
|
});
|
||
|
},
|
||
|
|
||
|
updateMachine: (modelUuid, updates) => {
|
||
|
set((state) => {
|
||
|
const machine = state.machines[modelUuid];
|
||
|
if (machine) {
|
||
|
Object.assign(machine, updates);
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Status updates
|
||
|
setMachineActive: (modelUuid, isActive) => {
|
||
|
set((state) => {
|
||
|
const machine = state.machines[modelUuid];
|
||
|
if (machine) {
|
||
|
machine.isActive = isActive;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
setMachineState: (modelUuid, newState) => {
|
||
|
set((state) => {
|
||
|
const machine = state.machines[modelUuid];
|
||
|
if (machine) {
|
||
|
machine.state = newState;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Time tracking
|
||
|
incrementActiveTime: (modelUuid, incrementBy) => {
|
||
|
set((state) => {
|
||
|
const machine = state.machines[modelUuid];
|
||
|
if (machine) {
|
||
|
machine.activeTime += incrementBy;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
incrementIdleTime: (modelUuid, incrementBy) => {
|
||
|
set((state) => {
|
||
|
const machine = state.machines[modelUuid];
|
||
|
if (machine) {
|
||
|
machine.idleTime += incrementBy;
|
||
|
}
|
||
|
});
|
||
|
},
|
||
|
|
||
|
// Helpers
|
||
|
getMachineById: (modelUuid) => {
|
||
|
return get().machines[modelUuid];
|
||
|
},
|
||
|
|
||
|
getMachinesByProduct: (productId) => {
|
||
|
return Object.values(get().machines).filter(
|
||
|
m => m.productId === productId
|
||
|
);
|
||
|
},
|
||
|
|
||
|
getActiveMachines: () => {
|
||
|
return Object.values(get().machines).filter(m => m.isActive);
|
||
|
},
|
||
|
|
||
|
getIdleMachines: () => {
|
||
|
return Object.values(get().machines).filter(
|
||
|
m => !m.isActive && m.state === 'idle'
|
||
|
);
|
||
|
},
|
||
|
}))
|
||
|
);
|