From dac3e0b716f1d6c30d81e92dc1de323ca7a7e240 Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Mon, 14 Dec 2020 16:13:44 +0100 Subject: [PATCH] Add [ESP620] to send http get URL --- esp3d/src/core/commands.cpp | 5 ++ esp3d/src/core/commands.h | 1 + esp3d/src/core/espcmd/ESP0.cpp | 1 + esp3d/src/core/espcmd/ESP600.cpp | 2 +- esp3d/src/core/espcmd/ESP620.cpp | 58 +++++++++++++++++++ esp3d/src/include/version.h | 2 +- .../notifications/notifications_service.cpp | 39 +++++++++++++ .../notifications/notifications_service.h | 2 + esp3d/src/modules/update/update_service.cpp | 2 - 9 files changed, 108 insertions(+), 4 deletions(-) create mode 100644 esp3d/src/core/espcmd/ESP620.cpp diff --git a/esp3d/src/core/commands.cpp b/esp3d/src/core/commands.cpp index c8941941..21cde1a2 100644 --- a/esp3d/src/core/commands.cpp +++ b/esp3d/src/core/commands.cpp @@ -528,6 +528,11 @@ bool Commands::execute_internal_command (int cmd, const char* cmd_params, level_ case 610: response = ESP610(cmd_params, auth_type, output); break; + //Send Notification using URL + //[ESP620]URL= [pwd=] + case 620: + response = ESP620(cmd_params, auth_type, output); + break; #endif //NOTIFICATION_FEATURE #if defined(FILESYSTEM_FEATURE) //Format ESP Filesystem diff --git a/esp3d/src/core/commands.h b/esp3d/src/core/commands.h index 2a44ccdb..4dd7d4d0 100644 --- a/esp3d/src/core/commands.h +++ b/esp3d/src/core/commands.h @@ -121,6 +121,7 @@ public: #if defined(NOTIFICATION_FEATURE) bool ESP600(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); bool ESP610(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); + bool ESP620(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); #endif //NOTIFICATION_FEATURE #if defined(FILESYSTEM_FEATURE) && defined(ESP_GCODE_HOST_FEATURE) bool ESP700(const char* cmd_params, level_authenticate_type auth_level, ESP3DOutput * output); diff --git a/esp3d/src/core/espcmd/ESP0.cpp b/esp3d/src/core/espcmd/ESP0.cpp index 7657e55e..64618b9e 100644 --- a/esp3d/src/core/espcmd/ESP0.cpp +++ b/esp3d/src/core/espcmd/ESP0.cpp @@ -112,6 +112,7 @@ const char * help[]= {"[ESP] - display this help", #if defined(NOTIFICATION_FEATURE) "[ESP600](message) - send notification", "[ESP610]type=(NONE/PUSHOVER/EMAIL/LINE) (T1=xxx) (T2=xxx) (TS=xxx) - display/set Notification settings", + "[ESP620]URL=http://XXXXXX - send GET notification", #endif //NOTIFICATION_FEATURE #if defined(FILESYSTEM_FEATURE) && defined(ESP_GCODE_HOST_FEATURE) "[ESP700](filename) - read ESP Filesystem file", diff --git a/esp3d/src/core/espcmd/ESP600.cpp b/esp3d/src/core/espcmd/ESP600.cpp index 05244351..0ce77896 100644 --- a/esp3d/src/core/espcmd/ESP600.cpp +++ b/esp3d/src/core/espcmd/ESP600.cpp @@ -25,7 +25,7 @@ #include "../../modules/authentication/authentication_service.h" #include "../../modules/notifications/notifications_service.h" //Send Notification -//[ESP600]msg [pwd=] +//[ESP600]msg [pwd=] bool Commands::ESP600(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) { bool response = true; diff --git a/esp3d/src/core/espcmd/ESP620.cpp b/esp3d/src/core/espcmd/ESP620.cpp new file mode 100644 index 00000000..d877e49e --- /dev/null +++ b/esp3d/src/core/espcmd/ESP620.cpp @@ -0,0 +1,58 @@ +/* + ESP620.cpp - ESP3D command class + + Copyright (c) 2014 Luc Lebosse. All rights reserved. + + This code is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This code is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with This code; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ +#include "../../include/esp3d_config.h" +#if defined (NOTIFICATION_FEATURE) +#include "../commands.h" +#include "../esp3doutput.h" +#include "../settings_esp3d.h" +#include "../../modules/authentication/authentication_service.h" +#include "../../modules/notifications/notifications_service.h" +//Send Notification using URL +//[ESP620]URL= [pwd=] +bool Commands::ESP620(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output) +{ + bool response = true; + String parameter; +#ifdef AUTHENTICATION_FEATURE + if (auth_type == LEVEL_GUEST) { + output->printERROR("Wrong authentication!", 401); + return false; + } +#else + (void)auth_type; +#endif //AUTHENTICATION_FEATURE + parameter = get_param (cmd_params, ""); + //get + if (parameter.length() == 0) { + output->printERROR ("Invalid message!"); + return false; + } else { + parameter = get_param (cmd_params, "URL="); + if (notificationsservice.GET(parameter.c_str())) { + output->printMSG ("ok"); + } else { + output->printERROR ("Cannot send notification!"); + return false; + } + } + return response; +} + +#endif //NOTIFICATION_FEATURE diff --git a/esp3d/src/include/version.h b/esp3d/src/include/version.h index fe4120b5..4da5a463 100644 --- a/esp3d/src/include/version.h +++ b/esp3d/src/include/version.h @@ -22,7 +22,7 @@ #define _VERSION_ESP3D_H //version and sources location -#define FW_VERSION "3.0.0.a75" +#define FW_VERSION "3.0.0.a76" #define REPOSITORY "https://github.com/luc-github/ESP3D/tree/3.0" #endif //_VERSION_ESP3D_H diff --git a/esp3d/src/modules/notifications/notifications_service.cpp b/esp3d/src/modules/notifications/notifications_service.cpp index 459d817e..d07096fb 100644 --- a/esp3d/src/modules/notifications/notifications_service.cpp +++ b/esp3d/src/modules/notifications/notifications_service.cpp @@ -46,11 +46,16 @@ typedef axTLS::WiFiClientSecure TSecureClient; #include typedef WiFiClientSecure TSecureClient; #endif //USING_AXTLS +#include +#include +#include #endif //ARDUINO_ARCH_ESP8266 #if defined(ARDUINO_ARCH_ESP32) #include typedef WiFiClientSecure TSecureClient; +#include +#include #endif //ARDUINO_ARCH_ESP32 #include @@ -464,6 +469,40 @@ bool NotificationsService::getEmailFromSettings() return true; } +bool NotificationsService::decode64(const char* encodedURL, char *decodedURL) +{ + size_t out_len = 0; +#if defined( ARDUINO_ARCH_ESP8266) + out_len = base64_decode_chars(encodedURL, strlen(encodedURL), decodedURL); +#endif //ARDUINO_ARCH_ESP8266 +#if defined( ARDUINO_ARCH_ESP32) + strcpy(base64_decode(encodedURL, strlen(encodedURL),&out_len); +#endif //ARDUINO_ARCH_ESP32 + log_esp3d("URLE: %s", encodedURL); + log_esp3d("URLD: %s", decodedURL); + return (out_len>0); +} + +bool NotificationsService::GET(const char * URL64) +{ + //TODO do we need https client ? + WiFiClient client; + HTTPClient http; //must be declared after WiFiClient for correct destruction order, because used by http.begin(client,...) + char * decodedurl[255]; + bool res = false; + if (decode64(URL64, (char*)decodedurl)) { + http.begin(client, (const char*)decodedurl); + int httpCode = http.GET(); + log_esp3d("HTTP code: %d", httpCode); + if (httpCode > 0) { + if(httpCode == HTTP_CODE_OK) { + res = true; + } + } + http.end(); + } + return res; +} bool NotificationsService::begin() { diff --git a/esp3d/src/modules/notifications/notifications_service.h b/esp3d/src/modules/notifications/notifications_service.h index 1aa8c7f1..224d1b19 100644 --- a/esp3d/src/modules/notifications/notifications_service.h +++ b/esp3d/src/modules/notifications/notifications_service.h @@ -33,6 +33,7 @@ public: void end(); void handle(); bool sendMSG(const char * title, const char * message); + bool GET(const char * URL64); const char * getTypeString(); bool started(); bool isAutonotification() @@ -53,6 +54,7 @@ private: String _settings; String _serveraddress; uint16_t _port; + bool decode64(const char* encodedURL, char *decodedURL); bool sendPushoverMSG(const char * title, const char * message); bool sendEmailMSG(const char * title, const char * message); bool sendLineMSG(const char * title, const char * message); diff --git a/esp3d/src/modules/update/update_service.cpp b/esp3d/src/modules/update/update_service.cpp index 6df8004b..077210d9 100644 --- a/esp3d/src/modules/update/update_service.cpp +++ b/esp3d/src/modules/update/update_service.cpp @@ -232,11 +232,9 @@ bool processingFileFunction (const char * section, const char * key, const char uint32_t v = 0; byte b = 0; bool done=false; - uint8_t size = 0; log_esp3d("[%s]%s=%s",section, key,value); //network / services / system sections if (strcasecmp("network",section)==0) { - size = sizeof(NetstringKeysVal)/sizeof(char*); if (!done) { done = processString(NetstringKeysVal,NetstringKeysPos,sizeof(NetstringKeysVal)/sizeof(char*), key, value, T, P ); }