From e38f0e1b24ebcac617a47f27acc6f0ac563ebf15 Mon Sep 17 00:00:00 2001 From: Zhaohe Date: Tue, 13 Dec 2022 22:11:13 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=B8=8A=E6=8A=A5=E6=9C=8D?= =?UTF-8?q?=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CMakeLists.txt | 3 +- src/main.cpp | 6 ++- src/service/fan_auto_control_service.cpp | 67 -------------------------------- src/service/fan_auto_control_service.hpp | 49 ----------------------- src/service/report_service.cpp | 27 +++++++++++++ src/service/report_service.hpp | 54 +++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 118 deletions(-) create mode 100644 src/service/report_service.cpp create mode 100644 src/service/report_service.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3dfc18f..130d88a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -58,4 +58,5 @@ zadd_executable( src/service/device_io_service.cpp src/service/device_io_service_mock.cpp src/service/main_control_service.cpp - src/service/light_control_service.cpp) + src/service/light_control_service.cpp + src/service/report_service.cpp) diff --git a/src/main.cpp b/src/main.cpp index bdd23b5..9b9781a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -11,7 +11,7 @@ #include "service/device_io_service_mock.hpp" #include "service/light_control_service.hpp" #include "service/main_control_service.hpp" - +#include "service/report_service.hpp" using namespace iflytop; using namespace core; using namespace std; @@ -87,6 +87,10 @@ int Main::main(int argc, char *argv[]) { GET_SERVICE(LightControlService)->initialize(); GET_SERVICE(LightControlService)->start(); + BUILD_AND_REG_SERRVICE(ReportService); + GET_SERVICE(ReportService)->initialize(); + GET_SERVICE(ReportService)->start(); + BUILD_AND_REG_SERRVICE(MainControlService); GET_SERVICE(MainControlService)->initialize(); diff --git a/src/service/fan_auto_control_service.cpp b/src/service/fan_auto_control_service.cpp index 3344bf7..e69de29 100644 --- a/src/service/fan_auto_control_service.cpp +++ b/src/service/fan_auto_control_service.cpp @@ -1,67 +0,0 @@ -#include "fan_auto_control_service.hpp" - -using namespace iflytop; -using namespace std; -using namespace core; - -void FanAutoControlService::initialize() { - GET_TO_SERVICE(config); - GET_TO_SERVICE(deviceIoService); -} - -void FanAutoControlService::start() { - logger->info("FanAutoControlService start"); - if (thread) return; - thread.reset(new Thread("FanAutoControlService", [this]() { - ThisThread thisThread; - - // 获得当前从1970到现在的绝对天数 - int day = time(nullptr) / 86400; - bool triggerOpen = false; - bool triggerClose = false; - - while (!thisThread.getExitFlag()) { - /** - * @brief - */ - // 更新当前绝对天数 - int nowDay = time(nullptr) / 86400; - if (nowDay != day) { - day = nowDay; - triggerOpen = false; - triggerClose = false; - } - - if (!triggerClose && config->get_lightControlMode() == "auto") { - // 判断是否到达开启时间 - time_t now = time(nullptr); - struct tm *tm = localtime(&now); - if (tm->tm_hour >= config->get_lightAutoCloseHour() || - (tm->tm_hour == config->get_lightAutoCloseHour() && tm->tm_min >= config->get_lightAutoCloseMin())) { - triggerClose = true; - deviceIoService->relayControl(DeviceIOService::kLightPower, false); - } - } - // - if (!triggerOpen && config->get_lightControlMode() == "auto") { - // 判断是否到达开启时间 - time_t now = time(nullptr); - struct tm *tm = localtime(&now); - if (tm->tm_hour >= config->get_lightAutoOpenHour() || - (tm->tm_hour == config->get_lightAutoOpenHour() && tm->tm_min >= config->get_lightAutoOpenMin())) { - logger->info("FanAutoControlService triggerOpen"); - triggerOpen = true; - deviceIoService->relayControl(DeviceIOService::kLightPower, true); - } - } - // - } - })); -} -void FanAutoControlService::stop() { - logger->info("FanAutoControlService stop"); - if (thread) { - thread->join(); - thread = nullptr; - } -} \ No newline at end of file diff --git a/src/service/fan_auto_control_service.hpp b/src/service/fan_auto_control_service.hpp index 04618dc..e69de29 100644 --- a/src/service/fan_auto_control_service.hpp +++ b/src/service/fan_auto_control_service.hpp @@ -1,49 +0,0 @@ -// -// Created by zwsd -// - -#pragma once -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "configs/config.hpp" -#include "iflytopcpp/core/spdlogfactory/logger.hpp" -// -#include "configs/config.hpp" -#include "iflytopcpp/core/thread/thread.hpp" -#include "service/device_io_service.hpp" -#include "zservice_container/zservice_container.hpp" -/** - * @brief - * - * service: FanAutoControlService - * - * 作用: - * 根据当前温度自动控制风扇等级 - */ - -namespace iflytop { -using namespace std; -using namespace core; -class FanAutoControlService : public enable_shared_from_this { - ENABLE_LOGGER(FanAutoControlService); - - shared_ptr config; - unique_ptr thread; - shared_ptr deviceIoService; - - public: - FanAutoControlService(){}; - void initialize(); - - void start(); - void stop(); -}; -} // namespace iflytop \ No newline at end of file diff --git a/src/service/report_service.cpp b/src/service/report_service.cpp new file mode 100644 index 0000000..9e03d0b --- /dev/null +++ b/src/service/report_service.cpp @@ -0,0 +1,27 @@ +#include "report_service.hpp" + +using namespace iflytop; +using namespace core; + +void ReportService::initialize() { + GET_TO_SERVICE(config); + GET_TO_SERVICE(deviceIoService); +} + +void ReportService::start() { + logger->info("ReportService start"); + if (thread) return; + thread.reset(new Thread("ReportService", [this]() { + ThisThread thisThread; + while (thisThread.getExitFlag()) { + thisThread.sleepForMs(1000); + } + })); +} +void ReportService::stop() { + logger->info("ReportService stop"); + if (thread) { + thread->join(); + thread.reset(); + } +} \ No newline at end of file diff --git a/src/service/report_service.hpp b/src/service/report_service.hpp new file mode 100644 index 0000000..55052f8 --- /dev/null +++ b/src/service/report_service.hpp @@ -0,0 +1,54 @@ +// +// Created by zwsd +// + +#pragma once +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "configs/config.hpp" +#include "iflytopcpp/core/spdlogfactory/logger.hpp" +// +#include "configs/config.hpp" +#include "iflytopcpp/core/thread/thread.hpp" +#include "service/device_io_service.hpp" +#include "zservice_container/zservice_container.hpp" + +/** + * @brief + * + * service: ReportService + * + * 监听事件: + * 依赖状态: + * 依赖服务: + * 作用: + * + */ + +namespace iflytop { +using namespace std; +using namespace core; +class ReportService : public enable_shared_from_this { + ENABLE_LOGGER(ReportService); + + shared_ptr config; + unique_ptr thread; + shared_ptr deviceIoService; + + public: + ReportService(){}; + + void initialize(); + + void start(); + void stop(); +}; +} // namespace iflytop \ No newline at end of file