Browse Source

添加上报服务

master
zhaohe 3 years ago
parent
commit
e38f0e1b24
  1. 3
      CMakeLists.txt
  2. 6
      src/main.cpp
  3. 67
      src/service/fan_auto_control_service.cpp
  4. 49
      src/service/fan_auto_control_service.hpp
  5. 27
      src/service/report_service.cpp
  6. 54
      src/service/report_service.hpp

3
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)

6
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();

67
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;
}
}

49
src/service/fan_auto_control_service.hpp

@ -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

27
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();
}
}

54
src/service/report_service.hpp

@ -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
Loading…
Cancel
Save