2025-10-15 14:27:21 +08:00
|
|
|
// 文件名: src/web/web_server.cc
|
|
|
|
|
#include "web_server.h"
|
|
|
|
|
#include "spdlog/spdlog.h"
|
2025-10-21 15:37:23 +08:00
|
|
|
#include <fstream>
|
|
|
|
|
#include <sstream>
|
2025-10-15 14:27:21 +08:00
|
|
|
|
2025-10-16 17:05:24 +08:00
|
|
|
WebServer::WebServer(SystemMonitor::SystemMonitor& monitor, DeviceManager& deviceManager, LiveDataCache& liveDataCache,uint16_t port)
|
|
|
|
|
: crow::Crow<crow::CORSHandler>(), m_monitor(monitor), m_device_manager(deviceManager), m_live_data_cache(liveDataCache), m_port(port)
|
2025-10-15 14:27:21 +08:00
|
|
|
{
|
2025-10-16 11:07:24 +08:00
|
|
|
auto& cors = this->get_middleware<crow::CORSHandler>();
|
|
|
|
|
cors
|
|
|
|
|
.global()
|
|
|
|
|
.origin("*")
|
|
|
|
|
.headers("Content-Type", "Authorization")
|
|
|
|
|
.methods("GET"_method, "POST"_method, "OPTIONS"_method);
|
|
|
|
|
|
|
|
|
|
this->loglevel(crow::LogLevel::Warning);
|
2025-10-15 14:27:21 +08:00
|
|
|
setup_routes();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
WebServer::~WebServer() {
|
|
|
|
|
stop();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::start() {
|
|
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
spdlog::warn("Web server is already running.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
m_thread = std::thread([this]() {
|
|
|
|
|
spdlog::info("Starting Web server on port {}", m_port);
|
2025-10-16 11:07:24 +08:00
|
|
|
this->port(m_port).run();
|
2025-10-15 14:27:21 +08:00
|
|
|
spdlog::info("Web server has stopped.");
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::stop() {
|
2025-10-16 11:07:24 +08:00
|
|
|
crow::Crow<crow::CORSHandler>::stop();
|
2025-10-15 14:27:21 +08:00
|
|
|
if (m_thread.joinable()) {
|
|
|
|
|
m_thread.join();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void WebServer::setup_routes() {
|
2025-10-16 11:07:24 +08:00
|
|
|
CROW_ROUTE((*this), "/api/system/status").methods("GET"_method)
|
2025-10-15 14:27:21 +08:00
|
|
|
([this] {
|
|
|
|
|
auto cpu_util = m_monitor.getCpuUtilization();
|
|
|
|
|
auto mem_info = m_monitor.getMemoryInfo();
|
|
|
|
|
|
|
|
|
|
crow::json::wvalue response;
|
|
|
|
|
response["cpu_usage_percentage"] = cpu_util.totalUsagePercentage;
|
|
|
|
|
response["memory_total_kb"] = mem_info.total_kb;
|
|
|
|
|
response["memory_free_kb"] = mem_info.available_kb;
|
|
|
|
|
response["memory_usage_percentage"] = (mem_info.total_kb > 0)
|
|
|
|
|
? (1.0 - static_cast<double>(mem_info.available_kb) / mem_info.total_kb) * 100.0
|
|
|
|
|
: 0.0;
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-16 11:07:24 +08:00
|
|
|
CROW_ROUTE((*this), "/api/devices").methods("GET"_method)
|
|
|
|
|
([this] {
|
|
|
|
|
auto devices_info = m_device_manager.get_all_device_info();
|
|
|
|
|
|
|
|
|
|
std::vector<crow::json::wvalue> devices_json;
|
|
|
|
|
for (const auto& info : devices_info) {
|
|
|
|
|
crow::json::wvalue device_obj;
|
|
|
|
|
|
|
|
|
|
device_obj["id"] = info.id;
|
|
|
|
|
device_obj["type"] = info.type;
|
|
|
|
|
device_obj["is_running"] = info.is_running;
|
|
|
|
|
|
|
|
|
|
crow::json::wvalue details_obj;
|
|
|
|
|
for (const auto& pair : info.connection_details) {
|
|
|
|
|
details_obj[pair.first] = pair.second;
|
|
|
|
|
}
|
|
|
|
|
device_obj["connection_details"] = std::move(details_obj);
|
|
|
|
|
|
|
|
|
|
devices_json.push_back(std::move(device_obj));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return crow::json::wvalue(devices_json);
|
|
|
|
|
});
|
2025-10-16 17:05:24 +08:00
|
|
|
|
|
|
|
|
// ================= [ 新增实时数据 API ] =================
|
|
|
|
|
CROW_ROUTE((*this), "/api/data/latest").methods("GET"_method)
|
|
|
|
|
([this] {
|
|
|
|
|
auto latest_data_map = m_live_data_cache.get_all_data();
|
|
|
|
|
|
|
|
|
|
crow::json::wvalue response;
|
|
|
|
|
for (const auto& pair : latest_data_map) {
|
|
|
|
|
// pair.first 是 device_id (string)
|
|
|
|
|
// pair.second 是 json_data (string)
|
|
|
|
|
// 我们需要将 json_data 字符串再次解析为 crow 的 json 对象
|
|
|
|
|
response[pair.first] = crow::json::load(pair.second);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return response;
|
|
|
|
|
});
|
|
|
|
|
|
2025-10-24 09:48:09 +08:00
|
|
|
|
2025-10-21 15:37:23 +08:00
|
|
|
|
2025-10-15 14:27:21 +08:00
|
|
|
}
|