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.

178 lines
4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
  1. /*
  2. ALEXA MODULE
  3. Copyright (C) 2016-2018 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. }
  25. void _alexaConfigure() {
  26. alexa.enable(wifiConnected() && alexaEnabled());
  27. }
  28. #if WEB_SUPPORT
  29. bool _alexaBodyCallback(AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total) {
  30. return alexa.process(request->client(), request->method() == HTTP_GET, request->url(), String((char *)data));
  31. }
  32. bool _alexaRequestCallback(AsyncWebServerRequest *request) {
  33. String body = (request->hasParam("body", true)) ? request->getParam("body", true)->value() : String();
  34. return alexa.process(request->client(), request->method() == HTTP_GET, request->url(), body);
  35. }
  36. #endif
  37. #if BROKER_SUPPORT
  38. void _alexaBrokerCallback(const unsigned char type, const char * topic, unsigned char id, const char * payload) {
  39. // Only process status messages
  40. if (BROKER_MSG_TYPE_STATUS != type) return;
  41. unsigned char value = atoi(payload);
  42. if (strcmp(MQTT_TOPIC_CHANNEL, topic) == 0) {
  43. alexa.setState(id+1, value > 0, value);
  44. }
  45. if (strcmp(MQTT_TOPIC_RELAY, topic) == 0) {
  46. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  47. if (id > 0) return;
  48. #endif
  49. alexa.setState(id, value, value > 0 ? 255 : 0);
  50. }
  51. }
  52. #endif // BROKER_SUPPORT
  53. // -----------------------------------------------------------------------------
  54. bool alexaEnabled() {
  55. return (getSetting("alexaEnabled", ALEXA_ENABLED).toInt() == 1);
  56. }
  57. void alexaSetup() {
  58. // Backwards compatibility
  59. moveSetting("fauxmoEnabled", "alexaEnabled");
  60. // Basic fauxmoESP configuration
  61. alexa.createServer(!WEB_SUPPORT);
  62. alexa.setPort(80);
  63. // Uses hostname as base name for all devices
  64. // TODO: use custom switch name when available
  65. String hostname = getSetting("hostname");
  66. // Lights
  67. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  68. // Global switch
  69. alexa.addDevice(hostname.c_str());
  70. // For each channel
  71. for (unsigned char i = 1; i <= lightChannels(); i++) {
  72. alexa.addDevice((hostname + " " + i).c_str());
  73. }
  74. // Relays
  75. #else
  76. unsigned int relays = relayCount();
  77. if (relays == 1) {
  78. alexa.addDevice(hostname.c_str());
  79. } else {
  80. for (unsigned int i=1; i<=relays; i++) {
  81. alexa.addDevice((hostname + " " + i).c_str());
  82. }
  83. }
  84. #endif
  85. // Load & cache settings
  86. _alexaConfigure();
  87. // Websockets
  88. #if WEB_SUPPORT
  89. webBodyRegister(_alexaBodyCallback);
  90. webRequestRegister(_alexaRequestCallback);
  91. wsOnSendRegister(_alexaWebSocketOnSend);
  92. wsOnReceiveRegister(_alexaWebSocketOnReceive);
  93. #endif
  94. // Register wifi callback
  95. wifiRegister([](justwifi_messages_t code, char * parameter) {
  96. if ((MESSAGE_CONNECTED == code) || (MESSAGE_DISCONNECTED == code)) {
  97. _alexaConfigure();
  98. }
  99. });
  100. // Callback
  101. alexa.onSetState([&](unsigned char device_id, const char * name, bool state, unsigned char value) {
  102. alexa_queue_element_t element;
  103. element.device_id = device_id;
  104. element.state = state;
  105. element.value = value;
  106. _alexa_queue.push(element);
  107. });
  108. // Register main callbacks
  109. #if BROKER_SUPPORT
  110. brokerRegister(_alexaBrokerCallback);
  111. #endif
  112. espurnaRegisterReload(_alexaConfigure);
  113. espurnaRegisterLoop(alexaLoop);
  114. }
  115. void alexaLoop() {
  116. alexa.handle();
  117. while (!_alexa_queue.empty()) {
  118. alexa_queue_element_t element = _alexa_queue.front();
  119. DEBUG_MSG_P(PSTR("[ALEXA] Device #%u state: %s value: %d\n"), element.device_id, element.state ? "ON" : "OFF", element.value);
  120. #if RELAY_PROVIDER == RELAY_PROVIDER_LIGHT
  121. if (0 == element.device_id) {
  122. relayStatus(0, element.state);
  123. } else {
  124. lightState(element.device_id - 1, element.state);
  125. lightChannel(element.device_id - 1, element.value);
  126. lightUpdate(true, true);
  127. }
  128. #else
  129. relayStatus(element.device_id, element.state);
  130. #endif
  131. _alexa_queue.pop();
  132. }
  133. }
  134. #endif