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.

189 lines
5.0 KiB

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