@sailingrotevista/rotevista-dash 4.0.4 → 4.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/app.js +28 -61
- package/package.json +1 -1
package/app.js
CHANGED
|
@@ -560,79 +560,42 @@ function startDisplayLoop() {
|
|
|
560
560
|
// 8. CONFIGURAZIONE E GRAFICI UTILS
|
|
561
561
|
// ==========================================================================
|
|
562
562
|
/**
|
|
563
|
-
* Recupera la configurazione
|
|
564
|
-
*
|
|
563
|
+
* Recupera la configurazione tramite le API ufficiali di Signal K.
|
|
564
|
+
* Sovrascrive i default locali con i parametri impostati nel server.
|
|
565
565
|
*/
|
|
566
566
|
async function fetchServerConfig() {
|
|
567
|
+
// Evita di cercare il server se siamo in locale (file://)
|
|
567
568
|
if (!window.location.protocol.includes("http")) return;
|
|
568
569
|
|
|
569
|
-
//
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
if (!window.location.protocol.includes("http")) return;
|
|
575
|
-
|
|
576
|
-
// Usiamo il percorso API ufficiale di Signal K per i plugin
|
|
577
|
-
const urls = [
|
|
578
|
-
'/signalk/v1/api/plugins/rotevista-dash',
|
|
579
|
-
'/signalk/v1/api/plugins/@sailingrotevista%2frotevista-dash'
|
|
580
|
-
];
|
|
581
|
-
|
|
582
|
-
for (let url of urls) {
|
|
583
|
-
try {
|
|
584
|
-
const response = await fetch(url);
|
|
585
|
-
if (response.ok) {
|
|
586
|
-
const data = await response.json();
|
|
587
|
-
|
|
588
|
-
// Nelle API ufficiali, i tuoi dati sono dentro 'data.settings' o 'data.configuration'
|
|
589
|
-
const actual = data.settings || data.configuration || data;
|
|
590
|
-
|
|
591
|
-
if (actual) {
|
|
592
|
-
const parseObj = (obj) => {
|
|
593
|
-
for (let k in obj) {
|
|
594
|
-
if (typeof obj[k] === 'object') parseObj(obj[k]);
|
|
595
|
-
else if (!isNaN(obj[k]) && obj[k] !== "") obj[k] = parseFloat(obj[k]);
|
|
596
|
-
}
|
|
597
|
-
};
|
|
598
|
-
parseObj(actual);
|
|
599
|
-
|
|
600
|
-
if (actual.alarms) CONFIG.alarms = { ...CONFIG.alarms, ...actual.alarms };
|
|
601
|
-
if (actual.graphs) CONFIG.graphs = { ...CONFIG.graphs, ...actual.graphs };
|
|
602
|
-
if (actual.averaging) CONFIG.averages = { ...CONFIG.averages, ...actual.averaging };
|
|
603
|
-
if (actual.scales) {
|
|
604
|
-
for (let k in actual.scales) {
|
|
605
|
-
CONFIG.scales[k] = { ...CONFIG.scales[k], ...actual.scales[k] };
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
console.log("Configurazione caricata con successo dall'API ufficiale.");
|
|
609
|
-
return;
|
|
610
|
-
}
|
|
611
|
-
}
|
|
612
|
-
} catch (e) { }
|
|
613
|
-
}
|
|
614
|
-
console.warn("Impossibile caricare la configurazione. Verificare 'Anonymous Read' nelle impostazioni di Signal K.");
|
|
615
|
-
}
|
|
570
|
+
// Percorsi API ufficiali di Signal K per i plugin
|
|
571
|
+
const urls = [
|
|
572
|
+
'/signalk/v1/api/plugins/rotevista-dash',
|
|
573
|
+
'/signalk/v1/api/plugins/@sailingrotevista%2frotevista-dash'
|
|
574
|
+
];
|
|
616
575
|
|
|
617
576
|
for (let url of urls) {
|
|
618
577
|
try {
|
|
619
578
|
const response = await fetch(url);
|
|
620
579
|
if (response.ok) {
|
|
621
580
|
const data = await response.json();
|
|
622
|
-
// Signal K mette i dati reali dentro l'oggetto configuration o direttamente nel body
|
|
623
|
-
const actual = data.configuration || data;
|
|
624
581
|
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
582
|
+
// Nelle API SK, i dati utente sono spesso in 'data.enabled' o 'data.settings'
|
|
583
|
+
// Noi cerchiamo l'oggetto che contiene le nostre chiavi (alarms, averaging, ecc.)
|
|
584
|
+
const actual = data.settings || data.configuration || data.options || data;
|
|
585
|
+
|
|
586
|
+
if (actual && (actual.alarms || actual.averaging || actual.graphs)) {
|
|
587
|
+
// FUNZIONE DI PARSING: Trasforma eventuali testi "12.5" in numeri 12.5 reali
|
|
588
|
+
const parseNumbers = (obj) => {
|
|
628
589
|
for (let k in obj) {
|
|
629
|
-
if (typeof obj[k] === 'object')
|
|
630
|
-
else if (!isNaN(obj[k]) && obj[k] !== ""
|
|
590
|
+
if (typeof obj[k] === 'object') parseNumbers(obj[k]);
|
|
591
|
+
else if (!isNaN(obj[k]) && obj[k] !== "" && typeof obj[k] === 'string') {
|
|
592
|
+
obj[k] = parseFloat(obj[k]);
|
|
593
|
+
}
|
|
631
594
|
}
|
|
632
595
|
};
|
|
633
|
-
|
|
596
|
+
parseNumbers(actual);
|
|
634
597
|
|
|
635
|
-
// MAPPATURA
|
|
598
|
+
// MAPPATURA DEI PARAMETRI NEL CONFIG LOCALE
|
|
636
599
|
if (actual.alarms) CONFIG.alarms = { ...CONFIG.alarms, ...actual.alarms };
|
|
637
600
|
if (actual.graphs) CONFIG.graphs = { ...CONFIG.graphs, ...actual.graphs };
|
|
638
601
|
if (actual.averaging) CONFIG.averages = { ...CONFIG.averages, ...actual.averaging };
|
|
@@ -641,11 +604,15 @@ async function fetchServerConfig() {
|
|
|
641
604
|
CONFIG.scales[k] = { ...CONFIG.scales[k], ...actual.scales[k] };
|
|
642
605
|
}
|
|
643
606
|
}
|
|
644
|
-
console.log("Configurazione caricata correttamente da:", url);
|
|
645
|
-
return; //
|
|
607
|
+
console.log("✅ Configurazione caricata correttamente da:", url);
|
|
608
|
+
return; // Successo: usciamo dal ciclo dei tentativi
|
|
646
609
|
}
|
|
610
|
+
} else if (response.status === 401) {
|
|
611
|
+
console.error("❌ Errore 401: Accesso negato. Abilita 'Anonymous Read' in Signal K Security.");
|
|
647
612
|
}
|
|
648
|
-
} catch (e) {
|
|
613
|
+
} catch (e) {
|
|
614
|
+
console.warn(`⚠️ Tentativo fallito su ${url}:`, e.message);
|
|
615
|
+
}
|
|
649
616
|
}
|
|
650
617
|
}
|
|
651
618
|
|