Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

91 lines
2.8 KiB

6 years ago
6 years ago
api: rework plain and JSON implementations (#2405) - match paths through a custom AsyncWebHandler instead of using generic not-found fallback handler - allow MQTT-like patterns when registering paths (`simple/path`, `path/+/something`, `path/#`) Replaces `relay/0`, `relay/1` etc. with `relay/+`. Magnitudes are plain paths, but using `/+` in case there's more than 1 magnitude of the same type. - restore `std::function` as callback container (no more single-byte arg nonsense). Still, limit to 1 type per handler type - adds JSON handlers which will receive JsonObject root as both input and output. Same logic as plain - GET returns resource data, PUT updates it. - breaking change to `apiAuthenticate(request)`, it no longer will do `request->send(403)` and expect this to be handled externally. - allow `Api-Key` header containing the key, works for both GET & PUT plain requests. The only way to set apikey for JSON. - add `ApiRequest::param` to retrieve both GET and PUT params (aka args), remove ApiBuffer - remove `API_BUFFER_SIZE`. Allow custom form-data key=value pairs for requests, allow to send basic `String`. - add `API_JSON_BUFFER_SIZE` for the JSON buffer (both input and output) - `/apis` replaced with `/api/list`, no longer uses custom handler and is an `apiRegister` callback - `/api/rpc` custom handler replaced with an `apiRegister` callback WIP further down: - no more `webLog` for API requests, unless `webAccessLog` / `WEB_ACCESS_LOG` is set to `1`. This also needs to happen to the other handlers. - migrate to ArduinoJson v6, since it become apparent it is actually a good upgrade :) - actually make use of JSON endpoints more, right now it's just existing GET for sensors and relays - fork ESPAsyncWebServer to cleanup path parsing and temporary objects attached to the request (also, fix things a lot of things based on PRs there...)
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. /*
  2. SSDP MODULE
  3. Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. Uses SSDP library by PawelDino (https://github.com/PawelDino)
  5. https://github.com/esp8266/Arduino/issues/2283#issuecomment-299635604
  6. */
  7. #include "ssdp.h"
  8. #if SSDP_SUPPORT
  9. #include <ESP8266SSDP.h>
  10. #include "web.h"
  11. #include "utils.h"
  12. const char _ssdp_template[] PROGMEM =
  13. "<?xml version=\"1.0\"?>"
  14. "<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"
  15. "<specVersion>"
  16. "<major>1</major>"
  17. "<minor>0</minor>"
  18. "</specVersion>"
  19. "<URLBase>http://%s:%u/</URLBase>"
  20. "<device>"
  21. "<deviceType>%s</deviceType>"
  22. "<friendlyName>%s</friendlyName>"
  23. "<presentationURL>/</presentationURL>"
  24. "<serialNumber>%u</serialNumber>"
  25. "<modelName>%s</modelName>"
  26. "<modelNumber>%s</modelNumber>"
  27. "<modelURL>%s</modelURL>"
  28. "<manufacturer>%s</manufacturer>"
  29. "<manufacturerURL>%s</manufacturerURL>"
  30. "<UDN>uuid:38323636-4558-4dda-9188-cda0e6%06x</UDN>"
  31. "</device>"
  32. "</root>\r\n"
  33. "\r\n";
  34. void ssdpSetup() {
  35. webServer().on("/description.xml", HTTP_GET, [](AsyncWebServerRequest *request) {
  36. DEBUG_MSG_P(PSTR("[SSDP] Schema request\n"));
  37. IPAddress ip = WiFi.localIP();
  38. uint32_t chipId = ESP.getChipId();
  39. char response[strlen_P(_ssdp_template) + 100];
  40. snprintf_P(response, sizeof(response), _ssdp_template,
  41. ip.toString().c_str(), // ip
  42. webPort(), // port
  43. SSDP_DEVICE_TYPE, // device type
  44. getSetting("hostname").c_str(), // friendlyName
  45. chipId, // serialNumber
  46. APP_NAME, // modelName
  47. APP_VERSION, // modelNumber
  48. APP_WEBSITE, // modelURL
  49. getBoardName().c_str(), // manufacturer
  50. "", // manufacturerURL
  51. chipId // UUID
  52. );
  53. request->send(200, "text/xml", response);
  54. });
  55. SSDP.setSchemaURL("description.xml");
  56. SSDP.setHTTPPort(webPort());
  57. SSDP.setDeviceType(SSDP_DEVICE_TYPE); //https://github.com/esp8266/Arduino/issues/2283
  58. SSDP.setSerialNumber(String(ESP.getChipId()));
  59. SSDP.setModelName(APP_NAME);
  60. SSDP.setModelNumber(APP_VERSION);
  61. SSDP.setModelURL(APP_WEBSITE);
  62. SSDP.setManufacturer(getBoardName());
  63. SSDP.setManufacturerURL("");
  64. SSDP.setURL("/");
  65. auto hostname = getSetting("hostname", getIdentifier());
  66. SSDP.setName(hostname);
  67. SSDP.begin();
  68. DEBUG_MSG_P(PSTR("[SSDP] Started for %s\n"), hostname.c_str());
  69. }
  70. #endif // SSDP_SUPPORT