mirror of
https://gitee.com/js-yhsec/energy_storage.git
synced 2026-05-27 18:59:26 +08:00
新增http、mqtt运行库,实现mqtt功能, 新增spdlog
This commit is contained in:
@@ -7,8 +7,9 @@
|
||||
#include "database/Dao.h"
|
||||
#include "app/Station.h"
|
||||
#include "app/Device.h"
|
||||
|
||||
|
||||
#include "protocol/HttpEntity.h"
|
||||
#include "common/Spdlogger.h"
|
||||
#include "protocol/MqttEntity.h"
|
||||
|
||||
void Application::init()
|
||||
{
|
||||
@@ -21,41 +22,77 @@ void Application::init()
|
||||
Config::option.database.user,
|
||||
Config::option.database.passwd,
|
||||
Config::option.database.dbname);
|
||||
XLOGI() << "[APP] set database option: host=" << Config::option.database.host
|
||||
<< ", port=" << Config::option.database.port
|
||||
<< ", user=" << Config::option.database.user
|
||||
<< ", dbname=" << Config::option.database.dbname;
|
||||
spdlog::info("[app] set database option: host={}, port={}, user={}, dbname={}",
|
||||
Config::option.database.host,
|
||||
Config::option.database.port,
|
||||
Config::option.database.user,
|
||||
Config::option.database.dbname);
|
||||
|
||||
// 连接数据库,读取基础信息
|
||||
|
||||
// 初始化系统基础数据
|
||||
appdata_.init();
|
||||
appdata.init();
|
||||
|
||||
// 创建设备处理线程
|
||||
std::thread([=]() { runThreadDevice(); }).detach();
|
||||
|
||||
// 创建HTTP服务线程
|
||||
std::thread([=]() {
|
||||
while (!isQuit) {
|
||||
MqttClient mqttCli;
|
||||
mqttCli.init("tcp://localhost:1883", "AAAAAAAAA", "", "", {"topic/test"}); // 不阻塞
|
||||
|
||||
HttpEntity http;
|
||||
http.listen("0.0.0.0", Config::option.http.port); // 阻塞
|
||||
}
|
||||
}).detach();
|
||||
|
||||
// 创建主业务循环线程
|
||||
std::thread([=]() { runThreadMain(); }).detach();
|
||||
}
|
||||
|
||||
|
||||
void Application::runThreadMain()
|
||||
void Application::runThreadDevice()
|
||||
{
|
||||
while (!isQuit())
|
||||
while (!isQuit)
|
||||
{
|
||||
|
||||
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Application::runThreadDevice()
|
||||
void Application::runThreadMain()
|
||||
{
|
||||
while (!isQuit())
|
||||
{
|
||||
std::string addr = "tcp://localhost:1883";
|
||||
mqttCli = std::make_shared<MqttClient>();
|
||||
mqttCli->init(addr, "ESS", "", "", {});
|
||||
|
||||
while (!isQuit)
|
||||
{
|
||||
// 连接场站
|
||||
static TimeTick ttStation;
|
||||
if (ttStation.elapse(10000))
|
||||
{
|
||||
if (!mqttCli->isConnected)
|
||||
{
|
||||
}
|
||||
else
|
||||
{
|
||||
for (auto& item: appdata.mapStation)
|
||||
{
|
||||
auto station = item.second;
|
||||
if (station && !station->isConnected)
|
||||
{
|
||||
std::vector<std::string> vecTopics = {"topic/test" + std::to_string(station->id)};
|
||||
mqttCli->subscribe(vecTopics, [=](int id)
|
||||
{
|
||||
station->isConnected = (id == 0);
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
|
||||
Reference in New Issue
Block a user