forked from CoretechR/OMOTE
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
change project to different memory layout to support rapidjson size add API wrapper for home assistant and put some example code in.
- Loading branch information
MatthewColvin
committed
Aug 10, 2024
1 parent
b243110
commit 5dacf0f
Showing
4 changed files
with
97 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
#include "Api.hpp" | ||
|
||
using namespace HomeAssistant; | ||
|
||
Api::Api() { curl_global_init(CURL_GLOBAL_DEFAULT); } | ||
|
||
Api::~Api() { curl_global_cleanup(); } | ||
|
||
rapidjson::Document Api::doStuff() { | ||
mCurlHandle = curl_easy_init(); | ||
struct curl_slist *headers = NULL; // List to hold headers | ||
|
||
headers = curl_slist_append(headers, jsonHeader); | ||
headers = curl_slist_append(headers, getSecurityHeader().c_str()); | ||
|
||
curl_easy_setopt(mCurlHandle, CURLOPT_URL, getURL().c_str()); | ||
curl_easy_setopt(mCurlHandle, CURLOPT_HTTPHEADER, headers); | ||
curl_easy_setopt(mCurlHandle, CURLOPT_WRITEFUNCTION, Api::write_callback); | ||
curl_easy_setopt(mCurlHandle, CURLOPT_WRITEDATA, this); | ||
auto code = curl_easy_perform(mCurlHandle); | ||
|
||
printf("Curl Code:%d", code); | ||
|
||
rapidjson::Document d; | ||
d.Parse(mResponse.c_str()); | ||
|
||
curl_slist_free_all(headers); | ||
curl_easy_cleanup(mCurlHandle); | ||
|
||
return d; | ||
} | ||
|
||
std::string Api::getSecurityHeader() { | ||
return "Authorization: Bearer " + std::string(token); | ||
} | ||
|
||
std::string Api::getURL() { return "http://" + mIp + ":" + mPort + "/api/"; } | ||
|
||
// Static | ||
size_t Api::write_callback(void *contents, size_t size, size_t nmemb, | ||
void *userp) { | ||
size_t total_size = size * nmemb; | ||
printf("%s", (char *)contents); // Print the received data | ||
|
||
static_cast<Api *>(userp)->mResponse = | ||
std::string((char *)contents, total_size); | ||
|
||
return total_size; // Return the number of bytes handled | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
#pragma once | ||
#include "curl/curl.h" | ||
#include "rapidjson/document.h" | ||
#include <string> | ||
namespace HomeAssistant { | ||
|
||
class Api { | ||
public: | ||
Api(); | ||
virtual ~Api(); | ||
|
||
rapidjson::Document doStuff(); | ||
|
||
private: | ||
static constexpr auto jsonHeader = "Content-Type: application/json"; | ||
static constexpr auto token = "nope :)"; | ||
|
||
std::string getSecurityHeader(); | ||
std::string getURL(); | ||
|
||
// Callback function to handle data received from the server | ||
static size_t write_callback(void *contents, size_t size, size_t nmemb, | ||
void *userp); | ||
|
||
std::string mIp = "192.168.86.105"; | ||
std::string mPort = "8123"; | ||
|
||
std::string mResponse = ""; | ||
|
||
CURL *mCurlHandle = nullptr; | ||
}; | ||
|
||
} // namespace HomeAssistant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
#include "curl/curl.h" | ||
|
||
namespace homeAssistant { | ||
|
||
class Entity { | ||
Entity(); | ||
}; | ||
|
||
} // namespace homeAssistant |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters