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.

192 lines
5.1 KiB

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