Skip to content

Commit

Permalink
add rapidjson to project
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 2 deletions.
49 changes: 49 additions & 0 deletions Platformio/HomeAssistant/Api.cpp
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
}
33 changes: 33 additions & 0 deletions Platformio/HomeAssistant/Api.hpp
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
10 changes: 10 additions & 0 deletions Platformio/HomeAssistant/Entity.hpp
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
7 changes: 5 additions & 2 deletions Platformio/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,17 +52,20 @@ build_flags =
-I OmoteUI/UIs/Matts
-I HAL
-I HAL/HardwareModules
-I HomeAssistant

lib_deps =
;lvgl/lvgl@^8.3.9
lvgl=https://github.com/lvgl/lvgl/archive/refs/tags/v8.3.9.zip

rapidjson = https://github.com/Tencent/rapidjson/archive/refs/tags/v1.1.0.zip

lib_archive = false
build_src_filter =
+<../OmoteUI/*>
-<../OmoteUI/UIs/Basic/*>
+<../HAL/*.cpp>
+<../HAL/HardwareModules/*.cpp>
+<../HomeAssistant/*.cpp>


[env:esp32]
Expand All @@ -72,6 +75,7 @@ framework = arduino
monitor_speed = 115200
board_build.f_flash = 80000000L
board_build.f_cpu = 240000000L
board_build.partitions = huge_app.csv
upload_speed = 1000000
lib_deps =
${env.lib_deps}
Expand Down Expand Up @@ -132,7 +136,6 @@ build_src_filter =
+<../HAL/Targets/ESP32/*>
${env.build_src_filter}


[env:x64_sim]
platform = native@^1.1.3
build_flags =
Expand Down

0 comments on commit 5dacf0f

Please sign in to comment.