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.

173 lines
4.6 KiB

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