generated from guanyuankai/bonus-edge-proxy
feat(battery): 增加电池服务。
This commit is contained in:
parent
79b2616493
commit
5fe5d69023
|
|
@ -20,12 +20,12 @@
|
||||||
"enable": true,
|
"enable": true,
|
||||||
"line": {
|
"line": {
|
||||||
"p1": {
|
"p1": {
|
||||||
"x": 0.2556999921798706,
|
"x": 0.3831999897956848,
|
||||||
"y": 0.848800003528595
|
"y": 0.7179999947547913
|
||||||
},
|
},
|
||||||
"p2": {
|
"p2": {
|
||||||
"x": 0.6531999707221985,
|
"x": 0.6578999757766724,
|
||||||
"y": 0.8278999924659729
|
"y": 0.6560999751091003
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"name": "Main_Gate_Line"
|
"name": "Main_Gate_Line"
|
||||||
|
|
|
||||||
16
src/main.cpp
16
src/main.cpp
|
|
@ -93,6 +93,15 @@ int main(int argc, char* argv[]) {
|
||||||
std::string db_database = config.getDbName();
|
std::string db_database = config.getDbName();
|
||||||
MysqlManager::GetInstance()->Initialize(db_host, db_user, db_pwd, db_database);
|
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 {
|
try {
|
||||||
spdlog::set_level(spdlog::level::from_str(config.getLogLevel()));
|
spdlog::set_level(spdlog::level::from_str(config.getLogLevel()));
|
||||||
spdlog::info("Edge Proxy starting up...");
|
spdlog::info("Edge Proxy starting up...");
|
||||||
|
|
@ -176,7 +185,7 @@ int main(int argc, char* argv[]) {
|
||||||
device_manager.load_and_start(config.getDevicesConfigPath());
|
device_manager.load_and_start(config.getDevicesConfigPath());
|
||||||
|
|
||||||
WebServer web_server(monitor, device_manager, live_data_cache, alarm_service,
|
WebServer web_server(monitor, device_manager, live_data_cache, alarm_service,
|
||||||
config.getWebServerPort());
|
battery_service, config.getWebServerPort());
|
||||||
web_server.set_shutdown_handler([&]() {
|
web_server.set_shutdown_handler([&]() {
|
||||||
spdlog::warn("Received shutdown command from Web API. Shutting down.");
|
spdlog::warn("Received shutdown command from Web API. Shutting down.");
|
||||||
g_io_context.stop();
|
g_io_context.stop();
|
||||||
|
|
@ -206,7 +215,10 @@ int main(int argc, char* argv[]) {
|
||||||
spdlog::info("[Shutdown] D. Disconnecting from MQTT broker...");
|
spdlog::info("[Shutdown] D. Disconnecting from MQTT broker...");
|
||||||
mqtt_client.disconnect();
|
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();
|
g_io_context.stop();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -9,12 +9,14 @@
|
||||||
#include "spdlog/spdlog.h"
|
#include "spdlog/spdlog.h"
|
||||||
|
|
||||||
WebServer::WebServer(SystemMonitor::SystemMonitor& monitor, DeviceManager& deviceManager,
|
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<crow::CORSHandler>(),
|
: crow::Crow<crow::CORSHandler>(),
|
||||||
m_monitor(monitor),
|
m_monitor(monitor),
|
||||||
m_device_manager(deviceManager),
|
m_device_manager(deviceManager),
|
||||||
m_live_data_cache(liveDataCache),
|
m_live_data_cache(liveDataCache),
|
||||||
m_alarm_service(alarm_service),
|
m_alarm_service(alarm_service),
|
||||||
|
m_battery_service(battery_service),
|
||||||
m_port(port) {
|
m_port(port) {
|
||||||
auto& cors = this->get_middleware<crow::CORSHandler>();
|
auto& cors = this->get_middleware<crow::CORSHandler>();
|
||||||
cors.global()
|
cors.global()
|
||||||
|
|
@ -121,4 +123,32 @@ void WebServer::setup_routes() {
|
||||||
return crow::response(400, response_json.dump());
|
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<uint8_t>(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"})");
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
@ -4,6 +4,7 @@
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
#include "alarm/alarm_service.h"
|
#include "alarm/alarm_service.h"
|
||||||
|
#include "batteryService/battery_service.h"
|
||||||
#include "crow.h"
|
#include "crow.h"
|
||||||
#include "crow/middlewares/cors.h"
|
#include "crow/middlewares/cors.h"
|
||||||
#include "dataCache/live_data_cache.h"
|
#include "dataCache/live_data_cache.h"
|
||||||
|
|
@ -14,7 +15,8 @@
|
||||||
class WebServer : public crow::Crow<crow::CORSHandler> {
|
class WebServer : public crow::Crow<crow::CORSHandler> {
|
||||||
public:
|
public:
|
||||||
WebServer(SystemMonitor::SystemMonitor& monitor, DeviceManager& deviceManager,
|
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();
|
||||||
|
|
||||||
WebServer(const WebServer&) = delete;
|
WebServer(const WebServer&) = delete;
|
||||||
|
|
@ -38,7 +40,7 @@ private:
|
||||||
DeviceManager& m_device_manager;
|
DeviceManager& m_device_manager;
|
||||||
LiveDataCache& m_live_data_cache;
|
LiveDataCache& m_live_data_cache;
|
||||||
AlarmService& m_alarm_service;
|
AlarmService& m_alarm_service;
|
||||||
|
BatteryService& m_battery_service;
|
||||||
uint16_t m_port;
|
uint16_t m_port;
|
||||||
|
|
||||||
std::thread m_thread;
|
std::thread m_thread;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue