сделал загрузку обновленной прошивки и само обновление раздельными

This commit is contained in:
Vladislav Ostapov 2024-11-14 15:42:05 +03:00
parent ed1bd12c95
commit 6d076f03cd
2 changed files with 34 additions and 18 deletions

View File

@ -81,19 +81,21 @@ class ServerResources {
std::unique_ptr<api_driver::ApiDriver> api; std::unique_ptr<api_driver::ApiDriver> api;
http::auth::AuthProvider auth{}; 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); std::ofstream f("/tmp/firmware.zip", std::ios::binary);
if (f.is_open()) { if (f.is_open()) {
f.write(req.payload.data(), static_cast<long>(req.payload.size())); f.write(req.payload.data(), static_cast<long>(req.payload.size()));
f.close(); f.close();
system("do-terminal-update.sh");
} else { } else {
throw std::runtime_error("File is not open"); throw std::runtime_error("File is not open");
} }
} }
static void doTerminalUpgrade() {
system("do-terminal-update.sh");
}
public: public:
static constexpr const char* INDEX_HTML = "static/main.html"; static constexpr const char* INDEX_HTML = "static/main.html";
static constexpr const char* LOGIN_HTML = "static/login.html"; static constexpr const char* LOGIN_HTML = "static/login.html";
@ -409,7 +411,7 @@ public:
if (req.method != "PUT") { if (req.method != "PUT") {
http::server::stockReply(http::server::bad_request, rep); http::server::stockReply(http::server::bad_request, rep);
} }
this->doTerminaFwUpdate(req); onUploadFirmware(req);
rep.status = http::server::ok; rep.status = http::server::ok;
rep.headers.clear(); rep.headers.clear();
@ -421,6 +423,18 @@ public:
result += "\"}"; result += "\"}";
rep.content.insert(rep.content.end(), result.c_str(), result.c_str() + result.size()); 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; ~ServerResources() = default;

View File

@ -629,7 +629,8 @@
<input type="file" accept="application/zip" @change="(e) => { this.uploadFw.filename = e.target.files[0] }"> <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> <span v-if="uploadFw.sha256 !== null">SHA256: {{ uploadFw.sha256 }}</span>
</label> </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>
<div hidden> <div hidden>
@ -808,6 +809,7 @@
debugSend: false, debugSend: false,
tcpAccel: false, tcpAccel: false,
firmwareUpload: false, firmwareUpload: false,
firmwareUpgrade: false
}, },
stat_rx: { stat_rx: {
@ -1312,14 +1314,14 @@
try { try {
this.submitStatus.firmwareUpload = true this.submitStatus.firmwareUpload = true
this.uploadFw.progress = 0
const blob = await readFileAsArrayBuffer(this.uploadFw.filename) const blob = await readFileAsArrayBuffer(this.uploadFw.filename)
const xhr = new XMLHttpRequest(); const xhr = new XMLHttpRequest();
const success = await new Promise((resolve) => { await new Promise((resolve) => {
xhr.upload.addEventListener("progress", (event) => { xhr.upload.addEventListener("progress", (event) => {
if (event.lengthComputable) { if (event.lengthComputable) {
this.uploadFw.progress = Math.round((event.loaded / event.total) * 1000) / 10; this.uploadFw.progress = Math.round((event.loaded / event.total) * 1000) / 10;
console.log("upload progress:", this.uploadFw.progress);
} }
}); });
xhr.addEventListener("loadend", () => { xhr.addEventListener("loadend", () => {
@ -1332,23 +1334,23 @@
xhr.setRequestHeader("Content-Type", "application/octet-stream"); xhr.setRequestHeader("Content-Type", "application/octet-stream");
xhr.send(blob); 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) { } catch (e) {
alert(`Ошибка загрузки файла: ${e}`); alert(`Ошибка загрузки файла: ${e}`);
} }
this.submitStatus.firmwareUpload = false 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) { performUpdateSettings(reloadParts) {
const doFetchSettings = async () => { const doFetchSettings = async () => {
let d = await fetch("/api/get/settings") let d = await fetch("/api/get/settings")