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.

185 lines
5.0 KiB

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