WebSocketSubscription.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include "WebSocketSubscription.h"
  2. #include <json.hpp>
  3. #include <Logging.h>
  4. #include <sstream>
  5. #include <unistd.h>
  6. namespace ToonBridge {
  7. namespace Toon {
  8. WebSocketSubscription::WebSocketSubscription(const ToonSettings& toonSettings) :
  9. m_toonSettings(toonSettings)
  10. {
  11. OpenWebSocket();
  12. }
  13. WebSocketSubscription::~WebSocketSubscription()
  14. {
  15. CloseWebSocket();
  16. }
  17. void WebSocketSubscription::Reconnect()
  18. {
  19. CloseWebSocket();
  20. sleep(3);
  21. OpenWebSocket();
  22. }
  23. void WebSocketSubscription::OpenWebSocket()
  24. {
  25. std::stringstream url;
  26. url << "https://api.toon.eu/toon/v3/";
  27. url << m_toonSettings.agreementId;
  28. url << "/webhooks";
  29. std::vector<std::string> subscribedActions;
  30. subscribedActions.push_back("Thermostat");
  31. subscribedActions.push_back("PowerUsage");
  32. subscribedActions.push_back("BoilerErrorInfo");
  33. nlohmann::json data;
  34. data["applicationId"] = m_toonSettings.applicationId;
  35. data["callbackUrl"] = m_toonSettings.callbackUrl;
  36. data["subscribedActions"] = subscribedActions;
  37. Http::HttpRequest request(url.str());
  38. request.Method(Http::HttpRequest::Method::POST);
  39. request.Headers(RequestHeaders());
  40. request.Data(data.dump());
  41. try
  42. {
  43. m_httpClient.Open(request);
  44. }
  45. catch (const std::exception& e)
  46. {
  47. std::stringstream ss;
  48. ss << "Error Opening Websocket: " << std::endl;
  49. ss << "Type : " << typeid(e).name() << std::endl;
  50. ss << "ERROR: " << e.what() << std::endl;
  51. Logging::Log(Logging::Severity::Error, ss.str());
  52. throw std::runtime_error("Error Connecting to Toon WebService.");
  53. }
  54. }
  55. void WebSocketSubscription::WebSocketStatus()
  56. {
  57. std::stringstream url;
  58. url << "https://api.toon.eu/toon/v3/";
  59. url << m_toonSettings.agreementId;
  60. url << "/webhooks/";
  61. Http::HttpRequest request(url.str());
  62. request.Headers(RequestHeaders());
  63. nlohmann::json answer = nlohmann::json::parse(m_httpClient.Open(request));
  64. }
  65. void WebSocketSubscription::CloseWebSocket()
  66. {
  67. std::stringstream url;
  68. url << "https://api.toon.eu/toon/v3/";
  69. url << m_toonSettings.agreementId;
  70. url << "/webhooks/";
  71. url << m_toonSettings.applicationId;
  72. Http::HttpRequest request(url.str());
  73. request.Method(Http::HttpRequest::Method::DELETE);
  74. request.Headers(RequestHeaders());
  75. std::string returnValue = m_httpClient.Open(request);
  76. std::stringstream ss;
  77. ss << "Closing Websocket: " << returnValue << std::endl;
  78. Logging::Log(Logging::Severity::Info, ss.str());
  79. }
  80. std::vector<std::string> WebSocketSubscription::RequestHeaders()
  81. {
  82. std::vector<std::string> headers;
  83. headers.push_back("cache-control: no-cache");
  84. headers.push_back("accept: application/json");
  85. headers.push_back("content-type: application/json");
  86. std::stringstream header;
  87. header << "authorization: Bearer " << m_toonSettings.accessToken;
  88. headers.push_back(header.str());
  89. header.str("");
  90. header << "X-CommonName: " << m_toonSettings.displayCommonName;
  91. headers.push_back(header.str());
  92. header.str("");
  93. header << "X-Agreement-ID: " << m_toonSettings.agreementId;
  94. headers.push_back(header.str());
  95. return headers;
  96. }
  97. } // namespace Toon
  98. } // namespace ToonBridge