diff --git a/code/espurna/api.ino b/code/espurna/api.ino index 57f4094d..8a9a750b 100644 --- a/code/espurna/api.ino +++ b/code/espurna/api.ino @@ -14,7 +14,6 @@ Copyright (C) 2016-2017 by Xose PĂ©rez #include typedef struct { - char * url; char * key; api_get_callback_f getFn = NULL; api_put_callback_f putFn = NULL; @@ -116,19 +115,23 @@ void _onAPIs(AsyncWebServerRequest *request) { bool asJson = _asJson(request); + char buffer[40]; + String output; if (asJson) { DynamicJsonBuffer jsonBuffer; JsonObject& root = jsonBuffer.createObject(); for (unsigned int i=0; i < _apis.size(); i++) { - root[_apis[i].key] = _apis[i].url; + snprintf_P(buffer, sizeof(buffer), PSTR("/api/%s"), _apis[i].key); + root[_apis[i].key] = String(buffer); } root.printTo(output); request->send(200, "application/json", output); } else { for (unsigned int i=0; i < _apis.size(); i++) { - output += _apis[i].key + String(" -> ") + _apis[i].url + String("\n"); + snprintf_P(buffer, sizeof(buffer), PSTR("/api/%s"), _apis[i].key); + output += _apis[i].key + String(" -> ") + String(buffer) + String("\n"); } request->send(200, "text/plain", output); } @@ -162,13 +165,12 @@ void _onRPC(AsyncWebServerRequest *request) { // ----------------------------------------------------------------------------- -void apiRegister(const char * url, const char * key, api_get_callback_f getFn, api_put_callback_f putFn) { +void apiRegister(const char * key, api_get_callback_f getFn, api_put_callback_f putFn) { // Store it web_api_t api; char buffer[40]; - snprintf_P(buffer, sizeof(buffer), PSTR("/api/%s"), url); - api.url = strdup(buffer); + snprintf_P(buffer, sizeof(buffer), PSTR("/api/%s"), key); api.key = strdup(key); api.getFn = getFn; api.putFn = putFn; diff --git a/code/espurna/config/prototypes.h b/code/espurna/config/prototypes.h index 0e081647..894eb850 100644 --- a/code/espurna/config/prototypes.h +++ b/code/espurna/config/prototypes.h @@ -18,7 +18,7 @@ AsyncWebServer * webServer(); // ----------------------------------------------------------------------------- typedef std::function api_get_callback_f; typedef std::function api_put_callback_f; -void apiRegister(const char * url, const char * key, api_get_callback_f getFn, api_put_callback_f putFn = NULL); +void apiRegister(const char * key, api_get_callback_f getFn, api_put_callback_f putFn = NULL); // ----------------------------------------------------------------------------- // WebSockets diff --git a/code/espurna/light.ino b/code/espurna/light.ino index 9b9f8a97..3200fcd2 100644 --- a/code/espurna/light.ino +++ b/code/espurna/light.ino @@ -724,7 +724,7 @@ void _lightAPISetup() { if (_light_has_color) { // DEPRECATE - apiRegister(MQTT_TOPIC_COLOR, MQTT_TOPIC_COLOR, + apiRegister(MQTT_TOPIC_COLOR, [](char * buffer, size_t len) { if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) { _toRGB(buffer, len, false); @@ -738,7 +738,7 @@ void _lightAPISetup() { } ); - apiRegister(MQTT_TOPIC_COLOR_RGB, MQTT_TOPIC_COLOR_RGB, + apiRegister(MQTT_TOPIC_COLOR_RGB, [](char * buffer, size_t len) { if (getSetting("useCSS", LIGHT_USE_CSS).toInt() == 1) { _toRGB(buffer, len, false); @@ -752,7 +752,7 @@ void _lightAPISetup() { } ); - apiRegister(MQTT_TOPIC_COLOR_HSV, MQTT_TOPIC_COLOR_HSV, + apiRegister(MQTT_TOPIC_COLOR_HSV, [](char * buffer, size_t len) { _toHSV(buffer, len); }, @@ -762,7 +762,7 @@ void _lightAPISetup() { } ); - apiRegister(MQTT_TOPIC_BRIGHTNESS, MQTT_TOPIC_BRIGHTNESS, + apiRegister(MQTT_TOPIC_BRIGHTNESS, [](char * buffer, size_t len) { snprintf_P(buffer, len, PSTR("%d"), _light_brightness); }, @@ -772,7 +772,7 @@ void _lightAPISetup() { } ); - apiRegister(MQTT_TOPIC_KELVIN, MQTT_TOPIC_KELVIN, + apiRegister(MQTT_TOPIC_KELVIN, [](char * buffer, size_t len) {}, [](const char * payload) { _fromKelvin(atol(payload)); @@ -780,7 +780,7 @@ void _lightAPISetup() { } ); - apiRegister(MQTT_TOPIC_MIRED, MQTT_TOPIC_MIRED, + apiRegister(MQTT_TOPIC_MIRED, [](char * buffer, size_t len) {}, [](const char * payload) { _fromMireds(atol(payload)); @@ -792,13 +792,10 @@ void _lightAPISetup() { for (unsigned int id=0; id 1)) topic = topic + "/" + String(magnitude.global); - apiRegister(topic.c_str(), topic.c_str(), [magnitude_id](char * buffer, size_t len) { + apiRegister(topic.c_str(), [magnitude_id](char * buffer, size_t len) { sensor_magnitude_t magnitude = _magnitudes[magnitude_id]; unsigned char decimals = _magnitudeDecimals(magnitude.type); double value = _sensor_realtime ? magnitude.current : magnitude.filtered;