实现MQTT功能, 修改HTTP接口

This commit is contained in:
lixiaoyuan
2025-09-04 19:31:04 +08:00
parent 61ed4f355f
commit d81bcd9983
30 changed files with 2029 additions and 697 deletions

View File

@@ -39,9 +39,6 @@ void Application::init()
// 创建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); // 阻塞
}
@@ -63,37 +60,41 @@ void Application::runThreadDevice()
void Application::runThreadMain()
{
std::string addr = "tcp://localhost:1883";
mqttCli = std::make_shared<MqttClient>();
mqttCli->init(addr, "ESS", "", "", {});
//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;
}
}
}
//// 连接场站
//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::string stationCode = station->code;
// std::vector<std::string> vecTopics = {
// "up/json" + stationCode + "/EMS_YX",
// "up/json" + stationCode + "/EMS_YC",
// "up/json" + stationCode + "/PCU_YX",
// "up/json" + stationCode + "/PCU_YC",
// };
// mqttCli->subscribe(vecTopics, [=](int id)
// {
// station->isConnected = (id == 0);
// });
// }
// break;
// }
// }
//}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}