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.

103 lines
2.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
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. Module key prefix: alx
  5. */
  6. #if ALEXA_SUPPORT
  7. #include <fauxmoESP.h>
  8. fauxmoESP alexa;
  9. struct AlexaDevChange {
  10. AlexaDevChange(unsigned char device_id, bool state) : device_id(device_id), state(state) {};
  11. unsigned char device_id = 0;
  12. bool state = false;
  13. };
  14. #include <queue>
  15. static std::queue<AlexaDevChange> _alexa_dev_changes;
  16. // -----------------------------------------------------------------------------
  17. // ALEXA
  18. // -----------------------------------------------------------------------------
  19. #if WEB_SUPPORT
  20. void _alexaWebSocketOnSend(JsonObject& root) {
  21. root["alxVisible"] = 1;
  22. root["alxEnabled"] = getSetting("alxEnabled", ALEXA_ENABLED).toInt() == 1;
  23. }
  24. #endif
  25. void _alexaConfigure() {
  26. alexa.enable(getSetting("alxEnabled", ALEXA_ENABLED).toInt() == 1);
  27. }
  28. bool _alexaKeyCheck(const char * key) {
  29. return (strncmp(key, "alx", 3) == 0);
  30. }
  31. void _alexaBackwards() {
  32. moveSetting("fauxmoEnabled", "alxEnabled"); // 1.9.0 - 2017-08-25
  33. moveSetting("alexaEnabled", "alxEnabled"); // 1.14.0 - 2018-06-27
  34. }
  35. // -----------------------------------------------------------------------------
  36. void alexaSetup() {
  37. // Check backwards compatibility
  38. _alexaBackwards();
  39. // Load & cache settings
  40. _alexaConfigure();
  41. // Websockets
  42. #if WEB_SUPPORT
  43. wsOnSendRegister(_alexaWebSocketOnSend);
  44. wsOnAfterParseRegister(_alexaConfigure);
  45. #endif
  46. unsigned int relays = relayCount();
  47. String hostname = getHostname();
  48. if (relays == 1) {
  49. alexa.addDevice(hostname.c_str());
  50. } else {
  51. for (unsigned int i=0; i<relays; i++) {
  52. alexa.addDevice((hostname + "_" + i).c_str());
  53. }
  54. }
  55. alexa.onSetState([&](unsigned char device_id, const char * name, bool state) {
  56. AlexaDevChange change(device_id, state);
  57. _alexa_dev_changes.push(change);
  58. });
  59. alexa.onGetState([](unsigned char device_id, const char * name) {
  60. return relayStatus(device_id);
  61. });
  62. settingsRegisterKeyCheck(_alexaKeyCheck);
  63. // Register loop
  64. espurnaRegisterLoop(alexaLoop);
  65. }
  66. void alexaLoop() {
  67. alexa.handle();
  68. while (!_alexa_dev_changes.empty()) {
  69. AlexaDevChange& change = _alexa_dev_changes.front();
  70. DEBUG_MSG_P(PSTR("[ALEXA] Device #%u state: %s\n"), change.device_id, change.state ? "ON" : "OFF");
  71. relayStatus(change.device_id, change.state);
  72. _alexa_dev_changes.pop();
  73. }
  74. }
  75. #endif