Browse Source

Changed apiRegister method

fastled
Xose Pérez 7 years ago
parent
commit
8b01681de2
5 changed files with 22 additions and 26 deletions
  1. +8
    -6
      code/espurna/api.ino
  2. +1
    -1
      code/espurna/config/prototypes.h
  3. +9
    -12
      code/espurna/light.ino
  4. +3
    -6
      code/espurna/relay.ino
  5. +1
    -1
      code/espurna/sensor.ino

+ 8
- 6
code/espurna/api.ino View File

@ -14,7 +14,6 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
#include <vector>
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;


+ 1
- 1
code/espurna/config/prototypes.h View File

@ -18,7 +18,7 @@ AsyncWebServer * webServer();
// -----------------------------------------------------------------------------
typedef std::function<void(char *, size_t)> api_get_callback_f;
typedef std::function<void(const char *)> 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


+ 9
- 12
code/espurna/light.ino View File

@ -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<lightChannels(); id++) {
char url[15];
snprintf_P(url, sizeof(url), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
char key[15];
snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_CHANNEL, id);
char key[10];
snprintf_P(key, sizeof(key), PSTR("%s%d"), MQTT_TOPIC_CHANNEL, id);
apiRegister(url, key,
apiRegister(key,
[id](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("%d"), lightChannel(id));
},


+ 3
- 6
code/espurna/relay.ino View File

@ -476,13 +476,10 @@ void relaySetupAPI() {
// API entry points (protected with apikey)
for (unsigned int relayID=0; relayID<relayCount(); relayID++) {
char url[15];
snprintf_P(url, sizeof(url), PSTR("%s/%d"), MQTT_TOPIC_RELAY, relayID);
char key[15];
snprintf_P(key, sizeof(key), PSTR("%s/%d"), MQTT_TOPIC_RELAY, relayID);
char key[10];
snprintf_P(key, sizeof(key), PSTR("%s%d"), MQTT_TOPIC_RELAY, relayID);
apiRegister(url, key,
apiRegister(key,
[relayID](char * buffer, size_t len) {
snprintf_P(buffer, len, PSTR("%d"), relayStatus(relayID) ? 1 : 0);
},


+ 1
- 1
code/espurna/sensor.ino View File

@ -161,7 +161,7 @@ void _sensorAPISetup() {
String topic = _magnitudeTopic(magnitude.type);
if (SENSOR_USE_INDEX || (_counts[magnitude.type] > 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;


Loading…
Cancel
Save