Files
ToonBridge/Toon/WebSocketSubscription.cpp

124 lines
3.0 KiB
C++

#include "WebSocketSubscription.h"
#include <json.hpp>
#include <Logging.h>
#include <sstream>
#include <unistd.h>
namespace ToonBridge {
namespace Toon {
WebSocketSubscription::WebSocketSubscription(const ToonSettings& toonSettings) :
m_toonSettings(toonSettings)
{
OpenWebSocket();
}
WebSocketSubscription::~WebSocketSubscription()
{
CloseWebSocket();
}
void WebSocketSubscription::Reconnect()
{
CloseWebSocket();
sleep(3);
OpenWebSocket();
}
void WebSocketSubscription::OpenWebSocket()
{
std::stringstream url;
url << "https://api.toon.eu/toon/v3/";
url << m_toonSettings.agreementId;
url << "/webhooks";
std::vector<std::string> subscribedActions;
subscribedActions.push_back("Thermostat");
subscribedActions.push_back("PowerUsage");
subscribedActions.push_back("BoilerErrorInfo");
nlohmann::json data;
data["applicationId"] = m_toonSettings.applicationId;
data["callbackUrl"] = m_toonSettings.callbackUrl;
data["subscribedActions"] = subscribedActions;
Http::HttpRequest request(url.str());
request.Method(Http::HttpRequest::Method::POST);
request.Headers(RequestHeaders());
request.Data(data.dump());
try
{
m_httpClient.Open(request);
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Error Opening Websocket: " << std::endl;
ss << "Type : " << typeid(e).name() << std::endl;
ss << "ERROR: " << e.what() << std::endl;
Logging::Log(Logging::Severity::Error, ss.str());
throw std::runtime_error("Error Connecting to Toon WebService.");
}
}
void WebSocketSubscription::WebSocketStatus()
{
std::stringstream url;
url << "https://api.toon.eu/toon/v3/";
url << m_toonSettings.agreementId;
url << "/webhooks/";
Http::HttpRequest request(url.str());
request.Headers(RequestHeaders());
nlohmann::json answer = nlohmann::json::parse(m_httpClient.Open(request));
}
void WebSocketSubscription::CloseWebSocket()
{
std::stringstream url;
url << "https://api.toon.eu/toon/v3/";
url << m_toonSettings.agreementId;
url << "/webhooks/";
url << m_toonSettings.applicationId;
Http::HttpRequest request(url.str());
request.Method(Http::HttpRequest::Method::DELETE);
request.Headers(RequestHeaders());
std::string returnValue = m_httpClient.Open(request);
std::stringstream ss;
ss << "Closing Websocket: " << returnValue << std::endl;
Logging::Log(Logging::Severity::Info, ss.str());
}
std::vector<std::string> WebSocketSubscription::RequestHeaders()
{
std::vector<std::string> headers;
headers.push_back("cache-control: no-cache");
headers.push_back("accept: application/json");
headers.push_back("content-type: application/json");
std::stringstream header;
header << "authorization: Bearer " << m_toonSettings.accessToken;
headers.push_back(header.str());
header.str("");
header << "X-CommonName: " << m_toonSettings.displayCommonName;
headers.push_back(header.str());
header.str("");
header << "X-Agreement-ID: " << m_toonSettings.agreementId;
headers.push_back(header.str());
return headers;
}
} // namespace Toon
} // namespace ToonBridge