This commit is contained in:
@@ -7,6 +7,61 @@ namespace lm {
|
||||
|
||||
void ApiServer::registerMediaRoutes(httplib::Server& svr)
|
||||
{
|
||||
svr.Get("/api/sites", [this](const httplib::Request&, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = nlohmann::json({{"sites", site_store_.list()}}).dump();
|
||||
});
|
||||
|
||||
svr.Post("/api/sites", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
nlohmann::json body;
|
||||
try
|
||||
{
|
||||
body = nlohmann::json::parse(req.body);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HttpUtil::jsonError(res, 400, "invalid JSON");
|
||||
}
|
||||
std::string err;
|
||||
const auto created = site_store_.create(body, err);
|
||||
if (!created)
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.status = 201;
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = created->dump();
|
||||
});
|
||||
|
||||
svr.Put(R"(/api/sites/([^/]+)$)", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
const std::string id = req.matches[1];
|
||||
nlohmann::json body;
|
||||
try
|
||||
{
|
||||
body = nlohmann::json::parse(req.body);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HttpUtil::jsonError(res, 400, "invalid JSON");
|
||||
}
|
||||
std::string err;
|
||||
if (!site_store_.update(id, body, err))
|
||||
return HttpUtil::jsonError(res, 404, err);
|
||||
const auto updated = site_store_.find(id);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = updated ? updated->dump() : nlohmann::json::object().dump();
|
||||
});
|
||||
|
||||
svr.Delete(R"(/api/sites/([^/]+)$)", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
const std::string id = req.matches[1];
|
||||
std::string err;
|
||||
if (!site_store_.remove(id, err))
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.status = 204;
|
||||
});
|
||||
|
||||
svr.Get("/api/maps", [this](const httplib::Request&, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
@@ -69,6 +124,7 @@ void ApiServer::registerMediaRoutes(httplib::Server& svr)
|
||||
std::string err;
|
||||
if (!map_store_.remove(id, err))
|
||||
return HttpUtil::jsonError(res, 404, err);
|
||||
robot_runtime_.clearActiveMapIf(id);
|
||||
res.status = 204;
|
||||
});
|
||||
|
||||
|
||||
@@ -80,6 +80,29 @@ void ApiServer::registerRobotRoutes(httplib::Server& svr)
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = robot_runtime_.status().dump();
|
||||
});
|
||||
|
||||
svr.Post("/api/robot/active_map", [this](const httplib::Request& req, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
nlohmann::json body;
|
||||
try
|
||||
{
|
||||
body = nlohmann::json::parse(req.body);
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
return HttpUtil::jsonError(res, 400, "invalid JSON");
|
||||
}
|
||||
const std::string map_id = body.value("map_id", "");
|
||||
if (map_id.empty())
|
||||
return HttpUtil::jsonError(res, 400, "map_id is required");
|
||||
if (!map_store_.find(map_id))
|
||||
return HttpUtil::jsonError(res, 404, "map not found");
|
||||
std::string err;
|
||||
if (!robot_runtime_.setActiveMap(map_id, err))
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = robot_runtime_.status().dump();
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace lm
|
||||
|
||||
@@ -17,6 +17,7 @@ ApiServer::ApiServer(StateRepository& repo,
|
||||
MissionScheduler& scheduler,
|
||||
RobotRuntime& robot_runtime,
|
||||
MapStore& map_store,
|
||||
SiteStore& site_store,
|
||||
SoundStore& sound_store,
|
||||
DashboardStore& dashboard_store)
|
||||
: repo_(repo),
|
||||
@@ -26,6 +27,7 @@ ApiServer::ApiServer(StateRepository& repo,
|
||||
scheduler_(scheduler),
|
||||
robot_runtime_(robot_runtime),
|
||||
map_store_(map_store),
|
||||
site_store_(site_store),
|
||||
sound_store_(sound_store),
|
||||
dashboard_store_(dashboard_store)
|
||||
{
|
||||
|
||||
@@ -9,6 +9,7 @@
|
||||
#include "robot/robot_runtime.hpp"
|
||||
#include "storage/dashboard_store.hpp"
|
||||
#include "storage/map_store.hpp"
|
||||
#include "storage/site_store.hpp"
|
||||
#include "storage/sound_store.hpp"
|
||||
#include "storage/state_repository.hpp"
|
||||
|
||||
@@ -24,6 +25,7 @@ public:
|
||||
MissionScheduler& scheduler,
|
||||
RobotRuntime& robot_runtime,
|
||||
MapStore& map_store,
|
||||
SiteStore& site_store,
|
||||
SoundStore& sound_store,
|
||||
DashboardStore& dashboard_store);
|
||||
|
||||
@@ -37,6 +39,7 @@ private:
|
||||
MissionScheduler& scheduler_;
|
||||
RobotRuntime& robot_runtime_;
|
||||
MapStore& map_store_;
|
||||
SiteStore& site_store_;
|
||||
SoundStore& sound_store_;
|
||||
DashboardStore& dashboard_store_;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user