2025-07-15 13:19:59 +02:00
|
|
|
import { defineStore } from "pinia";
|
|
|
|
import { v4 as uuid } from "uuid";
|
2025-07-25 12:45:59 +02:00
|
|
|
import { SocketManager } from "@/socketManager";
|
|
|
|
import { SocketConnectionTypes } from "@/enums/socketEnum";
|
2025-07-15 15:16:18 +02:00
|
|
|
import { useNotificationStore } from "../notification";
|
2025-07-15 13:19:59 +02:00
|
|
|
|
|
|
|
export const useScannerStore = defineStore("scanner", {
|
|
|
|
state: () => {
|
|
|
|
return {
|
|
|
|
inUse: false as boolean,
|
|
|
|
roomId: undefined as undefined | string,
|
|
|
|
results: [] as Array<string>,
|
|
|
|
connectedDevices: 0 as number,
|
|
|
|
};
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
startSession() {
|
|
|
|
if (this.inUse) return;
|
2025-07-15 15:16:18 +02:00
|
|
|
|
|
|
|
const notificationStore = useNotificationStore();
|
|
|
|
|
2025-07-15 13:19:59 +02:00
|
|
|
this.roomId = uuid();
|
|
|
|
this.inUse = true;
|
2025-07-15 15:16:18 +02:00
|
|
|
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");
|
|
|
|
});
|
2025-07-15 13:19:59 +02:00
|
|
|
},
|
|
|
|
endSession() {
|
|
|
|
this.inUse = false;
|
|
|
|
this.roomId = undefined;
|
|
|
|
this.results = [];
|
2025-07-15 15:16:18 +02:00
|
|
|
SocketManager.getConnection(SocketConnectionTypes.scanner)?.emit("session:close");
|
2025-07-15 13:19:59 +02:00
|
|
|
},
|
|
|
|
removeElementFromResults(el: string) {
|
|
|
|
this.results = this.results.filter((result) => result !== el);
|
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|