From c49142402efd254842eebf340fd93dddfa7407de Mon Sep 17 00:00:00 2001 From: Gijs Noorlander Date: Tue, 29 Oct 2019 18:07:48 +0100 Subject: [PATCH] [FEATURE_SD] Fix build issues when FEATURE_SD is defined (not finished) When enabling the FEATURE_SD, something with the define of __FlashStringHelper is not correct anymore. This needs a lot of changes which were needed anyway. But also arrays of __FlashStringHelper must be changed. All done so far, only need to have a look at static const __FlashStringHelper *gpMenu[8][3] = { --- src/Command.ino | 2 + src/Controller.ino | 2 +- src/Convert.ino | 46 +++++++------- src/ESPEasy_fdwdecl.h | 28 ++++++++- src/Misc.ino | 23 ++----- src/src/Commands/Common.cpp | 35 +++++------ src/src/Commands/Common.h | 39 ++++++------ src/src/Commands/MQTT.cpp | 69 ++++++++++++++++++++ src/src/Commands/MQTT.h | 64 ++----------------- src/src/Commands/Networks.cpp | 41 ++++++++++++ src/src/Commands/Networks.h | 40 +++--------- src/src/Commands/Rules.cpp | 54 ++++++++++++++++ src/src/Commands/Rules.h | 49 ++------------- src/src/Commands/SDCARD.cpp | 34 ++++++++++ src/src/Commands/SDCARD.h | 21 ++----- src/src/Commands/Settings.cpp | 91 +++++++++++++++++++++++++++ src/src/Commands/Settings.h | 89 +++----------------------- src/src/Commands/Time.cpp | 56 +++++++++++++++++ src/src/Commands/Time.h | 52 ++-------------- src/src/Commands/UDP.cpp | 66 ++++++++++++++++++++ src/src/Commands/UPD.h | 62 ++---------------- src/src/Commands/WiFi.cpp | 113 +++++++++++++++++++++++++++++++++ src/src/Commands/WiFi.h | 114 ++++------------------------------ 23 files changed, 671 insertions(+), 519 deletions(-) create mode 100644 src/src/Commands/MQTT.cpp create mode 100644 src/src/Commands/Networks.cpp create mode 100644 src/src/Commands/Rules.cpp create mode 100644 src/src/Commands/SDCARD.cpp create mode 100644 src/src/Commands/Settings.cpp create mode 100644 src/src/Commands/Time.cpp create mode 100644 src/src/Commands/UDP.cpp create mode 100644 src/src/Commands/WiFi.cpp diff --git a/src/Command.ino b/src/Command.ino index 72f6f920c..40f3dc979 100644 --- a/src/Command.ino +++ b/src/Command.ino @@ -24,6 +24,8 @@ #include "src/Commands/wd.h" #include "src/Commands/WiFi.h" +#include "define_plugin_sets.h" + /*********************************************************************************************\ * Registers command diff --git a/src/Controller.ino b/src/Controller.ino index 77037c614..5ad449fd0 100644 --- a/src/Controller.ino +++ b/src/Controller.ino @@ -365,7 +365,7 @@ void SendStatus(byte source, const String& status) } #ifdef USES_MQTT -boolean MQTTpublish(int controller_idx, const char *topic, const char *payload, boolean retained) +bool MQTTpublish(int controller_idx, const char *topic, const char *payload, boolean retained) { const bool success = MQTTDelayHandler.addToQueue(MQTT_queue_element(controller_idx, topic, payload, retained)); diff --git a/src/Convert.ino b/src/Convert.ino index 8ed5ad612..10ced371f 100644 --- a/src/Convert.ino +++ b/src/Convert.ino @@ -1,39 +1,35 @@ - /*********************************************************************************************\ Convert bearing in degree to bearing string \*********************************************************************************************/ String getBearing(int degrees) { - const __FlashStringHelper *bearing[] = { - F("N"), - F("NNE"), - F("NE"), - F("ENE"), - F("E"), - F("ESE"), - F("SE"), - F("SSE"), - F("S"), - F("SSW"), - F("SW"), - F("WSW"), - F("W"), - F("WNW"), - F("NW"), - F("NNW") - }; - int nr_directions = (int)(sizeof(bearing) / sizeof(bearing[0])); + const int nr_directions = 16; float stepsize = (360.0 / nr_directions); if (degrees < 0) { degrees += 360; } // Allow for bearing -360 .. 359 int bearing_idx = int((degrees + (stepsize / 2.0)) / stepsize) % nr_directions; - if (bearing_idx < 0) { - return ""; - } - else { - return bearing[bearing_idx]; + if (bearing_idx >= 0) { + switch (bearing_idx) { + case 0: return F("N"); + case 1: return F("NNE"); + case 2: return F("NE"); + case 3: return F("ENE"); + case 4: return F("E"); + case 5: return F("ESE"); + case 6: return F("SE"); + case 7: return F("SSE"); + case 8: return F("S"); + case 9: return F("SSW"); + case 10: return F("SW"); + case 11: return F("WSW"); + case 12: return F("W"); + case 13: return F("WNW"); + case 14: return F("NW"); + case 15: return F("NNW"); + } } + return ""; } float CelsiusToFahrenheit(float celsius) { diff --git a/src/ESPEasy_fdwdecl.h b/src/ESPEasy_fdwdecl.h index f5ca6c544..c169c095b 100644 --- a/src/ESPEasy_fdwdecl.h +++ b/src/ESPEasy_fdwdecl.h @@ -113,7 +113,7 @@ byte getProtocolIndex(byte Number); // void runPeriodicalMQTT(); // void updateMQTTclient_connected(); -// int firstEnabledMQTTController(); +int firstEnabledMQTTController(); // String getMQTT_state(); void callback(char *c_topic, byte *b_payload, @@ -122,6 +122,7 @@ void MQTTDisconnect(); bool MQTTConnect(int controller_idx); bool MQTTCheck(int controller_idx); void schedule_all_tasks_using_MQTT_controller(); +bool MQTTpublish(int controller_idx, const char *topic, const char *payload, boolean retained); #endif // ifdef USES_MQTT @@ -147,4 +148,29 @@ String parseStringKeepCase(const String& string, byte indexFind); String parseStringToEnd(const String& string, byte indexFind); String parseStringToEndKeepCase(const String& string, byte indexFind); +String describeAllowedIPrange(); +void clearAccessBlock(); +String rulesProcessingFile(const String& fileName, String& event); +int Calculate(const char *input, float* result); + + +# include +void WifiScan(); +void WiFiConnectRelaxed(); +void WifiDisconnect(); +void setAP(bool enable); +void setSTA(bool enable); +void setWifiMode(WiFiMode_t wifimode); + + +String SaveSettings(void); +String LoadSettings(); +unsigned long FreeMem(void); +void ResetFactory(); +void reboot(); +void SendUDPCommand(byte destUnit, char *data, byte dataLength); + +#include +void printDirectory(File dir, int numTabs); + #endif // ESPEASY_FWD_DECL_H diff --git a/src/Misc.ino b/src/Misc.ino index ddec17cc9..45ef5fdc5 100644 --- a/src/Misc.ino +++ b/src/Misc.ino @@ -2343,6 +2343,7 @@ void SendValueLogger(taskIndex_t TaskIndex) { #if !defined(BUILD_NO_DEBUG) || defined(FEATURE_SD) bool featureSD = false; + String logger; #ifdef FEATURE_SD featureSD = true; #endif @@ -2350,7 +2351,6 @@ void SendValueLogger(taskIndex_t TaskIndex) if (featureSD || loglevelActiveFor(LOG_LEVEL_DEBUG)) { deviceIndex_t DeviceIndex = getDeviceIndex_from_TaskIndex(TaskIndex); if (validDeviceIndex(DeviceIndex)) { - String logger; LoadTaskSettings(TaskIndex); for (byte varNr = 0; varNr < Device[DeviceIndex].ValueCount; varNr++) { @@ -2465,29 +2465,18 @@ void checkRAMtoLog(void){ myRamTracker.getTraceBuffer(); } -void checkRAM(const __FlashStringHelper* flashString, int a ) { - String s=String(a); - checkRAM(flashString,s); -} - -void checkRAM(const __FlashStringHelper* flashString, const String &a ) { - String s = flashString; - checkRAM(s,a); +void checkRAM(const String &flashString, int a ) { + checkRAM(flashString, String(a)); } void checkRAM(const String &flashString, const String &a ) { String s = flashString; - s+=" ("; - s+=a; - s+=")"; + s += " ("; + s += a; + s += ')'; checkRAM(s); } -void checkRAM( const __FlashStringHelper* flashString) -{ - checkRAM(String(flashString)); -} - void checkRAM( const String &descr ) { myRamTracker.registerRamState(descr); diff --git a/src/src/Commands/Common.cpp b/src/src/Commands/Common.cpp index 5f664ea36..66dc91316 100644 --- a/src/src/Commands/Common.cpp +++ b/src/src/Commands/Common.cpp @@ -42,13 +42,12 @@ String return_see_serial(struct EventStruct *event) return F("Output sent to serial"); } - -String Command_GetORSetIP(struct EventStruct *event, - const __FlashStringHelper *targetDescription, - const char *Line, - byte *IP, - IPAddress dhcpIP, - int arg) +String Command_GetORSetIP(struct EventStruct *event, + const String & targetDescription, + const char *Line, + byte *IP, + IPAddress dhcpIP, + int arg) { bool hasArgument = false; { @@ -81,12 +80,12 @@ String Command_GetORSetIP(struct EventStruct *event, return return_command_success(); } -String Command_GetORSetString(struct EventStruct *event, - const __FlashStringHelper *targetDescription, - const char *Line, - char *target, - size_t len, - int arg +String Command_GetORSetString(struct EventStruct *event, + const String & targetDescription, + const char *Line, + char *target, + size_t len, + int arg ) { bool hasArgument = false; @@ -118,11 +117,11 @@ String Command_GetORSetString(struct EventStruct *event, return return_command_success(); } -String Command_GetORSetBool(struct EventStruct *event, - const __FlashStringHelper *targetDescription, - const char *Line, - bool *value, - int arg) +String Command_GetORSetBool(struct EventStruct *event, + const String & targetDescription, + const char *Line, + bool *value, + int arg) { bool hasArgument = false; { diff --git a/src/src/Commands/Common.h b/src/src/Commands/Common.h index 2c91a9885..edb990fc0 100644 --- a/src/src/Commands/Common.h +++ b/src/src/Commands/Common.h @@ -9,30 +9,31 @@ class IPAddress; String return_command_success(); String return_command_failed(); String return_not_connected(); -String return_result(struct EventStruct *event, const String& result); +String return_result(struct EventStruct *event, + const String & result); String return_see_serial(struct EventStruct *event); -bool IsNumeric(const char *source); +bool IsNumeric(const char *source); -String Command_GetORSetIP(struct EventStruct *event, - const __FlashStringHelper *targetDescription, - const char *Line, - byte *IP, - IPAddress dhcpIP, - int arg); +String Command_GetORSetIP(struct EventStruct *event, + const String & targetDescription, + const char *Line, + byte *IP, + IPAddress dhcpIP, + int arg); -String Command_GetORSetString(struct EventStruct *event, - const __FlashStringHelper *targetDescription, - const char *Line, - char *target, - size_t len, - int arg +String Command_GetORSetString(struct EventStruct *event, + const String & targetDescription, + const char *Line, + char *target, + size_t len, + int arg ); -String Command_GetORSetBool(struct EventStruct *event, - const __FlashStringHelper *targetDescription, - const char *Line, - bool *value, - int arg); +String Command_GetORSetBool(struct EventStruct *event, + const String & targetDescription, + const char *Line, + bool *value, + int arg); #endif // COMMAND_COMMON_H diff --git a/src/src/Commands/MQTT.cpp b/src/src/Commands/MQTT.cpp new file mode 100644 index 000000000..16b45e36b --- /dev/null +++ b/src/src/Commands/MQTT.cpp @@ -0,0 +1,69 @@ +#include "MQTT.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" + +#include "../../ESPEasy_fdwdecl.h" + + +String Command_MQTT_Retain(struct EventStruct *event, const char *Line) +{ + return Command_GetORSetBool(event, F("MQTT Retain:"), + Line, + (bool *)&Settings.MQTTRetainFlag, + 1); +} + +String Command_MQTT_UseUnitNameAsClientId(struct EventStruct *event, const char *Line) +{ + return Command_GetORSetBool(event, F("MQTT Use Unit Name as ClientId:"), + Line, + (bool *)&Settings.MQTTUseUnitNameAsClientId, + 1); +} + +String Command_MQTT_messageDelay(struct EventStruct *event, const char *Line) +{ + if (HasArgv(Line, 2)) { + Settings.MessageDelay = event->Par1; + } + else { + String result = F("MQTT message delay:"); + result += Settings.MessageDelay; + serialPrintln(); + serialPrintln(result); + return result; + } + return return_command_success(); +} + +String Command_MQTT_Publish(struct EventStruct *event, const char *Line) +{ + if (WiFiConnected()) { + // ToDo TD-er: Not sure about this function, but at least it sends to an existing MQTTclient + int enabledMqttController = firstEnabledMQTTController(); + + if (enabledMqttController >= 0) { + // Command structure: Publish,, + String topic = parseStringKeepCase(Line, 1); + String value = parseStringKeepCase(Line, 2); + + if ((topic.length() > 0) && (value.length() > 0)) { + // @giig1967g: if payload starts with '=' then treat it as a Formula and evaluate accordingly + // The evaluated value is already present in event->Par2 + // FIXME TD-er: Is the evaluated value always present in event->Par2 ? + // Should it already be evaluated, or should we evaluate it now? + if (value.c_str()[0] != '=') { + MQTTpublish(enabledMqttController, topic.c_str(), value.c_str(), Settings.MQTTRetainFlag); + } + else { + MQTTpublish(enabledMqttController, topic.c_str(), String(event->Par2).c_str(), Settings.MQTTRetainFlag); + } + } + return return_command_success(); + } + return F("No MQTT controller enabled"); + } + return return_not_connected(); +} diff --git a/src/src/Commands/MQTT.h b/src/src/Commands/MQTT.h index 4ed37b460..7d78870a7 100644 --- a/src/src/Commands/MQTT.h +++ b/src/src/Commands/MQTT.h @@ -1,66 +1,12 @@ #ifndef COMMAND_MQTT_H #define COMMAND_MQTT_H +class String; -String Command_MQTT_Retain(struct EventStruct *event, const char *Line) -{ - return Command_GetORSetBool(event, F("MQTT Retain:"), - Line, - (bool *)&Settings.MQTTRetainFlag, - 1); -} +String Command_MQTT_Retain(struct EventStruct *event, const char *Line); +String Command_MQTT_UseUnitNameAsClientId(struct EventStruct *event, const char *Line); +String Command_MQTT_messageDelay(struct EventStruct *event, const char *Line); +String Command_MQTT_Publish(struct EventStruct *event, const char *Line); -String Command_MQTT_UseUnitNameAsClientId(struct EventStruct *event, const char *Line) -{ - return Command_GetORSetBool(event, F("MQTT Use Unit Name as ClientId:"), - Line, - (bool *)&Settings.MQTTUseUnitNameAsClientId, - 1); -} - -String Command_MQTT_messageDelay(struct EventStruct *event, const char *Line) -{ - if (HasArgv(Line, 2)) { - Settings.MessageDelay = event->Par1; - } - else { - String result = F("MQTT message delay:"); - result += Settings.MessageDelay; - serialPrintln(); - serialPrintln(result); - return result; - } - return return_command_success(); -} - -String Command_MQTT_Publish(struct EventStruct *event, const char *Line) -{ - if (WiFiConnected()) { - // ToDo TD-er: Not sure about this function, but at least it sends to an existing MQTTclient - int enabledMqttController = firstEnabledMQTTController(); - - if (enabledMqttController >= 0) { - // Command structure: Publish,, - String topic = parseStringKeepCase(Line, 1); - String value = parseStringKeepCase(Line, 2); - - if ((topic.length() > 0) && (value.length() > 0)) { - // @giig1967g: if payload starts with '=' then treat it as a Formula and evaluate accordingly - // The evaluated value is already present in event->Par2 - // FIXME TD-er: Is the evaluated value always present in event->Par2 ? - // Should it already be evaluated, or should we evaluate it now? - if (value.c_str()[0] != '=') { - MQTTpublish(enabledMqttController, topic.c_str(), value.c_str(), Settings.MQTTRetainFlag); - } - else { - MQTTpublish(enabledMqttController, topic.c_str(), String(event->Par2).c_str(), Settings.MQTTRetainFlag); - } - } - return return_command_success(); - } - return F("No MQTT controller enabled"); - } - return return_not_connected(); -} #endif // COMMAND_MQTT_H diff --git a/src/src/Commands/Networks.cpp b/src/src/Commands/Networks.cpp new file mode 100644 index 000000000..61385a1c8 --- /dev/null +++ b/src/src/Commands/Networks.cpp @@ -0,0 +1,41 @@ +#include "Networks.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" + +#include "../../ESPEasy_fdwdecl.h" + + +String Command_AccessInfo_Ls(struct EventStruct *event, const char* Line) +{ + String result = F("Allowed IP range : "); + result += describeAllowedIPrange(); + return return_result(event, result); +} + +String Command_AccessInfo_Clear (struct EventStruct *event, const char* Line) +{ + clearAccessBlock(); + return Command_AccessInfo_Ls(event, Line); +} + +String Command_DNS (struct EventStruct *event, const char* Line) +{ + return Command_GetORSetIP(event, F("DNS:"), Line, Settings.DNS,WiFi.dnsIP(0),1); +} + +String Command_Gateway (struct EventStruct *event, const char* Line) +{ + return Command_GetORSetIP(event, F("Gateway:"), Line, Settings.Gateway,WiFi.gatewayIP(),1); +} + +String Command_IP (struct EventStruct *event, const char* Line) +{ + return Command_GetORSetIP(event, F("IP:"), Line, Settings.IP,WiFi.localIP(),1); +} + +String Command_Subnet (struct EventStruct *event, const char* Line) +{ + return Command_GetORSetIP(event, F("Subnet:"), Line, Settings.Subnet,WiFi.subnetMask(),1); +} \ No newline at end of file diff --git a/src/src/Commands/Networks.h b/src/src/Commands/Networks.h index 238493d47..4a55c7676 100644 --- a/src/src/Commands/Networks.h +++ b/src/src/Commands/Networks.h @@ -1,40 +1,14 @@ #ifndef COMMAND_NETWORKS_H #define COMMAND_NETWORKS_H +class String; - -String Command_AccessInfo_Ls(struct EventStruct *event, const char* Line) -{ - String result = F("Allowed IP range : "); - result += describeAllowedIPrange(); - return return_result(event, result); -} - -String Command_AccessInfo_Clear (struct EventStruct *event, const char* Line) -{ - clearAccessBlock(); - return Command_AccessInfo_Ls(event, Line); -} - -String Command_DNS (struct EventStruct *event, const char* Line) -{ - return Command_GetORSetIP(event, F("DNS:"), Line, Settings.DNS,WiFi.dnsIP(0),1); -} - -String Command_Gateway (struct EventStruct *event, const char* Line) -{ - return Command_GetORSetIP(event, F("Gateway:"), Line, Settings.Gateway,WiFi.gatewayIP(),1); -} - -String Command_IP (struct EventStruct *event, const char* Line) -{ - return Command_GetORSetIP(event, F("IP:"), Line, Settings.IP,WiFi.localIP(),1); -} - -String Command_Subnet (struct EventStruct *event, const char* Line) -{ - return Command_GetORSetIP(event, F("Subnet:"), Line, Settings.Subnet,WiFi.subnetMask(),1); -} +String Command_AccessInfo_Ls(struct EventStruct *event, const char* Line); +String Command_AccessInfo_Clear (struct EventStruct *event, const char* Line); +String Command_DNS (struct EventStruct *event, const char* Line); +String Command_Gateway (struct EventStruct *event, const char* Line); +String Command_IP (struct EventStruct *event, const char* Line); +String Command_Subnet (struct EventStruct *event, const char* Line); #endif // COMMAND_NETWORKS_H diff --git a/src/src/Commands/Rules.cpp b/src/src/Commands/Rules.cpp new file mode 100644 index 000000000..fd9406b10 --- /dev/null +++ b/src/src/Commands/Rules.cpp @@ -0,0 +1,54 @@ +#include "Rules.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" +#include "../../ESPEasy-Globals.h" + + +#include "../../ESPEasy_fdwdecl.h" + + +String Command_Rules_Execute(struct EventStruct *event, const char *Line) +{ + String filename; + + if (GetArgv(Line, filename, 2)) { + String event; + rulesProcessingFile(filename, event); + } + return return_command_success(); +} + +String Command_Rules_UseRules(struct EventStruct *event, const char *Line) +{ + return Command_GetORSetBool(event, F("Rules:"), + Line, + (bool *)&Settings.UseRules, + 1); +} + +String Command_Rules_Events(struct EventStruct *event, const char *Line) +{ + String eventName = Line; + + eventName = eventName.substring(6); + eventName.replace('$', '#'); + + if (Settings.UseRules) { + rulesProcessing(eventName); + } + return return_command_success(); +} + +String Command_Rules_Let(struct EventStruct *event, const char *Line) +{ + String TmpStr1; + + if (GetArgv(Line, TmpStr1, 3)) { + float result = 0.0; + Calculate(TmpStr1.c_str(), &result); + customFloatVar[event->Par1 - 1] = result; + } + return return_command_success(); +} diff --git a/src/src/Commands/Rules.h b/src/src/Commands/Rules.h index 48cbee63c..db672f089 100644 --- a/src/src/Commands/Rules.h +++ b/src/src/Commands/Rules.h @@ -1,50 +1,11 @@ #ifndef COMMAND_RULES_H #define COMMAND_RULES_H +class String; - -String Command_Rules_Execute(struct EventStruct *event, const char *Line) -{ - String filename; - - if (GetArgv(Line, filename, 2)) { - String event; - rulesProcessingFile(filename, event); - } - return return_command_success(); -} - -String Command_Rules_UseRules(struct EventStruct *event, const char *Line) -{ - return Command_GetORSetBool(event, F("Rules:"), - Line, - (bool *)&Settings.UseRules, - 1); -} - -String Command_Rules_Events(struct EventStruct *event, const char *Line) -{ - String eventName = Line; - - eventName = eventName.substring(6); - eventName.replace('$', '#'); - - if (Settings.UseRules) { - rulesProcessing(eventName); - } - return return_command_success(); -} - -String Command_Rules_Let(struct EventStruct *event, const char *Line) -{ - String TmpStr1; - - if (GetArgv(Line, TmpStr1, 3)) { - float result = 0.0; - Calculate(TmpStr1.c_str(), &result); - customFloatVar[event->Par1 - 1] = result; - } - return return_command_success(); -} +String Command_Rules_Execute(struct EventStruct *event, const char *Line); +String Command_Rules_UseRules(struct EventStruct *event, const char *Line); +String Command_Rules_Events(struct EventStruct *event, const char *Line); +String Command_Rules_Let(struct EventStruct *event, const char *Line); #endif // COMMAND_RULES_H diff --git a/src/src/Commands/SDCARD.cpp b/src/src/Commands/SDCARD.cpp new file mode 100644 index 000000000..024323e0e --- /dev/null +++ b/src/src/Commands/SDCARD.cpp @@ -0,0 +1,34 @@ +#include "SDCARD.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" + +#include "../../ESPEasy_fdwdecl.h" + + + +#ifdef FEATURE_SD + +#include + + +String Command_SD_LS(struct EventStruct *event, const char* Line) +{ + File root = SD.open("/"); + root.rewindDirectory(); + printDirectory(root, 0); + root.close(); + return return_see_serial(event); +} + +String Command_SD_Remove(struct EventStruct *event, const char* Line) +{ + String fname = Line; + fname = fname.substring(9); + String result = F("Removing:"); + result += fname.c_str(); + SD.remove((char*)fname.c_str()); + return return_result(event, result); +} +#endif \ No newline at end of file diff --git a/src/src/Commands/SDCARD.h b/src/src/Commands/SDCARD.h index 21fd91e0a..f31bdf9db 100644 --- a/src/src/Commands/SDCARD.h +++ b/src/src/Commands/SDCARD.h @@ -1,26 +1,13 @@ #ifndef COMMAND_SDCARD_H #define COMMAND_SDCARD_H +class String; #ifdef FEATURE_SD -String Command_SD_LS(struct EventStruct *event, const char* Line) -{ - File root = SD.open("/"); - root.rewindDirectory(); - printDirectory(root, 0); - root.close(); - return return_see_serial(event); -} -String Command_SD_Remove(struct EventStruct *event, const char* Line) -{ - String fname = Line; - fname = fname.substring(9); - String result = F("Removing:"); - result += fname.c_str(); - SD.remove((char*)fname.c_str()); - return return_result(event, result); -} +String Command_SD_LS(struct EventStruct *event, const char* Line); +String Command_SD_Remove(struct EventStruct *event, const char* Line); + #endif #endif // COMMAND_SDCARD_H diff --git a/src/src/Commands/Settings.cpp b/src/src/Commands/Settings.cpp new file mode 100644 index 000000000..cdf534261 --- /dev/null +++ b/src/src/Commands/Settings.cpp @@ -0,0 +1,91 @@ +#include "Settings.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" +#include "../Globals/SecuritySettings.h" + +#include "../../ESPEasy_fdwdecl.h" + + + +String Command_Settings_Build(struct EventStruct *event, const char* Line) +{ + if (HasArgv(Line, 2)) { + Settings.Build = event->Par1; + } else { + serialPrintln(); + String result = F("Build:"); + result += Settings.Build; + return return_result(event, result); + } + return return_command_success(); +} + +String Command_Settings_Unit(struct EventStruct *event, const char* Line) +{ + if (HasArgv(Line, 2)) { + Settings.Unit = event->Par1; + }else { + serialPrintln(); + String result = F("Unit:"); + result += Settings.Unit; + return return_result(event, result); + } + return return_command_success(); +} + +String Command_Settings_Name(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetString(event, F("Name:"), + Line, + Settings.Name, + sizeof(Settings.Name), + 1); +} + +String Command_Settings_Password(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetString(event, F("Password:"), + Line, + SecuritySettings.Password, + sizeof(SecuritySettings.Password), + 1 + ); +} + +String Command_Settings_Save(struct EventStruct *event, const char* Line) +{ + SaveSettings(); + return return_command_success(); +} + +String Command_Settings_Load(struct EventStruct *event, const char* Line) +{ + LoadSettings(); + return return_command_success(); +} + +String Command_Settings_Print(struct EventStruct *event, const char* Line) +{ + serialPrintln(); + + serialPrintln(F("System Info")); + serialPrint(F(" IP Address : ")); serialPrintln(WiFi.localIP().toString()); + serialPrint(F(" Build : ")); serialPrintln(String((int)BUILD)); + serialPrint(F(" Name : ")); serialPrintln(Settings.Name); + serialPrint(F(" Unit : ")); serialPrintln(String((int)Settings.Unit)); + serialPrint(F(" WifiSSID : ")); serialPrintln(SecuritySettings.WifiSSID); + serialPrint(F(" WifiKey : ")); serialPrintln(SecuritySettings.WifiKey); + serialPrint(F(" WifiSSID2 : ")); serialPrintln(SecuritySettings.WifiSSID2); + serialPrint(F(" WifiKey2 : ")); serialPrintln(SecuritySettings.WifiKey2); + serialPrint(F(" Free mem : ")); serialPrintln(String(FreeMem())); + return return_see_serial(event); +} + +String Command_Settings_Reset(struct EventStruct *event, const char* Line) +{ + ResetFactory(); + reboot(); + return return_command_success(); +} diff --git a/src/src/Commands/Settings.h b/src/src/Commands/Settings.h index 0d7509546..1c5ab551b 100644 --- a/src/src/Commands/Settings.h +++ b/src/src/Commands/Settings.h @@ -1,88 +1,17 @@ #ifndef COMMAND_SETTINGS_H #define COMMAND_SETTINGS_H -#include "../Globals/SecuritySettings.h" -String Command_Settings_Build(struct EventStruct *event, const char* Line) -{ - if (HasArgv(Line, 2)) { - Settings.Build = event->Par1; - } else { - serialPrintln(); - String result = F("Build:"); - result += Settings.Build; - return return_result(event, result); - } - return return_command_success(); -} -String Command_Settings_Unit(struct EventStruct *event, const char* Line) -{ - if (HasArgv(Line, 2)) { - Settings.Unit = event->Par1; - }else { - serialPrintln(); - String result = F("Unit:"); - result += Settings.Unit; - return return_result(event, result); - } - return return_command_success(); -} - -String Command_Settings_Name(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetString(event, F("Name:"), - Line, - Settings.Name, - sizeof(Settings.Name), - 1); -} - -String Command_Settings_Password(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetString(event, F("Password:"), - Line, - SecuritySettings.Password, - sizeof(SecuritySettings.Password), - 1 - ); -} - -String Command_Settings_Save(struct EventStruct *event, const char* Line) -{ - SaveSettings(); - return return_command_success(); -} - -String Command_Settings_Load(struct EventStruct *event, const char* Line) -{ - LoadSettings(); - return return_command_success(); -} - -String Command_Settings_Print(struct EventStruct *event, const char* Line) -{ - serialPrintln(); - - serialPrintln(F("System Info")); - serialPrint(F(" IP Address : ")); serialPrintln(WiFi.localIP().toString()); - serialPrint(F(" Build : ")); serialPrintln(String((int)BUILD)); - serialPrint(F(" Name : ")); serialPrintln(Settings.Name); - serialPrint(F(" Unit : ")); serialPrintln(String((int)Settings.Unit)); - serialPrint(F(" WifiSSID : ")); serialPrintln(SecuritySettings.WifiSSID); - serialPrint(F(" WifiKey : ")); serialPrintln(SecuritySettings.WifiKey); - serialPrint(F(" WifiSSID2 : ")); serialPrintln(SecuritySettings.WifiSSID2); - serialPrint(F(" WifiKey2 : ")); serialPrintln(SecuritySettings.WifiKey2); - serialPrint(F(" Free mem : ")); serialPrintln(String(FreeMem())); - return return_see_serial(event); -} - -String Command_Settings_Reset(struct EventStruct *event, const char* Line) -{ - ResetFactory(); - reboot(); - return return_command_success(); -} +class String; +String Command_Settings_Build(struct EventStruct *event, const char* Line); +String Command_Settings_Unit(struct EventStruct *event, const char* Line); +String Command_Settings_Name(struct EventStruct *event, const char* Line); +String Command_Settings_Password(struct EventStruct *event, const char* Line); +String Command_Settings_Save(struct EventStruct *event, const char* Line); +String Command_Settings_Load(struct EventStruct *event, const char* Line); +String Command_Settings_Print(struct EventStruct *event, const char* Line); +String Command_Settings_Reset(struct EventStruct *event, const char* Line); #endif // COMMAND_SETTINGS_H diff --git a/src/src/Commands/Time.cpp b/src/src/Commands/Time.cpp new file mode 100644 index 000000000..b8aa4efdb --- /dev/null +++ b/src/src/Commands/Time.cpp @@ -0,0 +1,56 @@ +#include "Time.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" + +#include "../../ESPEasy_fdwdecl.h" + + +String Command_NTPHost(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetString(event, F("NTPHost:"), + Line, + Settings.NTPHost, + sizeof(Settings.NTPHost), + 1); +} + +String Command_useNTP(struct EventStruct *event, const char* Line) +{ + if (HasArgv(Line, 2)) { + Settings.UseNTP = event->Par1; + } else { + serialPrintln(); + String result = F("UseNTP:"); + result += toString(Settings.UseNTP); + return return_result(event, result); + } + return return_command_success(); +} + +String Command_TimeZone(struct EventStruct *event, const char* Line) +{ + if (HasArgv(Line, 2)) { + Settings.TimeZone = event->Par1; + } else { + serialPrintln(); + String result = F("TimeZone:"); + result += Settings.TimeZone; + return return_result(event, result); + } + return return_command_success(); +} + +String Command_DST(struct EventStruct *event, const char* Line) +{ + if (HasArgv(Line, 2)) { + Settings.DST = event->Par1; + }else { + serialPrintln(); + String result = F("DST:"); + result += toString(Settings.DST); + return return_result(event, result); + } + return return_command_success(); +} diff --git a/src/src/Commands/Time.h b/src/src/Commands/Time.h index 5563304d0..665b737fd 100644 --- a/src/src/Commands/Time.h +++ b/src/src/Commands/Time.h @@ -1,53 +1,11 @@ #ifndef COMMAND_TIME_H #define COMMAND_TIME_H +class String; -String Command_NTPHost(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetString(event, F("NTPHost:"), - Line, - Settings.NTPHost, - sizeof(Settings.NTPHost), - 1); -} - -String Command_useNTP(struct EventStruct *event, const char* Line) -{ - if (HasArgv(Line, 2)) { - Settings.UseNTP = event->Par1; - } else { - serialPrintln(); - String result = F("UseNTP:"); - result += toString(Settings.UseNTP); - return return_result(event, result); - } - return return_command_success(); -} - -String Command_TimeZone(struct EventStruct *event, const char* Line) -{ - if (HasArgv(Line, 2)) { - Settings.TimeZone = event->Par1; - } else { - serialPrintln(); - String result = F("TimeZone:"); - result += Settings.TimeZone; - return return_result(event, result); - } - return return_command_success(); -} - -String Command_DST(struct EventStruct *event, const char* Line) -{ - if (HasArgv(Line, 2)) { - Settings.DST = event->Par1; - }else { - serialPrintln(); - String result = F("DST:"); - result += toString(Settings.DST); - return return_result(event, result); - } - return return_command_success(); -} +String Command_NTPHost(struct EventStruct *event, const char* Line); +String Command_useNTP(struct EventStruct *event, const char* Line); +String Command_TimeZone(struct EventStruct *event, const char* Line); +String Command_DST(struct EventStruct *event, const char* Line); #endif // COMMAND_TIME_H diff --git a/src/src/Commands/UDP.cpp b/src/src/Commands/UDP.cpp new file mode 100644 index 000000000..c50300c9f --- /dev/null +++ b/src/src/Commands/UDP.cpp @@ -0,0 +1,66 @@ +#include "UDP.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" +#include "../../ESPEasy-Globals.h" + +#include "../../ESPEasy_fdwdecl.h" + +String Command_UDP_Test (struct EventStruct *event, const char* Line) +{ + for (byte x = 0; x < event->Par2; x++) + { + String eventName = "Test "; + eventName += x; + SendUDPCommand(event->Par1, (char*)eventName.c_str(), eventName.length()); + } + return return_command_success(); +} + +String Command_UDP_Port(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetBool(event, F("UDPPort:"), + Line, + (bool *)&Settings.UDPPort, + 1); +} + +String Command_UPD_SendTo(struct EventStruct *event, const char* Line) +{ + // FIXME TD-er: This one is not using parseString* function + String eventName = Line; + eventName = eventName.substring(7); + int index = eventName.indexOf(','); + if (index > 0) + { + eventName = eventName.substring(index + 1); + SendUDPCommand(event->Par1, (char*)eventName.c_str(), eventName.length()); + } + return return_command_success(); +} + + +String Command_UDP_SendToUPD(struct EventStruct *event, const char* Line) +{ + if (WiFiConnected()) { + String strLine = Line; + String ip = parseString(strLine, 2); + String port = parseString(strLine, 3); + if (!isInt(port)) return return_command_failed(); + String message = parseStringToEndKeepCase(strLine, 4); + IPAddress UDP_IP; + if(UDP_IP.fromString(ip)) { + portUDP.beginPacket(UDP_IP, port.toInt()); + #if defined(ESP8266) + portUDP.write(message.c_str(), message.length()); + #endif + #if defined(ESP32) + portUDP.write((uint8_t*)message.c_str(), message.length()); + #endif + portUDP.endPacket(); + } + return return_command_success(); + } + return return_not_connected(); +} diff --git a/src/src/Commands/UPD.h b/src/src/Commands/UPD.h index 1befa02e6..44d924312 100644 --- a/src/src/Commands/UPD.h +++ b/src/src/Commands/UPD.h @@ -1,63 +1,11 @@ #ifndef COMMAND_UDP_H #define COMMAND_UDP_H +class String; -String Command_UDP_Test (struct EventStruct *event, const char* Line) -{ - for (byte x = 0; x < event->Par2; x++) - { - String eventName = "Test "; - eventName += x; - SendUDPCommand(event->Par1, (char*)eventName.c_str(), eventName.length()); - } - return return_command_success(); -} - -String Command_UDP_Port(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetBool(event, F("UDPPort:"), - Line, - (bool *)&Settings.UDPPort, - 1); -} - -String Command_UPD_SendTo(struct EventStruct *event, const char* Line) -{ - // FIXME TD-er: This one is not using parseString* function - String eventName = Line; - eventName = eventName.substring(7); - int index = eventName.indexOf(','); - if (index > 0) - { - eventName = eventName.substring(index + 1); - SendUDPCommand(event->Par1, (char*)eventName.c_str(), eventName.length()); - } - return return_command_success(); -} - - -String Command_UDP_SendToUPD(struct EventStruct *event, const char* Line) -{ - if (WiFiConnected()) { - String strLine = Line; - String ip = parseString(strLine, 2); - String port = parseString(strLine, 3); - if (!isInt(port)) return return_command_failed(); - String message = parseStringToEndKeepCase(strLine, 4); - IPAddress UDP_IP; - if(UDP_IP.fromString(ip)) { - portUDP.beginPacket(UDP_IP, port.toInt()); - #if defined(ESP8266) - portUDP.write(message.c_str(), message.length()); - #endif - #if defined(ESP32) - portUDP.write((uint8_t*)message.c_str(), message.length()); - #endif - portUDP.endPacket(); - } - return return_command_success(); - } - return return_not_connected(); -} +String Command_UDP_Test (struct EventStruct *event, const char* Line); +String Command_UDP_Port(struct EventStruct *event, const char* Line); +String Command_UPD_SendTo(struct EventStruct *event, const char* Line); +String Command_UDP_SendToUPD(struct EventStruct *event, const char* Line); #endif // COMMAND_UDP_H diff --git a/src/src/Commands/WiFi.cpp b/src/src/Commands/WiFi.cpp new file mode 100644 index 000000000..20797aade --- /dev/null +++ b/src/src/Commands/WiFi.cpp @@ -0,0 +1,113 @@ +#include "WiFi.h" + +#include "../../ESPEasy_common.h" +#include "../Commands/Common.h" +#include "../Globals/Settings.h" + +#include "../../ESPEasy_fdwdecl.h" + + +String Command_Wifi_SSID(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetString(event, F("Wifi SSID:"), + Line, + SecuritySettings.WifiSSID, + sizeof(SecuritySettings.WifiSSID), + 1); +} + +String Command_Wifi_Key(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetString(event, F("Wifi Key:"), + Line, + SecuritySettings.WifiKey, + sizeof(SecuritySettings.WifiKey), + 1); +} + +String Command_Wifi_SSID2(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetString(event, F("Wifi2 SSID:"), + Line, + SecuritySettings.WifiSSID2, + sizeof(SecuritySettings.WifiSSID2), + 1); +} + +String Command_Wifi_Key2(struct EventStruct *event, const char* Line) +{ + return Command_GetORSetString(event, F("Wifi2 Key:"), + Line, + SecuritySettings.WifiKey2, + sizeof(SecuritySettings.WifiKey2), + 1); +} + +String Command_Wifi_Scan(struct EventStruct *event, const char* Line) +{ + WifiScan(); + return return_command_success(); +} + +String Command_Wifi_Connect(struct EventStruct *event, const char* Line) +{ + WiFiConnectRelaxed(); + return return_command_success(); +} + +String Command_Wifi_Disconnect(struct EventStruct *event, const char* Line) +{ + WifiDisconnect(); + return return_command_success(); +} + +String Command_Wifi_APMode(struct EventStruct *event, const char* Line) +{ + setAP(true); + return return_command_success(); +} + +String Command_Wifi_STAMode(struct EventStruct *event, const char* Line) +{ + setSTA(true); + return return_command_success(); +} + +String Command_Wifi_Mode(struct EventStruct *event, const char* Line) +{ + String TmpStr1; + if (GetArgv(Line, TmpStr1, 2)) { + WiFiMode_t mode = WIFI_MODE_MAX; + if (event->Par1 > 0) { + mode = (WiFiMode_t)(event->Par1 - 1); + } else { + TmpStr1.toLowerCase(); + if (strcmp_P(TmpStr1.c_str(), PSTR("off")) == 0) mode = WIFI_OFF; + else if (strcmp_P(TmpStr1.c_str(), PSTR("sta")) == 0) mode = WIFI_STA; + else if (strcmp_P(TmpStr1.c_str(), PSTR("ap")) == 0) mode = WIFI_AP; + else if (strcmp_P(TmpStr1.c_str(), PSTR("ap+sta")) == 0) mode = WIFI_AP_STA; + } + if (mode >= WIFI_OFF && mode < WIFI_MODE_MAX) { + setWifiMode(mode); + } else { + serialPrintln(); + return return_result(event, F("Wifi Mode: invalid arguments")); + } + } else { + serialPrintln(); + String result = F("WiFi Mode:"); + result += toString(WiFi.getMode()); + return return_result(event, result); + } + return return_command_success(); +} + +// FIXME: TD-er This is not an erase, but actually storing the current settings +// in the wifi settings of the core library +String Command_WiFi_Erase(struct EventStruct *event, const char* Line) +{ + WiFi.persistent(true); // use SDK storage of SSID/WPA parameters + WifiDisconnect(); // this will store empty ssid/wpa into sdk storage + WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters + return return_command_success(); +} diff --git a/src/src/Commands/WiFi.h b/src/src/Commands/WiFi.h index 0c401565a..28351e987 100644 --- a/src/src/Commands/WiFi.h +++ b/src/src/Commands/WiFi.h @@ -3,111 +3,23 @@ #include "../Globals/SecuritySettings.h" +class String; + #define WIFI_MODE_MAX (WiFiMode_t)4 -String Command_Wifi_SSID(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetString(event, F("Wifi SSID:"), - Line, - SecuritySettings.WifiSSID, - sizeof(SecuritySettings.WifiSSID), - 1); -} - -String Command_Wifi_Key(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetString(event, F("Wifi Key:"), - Line, - SecuritySettings.WifiKey, - sizeof(SecuritySettings.WifiKey), - 1); -} - -String Command_Wifi_SSID2(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetString(event, F("Wifi2 SSID:"), - Line, - SecuritySettings.WifiSSID2, - sizeof(SecuritySettings.WifiSSID2), - 1); -} - -String Command_Wifi_Key2(struct EventStruct *event, const char* Line) -{ - return Command_GetORSetString(event, F("Wifi2 Key:"), - Line, - SecuritySettings.WifiKey2, - sizeof(SecuritySettings.WifiKey2), - 1); -} - -String Command_Wifi_Scan(struct EventStruct *event, const char* Line) -{ - WifiScan(); - return return_command_success(); -} - -String Command_Wifi_Connect(struct EventStruct *event, const char* Line) -{ - WiFiConnectRelaxed(); - return return_command_success(); -} - -String Command_Wifi_Disconnect(struct EventStruct *event, const char* Line) -{ - WifiDisconnect(); - return return_command_success(); -} - -String Command_Wifi_APMode(struct EventStruct *event, const char* Line) -{ - setAP(true); - return return_command_success(); -} - -String Command_Wifi_STAMode(struct EventStruct *event, const char* Line) -{ - setSTA(true); - return return_command_success(); -} - -String Command_Wifi_Mode(struct EventStruct *event, const char* Line) -{ - String TmpStr1; - if (GetArgv(Line, TmpStr1, 2)) { - WiFiMode_t mode = WIFI_MODE_MAX; - if (event->Par1 > 0) { - mode = (WiFiMode_t)(event->Par1 - 1); - } else { - TmpStr1.toLowerCase(); - if (strcmp_P(TmpStr1.c_str(), PSTR("off")) == 0) mode = WIFI_OFF; - else if (strcmp_P(TmpStr1.c_str(), PSTR("sta")) == 0) mode = WIFI_STA; - else if (strcmp_P(TmpStr1.c_str(), PSTR("ap")) == 0) mode = WIFI_AP; - else if (strcmp_P(TmpStr1.c_str(), PSTR("ap+sta")) == 0) mode = WIFI_AP_STA; - } - if (mode >= WIFI_OFF && mode < WIFI_MODE_MAX) { - setWifiMode(mode); - } else { - serialPrintln(); - return return_result(event, F("Wifi Mode: invalid arguments")); - } - } else { - serialPrintln(); - String result = F("WiFi Mode:"); - result += toString(WiFi.getMode()); - return return_result(event, result); - } - return return_command_success(); -} +String Command_Wifi_SSID(struct EventStruct *event, const char* Line); +String Command_Wifi_Key(struct EventStruct *event, const char* Line); +String Command_Wifi_SSID2(struct EventStruct *event, const char* Line); +String Command_Wifi_Key2(struct EventStruct *event, const char* Line); +String Command_Wifi_Scan(struct EventStruct *event, const char* Line); +String Command_Wifi_Connect(struct EventStruct *event, const char* Line); +String Command_Wifi_Disconnect(struct EventStruct *event, const char* Line); +String Command_Wifi_APMode(struct EventStruct *event, const char* Line); +String Command_Wifi_STAMode(struct EventStruct *event, const char* Line); +String Command_Wifi_Mode(struct EventStruct *event, const char* Line); // FIXME: TD-er This is not an erase, but actually storing the current settings // in the wifi settings of the core library -String Command_WiFi_Erase(struct EventStruct *event, const char* Line) -{ - WiFi.persistent(true); // use SDK storage of SSID/WPA parameters - WifiDisconnect(); // this will store empty ssid/wpa into sdk storage - WiFi.persistent(false); // Do not use SDK storage of SSID/WPA parameters - return return_command_success(); -} +String Command_WiFi_Erase(struct EventStruct *event, const char* Line); #endif // COMMAND_WIFI_H