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.

193 lines
5.1 KiB

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
  1. /*
  2. ALEXA MODULE
  3. Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>
  4. */
  5. #include "alexa.h"
  6. #if ALEXA_SUPPORT
  7. #include <queue>
  8. #include "api.h"
  9. #include "broker.h"
  10. #include "light.h"
  11. #include "relay.h"
  12. #include "rpc.h"
  13. #include "web.h"
  14. #include "ws.h"
  15. #include <fauxmoESP.h>
  16. #include <ArduinoJson.h>
  17. struct alexa_queue_element_t {
  18. unsigned char device_id;
  19. bool state;
  20. unsigned char value;
  21. };
  22. static std::queue<alexa_queue_element_t> _alexa_queue;
  23. fauxmoESP _alexa;
  24. // -----------------------------------------------------------------------------
  25. // ALEXA
  26. // -----------------------------------------------------------------------------
  27. bool _alexaWebSocketOnKeyCheck(const char * key, JsonVariant& value) {
  28. return (strncmp(key, "alexa", 5) == 0);
  29. }
  30. void _alexaWebSocketOnConnected(JsonObject& root) {
  31. root["alexaEnabled"] = alexaEnabled();
  32. root["alexaName"] = getSetting("alexaName");
  33. }
  34. void _alexaConfigure() {
  35. _alexa.enable(wifiConnected() && alexaEnabled());
  36. }
  37. #if WEB_SUPPORT
  38. bool _alexaBodyCallback(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
  39. return _alexa.process(request->client(), request->method() == HTTP_GET, request->url(), String((char *)data));
  40. }
  41. bool _alexaRequestCallback(AsyncWebServerRequest *request) {
  42. String body = (request->hasParam("body", true)) ? request->getParam("body", true)->value() : String();
  43. return _alexa.process(request->client(), request->method() == HTTP_GET, request->url(), body);
  44. }
  45. #endif
  46. void _alexaBrokerCallback(const String& topic, unsigned char id, unsigned int value) {
  47. // Only process status messages for switches and channels
  48. if (!topic.equals(MQTT_TOPIC_CHANNEL)
  49. && !topic.equals(MQTT_TOPIC_RELAY)) {
  50. return;
  51. }
  52. if (topic.equals(MQTT_TOPIC_CHANNEL)) {
  53. _alexa.setState(id + 1, value > 0, value);
  54. }
  55. if (topic.equals(MQTT_TOPIC_RELAY)) {
  56. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  57. if (id > 0) return;
  58. #endif
  59. _alexa.setState(id, value, value > 0 ? 255 : 0);
  60. }
  61. }
  62. // -----------------------------------------------------------------------------
  63. bool alexaEnabled() {
  64. return getSetting("alexaEnabled", 1 == ALEXA_ENABLED);
  65. }
  66. void alexaLoop() {
  67. _alexa.handle();
  68. while (!_alexa_queue.empty()) {
  69. alexa_queue_element_t element = _alexa_queue.front();
  70. DEBUG_MSG_P(PSTR("[ALEXA] Device #%u state: %s value: %d\n"), element.device_id, element.state ? "ON" : "OFF", element.value);
  71. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  72. if (0 == element.device_id) {
  73. relayStatus(0, element.state);
  74. } else {
  75. lightState(element.device_id - 1, element.state);
  76. lightChannel(element.device_id - 1, element.value);
  77. lightUpdate(true, true);
  78. }
  79. #else
  80. relayStatus(element.device_id, element.state);
  81. #endif
  82. _alexa_queue.pop();
  83. }
  84. }
  85. void alexaSetup() {
  86. // Backwards compatibility
  87. moveSetting("fauxmoEnabled", "alexaEnabled");
  88. // Basic fauxmoESP configuration
  89. _alexa.createServer(!WEB_SUPPORT);
  90. _alexa.setPort(80);
  91. // Use custom alexa hostname if defined, device hostname otherwise
  92. String hostname = getSetting("alexaName", ALEXA_HOSTNAME);
  93. if (hostname.length() == 0) {
  94. hostname = getSetting("hostname");
  95. }
  96. // Lights
  97. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  98. // Global switch
  99. _alexa.addDevice(hostname.c_str());
  100. // For each channel
  101. for (unsigned char i = 1; i <= lightChannels(); i++) {
  102. _alexa.addDevice((hostname + " " + i).c_str());
  103. }
  104. // Relays
  105. #else
  106. unsigned int relays = relayCount();
  107. if (relays == 1) {
  108. _alexa.addDevice(hostname.c_str());
  109. } else {
  110. for (unsigned int i=1; i<=relays; i++) {
  111. _alexa.addDevice((hostname + " " + i).c_str());
  112. }
  113. }
  114. #endif
  115. // Load & cache settings
  116. _alexaConfigure();
  117. // Websockets
  118. #if WEB_SUPPORT
  119. webBodyRegister(_alexaBodyCallback);
  120. webRequestRegister(_alexaRequestCallback);
  121. wsRegister()
  122. .onVisible([](JsonObject& root) { root["alexaVisible"] = 1; })
  123. .onConnected(_alexaWebSocketOnConnected)
  124. .onKeyCheck(_alexaWebSocketOnKeyCheck);
  125. #endif
  126. // Register wifi callback
  127. wifiRegister([](justwifi_messages_t code, char * parameter) {
  128. if ((MESSAGE_CONNECTED == code) || (MESSAGE_DISCONNECTED == code)) {
  129. _alexaConfigure();
  130. }
  131. });
  132. // Callback
  133. _alexa.onSetState([&](unsigned char device_id, const char * name, bool state, unsigned char value) {
  134. alexa_queue_element_t element;
  135. element.device_id = device_id;
  136. element.state = state;
  137. element.value = value;
  138. _alexa_queue.push(element);
  139. });
  140. // Register main callbacks
  141. StatusBroker::Register(_alexaBrokerCallback);
  142. espurnaRegisterReload(_alexaConfigure);
  143. espurnaRegisterLoop(alexaLoop);
  144. }
  145. #endif