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.

86 lines
2.1 KiB

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