From 52af6e8fdfa3118ff61ad1a905ff55a75eec3fb2 Mon Sep 17 00:00:00 2001 From: marcoschwartz Date: Thu, 5 Jul 2018 15:41:18 +0200 Subject: [PATCH] Revert "Feature/handlers" --- aREST.h | 194 +++++++++++++++++++++++++------------------------------- 1 file changed, 86 insertions(+), 108 deletions(-) diff --git a/aREST.h b/aREST.h index e115893..ebfdd36 100644 --- a/aREST.h +++ b/aREST.h @@ -134,8 +134,6 @@ #define LIGHTWEIGHT 0 #endif - -// -------- for backwards compatibility -------- #ifdef AREST_NUMBER_VARIABLES #define NUMBER_VARIABLES AREST_NUMBER_VARIABLES #endif @@ -144,36 +142,21 @@ #define NUMBER_FUNCTIONS AREST_NUMBER_FUNCTIONS #endif -#if defined(NUMBER_VARIABLES) && defined(NUMBER_FUNCTIONS) - #define AREST_NUMBER_HANDLERS (NUMBER_VARIABLES + NUMBER_FUNCTIONS) -#elif defined(NUMBER_VARIABLES) - #if defined(__AVR_ATmega1280__) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(CORE_WILDFIRE) || defined(ESP8266) - #define NUMBER_FUNCTIONS 10 - #else - #define NUMBER_FUNCTIONS 5 - #endif - #define AREST_NUMBER_HANDLERS (NUMBER_VARIABLES + NUMBER_FUNCTIONS) -#elif defined(NUMBER_FUNCTIONS) +// Default number of max. exposed variables +#ifndef NUMBER_VARIABLES #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(CORE_WILDFIRE) || defined(ESP8266)|| defined(ESP32) || !defined(ADAFRUIT_CC3000_H) #define NUMBER_VARIABLES 10 #else #define NUMBER_VARIABLES 5 #endif - #define AREST_NUMBER_HANDLERS (NUMBER_VARIABLES + NUMBER_FUNCTIONS) -#endif -// -------- end -------- - - -#ifdef AREST_NUMBER_HANDLERS -#define NUMBER_HANDLERS AREST_NUMBER_HANDLERS #endif -// Default number of max. exposed handlers -#ifndef NUMBER_HANDLERS - #if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || defined(CORE_WILDFIRE) || defined(ESP8266)|| defined(ESP32) || !defined(ADAFRUIT_CC3000_H) - #define NUMBER_HANDLERS 20 +// Default number of max. exposed functions +#ifndef NUMBER_FUNCTIONS + #if defined(__AVR_ATmega1280__) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(CORE_WILDFIRE) || defined(ESP8266) + #define NUMBER_FUNCTIONS 10 #else - #define NUMBER_HANDLERS 10 + #define NUMBER_FUNCTIONS 5 #endif #endif @@ -186,30 +169,8 @@ class aREST { private: -struct Handler { - bool include_into_root_answer; - - Handler() : include_into_root_answer{false} { } - Handler(bool include) : include_into_root_answer{include} { } - - virtual void addToBuffer(aREST *arest, const String& name, const String& arguments) const = 0; -}; - - -struct Variable: Handler { - Variable() : Handler(true) { } - +struct Variable { virtual void addToBuffer(aREST *arest) const = 0; - - void addToBuffer(aREST *arest, const String& name, const String& arguments) const override { - if (LIGHTWEIGHT) { - addToBuffer(arest); - } else { - arest->addStringToBuffer(name.c_str(), true); - arest->addToBufferF(F(": ")); - addToBuffer(arest); - } - } }; @@ -225,25 +186,6 @@ struct TypedVariable: Variable { } }; - -struct FunctionHandler: Handler { - int (*func)(String); - - FunctionHandler(int (*f)(String)) : func{f} { } - - void addToBuffer(aREST *arest, const String& name, const String& arguments) const override { - int result = func(arguments); - - if (!LIGHTWEIGHT) { - arest->addToBufferF(F("\"return_value\": ")); - arest->addToBuffer(result, true); - // arest->addToBufferF(F(", \"message\": \"")); - // arest->addStringToBuffer(name.c_str()); - // arest->addToBufferF(F(" executed\", ")); - } - } -}; - public: public: @@ -264,9 +206,9 @@ aREST(char* rest_remote_server, int rest_port) { template void variable(const char *name, T *var, bool quotable) { - handlers[handlers_index] = new TypedVariable(var, quotable); - handler_names[handlers_index] = name; - handlers_index++; + variables[variables_index] = new TypedVariable(var, quotable); + variable_names[variables_index] = name; + variables_index++; } template @@ -275,13 +217,6 @@ void variable(const char *name, T *var) { } -void function(const char *name, int (*f)(String)) { - handlers[handlers_index] = new FunctionHandler(f); - handler_names[handlers_index] = name; - handlers_index++; -} - - private: void initialize() { @@ -387,6 +322,7 @@ void setKey(char* proKey, PubSubClient& client) { // Build client ID client_id = id + String(proKey); + } #endif @@ -1141,39 +1077,55 @@ void process(char c) { #endif } - // Handler request received ? + // Variable or function request received ? if (command == 'u') { - // Check if handler name is registered in array - for (uint8_t i = 0; i < handlers_index; i++) { - if (answer.startsWith(handler_names[i])) { + // Check if variable name is in int array + for (uint8_t i = 0; i < variables_index; i++) { + if (answer.startsWith(variable_names[i])) { // End here pin_selected = true; state = 'x'; // Set state - command = 'h'; + command = 'v'; + value = i; + + break; // We found what we're looking for + } + } + + // Check if function name is in array + for (uint8_t i = 0; i < functions_index; i++) { + if (answer.startsWith(functions_names[i])) { + + // End here + pin_selected = true; + state = 'x'; + + // Set state + command = 'f'; value = i; answer.trim(); - // We're expecting a string of the form ?xxxxx=, where xxxxx can be almost anything as long as it's followed by an '=' + // We're expecting a string of the form ?xxxxx=, where xxxxx can be almost anything as long as it's followed by an '=' // Get command -- Anything following the first '=' in answer will be put in the arguments string. arguments = ""; - uint16_t header_length = strlen(handler_names[i]); + uint16_t header_length = strlen(functions_names[i]); if (answer.substring(header_length, header_length + 1) == "?") { uint16_t footer_start = answer.length(); if (answer.endsWith(" HTTP/")) footer_start -= 6; // length of " HTTP/" - // Standard operation --> strip off anything preceeding the first "=", pass the rest to the handler + // Standard operation --> strip off anything preceeding the first "=", pass the rest to the function if(AREST_PARAMS_MODE == 0) { uint16_t eq_position = answer.indexOf('=', header_length); // Replacing 'magic number' 8 for fixed location of '=' if (eq_position != -1) arguments = answer.substring(eq_position + 1, footer_start); } - // All params mode --> pass all parameters, if any, to the handler. Handler will be resonsible for parsing + // All params mode --> pass all parameters, if any, to the function. Function will be resonsible for parsing else if(AREST_PARAMS_MODE == 1) { arguments = answer.substring(header_length + 1, footer_start); } @@ -1442,21 +1394,38 @@ bool send_command(bool headers, bool decodeArgs) { } } - // Handler selected - if (command == 'h') { - if (decodeArgs) { - urldecode(arguments); // Modifies arguments - } + // Variable selected + if (command == 'v') { // Send feedback to client if (LIGHTWEIGHT) { - addHandlerToBuffer(value, arguments); + variables[value]->addToBuffer(this); } else { addToBufferF(F("{")); - addHandlerToBuffer(value, arguments); + addVariableToBuffer(value); addToBufferF(F(", ")); } } + // Function selected + if (command == 'f') { + + // Execute function + if (decodeArgs) + urldecode(arguments); // Modifies arguments + + int result = functions[value](arguments); + + // Send feedback to client + if (!LIGHTWEIGHT) { + addToBufferF(F("{\"return_value\": ")); + addToBuffer(result, true); + addToBufferF(F(", ")); + // addToBufferF(F(", \"message\": \"")); + // addStringToBuffer(functions_names[value]); + // addToBufferF(F(" executed\", ")); + } + } + if (command == 'r' || command == 'u') { root_answer(); } @@ -1512,16 +1481,11 @@ virtual void root_answer() { else { addToBufferF(F("{\"variables\": {")); - bool isFirst = true; - for (uint8_t i = 0; i < handlers_index; i++){ - if (handlers[i]->include_into_root_answer) { - // variable should be included into root answer - if (isFirst) { - isFirst = false; - } else { - addToBufferF(F(", ")); - } - addHandlerToBuffer(i, String("")); + for (uint8_t i = 0; i < variables_index; i++){ + addVariableToBuffer(i); + + if (i < variables_index - 1) { + addToBufferF(F(", ")); } } @@ -1537,6 +1501,13 @@ virtual void root_answer() { } +void function(char * function_name, int (*f)(String)){ + + functions_names[functions_index] = function_name; + functions[functions_index] = f; + functions_index++; +} + // Set device ID void set_id(const String& device_id) { @@ -1868,8 +1839,10 @@ uint8_t esp_12_pin_map(uint8_t pin) { } -void addHandlerToBuffer(uint8_t index, const String& arguments) { - handlers[index]->addToBuffer(this, String(handler_names[index]), arguments); +void addVariableToBuffer(uint8_t index) { + addStringToBuffer(variable_names[index], true); + addToBufferF(F(": ")); + variables[index]->addToBuffer(this); } @@ -1930,10 +1903,10 @@ void setMQTTServer(char* new_mqtt_server){ // Status LED uint8_t status_led_pin; - // Handlers arrays - uint8_t handlers_index; - Handler* handlers[NUMBER_HANDLERS]; - const char * handler_names[NUMBER_HANDLERS]; + // Int variables arrays + uint8_t variables_index; + Variable* variables[NUMBER_VARIABLES]; + const char * variable_names[NUMBER_VARIABLES]; // MQTT client #if defined(PubSubClient_h) @@ -1955,6 +1928,11 @@ void setMQTTServer(char* new_mqtt_server){ #endif + // Functions array + uint8_t functions_index; + int (*functions[NUMBER_FUNCTIONS])(String); + char * functions_names[NUMBER_FUNCTIONS]; + // Memory debug #if defined(ESP8266) || defined(ESP32) int freeMemory;