171 lines
4.6 KiB
TypeScript
171 lines
4.6 KiB
TypeScript
import { create } from "zustand";
|
|
import { immer } from "zustand/middleware/immer";
|
|
|
|
interface ArmBotStore {
|
|
armBots: ArmBotStatus[];
|
|
|
|
addArmBot: (productId: string, event: RoboticArmEventSchema) => void;
|
|
removeArmBot: (modelUuid: string) => void;
|
|
|
|
updateArmBot: (
|
|
modelUuid: string,
|
|
updates: Partial<Omit<ArmBotStatus, "modelUuid" | "productId">>
|
|
) => void;
|
|
|
|
addCurrentAction: (modelUuid: string, actionUuid: string) => void;
|
|
removeCurrentAction: (modelUuid: string) => void;
|
|
|
|
addAction: (
|
|
modelUuid: string,
|
|
action: RoboticArmPointSchema["actions"][number]
|
|
) => void;
|
|
removeAction: (modelUuid: string, actionUuid: string) => void;
|
|
|
|
setArmBotActive: (modelUuid: string, isActive: boolean) => void;
|
|
|
|
incrementActiveTime: (modelUuid: string, incrementBy: number) => void;
|
|
incrementIdleTime: (modelUuid: string, incrementBy: number) => void;
|
|
|
|
getArmBotById: (modelUuid: string) => ArmBotStatus | undefined;
|
|
getArmBotsByProduct: (productId: string) => ArmBotStatus[];
|
|
getArmBotsByState: (state: string) => ArmBotStatus[];
|
|
getActiveArmBots: () => ArmBotStatus[];
|
|
getIdleArmBots: () => ArmBotStatus[];
|
|
getArmBotsByCurrentAction: (actionUuid: string) => ArmBotStatus[];
|
|
}
|
|
|
|
export const useArmBotStore = create<ArmBotStore>()(
|
|
immer((set, get) => ({
|
|
armBots: [],
|
|
|
|
addArmBot: (productId, event) => {
|
|
set((state) => {
|
|
state.armBots.push({
|
|
...event,
|
|
productId,
|
|
isActive: false,
|
|
idleTime: 0,
|
|
activeTime: 0,
|
|
state: "idle",
|
|
});
|
|
});
|
|
},
|
|
|
|
removeArmBot: (modelUuid) => {
|
|
set((state) => {
|
|
state.armBots = state.armBots.filter((a) => a.modelUuid !== modelUuid);
|
|
});
|
|
},
|
|
|
|
updateArmBot: (modelUuid, updates) => {
|
|
set((state) => {
|
|
const armBot = state.armBots.find((a) => a.modelUuid === modelUuid);
|
|
if (armBot) {
|
|
Object.assign(armBot, updates);
|
|
}
|
|
});
|
|
},
|
|
|
|
addCurrentAction: (modelUuid, actionUuid) => {
|
|
set((state) => {
|
|
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,
|
|
};
|
|
armBot.isActive = true;
|
|
}
|
|
}
|
|
});
|
|
},
|
|
|
|
removeCurrentAction: (modelUuid) => {
|
|
set((state) => {
|
|
const armBot = state.armBots.find((a) => a.modelUuid === modelUuid);
|
|
if (armBot) {
|
|
armBot.currentAction = undefined;
|
|
armBot.isActive = false;
|
|
}
|
|
});
|
|
},
|
|
|
|
addAction: (modelUuid, action) => {
|
|
set((state) => {
|
|
const armBot = state.armBots.find((a) => a.modelUuid === modelUuid);
|
|
if (armBot) {
|
|
armBot.point.actions.push(action);
|
|
}
|
|
});
|
|
},
|
|
|
|
removeAction: (modelUuid, actionUuid) => {
|
|
set((state) => {
|
|
const armBot = state.armBots.find((a) => a.modelUuid === modelUuid);
|
|
if (armBot) {
|
|
armBot.point.actions = armBot.point.actions.filter(
|
|
(a) => a.actionUuid !== actionUuid
|
|
);
|
|
}
|
|
});
|
|
},
|
|
|
|
setArmBotActive: (modelUuid, isActive) => {
|
|
set((state) => {
|
|
const armBot = state.armBots.find((a) => a.modelUuid === modelUuid);
|
|
if (armBot) {
|
|
armBot.isActive = isActive;
|
|
}
|
|
});
|
|
},
|
|
|
|
incrementActiveTime: (modelUuid, incrementBy) => {
|
|
set((state) => {
|
|
const armBot = state.armBots.find((a) => a.modelUuid === modelUuid);
|
|
if (armBot) {
|
|
armBot.activeTime += incrementBy;
|
|
}
|
|
});
|
|
},
|
|
|
|
incrementIdleTime: (modelUuid, incrementBy) => {
|
|
set((state) => {
|
|
const armBot = state.armBots.find((a) => a.modelUuid === modelUuid);
|
|
if (armBot) {
|
|
armBot.idleTime += incrementBy;
|
|
}
|
|
});
|
|
},
|
|
|
|
getArmBotById: (modelUuid) => {
|
|
return get().armBots.find((a) => a.modelUuid === modelUuid);
|
|
},
|
|
|
|
getArmBotsByProduct: (productId) => {
|
|
return get().armBots.filter((a) => a.productId === productId);
|
|
},
|
|
|
|
getArmBotsByState: (state) => {
|
|
return get().armBots.filter((a) => a.state === state);
|
|
},
|
|
|
|
getActiveArmBots: () => {
|
|
return get().armBots.filter((a) => a.isActive);
|
|
},
|
|
|
|
getIdleArmBots: () => {
|
|
return get().armBots.filter((a) => !a.isActive && a.state === "idle");
|
|
},
|
|
|
|
getArmBotsByCurrentAction: (actionUuid) => {
|
|
return get().armBots.filter(
|
|
(a) => a.currentAction?.actionUuid === actionUuid
|
|
);
|
|
},
|
|
}))
|
|
);
|