Skip to content

Commit

Permalink
Add a function to expand string with ESP variable
Browse files Browse the repository at this point in the history
Implement expandString function in Notifications Message as POC
  • Loading branch information
luc-github committed Feb 5, 2024
1 parent cc7c2cf commit 3474edc
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 20 deletions.
25 changes: 25 additions & 0 deletions esp3d/src/core/esp3d_string.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();
}
1 change: 1 addition & 0 deletions esp3d/src/core/esp3d_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
32 changes: 12 additions & 20 deletions esp3d/src/modules/notifications/notifications_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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;
Expand Down

0 comments on commit 3474edc

Please sign in to comment.