6 changed files with 88 additions and 118 deletions
-
3CMakeLists.txt
-
6src/main.cpp
-
67src/service/fan_auto_control_service.cpp
-
49src/service/fan_auto_control_service.hpp
-
27src/service/report_service.cpp
-
54src/service/report_service.hpp
@ -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; |
|
||||
} |
|
||||
} |
|
@ -1,49 +0,0 @@ |
|||||
//
|
|
||||
// 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,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(); |
||||
|
} |
||||
|
} |
@ -0,0 +1,54 @@ |
|||||
|
//
|
||||
|
// 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: ReportService |
||||
|
* |
||||
|
* 监听事件: |
||||
|
* 依赖状态: |
||||
|
* 依赖服务: |
||||
|
* 作用: |
||||
|
* |
||||
|
*/ |
||||
|
|
||||
|
namespace iflytop { |
||||
|
using namespace std; |
||||
|
using namespace core; |
||||
|
class ReportService : public enable_shared_from_this<ReportService> { |
||||
|
ENABLE_LOGGER(ReportService); |
||||
|
|
||||
|
shared_ptr<Config> config; |
||||
|
unique_ptr<Thread> thread; |
||||
|
shared_ptr<DeviceIOService> deviceIoService; |
||||
|
|
||||
|
public: |
||||
|
ReportService(){}; |
||||
|
|
||||
|
void initialize(); |
||||
|
|
||||
|
void start(); |
||||
|
void stop(); |
||||
|
}; |
||||
|
} // namespace iflytop
|
Write
Preview
Loading…
Cancel
Save
Reference in new issue