chức năng dashboard
This commit is contained in:
@@ -93,6 +93,8 @@ void MissionQueue::ensureRunnerDefaults()
|
||||
runner_["current_queue_id"] = nullptr;
|
||||
if (!runner_.contains("current_action"))
|
||||
runner_["current_action"] = nullptr;
|
||||
if (!runner_.contains("paused"))
|
||||
runner_["paused"] = false;
|
||||
}
|
||||
|
||||
void MissionQueue::startWorkerIfNeeded()
|
||||
@@ -255,6 +257,43 @@ bool MissionQueue::reorder(const nlohmann::json& ordered_ids, std::string& err)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MissionQueue::pause(std::string& err)
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
const std::string state = runner_.value("state", "idle");
|
||||
if (state != "running")
|
||||
{
|
||||
err = "no mission is running";
|
||||
return false;
|
||||
}
|
||||
paused_ = true;
|
||||
runner_["paused"] = true;
|
||||
runner_["state"] = "paused";
|
||||
runner_["message"] = "Mission tạm dừng";
|
||||
runner_["updated_at"] = IdUtil::nowIso8601();
|
||||
saveUnlocked();
|
||||
return true;
|
||||
}
|
||||
|
||||
bool MissionQueue::resume(std::string& err)
|
||||
{
|
||||
(void)err;
|
||||
paused_ = false;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(mu_);
|
||||
runner_["paused"] = false;
|
||||
if (runner_.value("state", "") == "paused")
|
||||
{
|
||||
runner_["state"] = "running";
|
||||
runner_["message"] = "Tiếp tục mission";
|
||||
runner_["updated_at"] = IdUtil::nowIso8601();
|
||||
}
|
||||
saveUnlocked();
|
||||
}
|
||||
wake_ = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
void MissionQueue::workerLoop()
|
||||
{
|
||||
while (!stop_)
|
||||
@@ -273,6 +312,8 @@ void MissionQueue::processQueueUnlocked()
|
||||
{
|
||||
if (!queue_.is_array())
|
||||
return;
|
||||
if (paused_)
|
||||
return;
|
||||
|
||||
for (auto& item : queue_)
|
||||
{
|
||||
@@ -335,6 +376,10 @@ void MissionQueue::executeActionsUnlocked(const nlohmann::json& actions,
|
||||
{
|
||||
if (!action.is_object())
|
||||
continue;
|
||||
while (paused_ && !stop_)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
if (stop_)
|
||||
return;
|
||||
|
||||
const std::string action_id = action.value("id", "");
|
||||
const std::string kind = action.value("kind", "action");
|
||||
@@ -433,7 +478,11 @@ void MissionQueue::sleepMs(int ms)
|
||||
return;
|
||||
const int step = 100;
|
||||
for (int elapsed = 0; elapsed < ms && !stop_; elapsed += step)
|
||||
{
|
||||
while (paused_ && !stop_)
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(100));
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(std::min(step, ms - elapsed)));
|
||||
}
|
||||
}
|
||||
|
||||
void MissionQueue::setRunnerState(const std::string& state, const std::string& message)
|
||||
|
||||
@@ -27,6 +27,8 @@ public:
|
||||
bool removeById(const std::string& id, std::string& err);
|
||||
bool clearAll(std::string& err);
|
||||
bool reorder(const nlohmann::json& ordered_ids, std::string& err);
|
||||
bool pause(std::string& err);
|
||||
bool resume(std::string& err);
|
||||
|
||||
private:
|
||||
std::filesystem::path queue_path_;
|
||||
@@ -37,6 +39,7 @@ private:
|
||||
std::thread worker_;
|
||||
std::atomic<bool> stop_{false};
|
||||
std::atomic<bool> wake_{false};
|
||||
std::atomic<bool> paused_{false};
|
||||
|
||||
void load();
|
||||
void saveUnlocked() const;
|
||||
|
||||
@@ -502,6 +502,24 @@ void ApiServer::registerRoutes(httplib::Server& svr)
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.status = 204;
|
||||
});
|
||||
|
||||
svr.Post("/api/mission_queue/pause", [this](const httplib::Request&, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
std::string err;
|
||||
if (!mission_queue_.pause(err))
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = mission_queue_.runnerStatus().dump();
|
||||
});
|
||||
|
||||
svr.Post("/api/mission_queue/continue", [this](const httplib::Request&, httplib::Response& res) {
|
||||
HttpUtil::addCors(res);
|
||||
std::string err;
|
||||
if (!mission_queue_.resume(err))
|
||||
return HttpUtil::jsonError(res, 400, err);
|
||||
res.set_header("Content-Type", "application/json; charset=utf-8");
|
||||
res.body = mission_queue_.runnerStatus().dump();
|
||||
});
|
||||
}
|
||||
|
||||
} // namespace lm
|
||||
|
||||
Reference in New Issue
Block a user