Chuyển lưu trữ dữ liệu sang data base
Some checks failed
Test / test (push) Has been cancelled

This commit is contained in:
2026-06-17 11:16:30 +07:00
parent 4054d81aaf
commit 098e1b2b69
45 changed files with 1971 additions and 1657 deletions

View File

@@ -9,6 +9,10 @@
#include "robot/robot_runtime.hpp"
#include "server/api_server.hpp"
#include "server/static_file_server.hpp"
#include "storage/dashboard_store.hpp"
#include "storage/database.hpp"
#include "storage/map_store.hpp"
#include "storage/sound_store.hpp"
#include "storage/state_repository.hpp"
#include <httplib.h>
@@ -25,14 +29,24 @@ LidarManagerApp::LidarManagerApp(int port,
int LidarManagerApp::run()
{
StateRepository repo(data_path_);
const std::filesystem::path data_dir = data_path_.parent_path();
Database database(data_dir);
std::string db_err;
if (!database.init(db_err))
{
std::fprintf(stderr, "database init failed: %s\n", db_err.c_str());
return 1;
}
StateRepository repo(data_path_, database);
repo.load();
const std::filesystem::path mission_queue_path = data_path_.parent_path() / "mission_queue.json";
const std::filesystem::path missions_store_path = data_path_.parent_path() / "missions.json";
MissionQueue mission_queue(mission_queue_path);
MissionStore mission_store(missions_store_path);
RobotRuntime robot_runtime(data_path_.parent_path() / "robot_runtime.json", mission_queue);
MissionQueue mission_queue(database);
MissionStore mission_store(database);
RobotRuntime robot_runtime(database, mission_queue);
MapStore map_store(database);
SoundStore sound_store(database);
DashboardStore dashboard_store(database);
const auto enqueue_fn = [&mission_store, &mission_queue](const nlohmann::json& request, std::string& err) -> bool {
nlohmann::json payload;
@@ -44,24 +58,33 @@ int LidarManagerApp::run()
ModbusTriggerService modbus(mission_store, enqueue_fn, 5502);
MissionScheduler scheduler(mission_store, enqueue_fn);
AuthService auth(data_path_.parent_path() / "auth.json");
AuthService auth(database);
httplib::Server svr;
svr.set_pre_routing_handler([&auth](const httplib::Request& req, httplib::Response& res) {
return auth.preRoute(req, res);
});
ApiServer api(repo, mission_queue, mission_store, modbus, scheduler, robot_runtime);
ApiServer api(repo,
mission_queue,
mission_store,
modbus,
scheduler,
robot_runtime,
map_store,
sound_store,
dashboard_store);
api.registerRoutes(svr);
auth.registerRoutes(svr);
StaticFileServer::mount(svr, www_root_);
std::fprintf(stderr,
"lidar_manager_web listening on http://0.0.0.0:%d (www=%s, state=%s, models=%s)\n",
"lidar_manager_web listening on http://0.0.0.0:%d (www=%s, db=%s, maps=%s, sounds=%s)\n",
port_,
www_root_.string().c_str(),
data_path_.string().c_str(),
(data_path_.parent_path() / "models").string().c_str());
database.dbPath().string().c_str(),
database.mapsDir().string().c_str(),
database.soundsDir().string().c_str());
std::fprintf(stderr, "MiR REST API: http://0.0.0.0:%d/api/v2.0.0/mission_queue\n", port_);
std::fprintf(stderr, "Modbus TCP triggers: port 5502 (coils 1001-2000)\n");