import { defineStore } from "pinia"; import { v4 as uuid } from "uuid"; import { SocketManager } from "@/socketManager"; import { SocketConnectionTypes } from "@/enums/socketEnum"; import { useNotificationStore } from "../notification"; export const useScannerStore = defineStore("scanner", { state: () => { return { inUse: false as boolean, roomId: undefined as undefined | string, results: [] as Array, connectedDevices: 0 as number, }; }, actions: { startSession() { if (this.inUse) return; const notificationStore = useNotificationStore(); this.roomId = uuid(); this.inUse = true; let connection = SocketManager.establishConnection(SocketConnectionTypes.scanner); connection.on("connect", () => { SocketManager.getConnection(SocketConnectionTypes.scanner)?.emit("session:create", this.roomId); }); connection.on("status-session:create", () => { notificationStore.push("Socket-Erfolg", `Scan-Session gestartet`, "success"); }); connection.on("status-session:close", () => { notificationStore.push("Socket", `Scan-Session beendet`, "info"); SocketManager.getConnection(SocketConnectionTypes.scanner)?.disconnect(); }); connection.on("package-scanner_join", (socketId: string) => { this.connectedDevices++; notificationStore.push("Scan-Verbindung", `Neuer Scanner verbunden`, "info"); }); connection.on("package-scanner_leave", (socketId: string) => { this.connectedDevices--; notificationStore.push("Scan-Verbindung", `Scanner getrennt`, "info"); }); connection.on("package-scan_receive", (result: string) => { this.results.push(result); notificationStore.push("Scan", `Neuen Scan erhalten`, "info"); }); }, endSession() { this.inUse = false; this.roomId = undefined; this.results = []; SocketManager.getConnection(SocketConnectionTypes.scanner)?.emit("session:close"); }, removeElementFromResults(el: string) { this.results = this.results.filter((result) => result !== el); }, }, });