unit/#95-use-smartphone-as-barcode-scanner #111

Merged
jkeffects merged 4 commits from unit/#95-use-smartphone-as-barcode-scanner into milestone/ff-admin-unit 2025-07-15 15:00:10 +00:00
3 changed files with 51 additions and 13 deletions
Showing only changes of commit 3e47d3ebf6 - Show all commits

4
src/enums/socketEnum.ts Normal file
View file

@ -0,0 +1,4 @@
export enum SocketConnectionTypes {
scanner = "/scanner",
pscanner = "/public_scanner",
}

View file

@ -1,21 +1,24 @@
import { Manager, Socket } from "socket.io-client"; import { Manager, Socket } from "socket.io-client";
import { refreshToken, url } from "./serverCom"; import { refreshToken, url } from "./serverCom";
import { useNotificationStore } from "./stores/notification"; import { useNotificationStore } from "./stores/notification";
import { SocketConnectionTypes } from "./enums/socketEnum";
export enum SocketConnectionTypes {
scanner = "/scanner",
pscanner = "/public_scanner",
}
export abstract class SocketManager { export abstract class SocketManager {
private readonly manager = new Manager(url, { private static readonly manager = new Manager(url, {
reconnection: true, reconnection: true,
reconnectionDelayMax: 10000, reconnectionDelayMax: 10000,
}); });
private readonly connections = new Map<SocketConnectionTypes, Socket>(); private static readonly connections = new Map<SocketConnectionTypes, Socket>();
public static establishConnection(
connection: SocketConnectionTypes,
restoreAfterDisconnect: boolean = false
): Socket {
const existingSocket = this.connections.get(connection);
if (existingSocket !== undefined && existingSocket.connected) return existingSocket!;
existingSocket?.removeAllListeners();
public establishConnection(connection: SocketConnectionTypes) {
if (this.connections.has(connection)) return this.connections.get(connection);
const notificationStore = useNotificationStore(); const notificationStore = useNotificationStore();
let socket = this.manager.socket(connection, { let socket = this.manager.socket(connection, {
auth: (cb) => { auth: (cb) => {
@ -29,7 +32,8 @@ export abstract class SocketManager {
this.socketHandleError(connection, err, true); this.socketHandleError(connection, err, true);
}); });
socket.on("disconnect", () => { socket.on("disconnect", () => {
this.establishConnection(connection); if (restoreAfterDisconnect) this.establishConnection(connection);
else notificationStore.push("Socket", `Verbindung getrennt`, "info");
}); });
socket.on("warning", (msg: string) => { socket.on("warning", (msg: string) => {
notificationStore.push("Socket-Warnung", msg, "warning"); notificationStore.push("Socket-Warnung", msg, "warning");
@ -45,11 +49,11 @@ export abstract class SocketManager {
return socket; return socket;
} }
public getConnection(connection: SocketConnectionTypes) { public static getConnection(connection: SocketConnectionTypes) {
return this.connections.get(connection); return this.connections.get(connection);
} }
public closeConnection(connection: SocketConnectionTypes) { public static closeConnection(connection: SocketConnectionTypes) {
let socket = this.connections.get(connection); let socket = this.connections.get(connection);
if (socket) { if (socket) {
socket.disconnect(); socket.disconnect();
@ -57,7 +61,7 @@ export abstract class SocketManager {
} }
} }
private socketHandleError(connection: SocketConnectionTypes, err: Error, onConnect = false) { private static socketHandleError(connection: SocketConnectionTypes, err: Error, onConnect = false) {
const notificationStore = useNotificationStore(); const notificationStore = useNotificationStore();
if (err.message == "xhr poll error") { if (err.message == "xhr poll error") {

View file

@ -1,5 +1,8 @@
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { v4 as uuid } from "uuid"; import { v4 as uuid } from "uuid";
import { SocketManager } from "../../socketManager";
import { SocketConnectionTypes } from "../../enums/socketEnum";
import { useNotificationStore } from "../notification";
export const useScannerStore = defineStore("scanner", { export const useScannerStore = defineStore("scanner", {
state: () => { state: () => {
@ -13,13 +16,40 @@ export const useScannerStore = defineStore("scanner", {
actions: { actions: {
startSession() { startSession() {
if (this.inUse) return; if (this.inUse) return;
const notificationStore = useNotificationStore();
this.roomId = uuid(); this.roomId = uuid();
this.inUse = true; 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() { endSession() {
this.inUse = false; this.inUse = false;
this.roomId = undefined; this.roomId = undefined;
this.results = []; this.results = [];
SocketManager.getConnection(SocketConnectionTypes.scanner)?.emit("session:close");
}, },
removeElementFromResults(el: string) { removeElementFromResults(el: string) {
this.results = this.results.filter((result) => result !== el); this.results = this.results.filter((result) => result !== el);