119 lines
2.7 KiB
C++
119 lines
2.7 KiB
C++
#include "Bridge.h"
|
|
#include <Logging.h>
|
|
#include <functional>
|
|
#include <sstream>
|
|
|
|
|
|
namespace ToonBridge {
|
|
namespace Toon {
|
|
|
|
Bridge::Bridge(int port, const ToonSettings& toonSettings, const MQTTSettings& mqttSettings) :
|
|
m_mqttClient(mqttSettings.hostname, mqttSettings.port),
|
|
m_messageHandler(m_mqttClient, mqttSettings.topic),
|
|
m_port(port),
|
|
m_toonSettings(toonSettings),
|
|
m_mqttSettings(mqttSettings)
|
|
{
|
|
Start();
|
|
}
|
|
|
|
Bridge::~Bridge()
|
|
{
|
|
std::stringstream ss;
|
|
ss << m_mqttSettings.topic << "/BridgeState";
|
|
|
|
MQTT::MQTTMessage message;
|
|
message.topic = ss.str();
|
|
message.payload = "Stop";
|
|
m_mqttClient.Send(message);
|
|
}
|
|
|
|
void Bridge::Wait()
|
|
{
|
|
m_pHttpServer->Wait();
|
|
}
|
|
|
|
void Bridge::Start()
|
|
{
|
|
Http::HttpServer::CallbackMethod httpCallback = std::bind(&Bridge::HttpCallback, this, std::placeholders::_1, std::placeholders::_2);
|
|
|
|
m_pHttpServer.reset(new Http::HttpServer(m_port, httpCallback));
|
|
|
|
m_pWebSocketSubscription.reset(new WebSocketSubscription(m_toonSettings));
|
|
m_messageHandler.Connect(std::bind(&Bridge::ReconnectCallback, this, m_pWebSocketSubscription.get()));
|
|
m_messageCheckTimer.StartContinuous(300000, static_cast<std::function<void()>>(std::bind(&Bridge::CheckMessageReceived, this)));
|
|
|
|
std::stringstream ss;
|
|
ss << m_mqttSettings.topic << "/BridgeState";
|
|
|
|
MQTT::MQTTMessage message;
|
|
message.topic = ss.str();
|
|
message.payload = ss.str();
|
|
m_mqttClient.Send(message);
|
|
}
|
|
|
|
Http::HttpServer::HttpReply Bridge::HttpCallback(const std::string& uri, const std::vector<Http::HttpPostData>& postData)
|
|
{
|
|
Http::HttpServer::HttpReply reply;
|
|
reply.status = Http::HttpServer::HttpReply::Status::Ok;
|
|
|
|
try
|
|
{
|
|
if (postData.size() > 0)
|
|
{
|
|
for (auto& item : postData)
|
|
{
|
|
if (item.name == "data")
|
|
m_messageHandler.HandleMessage(item.value);
|
|
}
|
|
}
|
|
}
|
|
catch (const std::exception& e)
|
|
{
|
|
std::string data;
|
|
for (auto& item : postData)
|
|
{
|
|
if (item.name == "data")
|
|
data = (item.value);
|
|
}
|
|
}
|
|
|
|
{
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
m_messageReceived = true;
|
|
}
|
|
|
|
return reply;
|
|
}
|
|
|
|
void Bridge::ReconnectCallback(ToonBridge::Toon::WebSocketSubscription* pSubscription)
|
|
{
|
|
std::stringstream ss;
|
|
ss << "Bridge::TimeToLiveCallback() - TTL Expired, reconnecting" << std::endl;
|
|
Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
|
pSubscription->Reconnect();
|
|
}
|
|
|
|
void Bridge::CheckMessageReceived()
|
|
{
|
|
bool messageReceived;
|
|
{
|
|
std::unique_lock<std::mutex> lock(m_mutex);
|
|
messageReceived = m_messageReceived;
|
|
m_messageReceived = false;
|
|
}
|
|
|
|
if (!messageReceived)
|
|
{
|
|
std::stringstream ss;
|
|
ss << "Bridge::CheckMessageReceived() - No message received for 5 minutes, reconnecting" << std::endl;
|
|
Logging::Log(Logging::Severity::Info, ss.str());
|
|
|
|
m_pWebSocketSubscription->Reconnect();
|
|
}
|
|
}
|
|
|
|
} // namespace Toon
|
|
} // namespace ToonBridge
|