Add a function to expand string with ESP variable

Implement expandString function in Notifications Message as POC
This commit is contained in:
Luc 2024-02-05 21:01:22 +08:00
parent cc7c2cfc73
commit 3474edcd03
3 changed files with 38 additions and 20 deletions

View File

@ -23,6 +23,10 @@
#include "../include/esp3d_config.h"
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
#include "../modules/network/netconfig.h"
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(TIMESTAMP_FEATURE)
#include "../modules/time/time_service.h"
#endif // TIMESTAMP_FEATURE
@ -186,4 +190,25 @@ if (c==9 || (c >= 32 && c <= 126) || c>=128) {
return true;
}
return false;
}
const char * esp3d_string::expandString(const char *s){
static String tmp;
tmp = s;
if (tmp.indexOf("%") != -1) {
#if defined(WIFI_FEATURE) || defined(ETH_FEATURE)
tmp.replace("%ESP_IP%", NetConfig::localIP().c_str());
tmp.replace("%ESP_NAME%", NetConfig::hostname());
#else
tmp.replace("%ESP_IP%", "???");
tmp.replace("%ESP_NAME%", "???");
#endif // WIFI_FEATURE || ETH_FEATURE
#if defined(TIMESTAMP_FEATURE)
tmp.replace("%ESP_DATETIME%", timeService.getCurrentTime());
#else
tmp.replace("%ESP_DATETIME%", "???");
#endif // TIMESTAMP_FEATURE
}
return tmp.c_str();
}

View File

@ -28,6 +28,7 @@ const char* getContentType(const char* filename);
const char* encodeString(const char* s);
const char* formatBytes(uint64_t bytes);
bool isPrintableChar(char c);
const char * expandString(const char *s);
} // namespace esp3d_string
#endif //_ESP3D_STRING_H

View File

@ -39,6 +39,7 @@
#include "../../core/esp3d_message.h"
#include "../../core/esp3d_settings.h"
#include "../../core/esp3d_string.h"
#include "../network/netconfig.h"
#include "notifications_service.h"
@ -134,18 +135,7 @@ bool NotificationsService::Wait4Answer(T& client,
}
bool NotificationsService::sendAutoNotification(const char* msg) {
if (!(NetConfig::started()) || (NetConfig::getMode() != ESP_WIFI_STA) ||
(!_started) || (!_autonotification)) {
esp3d_log("Auto notification rejected");
return false;
}
String msgtpl = msg;
// check if has variable to change
if (msgtpl.indexOf("%") != -1) {
msgtpl.replace("%ESP_IP%", WiFi.localIP().toString().c_str());
msgtpl.replace("%ESP_NAME%", NetConfig::hostname());
}
if (!sendMSG(ESP_NOTIFICATION_TITLE, msgtpl.c_str())) {
if (!sendMSG(ESP_NOTIFICATION_TITLE, msg)) {
esp3d_log_e("Auto notification failed");
return false;
} else {
@ -185,12 +175,14 @@ const char* NotificationsService::getTypeString() {
return "none";
}
bool NotificationsService::sendMSG(const char* title, const char* message) {
bool NotificationsService::sendMSG(const char* title, const char* messagetxt) {
if (!_started) {
esp3d_log_e("Error notification not started");
return false;
}
if (!((strlen(title) == 0) && (strlen(message) == 0))) {
if (!((strlen(title) == 0) && (strlen(messagetxt) == 0))) {
String message = esp3d_string::expandString(messagetxt);
if (_notificationType != ESP_HOMEASSISTANT_NOTIFICATION) {
// push to webui by default
#if defined(HTTP_FEATURE) || defined(WS_DATA_FEATURE)
@ -204,22 +196,22 @@ bool NotificationsService::sendMSG(const char* title, const char* message) {
}
switch (_notificationType) {
case ESP_PUSHOVER_NOTIFICATION:
return sendPushoverMSG(title, message);
return sendPushoverMSG(title, message.c_str());
break;
case ESP_EMAIL_NOTIFICATION:
return sendEmailMSG(title, message);
return sendEmailMSG(title, message.c_str());
break;
case ESP_LINE_NOTIFICATION:
return sendLineMSG(title, message);
return sendLineMSG(title, message.c_str());
break;
case ESP_TELEGRAM_NOTIFICATION:
return sendTelegramMSG(title, message);
return sendTelegramMSG(title, message.c_str());
break;
case ESP_IFTTT_NOTIFICATION:
return sendIFTTTMSG(title, message);
return sendIFTTTMSG(title, message.c_str());
break;
case ESP_HOMEASSISTANT_NOTIFICATION:
return sendHomeAssistantMSG(title, message);
return sendHomeAssistantMSG(title, message.c_str());
break;
default:
break;