Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Registered functions return String instead of int #279

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions aREST.h
Original file line number Diff line number Diff line change
Expand Up @@ -1487,12 +1487,12 @@ bool send_command(bool headers, bool decodeArgs) {
if (decodeArgs)
urldecode(arguments); // Modifies arguments

int result = functions[value](arguments);
String result = functions[value](arguments);

// Send feedback to client
if (!LIGHTWEIGHT) {
addToBufferF(F("{\"return_value\": "));
addToBuffer(result, true);
addStringResultToBuffer(result.c_str(), true);
addToBufferF(F(", "));
// addToBufferF(F(", \"message\": \""));
// addStringToBuffer(functions_names[value]);
Expand Down Expand Up @@ -1575,7 +1575,7 @@ virtual void root_answer() {
}


void function(char * function_name, int (*f)(String)){
void function(char * function_name, String (*f)(String)){

functions_names[functions_index] = function_name;
functions[functions_index] = f;
Expand Down Expand Up @@ -1751,6 +1751,11 @@ void addToBuffer(T(*toAdd)(), bool quotable=true) {
addToBuffer(toAdd(), quotable);
}

// Register a function that allows the functions registered in the code to return String instead of int. It is more useful as a String can contain JSON or XML
template <typename T>
void addStringResultToBuffer(T toAdd, bool quotable=false) {
addStringToBuffer(String(toAdd).c_str(), false); // Except for our overrides, this will be adding numbers, which don't get quoted
}

// // Add to output buffer
// void addToBuffer(const __FlashStringHelper *toAdd, bool quotable){
Expand Down Expand Up @@ -2012,7 +2017,7 @@ void setMQTTServer(char* new_mqtt_server){

// Functions array
uint8_t functions_index;
int (*functions[NUMBER_FUNCTIONS])(String);
String (*functions[NUMBER_FUNCTIONS])(String);
char * functions_names[NUMBER_FUNCTIONS];

// Memory debug
Expand Down