diff --git a/ActionHandler.h.d1.bin b/ActionHandler.h.d1.bin index 55e1446..8591d56 100644 Binary files a/ActionHandler.h.d1.bin and b/ActionHandler.h.d1.bin differ diff --git a/ButtonHandler.h b/ButtonHandler.h index 6d96016..8c2cc73 100644 --- a/ButtonHandler.h +++ b/ButtonHandler.h @@ -13,9 +13,9 @@ void handlePress(int keyNumber) { handleSocketCommand(buttons["pages"][page]["buttons"][keyNumber - 1]["command_id"].as()); } else if (buttons["pages"][page]["buttons"][keyNumber - 1]["type"].as() == "apiCall") { if (buttons["pages"][page]["buttons"][keyNumber - 1].containsKey("fingerprint")) { - handleApiCall(buttons["pages"][page]["buttons"][keyNumber - 1]["url"].as(), buttons["pages"][page]["buttons"][keyNumber - 1]["fingerprint"].as()); + handleApiCall(buttons["pages"][page]["buttons"][keyNumber - 1]["url"].as(), buttons["pages"][page]["buttons"][keyNumber - 1]["requestType"].as(), buttons["pages"][page]["buttons"][keyNumber - 1]["headers"].to(), buttons["pages"][page]["buttons"][keyNumber - 1]["body"].as(), buttons["pages"][page]["buttons"][keyNumber - 1]["fingerprint"].as()); } else { - handleApiCall(buttons["pages"][page]["buttons"][keyNumber - 1]["url"].as()); + handleApiCall(buttons["pages"][page]["buttons"][keyNumber - 1]["url"].as(), buttons["pages"][page]["buttons"][keyNumber - 1]["requestType"].as(), buttons["pages"][page]["buttons"][keyNumber - 1]["headers"].to(), buttons["pages"][page]["buttons"][keyNumber - 1]["body"].as()); } }else if(buttons["pages"][page]["buttons"][keyNumber - 1]["type"].as() == "mqtt"){ callMqttCommand(buttons["pages"][page]["buttons"][keyNumber - 1]["topic"].as(), buttons["pages"][page]["buttons"][keyNumber - 1]["payload"].as()); diff --git a/ExecutionHandler.h b/ExecutionHandler.h index ffeca0f..c7b70f7 100644 --- a/ExecutionHandler.h +++ b/ExecutionHandler.h @@ -1,16 +1,62 @@ -//Executes the API call with a provided fingerprint -void handleApiCall(String url, String fingerprint = configJson["fingerprint"]) { +void addHeadersToCall(HTTPClient& http, JsonArray httpHeaders){ + for(JsonVariant v : httpHeaders){ + http.addHeader(v["headerKey"].as(), v["headerValue"].as()); + } +} + + +void handleGETRequest(String url, JsonArray httpHeaders, String fingerprint){ if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status HTTPClient http; //Declare an object of class HTTPClient - http.begin(url, fingerprint); //Specify request destination + if(fingerprint != ""){ + http.begin(url, fingerprint); //Specify request destination + } + else{ + http.begin(url); //Specify request destination + } + + if(httpHeaders.size() > 0){ + addHeadersToCall(http, httpHeaders); + } int httpCode = http.GET(); //Send the request if (httpCode > 0) { //Check the returning code String payload = http.getString(); //Get the request response payload } - http.end(); //Close connection + http.end(); } } +void handlePOSTRequest(String url, String body, JsonArray httpHeaders, String fingerprint){ + if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status + HTTPClient http; //Declare an object of class HTTPClient + if(fingerprint != ""){ + http.begin(url, fingerprint); //Specify request destination + } + else{ + http.begin(url); //Specify request destination + } + if(httpHeaders.size() > 0){ + addHeadersToCall(http, httpHeaders); + } + int httpCode = http.POST(body); //Send the request + if (httpCode > 0) { //Check the returning code + String payload = http.getString(); //Get the request response payload + } + http.end(); + } +} + + + +//Executes the API call with a provided fingerprint +void handleApiCall(String url, String requestType, JsonArray httpHeaders, String body, String fingerprint = configJson["fingerprint"]) { + if(requestType == "POST"){ + handlePOSTRequest(url, body, httpHeaders, fingerprint); + } + else{ + handleGETRequest(url, httpHeaders, fingerprint); + } +} //Handles the changing of the pages void handleBoardChange(int keyNumber) { diff --git a/SysVariables.h b/SysVariables.h index 76d7061..f14bb95 100644 --- a/SysVariables.h +++ b/SysVariables.h @@ -1,6 +1,6 @@ //All availables variables are here #define ServerVersion "1.0" -#define FirmwareVersion "v 0.4" +#define FirmwareVersion "v 0.5" String webpage = ""; bool SD_present = false; diff --git a/macrodeck.ino b/macrodeck.ino index b2809b0..ae6b31e 100644 --- a/macrodeck.ino +++ b/macrodeck.ino @@ -58,17 +58,22 @@ void setup() { buttons = doc.as(); if (!buttons.isNull()) { buttonConfigJson = buttonConfigDoc.as(); - client.begin(configJson["socketHost"].as(), configJson["socketPort"].as(), path); - mqttClient.setServer(configJson["mqttBroker"].as(), configJson["mqttPort"].as()); - while (!mqttClient.connected()) { - String client_id = configJson["mqttClientID"].as(); - if (mqttClient.connect(client_id.c_str(), configJson["mqttUsername"].as(), configJson["mqttPassword"].as())) { - } else { - tft.print("failed with state "); - tft.print(mqttClient.state()); - delay(2000); + if (configJson["deckboardConnection"].as()){ + client.begin(configJson["socketHost"].as(), configJson["socketPort"].as(), path); + } + if (configJson["mqttConnection"].as()){ + mqttClient.setServer(configJson["mqttBroker"].as(), configJson["mqttPort"].as()); + while (!mqttClient.connected()) { + String client_id = configJson["mqttClientID"].as(); + if (mqttClient.connect(client_id.c_str(), configJson["mqttUsername"].as(), configJson["mqttPassword"].as())) { + } else { + tft.print("failed with state "); + tft.print(mqttClient.state()); + delay(2000); + } } } + setCurrentBoard(); interateOverButtonsOnPage(); } @@ -142,8 +147,12 @@ void loop() { tft.println("development is missing. Please update the configuration through the app"); } delay(100); - client.loop(); - mqttClient.loop(); + if (configJson["deckboardConnection"].as()){ + client.loop(); + } + if (configJson["mqttConnection"].as()){ + mqttClient.loop(); + } webserver.handleClient(); tft.setTextColor(TFT_WHITE, TFT_BLACK);