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.

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