7 changed files with 250 additions and 5 deletions
-
3CMakeLists.txt
-
12src/configs/config.hpp
-
5src/main.cpp
-
67src/service/fan_auto_control_service.cpp
-
49src/service/fan_auto_control_service.hpp
-
70src/service/light_control_service.cpp
-
49src/service/light_control_service.hpp
@ -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; |
||||
|
} |
||||
|
} |
@ -0,0 +1,49 @@ |
|||||
|
//
|
||||
|
// Created by zwsd
|
||||
|
//
|
||||
|
|
||||
|
#pragma once
|
||||
|
#include <fstream>
|
||||
|
#include <iostream>
|
||||
|
#include <list>
|
||||
|
#include <map>
|
||||
|
#include <memory>
|
||||
|
#include <set>
|
||||
|
#include <sstream>
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
|
||||
|
#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<FanAutoControlService> { |
||||
|
ENABLE_LOGGER(FanAutoControlService); |
||||
|
|
||||
|
shared_ptr<Config> config; |
||||
|
unique_ptr<Thread> thread; |
||||
|
shared_ptr<DeviceIOService> deviceIoService; |
||||
|
|
||||
|
public: |
||||
|
FanAutoControlService(){}; |
||||
|
void initialize(); |
||||
|
|
||||
|
void start(); |
||||
|
void stop(); |
||||
|
}; |
||||
|
} // namespace iflytop
|
@ -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; |
||||
|
} |
||||
|
} |
@ -0,0 +1,49 @@ |
|||||
|
//
|
||||
|
// Created by zwsd
|
||||
|
//
|
||||
|
|
||||
|
#pragma once
|
||||
|
#include <fstream>
|
||||
|
#include <iostream>
|
||||
|
#include <list>
|
||||
|
#include <map>
|
||||
|
#include <memory>
|
||||
|
#include <set>
|
||||
|
#include <sstream>
|
||||
|
#include <string>
|
||||
|
#include <vector>
|
||||
|
|
||||
|
#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<LightControlService> { |
||||
|
ENABLE_LOGGER(LightControlService); |
||||
|
|
||||
|
shared_ptr<Config> config; |
||||
|
unique_ptr<Thread> thread; |
||||
|
shared_ptr<DeviceIOService> deviceIoService; |
||||
|
|
||||
|
public: |
||||
|
LightControlService(){}; |
||||
|
void initialize(); |
||||
|
|
||||
|
void start(); |
||||
|
void stop(); |
||||
|
}; |
||||
|
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue