增加了下载功能

This commit is contained in:
zjk
2025-02-13 12:58:13 +08:00
parent f83f737a79
commit 734226a33a
4 changed files with 160 additions and 64 deletions
+46 -11
View File
@@ -10,6 +10,7 @@
#include <thread>
#include <mutex>
#include "../pPowerManger/SQLiteDB.h"
#include <MOOS/libMOOS/Comms/MOOSAsyncCommClient.h>
using namespace std;
@@ -24,38 +25,72 @@ string read_file(const string& path) {
int main() {
crow::SimpleApp app;
SQLiteDB db("/home/zjk/project/H100/H100PowerManger/data/power_data.db");
// 初始化 SQLite 数据库
MOOS::MOOSAsyncCommClient moosClient;
moosClient.Run("localhost", 9000, "appPowerMangerWebUI");
// 路由返回 HTML 页面
// 定义路由,处理根路径的请求
CROW_ROUTE(app, "/")([](){
// 读取 HTML 文件
string html_content = read_file("/home/zjk/project/H100/H100PowerManger/src/appPowerMangerWebUI/index.html");
return html_content;
});
// 定义路由,处理 /api/power-data 的请求
CROW_ROUTE(app, "/api/db-status")
([&db]() {
crow::json::wvalue response;
if (db.isDatabaseConnected("/home/zjk/project/H100/H100PowerManger/data", "power_data.db")) {
response["connected"] = true;
response["database"] = "YourDatabaseName"; // 替换为实际的数据库名称
response["database"] = db.getDBPatch(); // 替换为实际的数据库名称
} else {
response["connected"] = false;
}
return response;
});
// 定义路由,处理 /api/power-data 的请求
CROW_ROUTE(app, "/api/download-db")
([&db]() {
std::ifstream file("/home/zjk/project/H100/H100PowerManger/data/power_data.db", std::ios::binary);
if (!file) {
return crow::response(404, "Database file not found");
}
std::vector<char> buffer((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
// 将 buffer.size() 转换为 std::string
std::string contentLength = std::to_string(buffer.size());
crow::response response(buffer.size(), contentLength, "application/octet-stream");
response.add_header("Content-Disposition", "attachment; filename=power_data.db");
// 获取文件大小
file.seekg(0, std::ios::end);
size_t fileSize = file.tellg();
file.seekg(0, std::ios::beg);
// 创建响应对象
crow::response response;
response.code = 200;
response.set_header("Content-Type", "application/octet-stream");
response.set_header("Content-Disposition", "attachment; filename=power_data.db");
response.set_header("Content-Length", std::to_string(fileSize));
// 直接返回文件流
response.body = std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
file.close();
return response;
});
// 定义路由,处理 /api/moos-db 的请求
CROW_ROUTE(app, "/api/moos-db")
([&moosClient]() -> crow::json::wvalue {
crow::json::wvalue response;
if (moosClient.IsConnected()) {
response["connected"] = true;
response["host"] = moosClient.GetDBHostname();
response["port"] = std::to_string(moosClient.GetDBHostPort());
}
else {
response["connected"] = false;
}
return response;
});
// 定义路由,处理 /api/moos-notify 的请求(POST)
CROW_ROUTE(app, "/api/moos-notify")
([&moosClient]() {
crow::json::wvalue response;
response["message"] = "Received MOOS notification";
return response;
});
@@ -63,4 +98,4 @@ int main() {
app.port(18080).multithreaded().run();
return 0;
}
}