Add Reconnection Timer

This commit is contained in:
2020-09-03 09:06:09 +02:00
parent f9547111b5
commit cb45769c20
7 changed files with 68 additions and 20 deletions
+41 -10
View File
@@ -40,7 +40,8 @@ void Bridge::Start()
m_pHttpServer.reset(new Http::HttpServer(m_port, httpCallback));
m_pWebSocketSubscription.reset(new WebSocketSubscription(m_toonSettings));
m_messageHandler.Connect(std::bind(&Bridge::TimeToLiveCallback, this, m_pWebSocketSubscription.get(), std::placeholders::_1));
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";
@@ -60,27 +61,57 @@ Http::HttpServer::HttpReply Bridge::HttpCallback(const std::string& uri, const s
{
if (postData.size() > 0)
{
for (auto& data : postData)
for (auto& item : postData)
{
if (data.name == "data")
m_messageHandler.HandleMessage(data.value);
if (item.name == "data")
m_messageHandler.HandleMessage(item.value);
}
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Bridge::HttpCallback() - Error: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
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::TimeToLiveCallback(ToonBridge::Toon::WebSocketSubscription* pSubscription, int timeToLive)
void Bridge::ReconnectCallback(ToonBridge::Toon::WebSocketSubscription* pSubscription)
{
if (timeToLive < 20)
pSubscription->Reconnect();
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