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.

101 lines
2.5 KiB

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