From 49b575251cc57140d11c195c4768283830667cce Mon Sep 17 00:00:00 2001 From: CommanderRedYT Date: Wed, 29 Dec 2021 21:46:26 +0100 Subject: [PATCH] Added ifttt support --- main/espnowfunctions.cpp | 72 +++++++++++++++++++++++++++++++++++++++- main/espnowfunctions.h | 7 ++++ main/presets.cpp | 6 +++- main/stringsettings.h | 6 +++- 4 files changed, 88 insertions(+), 3 deletions(-) diff --git a/main/espnowfunctions.cpp b/main/espnowfunctions.cpp index fe1b5bc..c8310c2 100644 --- a/main/espnowfunctions.cpp +++ b/main/espnowfunctions.cpp @@ -7,6 +7,9 @@ #include #include #include +#include +#include +#include #include "globals.h" #include "utils.h" @@ -32,6 +35,68 @@ uint8_t initialized{0}; bool receiveTimeStamp{true}; bool receiveTsFromOtherBobbycars{true}; +namespace ifttt { +namespace { +cpputils::DelayedConstruction http_request; +} +void setup_request() +{ + if (!http_request.constructed()) + { + http_request.construct("qr_request", espcpputils::CoreAffinity::Core0); + } +} + +tl::expected start_qr_request(std::string event) +{ + if (!http_request.constructed()) + { + return tl::make_unexpected("request im oarsch"); + } + + if (const auto res = http_request->start(fmt::format("http://maker.ifttt.com/trigger/{}/with/key/{}", event, stringSettings.ifttt_key)); !res) + { + return res; + } + return{}; +} + +tl::expected check_request() +{ + if (!http_request.constructed()) + { + return tl::make_unexpected("request im oarsch"); + } + + if (!http_request->finished()) + { + return tl::make_unexpected("request has not finished"); + } + + const auto helper = cpputils::makeCleanupHelper([](){ http_request->clearFinished(); }); + + if (const auto result = http_request->result(); !result) + { + return tl::make_unexpected(result.error()); + } + else + { + ESP_LOGI(TAG, "%.*s", http_request->buffer().size(), http_request->buffer().data()); + return http_request->takeBuffer(); + } +} + +bool get_request_running() +{ + if (!http_request.constructed()) + { + return false; + } + + return http_request->finished(); +} +} // namespace + namespace { extern "C" void onReceive(const uint8_t *mac_addr, const uint8_t *data, int len) { @@ -68,10 +133,15 @@ extern "C" void onReceive(const uint8_t *mac_addr, const uint8_t *data, int len) return; } - if (msg_type == "BOBBYOPEN" && msg_value == stringSettings.esp_now_door_id) { + if (msg_type == "BOBBYOPEN" && msg_value == stringSettings.esp_now_door_id) + { //Serial.println("Bob is opening the door"); digitalWrite(PIN_RELAY, HIGH); ledtimer = espchrono::millis_clock::now(); + } + else if(msg_type == "BOBBYOPEN" && msg_value == stringSettings.esp_now_ifttt_door_id) + { + } /*const std::string_view data_str{(const char *)data, size_t(data_len)}; diff --git a/main/espnowfunctions.h b/main/espnowfunctions.h index 99f7726..5a45479 100644 --- a/main/espnowfunctions.h +++ b/main/espnowfunctions.h @@ -6,6 +6,7 @@ #include #include #include +#include namespace espnow { extern uint16_t lastYear; @@ -26,5 +27,11 @@ void initESPNow(); void handle(); void onRecvTs(uint64_t millis, bool isFromBobbycar = false); esp_err_t send_espnow_message(std::string_view message); +namespace ifttt { +void setup_request(); +tl::expected start_qr_request(std::string event); +tl::expected check_request(); +bool get_request_running(); +} // namespace ifttt } // namespace espnow #endif diff --git a/main/presets.cpp b/main/presets.cpp index ef0b1bd..a43faee 100644 --- a/main/presets.cpp +++ b/main/presets.cpp @@ -61,8 +61,12 @@ StringSettings makeDefaultStringSettings() .otaServerBranch = {}, #endif .webserver_password = {}, +#ifdef FEATURE_ESPNOW .esp_now_door_id = {}, - .esp_now_door_token = {} + .esp_now_door_token = {}, +#endif + .ifttt_key = {}, + .esp_now_ifttt_door_id = {}, }; } } // namespace presets diff --git a/main/stringsettings.h b/main/stringsettings.h index 2964d76..6581819 100644 --- a/main/stringsettings.h +++ b/main/stringsettings.h @@ -61,6 +61,8 @@ struct StringSettings std::string esp_now_door_id; std::string esp_now_door_token; #endif + std::string ifttt_key; + std::string esp_now_ifttt_door_id; }; template @@ -137,9 +139,11 @@ void StringSettings::executeForEveryCommonSetting(T &&callable) callable("webpw", webserver_password); #ifdef FEATURE_ESPNOW -#endif callable("espnow_doorId", esp_now_door_id); callable("espnow_doorTo", esp_now_door_token); +#endif + callable("iftttkey", ifttt_key); + callable("espnowiftt", esp_now_ifttt_door_id); } template