Initial commit
This commit is contained in:
@@ -0,0 +1,102 @@
|
||||
#include "WebSocketSubscription.h"
|
||||
#include <json.hpp>
|
||||
#include <sstream>
|
||||
|
||||
|
||||
namespace ToonBridge {
|
||||
namespace Toon {
|
||||
|
||||
WebSocketSubscription::WebSocketSubscription(const ToonSettings& toonSettings) :
|
||||
m_toonSettings(toonSettings)
|
||||
{
|
||||
OpenWebSocket();
|
||||
}
|
||||
|
||||
WebSocketSubscription::~WebSocketSubscription()
|
||||
{
|
||||
CloseWebSocket();
|
||||
}
|
||||
|
||||
void WebSocketSubscription::Reconnect()
|
||||
{
|
||||
CloseWebSocket();
|
||||
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("BoilerError");
|
||||
|
||||
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());
|
||||
|
||||
m_httpClient.Open(request);
|
||||
}
|
||||
|
||||
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());
|
||||
|
||||
m_httpClient.Open(request);
|
||||
}
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user