// Function to calculate similarity percentage between two strings function calculateSimilarity(str1, str2) { // Normalize strings by converting them to lowercase str1 = str1.toLowerCase(); str2 = str2.toLowerCase(); let matchCount = 0; // Check how many characters in str1 exist in str2 for (let char of str1) { if (str2.includes(char)) { matchCount++; } } // Calculate similarity percentage return (matchCount / str1.length) * 100; } function validateData(strasse, plz, stadt, info) { const thresholds = 90; // Similarity threshold in percentage // Validate each field against the info string return !(calculateSimilarity(strasse, info) < thresholds || calculateSimilarity(plz, info) < thresholds || calculateSimilarity(stadt, info) < thresholds); } Vue.component('radius-ont-parser', { template: `

Schritt 1: Excel (XLSX) Upload

Schritt 2: Spaltenzuordnung

Schritt 3: Ergebnisse

ONT SN
{{ row[selectedColumns.kundennummer] }} {{ row[selectedColumns.anschlussstrasse] }} {{ row[selectedColumns.anschlussplz] }} {{ row[selectedColumns.anschlusscity] }} {{ row.ont_sn }}
{{ Math.round(progress) }}%
Processing {{ currentRow + 1 }} of {{ totalRows }}
`, data() { return { step: 1, headers: [], parsedData: [], processedData: [], selectedColumns: { kundennummer: 'crmPartner', anschlussstrasse: 'AnlStrasse', anschlussplz: 'AnlPlz', anschlusscity: 'AnlOrt' }, requiredFields: [ { key: 'kundennummer', label: 'Kundennummer' }, { key: 'anschlussstrasse', label: 'Anschlussstraße' }, { key: 'anschlussplz', label: 'Anschluss PLZ' }, { key: 'anschlusscity', label: 'Anschluss City' } ], loading: false, progress: 0, currentRow: 0, totalRows: 0 }; }, methods: { async handleFileUpload(event) { const file = event.target.files[0]; if (!file) return; // Load XLSX library dynamically await this.loadXLSX(); const reader = new FileReader(); reader.onload = (e) => { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: "array" }); const worksheet = workbook.Sheets[workbook.SheetNames[0]]; // Read entire sheet as rows of arrays (no header detection yet) const allRows = XLSX.utils.sheet_to_json(worksheet, { header: 1, // Return rows as arrays blankrows: false // Skip blank rows }); // If there's only one row or something unexpected, do a basic parse if (allRows.length < 2) { const fallbackData = XLSX.utils.sheet_to_json(worksheet); this.parsedData = fallbackData; this.headers = Object.keys(fallbackData[0] || {}); this.step = 2; return; } const firstRow = allRows[0] || []; const secondRow = allRows[1] || []; // Count how many cells in each row are empty const firstRowEmptyCount = firstRow.length - firstRow.filter(Boolean).length; const secondRowEmptyCount = secondRow.length - secondRow.filter(Boolean).length; // If the difference in empty cells is more than 25% of the number of columns in the second row, // assume the first row is mostly empty and use the second row as the header const useSecondRowAsHeader = (firstRowEmptyCount - secondRowEmptyCount) > 0.25 * secondRow.length; // Now parse again with the correct header row if (useSecondRowAsHeader) { this.parsedData = XLSX.utils.sheet_to_json(worksheet, { range: 1, // Start reading data after the first row header: secondRow, // Use the second row as the header defval: "" // Optional: fill empty cells with empty string }).slice(1); // Skip the first row this.headers = secondRow; } else { this.parsedData = XLSX.utils.sheet_to_json(worksheet, { range: 0, // Start at the first row header: firstRow, // Use the first row as the header defval: "" }); this.headers = firstRow; } this.step = 2; }; reader.readAsArrayBuffer(file); }, async loadXLSX() { if (!window.XLSX) { await new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = 'https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js'; script.onload = resolve; script.onerror = reject; document.head.appendChild(script); }); } }, async startProcessing() { this.loading = true; this.totalRows = this.parsedData.length; const processedRows = []; mainLoop: for (let i = 0; i < this.parsedData.length; i++) { this.currentRow = i; this.progress = ((i + 1) / this.parsedData.length) * 100; // Simulate processing await this.sleep(100); // Process row here const row = this.parsedData[i]; const findUserResponse = await fetch(window.TT_CONFIG['BASE_PATH'] + '/Radius/proxyUnsecureHTTPRequestToRadius?custnume=' + row[this.selectedColumns.kundennummer]); const findUserData = await findUserResponse.json(); if (findUserData.length === 0) { row.ont_sn = 'N/A - Kein Benutzer mit dieser Kundennummer gefunden'; processedRows.push(row); } else if (findUserData.length === 1) { const username = findUserData[0].username; const radacctResponse = await fetch(window.TT_CONFIG['BASE_PATH'] + '/Radius/proxyUnsecureHTTPRequestToRadius?skipAdditional=true&action2=fetchRadacct&username=' + username); const radacctData = await radacctResponse.json(); row.ont_sn = radacctData.ont_sn || 'N/A - Keine ONT SN gefunden'; processedRows.push(row); } else if (findUserData.length > 1) { // check string simulairty of strasse, plz, stadt and atleast of 90% of each should be inside findUserData[].info // if not, ont_sn = N/A - Anschluss konnte nicht zugeordnet werden const strasse = row[this.selectedColumns.anschlussstrasse]; const plz = row[this.selectedColumns.anschlussplz]; const stadt = row[this.selectedColumns.anschlusscity]; const info = findUserData[0].info; for (let user of findUserData) { if (validateData(strasse, plz, stadt, info)) { const username = user.username; const radacctResponse = await fetch(window.TT_CONFIG['BASE_PATH'] + '/Radius/proxyUnsecureHTTPRequestToRadius?skipAdditional=true&action2=fetchRadacct&username=' + username); const radacctData = await radacctResponse.json(); row.ont_sn = radacctData.ont_sn || 'N/A - Keine ONT SN gefunden'; processedRows.push(row); continue mainLoop; } } row.ont_sn = 'N/A - Anschluss konnte nicht zugeordnet werden'; processedRows.push(row); } } this.loading = false; this.processedData = processedRows; this.step = 3; }, downloadResults() { const ws = XLSX.utils.json_to_sheet(this.processedData); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "Results"); XLSX.writeFile(wb, "results.xlsx"); }, sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } } }); Vue.component('radius-online-state', { props: ['username'], template: `
`, data: () => ({ data: null, observer: null, observed: false }), mounted() { this.observer = new IntersectionObserver(entries => { if (entries[0].isIntersecting && !this.observed) { this.observed = true; this.fetchOnlineState(); } }, { threshold: 0.1 }); this.observer.observe(this.$refs.container); }, beforeDestroy() { this.observer?.disconnect(); }, methods: { async fetchOnlineState() { const response = await fetch(`${window.TT_CONFIG['BASE_PATH']}/Radius/proxyUnsecureHTTPRequestToRadius?action2=fetchRadacct&username=${this.username}`); if (response.ok) this.data = await response.json(); } } }) Vue.component('radius', { template: `
Kundennummer Username Info Status Aktionen
{{ user.customerNumber }} {{ user.username }} {{ user.info }}

