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.

181 lines
4.9 KiB

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