From 5fe5d69023c412e2bbf6556c7953344073c65d09 Mon Sep 17 00:00:00 2001 From: GuanYuankai Date: Fri, 16 Jan 2026 16:20:20 +0800 Subject: [PATCH] =?UTF-8?q?feat(battery):=20=E5=A2=9E=E5=8A=A0=E7=94=B5?= =?UTF-8?q?=E6=B1=A0=E6=9C=8D=E5=8A=A1=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config/config.json | 8 ++++---- src/main.cpp | 16 ++++++++++++++-- src/web/web_server.cc | 32 +++++++++++++++++++++++++++++++- src/web/web_server.h | 6 ++++-- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/config/config.json b/config/config.json index 9f83ade..714c068 100644 --- a/config/config.json +++ b/config/config.json @@ -20,12 +20,12 @@ "enable": true, "line": { "p1": { - "x": 0.2556999921798706, - "y": 0.848800003528595 + "x": 0.3831999897956848, + "y": 0.7179999947547913 }, "p2": { - "x": 0.6531999707221985, - "y": 0.8278999924659729 + "x": 0.6578999757766724, + "y": 0.6560999751091003 } }, "name": "Main_Gate_Line" diff --git a/src/main.cpp b/src/main.cpp index 8852785..d544147 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,6 +93,15 @@ int main(int argc, char* argv[]) { std::string db_database = config.getDbName(); MysqlManager::GetInstance()->Initialize(db_host, db_user, db_pwd, db_database); + std::string uart_port = "/dev/ttyS4"; + BatteryService battery_service(uart_port, 115200); + + if (!battery_service.start()) { + spdlog::warn( + "Battery Service failed to start on {}, proceeding without battery monitoring.", + uart_port); + } + try { spdlog::set_level(spdlog::level::from_str(config.getLogLevel())); spdlog::info("Edge Proxy starting up..."); @@ -176,7 +185,7 @@ int main(int argc, char* argv[]) { device_manager.load_and_start(config.getDevicesConfigPath()); WebServer web_server(monitor, device_manager, live_data_cache, alarm_service, - config.getWebServerPort()); + battery_service, config.getWebServerPort()); web_server.set_shutdown_handler([&]() { spdlog::warn("Received shutdown command from Web API. Shutting down."); g_io_context.stop(); @@ -206,7 +215,10 @@ int main(int argc, char* argv[]) { spdlog::info("[Shutdown] D. Disconnecting from MQTT broker..."); mqtt_client.disconnect(); - spdlog::info("[Shutdown] E. Stopping main event loop..."); + spdlog::info("[Shutdown] F. Stopping Battery Service..."); + battery_service.stop(); + + spdlog::info("[Shutdown] G. Stopping main event loop..."); g_io_context.stop(); }); diff --git a/src/web/web_server.cc b/src/web/web_server.cc index c7999de..38b85c9 100644 --- a/src/web/web_server.cc +++ b/src/web/web_server.cc @@ -9,12 +9,14 @@ #include "spdlog/spdlog.h" WebServer::WebServer(SystemMonitor::SystemMonitor& monitor, DeviceManager& deviceManager, - LiveDataCache& liveDataCache, AlarmService& alarm_service, uint16_t port) + LiveDataCache& liveDataCache, AlarmService& alarm_service, + BatteryService& battery_service, uint16_t port) : crow::Crow(), m_monitor(monitor), m_device_manager(deviceManager), m_live_data_cache(liveDataCache), m_alarm_service(alarm_service), + m_battery_service(battery_service), m_port(port) { auto& cors = this->get_middleware(); cors.global() @@ -121,4 +123,32 @@ void WebServer::setup_routes() { return crow::response(400, response_json.dump()); } }); + + CROW_ROUTE((*this), "/api/v1/battery/status").methods("GET"_method)([&]() { + auto json_data = m_battery_service.getStatusJson(); + json response = {{"code", 200}, {"status", "success"}, {"data", json_data}}; + return crow::response(200, response.dump()); + }); + + // [新增] 控制电池指令 + CROW_ROUTE((*this), "/api/v1/battery/control") + .methods("POST"_method)([&](const crow::request& req) { + auto json_body = crow::json::load(req.body); + if (!json_body) + return crow::response(400, "Invalid JSON"); + + if (json_body.has("mode")) { + // mode: 1 (Work), 2 (Sleep) + int mode = json_body["mode"].i(); + m_battery_service.setMode(static_cast(mode)); + } + + if (json_body.has("alarm")) { + // alarm: true/false + bool alarm = json_body["alarm"].b(); + m_battery_service.setAlarm(alarm); + } + + return crow::response(200, R"({"code": 200, "message": "Command sent"})"); + }); } \ No newline at end of file diff --git a/src/web/web_server.h b/src/web/web_server.h index e91f5f0..c2df0f6 100644 --- a/src/web/web_server.h +++ b/src/web/web_server.h @@ -4,6 +4,7 @@ #include #include "alarm/alarm_service.h" +#include "batteryService/battery_service.h" #include "crow.h" #include "crow/middlewares/cors.h" #include "dataCache/live_data_cache.h" @@ -14,7 +15,8 @@ class WebServer : public crow::Crow { public: WebServer(SystemMonitor::SystemMonitor& monitor, DeviceManager& deviceManager, - LiveDataCache& liveDataCache, AlarmService& alarm_service, uint16_t port = 8080); + LiveDataCache& liveDataCache, AlarmService& alarm_service, + BatteryService& battery_service, uint16_t port = 8080); ~WebServer(); WebServer(const WebServer&) = delete; @@ -38,7 +40,7 @@ private: DeviceManager& m_device_manager; LiveDataCache& m_live_data_cache; AlarmService& m_alarm_service; - + BatteryService& m_battery_service; uint16_t m_port; std::thread m_thread;