Shelly: Updater-Skript (A/B, Firmware), Schlüssel aus KVS
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
8d02e5c5a6
commit
91a84eae25
2 files changed
+180
-8
No files matched your search
@@ -0,0 +1,167 @@
|
||||
// Updater – hält das Heizungsskript und die Shelly-Firmware aktuell (Shelly Gen2+)
|
||||
//
|
||||
// Ablauf:
|
||||
// - einmal am Tag (CFG.hour, Standard 03:00) und 1 Minute nach dem Start:
|
||||
// shelly/heizung.js vom Branch "release" im Git laden. Ist die Version eine andere als die
|
||||
// laufende, wird sie installiert.
|
||||
// - A/B: Es gibt zwei Skriptplätze ("Heizung" und "Heizung B"). Die neue Version kommt in den
|
||||
// gerade NICHT laufenden Platz und wird gestartet. Läuft sie nach 15 s nicht sauber, startet
|
||||
// wieder die alte (unverändert gebliebene) Version. "Run on startup" hat nur der aktive Platz.
|
||||
// - Geprüft wird vorher: HTTP 200, Versionsnummer vorhanden, Endmarke "// ENDE heizung.js".
|
||||
// - Firmware: täglich ab CFG.hour:30 nach einer stabilen Firmware suchen und installieren
|
||||
// (Neustart ~1 Min.; Einschaltverhalten AN, danach übernimmt das Heizungsskript).
|
||||
//
|
||||
// Update ausrollen: Änderung nach main pushen, dann main nach release übernehmen
|
||||
// (siehe README.md, Abschnitt "Updates").
|
||||
|
||||
let CFG = {
|
||||
url: "https://git.thebille.net/topsith/B-rgerhaus_Heizungspumpe/raw/branch/release/shelly/heizung.js",
|
||||
names: ["Heizung", "Heizung B"],
|
||||
hour: 3, // Stunde für Skript- (xx:00) und Firmware-Prüfung (xx:30)
|
||||
firmware: true, // Firmware automatisch aktualisieren
|
||||
marker: "// ENDE heizung.js",
|
||||
version: "1.0.0",
|
||||
};
|
||||
|
||||
let busy = false;
|
||||
let scriptDay = -1;
|
||||
let fwDay = -1;
|
||||
let failedVersion = ""; // nicht jede Nacht erneut eine kaputte Version probieren
|
||||
|
||||
function log(s) {
|
||||
print("Updater: " + s);
|
||||
}
|
||||
|
||||
function versionOf(code) {
|
||||
let i = code.indexOf('version: "');
|
||||
if (i < 0) return "";
|
||||
i += 10;
|
||||
let j = code.indexOf('"', i);
|
||||
return j > i && j - i < 20 ? code.slice(i, j) : "";
|
||||
}
|
||||
|
||||
function done(msg) {
|
||||
if (msg) log(msg);
|
||||
busy = false;
|
||||
}
|
||||
|
||||
// ---- Skript-Update ----
|
||||
|
||||
function checkScript() {
|
||||
if (busy) return;
|
||||
busy = true;
|
||||
Shelly.call("Script.List", {}, function (res, err) {
|
||||
if (err !== 0) return done("Script.List fehlgeschlagen " + err);
|
||||
// aktiv = der laufende Platz, sonst der mit "Run on startup", sonst der erste
|
||||
let found = [];
|
||||
for (let i = 0; i < res.scripts.length; i++) {
|
||||
if (CFG.names.indexOf(res.scripts[i].name) >= 0) found.push(res.scripts[i]);
|
||||
}
|
||||
if (found.length === 0) return done("kein Heizungsskript gefunden");
|
||||
let a = 0;
|
||||
if (found.length > 1 && !found[0].running && (found[1].running || (found[1].enable && !found[0].enable))) a = 1;
|
||||
let active = found[a];
|
||||
let other = found.length > 1 ? found[1 - a] : null;
|
||||
Shelly.call("Script.GetCode", { id: active.id, offset: 0, len: 2048 }, function (r, e) {
|
||||
let current = e === 0 && r ? versionOf(r.data) : "";
|
||||
download(active, other, current);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function download(active, other, current) {
|
||||
Shelly.call("HTTP.GET", { url: CFG.url, timeout: 30 }, function (res, err, msg) {
|
||||
if (err !== 0 || !res) return done("Download fehlgeschlagen: " + err + " " + msg);
|
||||
if (res.code !== 200) return done("Download fehlgeschlagen: HTTP " + res.code);
|
||||
let code = res.body;
|
||||
let v = versionOf(code);
|
||||
if (!v || code.indexOf(CFG.marker) < 0) return done("Datei unvollständig oder ohne Version – ignoriert");
|
||||
if (v === current) return done("aktuell (" + v + ")");
|
||||
if (v === failedVersion) return done(v + " lief schon einmal nicht – übersprungen");
|
||||
log("installiere " + v + " (laufend: " + (current || "?") + ")");
|
||||
if (other !== null) return upload(active, other.id, code, v);
|
||||
let name = CFG.names[0] === active.name ? CFG.names[1] : CFG.names[0];
|
||||
Shelly.call("Script.Create", { name: name }, function (r, e) {
|
||||
if (e !== 0) return done("Script.Create fehlgeschlagen " + e);
|
||||
upload(active, r.id, code, v);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Code in Stücken übertragen, nur an Zeilenenden trennen (Umlaute bleiben heil)
|
||||
function upload(active, targetId, code, v) {
|
||||
let pos = 0;
|
||||
function next() {
|
||||
if (pos >= code.length) return switchOver(active, targetId, v);
|
||||
let end = pos + 1024;
|
||||
if (end < code.length) {
|
||||
let nl = code.lastIndexOf("\n", end);
|
||||
if (nl > pos) end = nl + 1;
|
||||
}
|
||||
let part = code.slice(pos, end);
|
||||
Shelly.call("Script.PutCode", { id: targetId, code: part, append: pos > 0 }, function (r, e) {
|
||||
if (e !== 0) return done("Script.PutCode fehlgeschlagen " + e + " – alte Version läuft weiter");
|
||||
pos = end;
|
||||
next();
|
||||
});
|
||||
}
|
||||
next();
|
||||
}
|
||||
|
||||
function switchOver(active, targetId, v) {
|
||||
Shelly.call("Script.Stop", { id: active.id }, function () {
|
||||
Shelly.call("Script.Start", { id: targetId }, function () {
|
||||
Timer.set(15000, false, function () {
|
||||
Shelly.call("Script.GetStatus", { id: targetId }, function (st, e) {
|
||||
let ok = e === 0 && st && st.running && !(st.errors && st.errors.length);
|
||||
if (ok) {
|
||||
Shelly.call("Script.SetConfig", { id: targetId, config: { enable: true } });
|
||||
Shelly.call("Script.SetConfig", { id: active.id, config: { enable: false } });
|
||||
return done("Version " + v + " läuft (Platz " + targetId + ")");
|
||||
}
|
||||
failedVersion = v;
|
||||
Shelly.call("Script.Stop", { id: targetId }, function () {
|
||||
Shelly.call("Script.Start", { id: active.id }, function () {
|
||||
done("Version " + v + " fehlerhaft – zurück auf die alte Version");
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// ---- Firmware-Update ----
|
||||
|
||||
function checkFirmware() {
|
||||
Shelly.call("Shelly.CheckForUpdate", {}, function (res, err) {
|
||||
if (err !== 0) return log("Firmware-Prüfung fehlgeschlagen " + err);
|
||||
if (!res || !res.stable) return log("Firmware aktuell");
|
||||
log("Firmware-Update auf " + res.stable.version);
|
||||
Shelly.call("Shelly.Update", { stage: "stable" });
|
||||
});
|
||||
}
|
||||
|
||||
// ---- Zeitplan ----
|
||||
|
||||
function tick() {
|
||||
let sys = Shelly.getComponentStatus("sys");
|
||||
if (!sys.unixtime || !sys.time) return;
|
||||
let day = Math.floor(sys.unixtime / 86400);
|
||||
let parts = sys.time.split(":");
|
||||
let hour = parseInt(parts[0], 10);
|
||||
let minute = parseInt(parts[1], 10);
|
||||
if (hour !== CFG.hour) return;
|
||||
if (day !== scriptDay) {
|
||||
scriptDay = day;
|
||||
checkScript();
|
||||
}
|
||||
if (CFG.firmware && minute >= 30 && day !== fwDay && !busy) {
|
||||
fwDay = day;
|
||||
checkFirmware();
|
||||
}
|
||||
}
|
||||
|
||||
log("Version " + CFG.version + " gestartet");
|
||||
Timer.set(60 * 1000, false, checkScript);
|
||||
Timer.set(5 * 60 * 1000, true, tick);
|
||||
Reference in new issue
Block a user