From 9ef76a7c26bb6e36a4069a08bbe24b3eaeb1225a Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 15 Jul 2025 11:52:44 +0200 Subject: [PATCH 1/4] add socketio to app --- src/serverCom.ts | 4 +- src/socketManager.ts | 81 ++++++++++++++++++++++++++++++++++++++ src/stores/notification.ts | 2 +- 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 src/socketManager.ts diff --git a/src/serverCom.ts b/src/serverCom.ts index ad4d39f..5b10d28 100644 --- a/src/serverCom.ts +++ b/src/serverCom.ts @@ -78,7 +78,7 @@ http.interceptors.response.use( } ); -export async function refreshToken(): Promise { +async function refreshToken(): Promise { return new Promise(async (resolve, reject) => { await http .post(`/auth/refresh`, { @@ -135,4 +135,4 @@ async function* streamingFetch(path: string, abort?: AbortController) { } } -export { http, newEventSource, streamingFetch, host, url }; +export { http, newEventSource, streamingFetch, host, url, refreshToken }; diff --git a/src/socketManager.ts b/src/socketManager.ts new file mode 100644 index 0000000..85c0c44 --- /dev/null +++ b/src/socketManager.ts @@ -0,0 +1,81 @@ +import { Manager, Socket } from "socket.io-client"; +import { refreshToken, url } from "./serverCom"; +import { useNotificationStore } from "./stores/notification"; + +export enum SocketConnectionTypes { + scanner = "/scanner", + pscanner = "/public_scanner", +} + +export abstract class SocketManager { + private readonly manager = new Manager(url, { + reconnection: true, + reconnectionDelayMax: 10000, + }); + private readonly connections = new Map(); + + public establishConnection(connection: SocketConnectionTypes) { + if (this.connections.has(connection)) return this.connections.get(connection); + const notificationStore = useNotificationStore(); + let socket = this.manager.socket(connection, { + auth: (cb) => { + cb({ token: localStorage.getItem("accessToken") }); + }, + }); + socket.on("connect", () => { + notificationStore.push("Socket-Erfolg", `Verbindung aufgebaut`, "success"); + }); + socket.on("connect_error", (err) => { + this.socketHandleError(connection, err, true); + }); + socket.on("disconnect", () => { + this.establishConnection(connection); + }); + socket.on("warning", (msg: string) => { + notificationStore.push("Socket-Warnung", msg, "warning"); + }); + socket.on("error", (msg: string) => { + this.socketHandleError(connection, { + name: "Error", + message: msg, + }); + }); + + this.connections.set(connection, socket); + return socket; + } + + public getConnection(connection: SocketConnectionTypes) { + return this.connections.get(connection); + } + + public closeConnection(connection: SocketConnectionTypes) { + let socket = this.connections.get(connection); + if (socket) { + socket.disconnect(); + this.connections.delete(connection); + } + } + + private socketHandleError(connection: SocketConnectionTypes, err: Error, onConnect = false) { + const notificationStore = useNotificationStore(); + + if (err.message == "xhr poll error") { + notificationStore.push("Socket-Netzwerk-Fehler", "Reconnect Versuch in 10s", "error"); + } else if (err.message == "Token expired") { + notificationStore.push("Session", "Session wird verlängert", "info"); + refreshToken() + .then(() => { + notificationStore.push("Session", "Session erfolgreich verlängert", "success"); + this.closeConnection(connection); + }) + .catch(() => { + notificationStore.push("Session-Fehler", "Anmeldung wurde nicht verlängert", "error"); + }); + } else if (onConnect) { + notificationStore.push("Socket-Fehler", `Verbindung fehlgeschlagen`, "error"); + } else { + notificationStore.push("Socket-Fehler", err.message, "error"); + } + } +} diff --git a/src/stores/notification.ts b/src/stores/notification.ts index 872b108..0ec8fc0 100644 --- a/src/stores/notification.ts +++ b/src/stores/notification.ts @@ -8,7 +8,7 @@ export interface Notification { indicator: boolean; } -export type NotificationType = "info" | "warning" | "error"; +export type NotificationType = "info" | "warning" | "error" | "success"; export const useNotificationStore = defineStore("notification", { state: () => { -- 2.47.3 From ed947e5777d3a02f92da5a7cfe14bb5f88ced082 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Tue, 15 Jul 2025 13:19:59 +0200 Subject: [PATCH 2/4] remodel scan process and visualize external --- src/components/Notification.vue | 7 ++ src/components/Pagination.vue | 2 +- src/components/TextCopy.vue | 2 +- src/components/scanner/ManageScanModal.vue | 64 +++++++++++++++ src/components/scanner/Phone.vue | 80 +++++++++++++++++++ src/components/{ => scanner}/ScanInput.vue | 4 +- src/components/scanner/ScanQRCodeModal.vue | 51 ++++++++++++ src/components/scanner/ScanResultsModal.vue | 41 ++++++++++ src/components/scanner/ScanSidebarInfo.vue | 42 ++++++++++ .../{CodeDetector.vue => scanner/Scanner.vue} | 20 ++--- .../search/EquipmentSearchSelect.vue | 2 +- src/components/search/VehicleSearchSelect.vue | 2 +- .../search/WearableSearchSelect.vue | 2 +- src/helpers/{codeScanner.ts => scanner.ts} | 0 src/stores/admin/scanner.ts | 28 +++++++ src/views/admin/View.vue | 6 ++ .../admin/unit/equipment/CreateEquipment.vue | 2 +- .../admin/unit/equipment/UpdateEquipment.vue | 2 +- .../inspectionPlan/UpdateInspectionPlan.vue | 2 +- .../admin/unit/vehicle/CreateVehicle.vue | 2 +- .../admin/unit/vehicle/UpdateVehicle.vue | 2 +- .../admin/unit/wearable/CreateWearable.vue | 2 +- .../admin/unit/wearable/UpdateWearable.vue | 2 +- 23 files changed, 338 insertions(+), 29 deletions(-) create mode 100644 src/components/scanner/ManageScanModal.vue create mode 100644 src/components/scanner/Phone.vue rename src/components/{ => scanner}/ScanInput.vue (94%) create mode 100644 src/components/scanner/ScanQRCodeModal.vue create mode 100644 src/components/scanner/ScanResultsModal.vue create mode 100644 src/components/scanner/ScanSidebarInfo.vue rename src/components/{CodeDetector.vue => scanner/Scanner.vue} (76%) rename src/helpers/{codeScanner.ts => scanner.ts} (100%) create mode 100644 src/stores/admin/scanner.ts diff --git a/src/components/Notification.vue b/src/components/Notification.vue index e320e19..d302eef 100644 --- a/src/components/Notification.vue +++ b/src/components/Notification.vue @@ -20,6 +20,7 @@ notification.type == 'error' ? 'border border-red-400' : '', notification.type == 'warning' ? 'border border-red-400' : '', notification.type == 'info' ? 'border border-gray-400' : '', + notification.type == 'success' ? 'border border-green-400' : '', ]" >