diff --git a/CMakeLists.txt b/CMakeLists.txt index b10e8d3..3dfc18f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,4 +57,5 @@ zadd_executable( src/main.cpp src/service/device_io_service.cpp src/service/device_io_service_mock.cpp - src/service/main_control_service.cpp) + src/service/main_control_service.cpp + src/service/light_control_service.cpp) diff --git a/src/configs/config.hpp b/src/configs/config.hpp index c7b3c51..9854fd0 100644 --- a/src/configs/config.hpp +++ b/src/configs/config.hpp @@ -14,10 +14,14 @@ #include "iflytopcpp/core/components/config_template/config_template.hpp" -#define ConfigELEMENT_LIST(marco) \ - /**/ \ - marco(string /* */, deviceId, "") /**/ \ - marco(size_t /* */, safe_disk_size_g, 50) /*当剩余磁盘空间小于该数值时停止同步*/ +#define ConfigELEMENT_LIST(marco) \ + /**/ \ + marco(string /* */, deviceId, "") /**/ \ + marco(string /* */, lightControlMode, "auto") /*照明灯控制模式 auto/manual*/ \ + marco(int /* */, lightAutoOpenHour, 18) /**/ \ + marco(int /* */, lightAutoOpenMin, 0) /**/ \ + marco(int /* */, lightAutoCloseHour, 6) /**/ \ + marco(int /* */, lightAutoCloseMin, 0) /**/ configTemplateDEFILE_CONFIG_SERVICE2( // Config, // diff --git a/src/main.cpp b/src/main.cpp index f9fb783..bdd23b5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,6 +9,7 @@ // #include "service/device_io_service.hpp" #include "service/device_io_service_mock.hpp" +#include "service/light_control_service.hpp" #include "service/main_control_service.hpp" using namespace iflytop; @@ -82,6 +83,10 @@ int Main::main(int argc, char *argv[]) { BUILD_AND_REG_SERRVICE(ZWebService); GET_SERVICE(ZWebService)->initialize(); + BUILD_AND_REG_SERRVICE(LightControlService); + GET_SERVICE(LightControlService)->initialize(); + GET_SERVICE(LightControlService)->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 new file mode 100644 index 0000000..3344bf7 --- /dev/null +++ b/src/service/fan_auto_control_service.cpp @@ -0,0 +1,67 @@ +#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 new file mode 100644 index 0000000..04618dc --- /dev/null +++ b/src/service/fan_auto_control_service.hpp @@ -0,0 +1,49 @@ +// +// 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/light_control_service.cpp b/src/service/light_control_service.cpp new file mode 100644 index 0000000..6545b20 --- /dev/null +++ b/src/service/light_control_service.cpp @@ -0,0 +1,70 @@ +#include "light_control_service.hpp" + +using namespace iflytop; +using namespace std; +using namespace core; + +void LightControlService::initialize() { + GET_TO_SERVICE(config); + GET_TO_SERVICE(deviceIoService); +} + +void LightControlService::start() { + logger->info("LightControlService start"); + if (thread) return; + thread.reset(new Thread("LightControlService", [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())) { + logger->info("LightControlService triggerClose"); + 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("LightControlService triggerOpen"); + triggerOpen = true; + deviceIoService->relayControl(DeviceIOService::kLightPower, true); + } + } + + thisThread.sleepForMs(10000); + // + } + })); +} +void LightControlService::stop() { + logger->info("LightControlService stop"); + if (thread) { + thread->join(); + thread = nullptr; + } +} \ No newline at end of file diff --git a/src/service/light_control_service.hpp b/src/service/light_control_service.hpp new file mode 100644 index 0000000..9b0c008 --- /dev/null +++ b/src/service/light_control_service.hpp @@ -0,0 +1,49 @@ +// +// 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: LightControlService + * + * 作用: + * 根据当前温度自动控制风扇等级 + */ + +namespace iflytop { +using namespace std; +using namespace core; +class LightControlService : public enable_shared_from_this { + ENABLE_LOGGER(LightControlService); + + shared_ptr config; + unique_ptr thread; + shared_ptr deviceIoService; + + public: + LightControlService(){}; + void initialize(); + + void start(); + void stop(); +}; +} // namespace iflytop \ No newline at end of file