Add task timer.

This commit is contained in:
amass 2024-01-06 00:12:59 +08:00
parent 0dafd7be72
commit 02ccd80cd9
4 changed files with 62 additions and 3 deletions

View File

@ -10,7 +10,7 @@
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "gnu++17",
"cppStandard": "c++20",
"intelliSenseMode": "linux-gcc-x64"
}
],

View File

@ -2,6 +2,9 @@ cmake_minimum_required(VERSION 3.17)
project(Older)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(Kylin
GIT_REPOSITORY https://gitea.amass.fun/amass/Kylin.git

View File

@ -1,13 +1,16 @@
#include "SharedState.h"
#include "Database.h"
#include "DateTime.h"
#include "HttpSession.h"
#include "ServiceLogic.h"
#include "ServiceManager.h"
#include "WeChatContext/CorporationContext.h"
#include "WebsocketSession.h"
#include "Database.h"
SharedState::SharedState(boost::asio::io_context &ioContext, std::string doc_root)
: m_ioContext(ioContext), m_router{std::make_shared<boost::urls::router<Handler>>()},
m_docRoot(std::move(doc_root)) {
m_docRoot(std::move(doc_root)), m_timer(ioContext) {
alarmTask();
m_router->insert("/{path*}",[this](HttpSession &session, const Request &request, const boost::urls::matches &matches) {
using namespace boost::beast;
@ -161,3 +164,51 @@ void SharedState::send(std::string message) {
for (auto const &wp : v)
if (auto sp = wp.lock()) sp->send(ss);
}
void SharedState::alarmTask() {
int hour = 10;
int minute = 30;
auto alarmTime = DateTime::currentDateTime();
alarmTime.setHour(hour);
alarmTime.setMinute(minute);
if (std::chrono::system_clock::now() > alarmTime()) {
alarmTime = alarmTime.tomorrow();
}
m_timer.expires_at(alarmTime());
m_timer.async_wait([this](const boost::system::error_code &error) mutable {
if (error) {
LOG(error) << error.message();
return;
}
auto database = Amass::Singleton<Database>::instance();
auto tasks = database->tasks();
bool founded = false;
std::string content;
for (auto &task : tasks) {
if (founded) break;
for (auto &child : task.children) {
if (!child.finished) {
content = child.content;
founded = true;
break;
}
}
if (!founded && !task.finished) {
content = task.content;
founded = true;
}
}
if (founded) {
std::ostringstream oss;
oss << "待完成事项:" << std::endl;
oss << "==========" << std::endl;
oss << content << std::endl;
oss << "==========" << std::endl;
oss << "每天都要过得充实开心哦~";
auto manager = Amass::Singleton<ServiceManager>::instance();
if (manager) manager->sendMessage(NotifyServerChan, oss.str());
}
alarmTask();
});
}

View File

@ -3,6 +3,7 @@
#include "router.hpp"
#include <boost/asio/io_context.hpp>
#include <boost/asio/system_timer.hpp>
#include <boost/beast/http/string_body.hpp>
#include <boost/smart_ptr.hpp>
#include <memory>
@ -47,12 +48,16 @@ public:
*/
void send(std::string message);
protected:
void alarmTask();
private:
boost::asio::io_context &m_ioContext;
std::shared_ptr<boost::urls::router<Handler>> m_router;
std::string m_docRoot;
std::string m_galleryRoot = "/root/photos";
std::string m_fileRoot;
boost::asio::system_timer m_timer;
};
using SharedStatePtr = std::shared_ptr<SharedState>;