Freie NAT Benutzer ({{ freeNatUsers.length }})

Username Info
{{ user.username }} {{ user.info }}

Freie STF Benutzer ({{ freeStfUsers.length }})

Username Info
{{ user.username }} {{ user.info }}
Status: {{ radacctData.online ? 'Online' : 'Offline' }}
IP: {{ radacctData.ip }}
Username: {{ radacctData.username }}
Customer Number: {{ radacctData.customerNumber }}
Customer Name: {{ radacctData.customerName }}
Info: {{ radacctData.info }}
WLAN Password: {{ radacctData.wlanPassword }}
Bandbreite: {{ radacctData.actualBandwidth }}
`, data() { return { view: 'radius', billAddrAutoCompleteUrl: window.TT_CONFIG['BASE_PATH'] + '/Address/Api?do=findAddress&fibu_primary_account=1', radiusUsers: [], freeNatUsers: [], freeStfUsers: [], username: '', info: '', custnum: '', window: window, showRadacctModal: false, checkOnlineState: 0, radacctData: null, isLoading: false, searchCount: 0, } }, async mounted() { console.log("hallo"); await this.loadFreeUsers(); }, methods: { hideRadacctModal() { this.showRadacctModal = false; }, async loadRadiusUsers() { this.isLoading = true; this.radiusUsers = []; let custnum = ''; if (this.$refs.billAddr.displayValue.length > 5) { custnum = this.$refs.billAddr.displayValue.match(/\[(\d+)]/)[1]; } const params = new URLSearchParams({ username: this.username, info: this.info, custnum: custnum, }); const response = await fetch(`${window.TT_CONFIG['BASE_PATH']}/Radius/proxyUnsecureHTTPRequestToRadius?${params.toString()}`); if (response.ok) { const users = await response.json() if (users.length < 6) { this.checkOnlineState = 1; } this.radiusUsers = users; } else { console.error('Failed to load radius users'); } this.isLoading = false; this.searchCount = this.searchCount + 1; }, async fetchRadacctData(username) { const params = new URLSearchParams({ action2: 'fetchRadacct', username: username, }); const response = await fetch(`${window.TT_CONFIG['BASE_PATH']}/Radius/proxyUnsecureHTTPRequestToRadius?${params.toString()}`); if (response.ok) { this.radacctData = await response.json(); this.showRadacctModal = true; } else { console.error('Failed to fetch radacct data'); } }, async loadFreeUsers() { try { const natResponse = await fetch(window.TT_CONFIG['BASE_PATH'] + '/Radius/proxyUnsecureHTTPRequestToRadius?action2=free_user&filter=nat'); const stfResponse = await fetch(window.TT_CONFIG['BASE_PATH'] + '/Radius/proxyUnsecureHTTPRequestToRadius?action2=free_user&filter=stf'); if (natResponse.ok && stfResponse.ok) { const natData = await natResponse.json(); const stfData = await stfResponse.json(); this.freeNatUsers = natData.users; this.freeStfUsers = stfData.users; } else { console.error('Failed to load free users'); } } catch (error) { console.error('Error loading free users:', error); } }, }, }); Vue.component('radius-ont-finder', { template: `

