сделал загрузку обновленной прошивки и само обновление раздельными
This commit is contained in:
parent
ed1bd12c95
commit
6d076f03cd
22
src/main.cpp
22
src/main.cpp
@ -81,19 +81,21 @@ class ServerResources {
|
||||
std::unique_ptr<api_driver::ApiDriver> api;
|
||||
http::auth::AuthProvider auth{};
|
||||
|
||||
void doTerminaFwUpdate(const http::server::Request& req) {
|
||||
static void onUploadFirmware(const http::server::Request& req) {
|
||||
std::ofstream f("/tmp/firmware.zip", std::ios::binary);
|
||||
|
||||
if (f.is_open()) {
|
||||
f.write(req.payload.data(), static_cast<long>(req.payload.size()));
|
||||
f.close();
|
||||
|
||||
system("do-terminal-update.sh");
|
||||
} else {
|
||||
throw std::runtime_error("File is not open");
|
||||
}
|
||||
}
|
||||
|
||||
static void doTerminalUpgrade() {
|
||||
system("do-terminal-update.sh");
|
||||
}
|
||||
|
||||
public:
|
||||
static constexpr const char* INDEX_HTML = "static/main.html";
|
||||
static constexpr const char* LOGIN_HTML = "static/login.html";
|
||||
@ -409,7 +411,7 @@ public:
|
||||
if (req.method != "PUT") {
|
||||
http::server::stockReply(http::server::bad_request, rep);
|
||||
}
|
||||
this->doTerminaFwUpdate(req);
|
||||
onUploadFirmware(req);
|
||||
|
||||
rep.status = http::server::ok;
|
||||
rep.headers.clear();
|
||||
@ -421,6 +423,18 @@ public:
|
||||
result += "\"}";
|
||||
rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
|
||||
}));
|
||||
|
||||
s.resources.emplace_back(std::make_unique<http::auth::AuthRequiredResource>("/api/doFirmwareUpgrade", this->auth, http::auth::User::UPDATE_FIRMWARE, [this](const auto& req, auto& rep) {
|
||||
if (req.method != "POST") {
|
||||
http::server::stockReply(http::server::bad_request, rep);
|
||||
}
|
||||
doTerminalUpgrade();
|
||||
rep.status = http::server::ok;
|
||||
rep.headers.clear();
|
||||
rep.headers.push_back({.name = "Content-Type", .value = toString(mime_types::json)});
|
||||
const auto result = api->loadFirmwareVersion();
|
||||
rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size());
|
||||
}));
|
||||
}
|
||||
|
||||
~ServerResources() = default;
|
||||
|
@ -629,7 +629,8 @@
|
||||
<input type="file" accept="application/zip" @change="(e) => { this.uploadFw.filename = e.target.files[0] }">
|
||||
<span v-if="uploadFw.sha256 !== null">SHA256: {{ uploadFw.sha256 }}</span>
|
||||
</label>
|
||||
<button class="dangerous-button" @click="settingsUploadUpdate()">Обновить встроенное ПО <span class="submit-spinner" v-show="submitStatus.firmwareUpload"></span></button>
|
||||
<button class="action-button" @click="settingsUploadUpdate()">Загрузить<span class="submit-spinner" v-show="submitStatus.firmwareUpload"></span></button>
|
||||
<button class="dangerous-button" v-show="uploadFw.sha256 !== null" @click="settingsPerformFirmwareUpgrade()">Обновить встроенное ПО <span class="submit-spinner" v-show="submitStatus.firmwareUpgrade"></span></button>
|
||||
</div>
|
||||
|
||||
<div hidden>
|
||||
@ -808,6 +809,7 @@
|
||||
debugSend: false,
|
||||
tcpAccel: false,
|
||||
firmwareUpload: false,
|
||||
firmwareUpgrade: false
|
||||
},
|
||||
|
||||
stat_rx: {
|
||||
@ -1312,14 +1314,14 @@
|
||||
|
||||
try {
|
||||
this.submitStatus.firmwareUpload = true
|
||||
this.uploadFw.progress = 0
|
||||
const blob = await readFileAsArrayBuffer(this.uploadFw.filename)
|
||||
|
||||
const xhr = new XMLHttpRequest();
|
||||
const success = await new Promise((resolve) => {
|
||||
await new Promise((resolve) => {
|
||||
xhr.upload.addEventListener("progress", (event) => {
|
||||
if (event.lengthComputable) {
|
||||
this.uploadFw.progress = Math.round((event.loaded / event.total) * 1000) / 10;
|
||||
console.log("upload progress:", this.uploadFw.progress);
|
||||
}
|
||||
});
|
||||
xhr.addEventListener("loadend", () => {
|
||||
@ -1332,23 +1334,23 @@
|
||||
xhr.setRequestHeader("Content-Type", "application/octet-stream");
|
||||
xhr.send(blob);
|
||||
});
|
||||
console.log("success:", success);
|
||||
// const result = await fetch('', {
|
||||
// method: 'POST',
|
||||
// body: await readFileAsArrayBuffer(this.uploadFw.filename),
|
||||
// headers: {
|
||||
// 'Content-Type': 'application/zip',
|
||||
// },
|
||||
// onuploadprogress: (progressEvent) => {
|
||||
// this.uploadFw.progress = Math.round((progressEvent.loaded / progressEvent.total) * 100);
|
||||
// },
|
||||
// })
|
||||
} catch (e) {
|
||||
alert(`Ошибка загрузки файла: ${e}`);
|
||||
}
|
||||
this.submitStatus.firmwareUpload = false
|
||||
},
|
||||
|
||||
async settingsPerformFirmwareUpgrade() {
|
||||
if (this.submitStatus.firmwareUpgrade) { return }
|
||||
this.submitStatus.firmwareUpgrade = true
|
||||
try {
|
||||
await fetch('/api/doFirmwareUpgrade', { method: 'POST' })
|
||||
} catch (e) {
|
||||
console.log("failed to perform upgrade firmware: ", e)
|
||||
}
|
||||
this.submitStatus.firmwareUpgrade = false
|
||||
},
|
||||
|
||||
performUpdateSettings(reloadParts) {
|
||||
const doFetchSettings = async () => {
|
||||
let d = await fetch("/api/get/settings")
|
||||
|
Loading…
x
Reference in New Issue
Block a user