Browse Source

API is not restful (issue a PUT to change a relay status). It can be disabled from web UI (#1192)

ota
Xose Pérez 6 years ago
parent
commit
5488bfaf7f
15 changed files with 18046 additions and 17807 deletions
  1. +13
    -3
      code/espurna/api.ino
  2. +5
    -0
      code/espurna/config/general.h
  3. BIN
      code/espurna/data/index.all.html.gz
  4. BIN
      code/espurna/data/index.light.html.gz
  5. BIN
      code/espurna/data/index.rfbridge.html.gz
  6. BIN
      code/espurna/data/index.rfm69.html.gz
  7. BIN
      code/espurna/data/index.sensor.html.gz
  8. BIN
      code/espurna/data/index.small.html.gz
  9. +3139
    -3105
      code/espurna/static/index.all.html.gz.h
  10. +2999
    -2965
      code/espurna/static/index.light.html.gz.h
  11. +2597
    -2565
      code/espurna/static/index.rfbridge.html.gz.h
  12. +4080
    -4034
      code/espurna/static/index.rfm69.html.gz.h
  13. +2649
    -2615
      code/espurna/static/index.sensor.html.gz.h
  14. +2553
    -2520
      code/espurna/static/index.small.html.gz.h
  15. +11
    -0
      code/html/index.html

+ 13
- 3
code/espurna/api.ino View File

@ -19,6 +19,7 @@ typedef struct {
api_put_callback_f putFn = NULL;
} web_api_t;
std::vector<web_api_t> _apis;
bool _api_restful = API_RESTFUL;
// -----------------------------------------------------------------------------
@ -31,8 +32,14 @@ void _apiWebSocketOnSend(JsonObject& root) {
root["apiEnabled"] = getSetting("apiEnabled", API_ENABLED).toInt() == 1;
root["apiKey"] = getSetting("apiKey");
root["apiRealTime"] = getSetting("apiRealTime", API_REAL_TIME_VALUES).toInt() == 1;
root["apiRestFul"] = _api_restful;
}
void _apiConfigure() {
_api_restful = getSetting("apiRestFul", API_RESTFUL).toInt() == 1;
}
// -----------------------------------------------------------------------------
// API
// -----------------------------------------------------------------------------
@ -158,9 +165,11 @@ bool _apiRequestCallback(AsyncWebServerRequest *request) {
// Check if its a PUT
if (api.putFn != NULL) {
if (request->hasParam("value", request->method() == HTTP_PUT)) {
AsyncWebParameter* p = request->getParam("value", request->method() == HTTP_PUT);
(api.putFn)((p->value()).c_str());
if (!_api_restful || (request->method() == HTTP_PUT)) {
if (request->hasParam("value", request->method() == HTTP_PUT)) {
AsyncWebParameter* p = request->getParam("value", request->method() == HTTP_PUT);
(api.putFn)((p->value()).c_str());
}
}
}
@ -215,6 +224,7 @@ void apiSetup() {
wsOnSendRegister(_apiWebSocketOnSend);
wsOnReceiveRegister(_apiWebSocketOnReceive);
webRequestRegister(_apiRequestCallback);
espurnaRegisterReload(_apiConfigure);
}
#endif // API_SUPPORT

+ 5
- 0
code/espurna/config/general.h View File

@ -458,6 +458,11 @@
#define API_ENABLED 0 // Do not enable API by default
#endif
#ifndef API_RESTFUL
#define API_RESTFUL 1 // A restful API requires changes to be issued as PUT requests
// Setting this to 0 will allow using GET to change relays, for instance
#endif
#ifndef API_BUFFER_SIZE
#define API_BUFFER_SIZE 15 // Size of the buffer for HTTP GET API responses
#endif


BIN
code/espurna/data/index.all.html.gz View File


BIN
code/espurna/data/index.light.html.gz View File


BIN
code/espurna/data/index.rfbridge.html.gz View File


BIN
code/espurna/data/index.rfm69.html.gz View File


BIN
code/espurna/data/index.sensor.html.gz View File


BIN
code/espurna/data/index.small.html.gz View File


+ 3139
- 3105
code/espurna/static/index.all.html.gz.h
File diff suppressed because it is too large
View File


+ 2999
- 2965
code/espurna/static/index.light.html.gz.h
File diff suppressed because it is too large
View File


+ 2597
- 2565
code/espurna/static/index.rfbridge.html.gz.h
File diff suppressed because it is too large
View File


+ 4080
- 4034
code/espurna/static/index.rfm69.html.gz.h
File diff suppressed because it is too large
View File


+ 2649
- 2615
code/espurna/static/index.sensor.html.gz.h
File diff suppressed because it is too large
View File


+ 2553
- 2520
code/espurna/static/index.small.html.gz.h
File diff suppressed because it is too large
View File


+ 11
- 0
code/html/index.html View File

@ -566,6 +566,17 @@
<div class="pure-u-1 pure-u-lg-1-4"><input type="checkbox" name="apiEnabled" /></div>
</div>
<div class="pure-g module module-api">
<label class="pure-u-1 pure-u-lg-1-4">Restful API</label>
<div class="pure-u-1 pure-u-lg-1-4"><input type="checkbox" name="apiRestFul" /></div>
<div class="pure-u-0 pure-u-lg-1-2"></div>
<div class="pure-u-0 pure-u-lg-1-4"></div>
<div class="pure-u-1 pure-u-lg-3-4 hint">
If enabled, API requests to change a status (like a relay) must be done using PUT.
If disabled you can issue them as GET requests (easier from a browser).
</div>
</div>
<div class="pure-g module module-api">
<label class="pure-u-1 pure-u-lg-1-4">Real time API</label>
<div class="pure-u-1 pure-u-lg-1-4"><input type="checkbox" name="apiRealTime" /></div>


Loading…
Cancel
Save