Skip to content

Commit

Permalink
Update all commands to handle error output properly if output is json
Browse files Browse the repository at this point in the history
Update documentations commands 114~290 - WIP
Fix ESP220 output for json
  • Loading branch information
luc-github committed Nov 3, 2023
1 parent 853d352 commit b7e9d44
Show file tree
Hide file tree
Showing 64 changed files with 5,920 additions and 4,210 deletions.
1,394 changes: 1,363 additions & 31 deletions docs/espXXX.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion esp3d/src/core/espcmd/ESP104.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include "../settings_esp3d.h"

#define COMMANDID 104
// Set STA fallback mode state at boot which can be BT, WIFI-AP, OFF
// Set STA fallback mode state at boot which can be BT, WIFI-SETUP, OFF
//[ESP104]<state> json=<no> pwd=<admin password>
bool Commands::ESP104(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
Expand Down
110 changes: 57 additions & 53 deletions esp3d/src/core/espcmd/ESP105.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,71 +18,75 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined (WIFI_FEATURE)
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#include "../../modules/wifi/wificonfig.h"
#include "../../modules/authentication/authentication_service.h"
#define COMMANDID 105
//AP SSID
//[ESP105]<SSID> [json=no] [pwd=<admin password>]
bool Commands::ESP105(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
{
bool noError = true;
bool json = has_tag (cmd_params, "json");
String response;
String parameter;
int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead

#define COMMANDID 105
// AP SSID
//[ESP105]<SSID> [json=no] [pwd=<admin password>]
bool Commands::ESP105(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
bool json = has_tag(cmd_params, "json");
String response;
String parameter;
int errorCode = 200; // unless it is a server error use 200 as default and
// set error in json instead

#ifdef AUTHENTICATION_FEATURE
if (auth_type == LEVEL_GUEST) {
response = format_response(COMMANDID, json, false, "Guest user can't use this command");
noError = false;
errorCode = 401;
}
if (auth_type == LEVEL_GUEST) {
response = format_response(COMMANDID, json, false,
"Guest user can't use this command");
noError = false;
errorCode = 401;
}
#else
(void)auth_type;
#endif //AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param (cmd_params, ""));
//get
if (parameter.length() == 0) {
response = format_response(COMMANDID, json, true, Settings_ESP3D::read_string(ESP_AP_SSID));
} else { //set
(void)auth_type;
#endif // AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param(cmd_params, ""));
// get
if (parameter.length() == 0) {
response = format_response(COMMANDID, json, true,
Settings_ESP3D::read_string(ESP_AP_SSID));
} else { // set
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
response = format_response(COMMANDID, json, false, "Wrong authentication level");
noError = false;
errorCode = 401;
}
#endif //AUTHENTICATION_FEATURE
if (noError) {
if (!WiFiConfig::isSSIDValid (parameter.c_str() ) ) {
response = format_response(COMMANDID, json, false, "Incorrect SSID");
noError = false;
} else {
if(!Settings_ESP3D::write_string(ESP_AP_SSID, parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
response = format_response(COMMANDID, json, true, "ok");
}
}
}
if (auth_type != LEVEL_ADMIN) {
response = format_response(COMMANDID, json, false,
"Wrong authentication level");
noError = false;
errorCode = 401;
}
#endif // AUTHENTICATION_FEATURE
if (noError) {
if (!WiFiConfig::isSSIDValid(parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Incorrect SSID");
noError = false;
} else {
if (!Settings_ESP3D::write_string(ESP_AP_SSID, parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
response = format_response(COMMANDID, json, true, "ok");
}
}
}
}
}
if (json) {
output->printLN(response.c_str());
} else {
if (noError) {
if (json) {
output->printLN (response.c_str() );
} else {
output->printMSG (response.c_str() );
}
output->printMSG(response.c_str());
} else {
output->printERROR(response.c_str(), errorCode);
output->printERROR(response.c_str(), errorCode);
}
return noError;
}
return noError;
}

#endif //WIFI_FEATURE
#endif // WIFI_FEATURE
98 changes: 51 additions & 47 deletions esp3d/src/core/espcmd/ESP106.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,65 +18,69 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined (WIFI_FEATURE)
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/wifi/wificonfig.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#include "../../modules/wifi/wificonfig.h"
#include "../../modules/authentication/authentication_service.h"
#define COMMANDID 106
//AP Password

#define COMMANDID 106
// AP Password
//[ESP106]<Password> [json=no] [pwd=<admin password>]
bool Commands::ESP106(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
{
bool noError = true;
bool json = has_tag (cmd_params, "json");
bool clearSetting = has_tag (cmd_params,"NOPASSWORD");
String response;
String parameter;
int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead
bool Commands::ESP106(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
bool json = has_tag(cmd_params, "json");
bool clearSetting = has_tag(cmd_params, "NOPASSWORD");
String response;
String parameter;
int errorCode = 200; // unless it is a server error use 200 as default and
// set error in json instead

#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
response = format_response(COMMANDID, json, false, "Wrong authentication level");
noError = false;
errorCode = 401;
}
if (auth_type != LEVEL_ADMIN) {
response =
format_response(COMMANDID, json, false, "Wrong authentication level");
noError = false;
errorCode = 401;
}
#else
(void)auth_type;
#endif //AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param (cmd_params, ""));
if (parameter.length() == 0) {
response = format_response(COMMANDID, json, false, "Password not displayable");
noError = false;
(void)auth_type;
#endif // AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param(cmd_params, ""));
if (parameter.length() == 0) {
response =
format_response(COMMANDID, json, false, "Password not displayable");
noError = false;
} else {
if (clearSetting) {
parameter = "";
}
if (!WiFiConfig::isPasswordValid(parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
if (!Settings_ESP3D::write_string(ESP_AP_PASSWORD, parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
if (clearSetting) {
parameter="";
}
if (!WiFiConfig::isPasswordValid (parameter.c_str() ) ) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
if(!Settings_ESP3D::write_string(ESP_AP_PASSWORD, parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
response = format_response(COMMANDID, json, true, "ok");
}
}
response = format_response(COMMANDID, json, true, "ok");
}
}
}
}
if (json) {
output->printLN(response.c_str());
} else {
if (noError) {
if (json) {
output->printLN (response.c_str() );
} else {
output->printMSG (response.c_str() );
}
output->printMSG(response.c_str());
} else {
output->printERROR(response.c_str(), errorCode);
output->printERROR(response.c_str(), errorCode);
}
return noError;
}
return noError;
}

#endif //WIFI_FEATURE
#endif // WIFI_FEATURE
111 changes: 59 additions & 52 deletions esp3d/src/core/espcmd/ESP107.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,70 +18,77 @@
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "../../include/esp3d_config.h"
#if defined (WIFI_FEATURE)
#if defined(WIFI_FEATURE)
#include "../../modules/authentication/authentication_service.h"
#include "../../modules/network/netconfig.h"
#include "../../modules/wifi/wificonfig.h"
#include "../commands.h"
#include "../esp3doutput.h"
#include "../settings_esp3d.h"
#include "../../modules/wifi/wificonfig.h"
#include "../../modules/network/netconfig.h"
#include "../../modules/authentication/authentication_service.h"
#define COMMANDID 107
//Change AP IP

#define COMMANDID 107
// Change AP IP
//[ESP107]<IP> [json=no] pwd=<admin password>
bool Commands::ESP107(const char* cmd_params, level_authenticate_type auth_type, ESP3DOutput * output)
{
bool noError = true;
bool json = has_tag (cmd_params, "json");
String response;
String parameter;
int errorCode = 200; //unless it is a server error use 200 as default and set error in json instead
bool Commands::ESP107(const char* cmd_params, level_authenticate_type auth_type,
ESP3DOutput* output) {
bool noError = true;
bool json = has_tag(cmd_params, "json");
String response;
String parameter;
int errorCode = 200; // unless it is a server error use 200 as default and
// set error in json instead

#ifdef AUTHENTICATION_FEATURE
if (auth_type == LEVEL_GUEST) {
response = format_response(COMMANDID, json, false, "Guest user can't use this command");
noError = false;
errorCode = 401;
}
if (auth_type == LEVEL_GUEST) {
response = format_response(COMMANDID, json, false,
"Guest user can't use this command");
noError = false;
errorCode = 401;
}
#else
(void)auth_type;
#endif //AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param (cmd_params, ""));
//get
if (parameter.length() == 0) {
response = format_response(COMMANDID, json, true, Settings_ESP3D::read_IP_String(ESP_AP_IP_VALUE).c_str());
} else { //set
(void)auth_type;
#endif // AUTHENTICATION_FEATURE
if (noError) {
parameter = clean_param(get_param(cmd_params, ""));
// get
if (parameter.length() == 0) {
response = format_response(
COMMANDID, json, true,
Settings_ESP3D::read_IP_String(ESP_AP_IP_VALUE).c_str());
} else { // set
#ifdef AUTHENTICATION_FEATURE
if (auth_type != LEVEL_ADMIN) {
response = format_response(COMMANDID, json, false, "Wrong authentication level");
noError = false;
errorCode = 401;
}
#endif //AUTHENTICATION_FEATURE
if (noError) {
if ( !NetConfig::isValidIP(parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Incorrect IP");
noError = false;
}
if ( !Settings_ESP3D::write_IP_String(ESP_AP_IP_VALUE, parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
response = format_response(COMMANDID, json, true, "ok");
}
}
if (auth_type != LEVEL_ADMIN) {
response = format_response(COMMANDID, json, false,
"Wrong authentication level");
noError = false;
errorCode = 401;
}
#endif // AUTHENTICATION_FEATURE
if (noError) {
if (!NetConfig::isValidIP(parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Incorrect IP");
noError = false;
}
}
if (noError) {
if (json) {
output->printLN (response.c_str() );
if (!Settings_ESP3D::write_IP_String(ESP_AP_IP_VALUE,
parameter.c_str())) {
response = format_response(COMMANDID, json, false, "Set failed");
noError = false;
} else {
output->printMSG (response.c_str() );
response = format_response(COMMANDID, json, true, "ok");
}
}
}
}
if (json) {
output->printLN(response.c_str());
} else {
if (noError) {
output->printMSG(response.c_str());
} else {
output->printERROR(response.c_str(), errorCode);
output->printERROR(response.c_str(), errorCode);
}
return noError;
}
return noError;
}

#endif //WIFI_FEATURE
#endif // WIFI_FEATURE
Loading

0 comments on commit b7e9d44

Please sign in to comment.