cleanup reboot logic, fix json upload

This commit is contained in:
2025-01-04 00:04:40 +01:00
parent 0f77ac163a
commit c070e68349
5 changed files with 196 additions and 92 deletions

View File

@@ -116,6 +116,9 @@
<br>
<textarea id="json" cols=50 rows=10></textarea>
<script src="bundle.js"></script>
<button id="exit">Exit</button>
<button id="reboot">Reboot</button>
</div>
<div id="progressPane" class="progressPane">

View File

@@ -18,37 +18,37 @@ import { BatteryView } from "./batteryview";
export class Controller {
updateRTCData() {
fetch(PUBLIC_URL + "/time")
.then(response => response.json())
.then(json => json as GetTime)
.then(time => {
controller.timeView.update(time.native, time.rtc)
})
.catch(error => {
controller.timeView.update("n/a","n/a")
console.log(error);
});
.then(response => response.json())
.then(json => json as GetTime)
.then(time => {
controller.timeView.update(time.native, time.rtc)
})
.catch(error => {
controller.timeView.update("n/a", "n/a")
console.log(error);
});
}
updateBatteryData() {
fetch(PUBLIC_URL + "/battery")
.then(response => response.json())
.then(json => json as BatteryState)
.then(battery => {
controller.batteryView.update(battery)
})
.catch(error => {
controller.batteryView.update(null)
console.log(error);
});
.then(response => response.json())
.then(json => json as BatteryState)
.then(battery => {
controller.batteryView.update(battery)
})
.catch(error => {
controller.batteryView.update(null)
console.log(error);
});
}
uploadNewFirmware(file: File) {
var current = 0;
var max = 100;
controller.progressview.addProgress("ota_upload", (current/max) *100 , "Uploading firmeware ("+current+"/" + max+")")
controller.progressview.addProgress("ota_upload", (current / max) * 100, "Uploading firmeware (" + current + "/" + max + ")")
var ajax = new XMLHttpRequest();
ajax.upload.addEventListener("progress", event => {
current = event.loaded/1000;
max = event.total/1000;
controller.progressview.addProgress("ota_upload", (current/max) *100 , "Uploading firmeware ("+current+"/" + max+")")
current = event.loaded / 1000;
max = event.total / 1000;
controller.progressview.addProgress("ota_upload", (current / max) * 100, "Uploading firmeware (" + current + "/" + max + ")")
}, false);
ajax.addEventListener("load", () => {
//TODO wait for reboot here!
@@ -68,24 +68,24 @@ export class Controller {
version() {
controller.progressview.addIndeterminate("version", "Getting buildVersion")
fetch(PUBLIC_URL + "/version")
.then(response => response.json())
.then(json => json as VersionInfo)
.then(versionInfo => {
controller.progressview.removeProgress("version")
controller.firmWareView.setVersion(versionInfo);
})
.then(response => response.json())
.then(json => json as VersionInfo)
.then(versionInfo => {
controller.progressview.removeProgress("version")
controller.firmWareView.setVersion(versionInfo);
})
}
downloadConfig() {
controller.progressview.addIndeterminate("get_config", "Downloading Config")
fetch(PUBLIC_URL + "/get_config")
.then(response => response.json())
.then(loaded => {
var currentConfig = loaded as PlantControllerConfig;
this.setConfig(currentConfig);
//sync json view initially
this.configChanged();
controller.progressview.removeProgress("get_config")
})
.then(response => response.json())
.then(loaded => {
var currentConfig = loaded as PlantControllerConfig;
this.setConfig(currentConfig);
//sync json view initially
this.configChanged();
controller.progressview.removeProgress("get_config")
})
}
uploadConfig(json: string, statusCallback: (status: string) => void) {
controller.progressview.addIndeterminate("set_config", "Uploading Config")
@@ -95,9 +95,9 @@ export class Controller {
})
.then(response => response.text())
.then(text => statusCallback(text))
controller.progressview.removeProgress("set_config")
controller.progressview.removeProgress("set_config")
}
syncRTCFromBrowser(){
syncRTCFromBrowser() {
controller.progressview.addIndeterminate("write_rtc", "Writing RTC")
var value: SetTime = {
time: new Date().toISOString()
@@ -117,17 +117,17 @@ export class Controller {
console.log(pretty)
controller.submitView.setJson(pretty);
}
testPlant(plantId: number) {
let counter = 0
let limit = 30
controller.progressview.addProgress("test_pump", counter/limit*100, "Testing pump " + (plantId+1) + " for " + (limit-counter)+"s")
controller.progressview.addProgress("test_pump", counter / limit * 100, "Testing pump " + (plantId + 1) + " for " + (limit - counter) + "s")
let timerId: string | number | NodeJS.Timeout | undefined
function updateProgress(){
function updateProgress() {
counter++;
controller.progressview.addProgress("test_pump", counter/limit*100, "Testing pump " + (plantId+1) + " for " + (limit-counter)+"s")
controller.progressview.addProgress("test_pump", counter / limit * 100, "Testing pump " + (plantId + 1) + " for " + (limit - counter) + "s")
timerId = setTimeout(updateProgress, 1000);
}
@@ -137,21 +137,21 @@ export class Controller {
pump: plantId
}
var pretty = JSON.stringify(body, undefined, 1);
fetch(PUBLIC_URL + "/pumptest", {
method: "POST",
body: pretty
})
.then(response => response.text())
.then(
.then(
text => {
clearTimeout(timerId);
controller.progressview.removeProgress("test_pump");
}
)
)
}
getConfig(): PlantControllerConfig{
getConfig(): PlantControllerConfig {
return {
network: controller.networkView.getConfig(),
tank: controller.tankView.getConfig(),
@@ -163,12 +163,12 @@ export class Controller {
scanWifi() {
let counter = 0
let limit = 5
controller.progressview.addProgress("scan_ssid", counter/limit*100, "Scanning for SSIDs for " + (limit-counter)+"s")
controller.progressview.addProgress("scan_ssid", counter / limit * 100, "Scanning for SSIDs for " + (limit - counter) + "s")
let timerId: string | number | NodeJS.Timeout | undefined
function updateProgress(){
function updateProgress() {
counter++;
controller.progressview.addProgress("scan_ssid", counter/limit*100, "Scanning for SSIDs for " + (limit-counter)+"s")
controller.progressview.addProgress("scan_ssid", counter / limit * 100, "Scanning for SSIDs for " + (limit - counter) + "s")
timerId = setTimeout(updateProgress, 1000);
}
@@ -200,15 +200,15 @@ export class Controller {
this.plantViews.setConfig(current.plants);
}
measure_moisture (){
measure_moisture() {
let counter = 0
let limit = 2
controller.progressview.addProgress("measure_moisture", counter/limit*100, "Measure Moisture " + (limit-counter)+"s")
controller.progressview.addProgress("measure_moisture", counter / limit * 100, "Measure Moisture " + (limit - counter) + "s")
let timerId: string | number | NodeJS.Timeout | undefined
function updateProgress(){
function updateProgress() {
counter++;
controller.progressview.addProgress("measure_moisture", counter/limit*100, "Measure Moisture " + (limit-counter)+"s")
controller.progressview.addProgress("measure_moisture", counter / limit * 100, "Measure Moisture " + (limit - counter) + "s")
timerId = setTimeout(updateProgress, 1000);
}
@@ -216,28 +216,62 @@ export class Controller {
fetch(PUBLIC_URL + "/moisture")
.then(response => response.json())
.then(json => json as Moistures)
.then(time => {
controller.plantViews.update(time.moisture_a, time.moisture_b)
clearTimeout(timerId);
controller.progressview.removeProgress("measure_moisture");
.then(response => response.json())
.then(json => json as Moistures)
.then(time => {
controller.plantViews.update(time.moisture_a, time.moisture_b)
clearTimeout(timerId);
controller.progressview.removeProgress("measure_moisture");
})
.catch(error => {
clearTimeout(timerId);
controller.progressview.removeProgress("measure_moisture");
console.log(error);
});
}
exit() {
fetch(PUBLIC_URL + "/exit", {
method: "POST",
})
.catch(error => {
clearTimeout(timerId);
controller.progressview.removeProgress("measure_moisture");
console.log(error);
});
controller.progressview.addIndeterminate("rebooting", "Returned to normal mode, you can close this site now")
}
waitForReboot() {
console.log("Check if controller online again")
fetch(PUBLIC_URL + "/version", {
method: "POST",
signal: AbortSignal.timeout(5000)
}).then(response => {
console.log("Reached controller, reloading")
window.location.reload();
})
.catch(err => {
console.log("Not reached yet, retrying")
setTimeout(controller.waitForReboot, 1000)
})
}
reboot() {
fetch(PUBLIC_URL + "/reboot", {
method: "POST",
})
controller.progressview.addIndeterminate("rebooting", "Rebooting")
setTimeout(this.waitForReboot, 1000)
}
readonly rebootBtn: HTMLButtonElement
readonly exitBtn: HTMLButtonElement
readonly timeView: TimeView;
readonly plantViews: PlantViews;
readonly networkView: NetworkConfigView;
readonly tankView: TankConfigView;
readonly nightLampView: NightLampView;
readonly submitView: SubmitView;
readonly firmWareView : OTAView;
readonly firmWareView: OTAView;
readonly progressview: ProgressView;
readonly batteryView: BatteryView;
constructor() {
@@ -250,6 +284,14 @@ export class Controller {
this.submitView = new SubmitView(this)
this.firmWareView = new OTAView(this)
this.progressview = new ProgressView(this)
this.rebootBtn = document.getElementById("reboot") as HTMLButtonElement
this.rebootBtn.onclick = () => {
controller.reboot();
}
this.exitBtn = document.getElementById("exit") as HTMLButtonElement
this.exitBtn.onclick = () => {
controller.exit();
}
}
}
const controller = new Controller();
@@ -258,4 +300,5 @@ controller.updateBatteryData();
controller.downloadConfig();
//controller.measure_moisture();
controller.version();
controller.progressview.removeProgress("rebooting");