From 02ccd80cd9e8690cbaa4d341e4c74108147e8bf1 Mon Sep 17 00:00:00 2001 From: amass <168062547@qq.com> Date: Sat, 6 Jan 2024 00:12:59 +0800 Subject: [PATCH] Add task timer. --- .vscode/c_cpp_properties.json | 2 +- CMakeLists.txt | 3 ++ Server/SharedState.cpp | 55 +++++++++++++++++++++++++++++++++-- Server/SharedState.h | 5 ++++ 4 files changed, 62 insertions(+), 3 deletions(-) diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index b7d94f2..53765de 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -10,7 +10,7 @@ "defines": [], "compilerPath": "/usr/bin/gcc", "cStandard": "c17", - "cppStandard": "gnu++17", + "cppStandard": "c++20", "intelliSenseMode": "linux-gcc-x64" } ], diff --git a/CMakeLists.txt b/CMakeLists.txt index 80ad66d..6781dc8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 diff --git a/Server/SharedState.cpp b/Server/SharedState.cpp index 0904ae5..aa44e96 100644 --- a/Server/SharedState.cpp +++ b/Server/SharedState.cpp @@ -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>()}, - 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::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::instance(); + if (manager) manager->sendMessage(NotifyServerChan, oss.str()); + } + + alarmTask(); + }); +} diff --git a/Server/SharedState.h b/Server/SharedState.h index 5819ad5..4a68a1f 100644 --- a/Server/SharedState.h +++ b/Server/SharedState.h @@ -3,6 +3,7 @@ #include "router.hpp" #include +#include #include #include #include @@ -47,12 +48,16 @@ public: */ void send(std::string message); +protected: + void alarmTask(); + private: boost::asio::io_context &m_ioContext; std::shared_ptr> 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;