Schritt 1: Excel (XLSX) Upload

Bitte laden Sie eine XLSX-Datei hoch, die mindestens eine Spalte mit dem Header 'Serial' für die ONT-Seriennummern enthält. Erwartete Spalten (optional, aber zur Anzeige empfohlen): Nummer, Serial, ONT-Type, Meter, Pegel.

{{ uploadError }}

Schritt 2: Ergebnisse

{{ header }} Username Kundennummer Kundenname Info
{{ row[header] }} {{ row.fetched_username }} {{ row.fetched_customerNumber }} {{ row.fetched_customerName }} {{ row.fetched_info }}
Loading...
{{ Math.round(progress) }}%
Verarbeite Zeile {{ currentRow + 1 }} von {{ totalRows }}
Aktuelle ONT SN: {{ currentSerial }}
`, data() { return { step: 1, // 1: Upload, 2: Results parsedData: [], // Raw data from XLSX processedData: [], // Data after API calls originalHeaders: [], // Headers from the uploaded XLSX loading: false, progress: 0, currentRow: 0, totalRows: 0, currentSerial: '', // Track the serial being processed for display uploadError: null, // To display errors during upload/parsing // Define the key column name expected in the XLSX for the ONT Serial Number serialColumnName: 'Serial', // IMPORTANT: Adjust if the header name in the XLSX is different // Define keys for the fetched data to avoid conflicts with original headers fetchedKeys: { username: 'fetched_username', customerNumber: 'fetched_customerNumber', customerName: 'fetched_customerName', info: 'fetched_info' }, // Base path for the API - ensure TT_CONFIG is available globally apiBasePath: window.TT_CONFIG ? window.TT_CONFIG['BASE_PATH'] : '/default/path/to/api' // Provide a fallback or handle error if TT_CONFIG is missing }; }, methods: { /** * Resets the component state to allow a new file upload. */ resetComponent() { this.step = 1; this.parsedData = []; this.processedData = []; this.originalHeaders = []; this.loading = false; this.progress = 0; this.currentRow = 0; this.totalRows = 0; this.currentSerial = ''; this.uploadError = null; // Reset the file input visually (optional, requires ref) const input = this.$el.querySelector('input[type="file"]'); if (input) { input.value = ''; } }, /** * Handles the file input change event. * Loads the XLSX library if needed, reads the file, * parses the data, validates the 'Serial' column, * and triggers processing. * @param {Event} event - The file input change event. */ async handleFileUpload(event) { const file = event.target.files[0]; this.uploadError = null; // Clear previous errors if (!file) return; this.loading = true; // Show loading indicator early try { // Load XLSX library dynamically if not already loaded await this.loadXLSX(); const reader = new FileReader(); reader.onload = (e) => { try { const data = new Uint8Array(e.target.result); const workbook = XLSX.read(data, { type: "array" }); const worksheetName = workbook.SheetNames[0]; const worksheet = workbook.Sheets[worksheetName]; // Parse the sheet into an array of objects, automatically detecting headers this.parsedData = XLSX.utils.sheet_to_json(worksheet, { defval: "" }); // Use defval to handle empty cells if (this.parsedData.length === 0) { this.uploadError = "Die hochgeladene Datei ist leer oder konnte nicht gelesen werden."; this.loading = false; return; } // Get headers from the first row of parsed data this.originalHeaders = Object.keys(this.parsedData[0]); // --- Validation: Check if the required 'Serial' column exists --- if (!this.originalHeaders.includes(this.serialColumnName)) { this.uploadError = `Fehler: Die erforderliche Spalte '${this.serialColumnName}' wurde in der hochgeladenen Datei nicht gefunden. Gefundene Spalten: ${this.originalHeaders.join(', ')}`; this.loading = false; this.parsedData = []; // Clear data if invalid this.originalHeaders = []; // Keep step at 1 to show the error return; } // --- End Validation --- // If validation passes, proceed to processing this.startProcessing(); } catch (parseError) { console.error("Error parsing XLSX file:", parseError); this.uploadError = `Fehler beim Verarbeiten der XLSX-Datei: ${parseError.message}`; this.loading = false; this.step = 1; // Stay on upload step } }; reader.onerror = (err) => { console.error("FileReader error:", err); this.uploadError = "Fehler beim Lesen der Datei."; this.loading = false; this.step = 1; }; reader.readAsArrayBuffer(file); } catch (libLoadError) { console.error("Error loading XLSX library:", libLoadError); this.uploadError = "Fehler beim Laden der erforderlichen Bibliothek (xlsx). Bitte versuchen Sie es erneut."; this.loading = false; this.step = 1; } }, /** * Dynamically loads the SheetJS (XLSX) library if it's not already available. */ async loadXLSX() { if (!window.XLSX) { console.log("Loading XLSX library..."); await new Promise((resolve, reject) => { const script = document.createElement('script'); script.src = 'https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.17.0/xlsx.full.min.js'; // Consider using a newer version if available script.async = true; script.onload = () => { console.log("XLSX library loaded."); resolve(); }; script.onerror = (err) => { console.error("Failed to load XLSX script:", err); reject(new Error("Could not load XLSX library")); }; document.head.appendChild(script); }); } }, /** * Processes the parsed data row by row, fetching details from the Radius API. */ async startProcessing() { this.loading = true; this.totalRows = this.parsedData.length; this.processedData = []; // Clear previous results this.progress = 0; this.currentRow = 0; this.currentSerial = ''; const apiUrlBase = `${this.apiBasePath}/Radius/proxyUnsecureHTTPRequestToRadius?ont_sn=`; const notFoundMessage = 'N/A - Keinen Benutzer mit dieser ONT SN gefunden'; for (let i = 0; i < this.parsedData.length; i++) { this.currentRow = i; const row = { ...this.parsedData[i] }; // Create a copy to avoid modifying original parsed data const serialNumber = row[this.serialColumnName]?.trim(); // Get serial number, trim whitespace this.currentSerial = serialNumber || 'Leer'; // Update display this.progress = ((i + 1) / this.totalRows) * 100; // Initialize fetched data fields row[this.fetchedKeys.username] = ''; row[this.fetchedKeys.customerNumber] = ''; row[this.fetchedKeys.customerName] = ''; row[this.fetchedKeys.info] = ''; if (!serialNumber) { // Handle rows with empty serial numbers row[this.fetchedKeys.username] = 'N/A - Leere Seriennummer'; row[this.fetchedKeys.customerNumber] = 'N/A'; row[this.fetchedKeys.customerName] = 'N/A'; row[this.fetchedKeys.info] = 'N/A'; this.processedData.push(row); await this.sleep(10); // Small delay for UI update even for empty rows continue; // Move to the next row } try { const response = await fetch(apiUrlBase + encodeURIComponent(serialNumber)); if (!response.ok) { // Handle HTTP errors (e.g., 404, 500) console.error(`API Error for SN ${serialNumber}: ${response.status} ${response.statusText}`); row[this.fetchedKeys.username] = `N/A - API Fehler (${response.status})`; row[this.fetchedKeys.customerNumber] = 'N/A'; row[this.fetchedKeys.customerName] = 'N/A'; row[this.fetchedKeys.info] = 'N/A'; } else { const data = await response.json(); if (Array.isArray(data) && data.length > 0) { // Assuming the first result is the relevant one if multiple are returned const userData = data[0]; row[this.fetchedKeys.username] = userData.username || 'N/A'; row[this.fetchedKeys.customerNumber] = userData.customerNumber || 'N/A'; row[this.fetchedKeys.customerName] = userData.customerName || 'N/A'; row[this.fetchedKeys.info] = userData.info || 'N/A'; } else { // Handle case where API returns success but an empty array or unexpected format row[this.fetchedKeys.username] = notFoundMessage; row[this.fetchedKeys.customerNumber] = 'N/A'; row[this.fetchedKeys.customerName] = 'N/A'; row[this.fetchedKeys.info] = 'N/A'; } } } catch (error) { console.error(`Error fetching data for SN ${serialNumber}:`, error); row[this.fetchedKeys.username] = 'N/A - Fehler bei API-Abfrage'; row[this.fetchedKeys.customerNumber] = 'N/A'; row[this.fetchedKeys.customerName] = 'N/A'; row[this.fetchedKeys.info] = 'N/A'; } this.processedData.push(row); // Optional small delay to prevent UI freeze on large files and allow progress update if (i % 20 === 0) { // Update UI roughly every 20 rows await this.sleep(20); } } this.loading = false; this.step = 2; // Move to results view this.currentSerial = ''; // Clear serial display }, /** * Creates and triggers the download of an XLSX file containing the processed results. */ downloadResults() { if (!this.processedData.length) return; try { // Prepare data for export: Select and order columns const dataToExport = this.processedData.map(row => { const exportRow = {}; // Include original columns first this.originalHeaders.forEach(header => { exportRow[header] = row[header]; }); // Add fetched data with user-friendly headers exportRow['Username'] = row[this.fetchedKeys.username]; exportRow['Kundennummer'] = row[this.fetchedKeys.customerNumber]; exportRow['Kundenname'] = row[this.fetchedKeys.customerName]; exportRow['Info'] = row[this.fetchedKeys.info]; return exportRow; }); const ws = XLSX.utils.json_to_sheet(dataToExport); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, "ONT_Finder_Results"); // Sheet name // Generate filename (e.g., results_YYYYMMDD_HHMMSS.xlsx) const timestamp = new Date().toISOString().replace(/[-:.]/g, "").slice(0, 14); const filename = `ont_finder_results_${timestamp}.xlsx`; XLSX.writeFile(wb, filename); } catch (error) { console.error("Error generating results file:", error); alert("Fehler beim Erstellen der Excel-Datei für den Download."); // Simple alert for user feedback } }, /** * Utility function to pause execution for a specified duration. * Useful for allowing UI updates during long loops. * @param {number} ms - Milliseconds to sleep. */ sleep(ms) { return new Promise(resolve => setTimeout(resolve, ms)); } }, /** * Lifecycle hook called when the component is mounted. * Checks if the global TT_CONFIG is available. */ mounted() { if (!window.TT_CONFIG || !window.TT_CONFIG['BASE_PATH']) { console.warn("Global TT_CONFIG or TT_CONFIG['BASE_PATH'] not found. API calls may fail. Using fallback path:", this.apiBasePath); // Optionally display a warning to the user // this.uploadError = "Konfiguration für API-Pfad nicht gefunden. Funktionalität möglicherweise beeinträchtigt."; } else { // Update apiBasePath if TT_CONFIG was found after initial data setup (less likely but safe) this.apiBasePath = window.TT_CONFIG['BASE_PATH']; } // Ensure XLSX is loaded once the component is ready, in case it's needed immediately // Although handleFileUpload loads it, pre-loading might be slightly smoother if needed elsewhere later // this.loadXLSX().catch(err => console.error("Pre-loading XLSX library failed:", err)); } });