2025-05-19 09:54:33 +08:00
|
|
|
|
#include "Application.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include "common/Utils.h"
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
#include "Config.h"
|
|
|
|
|
|
#include "app/Dao.h"
|
|
|
|
|
|
#include "app/Device.h"
|
|
|
|
|
|
|
2025-05-19 09:54:33 +08:00
|
|
|
|
void Application::init()
|
|
|
|
|
|
{
|
2025-07-18 09:08:09 +08:00
|
|
|
|
Config::init("assets/config/app.json");
|
|
|
|
|
|
|
|
|
|
|
|
// 设置数据库配置
|
|
|
|
|
|
DaoEntity::setOption(Config::option.database.host,
|
|
|
|
|
|
Config::option.database.port,
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
// 连接数据库,读取基础信息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 读取设备信息,连接设备
|
|
|
|
|
|
this->initDevice();
|
|
|
|
|
|
std::thread([=]() { runThreadDevice(); }).detach();
|
|
|
|
|
|
|
|
|
|
|
|
// 创建主业务循环线程
|
|
|
|
|
|
std::thread([=]() { runThreadMain(); }).detach();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Application::initDevice()
|
|
|
|
|
|
{
|
|
|
|
|
|
DaoEntity dao("");
|
|
|
|
|
|
std::string sql = "select * from device;";
|
|
|
|
|
|
vector<DataFields> result;
|
|
|
|
|
|
dao.exec(sql, result);
|
|
|
|
|
|
for (auto& fields: result)
|
|
|
|
|
|
{
|
|
|
|
|
|
Device::add(fields);
|
|
|
|
|
|
}
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Application::runThreadMain()
|
|
|
|
|
|
{
|
2025-07-18 09:08:09 +08:00
|
|
|
|
while (!isQuit())
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void Application::runThreadDevice()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (!isQuit())
|
|
|
|
|
|
{
|
|
|
|
|
|
|
2025-05-19 09:54:33 +08:00
|
|
|
|
|
|
|
|
|
|
|
2025-07-18 09:08:09 +08:00
|
|
|
|
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
|
|
|
|
|
}
|
2025-05-19 09:54:33 +08:00
|
|
|
|
}
|