diff --git a/Platformio/HomeAssistant/Api.cpp b/Platformio/HomeAssistant/Api.cpp new file mode 100644 index 00000000..f624a43e --- /dev/null +++ b/Platformio/HomeAssistant/Api.cpp @@ -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(userp)->mResponse = + std::string((char *)contents, total_size); + + return total_size; // Return the number of bytes handled +} \ No newline at end of file diff --git a/Platformio/HomeAssistant/Api.hpp b/Platformio/HomeAssistant/Api.hpp new file mode 100644 index 00000000..0a938fc6 --- /dev/null +++ b/Platformio/HomeAssistant/Api.hpp @@ -0,0 +1,33 @@ +#pragma once +#include "curl/curl.h" +#include "rapidjson/document.h" +#include +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 \ No newline at end of file diff --git a/Platformio/HomeAssistant/Entity.hpp b/Platformio/HomeAssistant/Entity.hpp new file mode 100644 index 00000000..4d597fe8 --- /dev/null +++ b/Platformio/HomeAssistant/Entity.hpp @@ -0,0 +1,10 @@ +#pragma once +#include "curl/curl.h" + +namespace homeAssistant { + +class Entity { + Entity(); +}; + +} // namespace homeAssistant \ No newline at end of file diff --git a/Platformio/platformio.ini b/Platformio/platformio.ini index ed5fb361..435add4b 100644 --- a/Platformio/platformio.ini +++ b/Platformio/platformio.ini @@ -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] @@ -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} @@ -132,7 +136,6 @@ build_src_filter = +<../HAL/Targets/ESP32/*> ${env.build_src_filter} - [env:x64_sim] platform = native@^1.1.3 build_flags =