wifi-fallback.js toevoegen
This commit is contained in:
parent
ffa243810a
commit
bc1b58905d
1 changed files with 75 additions and 0 deletions
75
wifi-fallback.js
Normal file
75
wifi-fallback.js
Normal file
|
|
@ -0,0 +1,75 @@
|
||||||
|
// WiFi AP Fallback Script v1.1
|
||||||
|
// Fix: volledige AP-config meegeven bij SetConfig
|
||||||
|
|
||||||
|
let CONFIG = {
|
||||||
|
checkInterval: 60,
|
||||||
|
apDisableOnReconnect: true,
|
||||||
|
debugLog: true
|
||||||
|
};
|
||||||
|
|
||||||
|
let apActive = false;
|
||||||
|
let apConfig = null; // Bewaar originele AP-config
|
||||||
|
|
||||||
|
function log(msg) {
|
||||||
|
if (CONFIG.debugLog) print("[AP-Fallback] " + msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
function setAP(enable) {
|
||||||
|
if (enable === apActive || apConfig === null) return;
|
||||||
|
|
||||||
|
log(enable ? "WiFi verloren - AP inschakelen..." : "WiFi hersteld - AP uitschakelen...");
|
||||||
|
|
||||||
|
// Volledige AP-config meesturen, alleen 'enable' aanpassen
|
||||||
|
let newApCfg = {
|
||||||
|
ssid: apConfig.ssid,
|
||||||
|
enable: enable
|
||||||
|
};
|
||||||
|
|
||||||
|
// Wachtwoord alleen meesturen als het ingesteld is
|
||||||
|
if (apConfig.is_open === false) {
|
||||||
|
newApCfg.pass = apConfig.pass || "";
|
||||||
|
}
|
||||||
|
|
||||||
|
Shelly.call("WiFi.SetConfig", { ap: newApCfg }, function (res, err) {
|
||||||
|
if (err !== 0) {
|
||||||
|
log("Fout bij instellen AP: " + JSON.stringify(err));
|
||||||
|
} else {
|
||||||
|
apActive = enable;
|
||||||
|
log("AP is nu " + (enable ? "AAN" : "UIT"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkWiFi() {
|
||||||
|
Shelly.call("WiFi.GetStatus", {}, function (res, err) {
|
||||||
|
if (err !== 0) {
|
||||||
|
log("Kan WiFi-status niet ophalen");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
let connected = (res.sta_ip !== null && res.sta_ip !== "");
|
||||||
|
|
||||||
|
if (!connected) {
|
||||||
|
setAP(true);
|
||||||
|
} else if (connected && CONFIG.apDisableOnReconnect && apActive) {
|
||||||
|
setAP(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Eerst huidige AP-config ophalen als referentie
|
||||||
|
Shelly.call("WiFi.GetConfig", {}, function (res, err) {
|
||||||
|
if (err !== 0 || !res.ap) {
|
||||||
|
log("Kan AP-config niet ophalen - script stopt");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
apConfig = res.ap;
|
||||||
|
apActive = res.ap.enable;
|
||||||
|
log("Start - AP: " + (apActive ? "AAN" : "UIT") + ", SSID: " + apConfig.ssid);
|
||||||
|
|
||||||
|
// Pas starten na ophalen config
|
||||||
|
Timer.set(CONFIG.checkInterval * 1000, true, checkWiFi);
|
||||||
|
checkWiFi();
|
||||||
|
log("Controle elke " + CONFIG.checkInterval + "s");
|
||||||
|
});
|
||||||
Loading…
Reference in a new issue