Fork of the excellent esp8266-react - https://github.com/rjwats/esp8266-react
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.

152 lines
4.2 KiB

  1. #ifndef MqttSettingsService_h
  2. #define MqttSettingsService_h
  3. #include <StatefulService.h>
  4. #include <HttpEndpoint.h>
  5. #include <FSPersistence.h>
  6. #include <AsyncMqttClient.h>
  7. #include <ESPUtils.h>
  8. #define MQTT_RECONNECTION_DELAY 5000
  9. #define MQTT_SETTINGS_FILE "/config/mqttSettings.json"
  10. #define MQTT_SETTINGS_SERVICE_PATH "/rest/mqttSettings"
  11. #ifndef FACTORY_MQTT_ENABLED
  12. #define FACTORY_MQTT_ENABLED false
  13. #endif
  14. #ifndef FACTORY_MQTT_HOST
  15. #define FACTORY_MQTT_HOST "test.mosquitto.org"
  16. #endif
  17. #ifndef FACTORY_MQTT_PORT
  18. #define FACTORY_MQTT_PORT 1883
  19. #endif
  20. #ifndef FACTORY_MQTT_USERNAME
  21. #define FACTORY_MQTT_USERNAME ""
  22. #endif
  23. #ifndef FACTORY_MQTT_PASSWORD
  24. #define FACTORY_MQTT_PASSWORD ""
  25. #endif
  26. #ifndef FACTORY_MQTT_CLIENT_ID
  27. #define FACTORY_MQTT_CLIENT_ID generateClientId()
  28. #endif
  29. #ifndef FACTORY_MQTT_KEEP_ALIVE
  30. #define FACTORY_MQTT_KEEP_ALIVE 16
  31. #endif
  32. #ifndef FACTORY_MQTT_CLEAN_SESSION
  33. #define FACTORY_MQTT_CLEAN_SESSION true
  34. #endif
  35. #ifndef FACTORY_MQTT_MAX_TOPIC_LENGTH
  36. #define FACTORY_MQTT_MAX_TOPIC_LENGTH 128
  37. #endif
  38. static String generateClientId() {
  39. #ifdef ESP32
  40. return ESPUtils::defaultDeviceValue("esp32-");
  41. #elif defined(ESP8266)
  42. return ESPUtils::defaultDeviceValue("esp8266-");
  43. #endif
  44. }
  45. class MqttSettings {
  46. public:
  47. // host and port - if enabled
  48. bool enabled;
  49. String host;
  50. uint16_t port;
  51. // username and password
  52. String username;
  53. String password;
  54. // client id settings
  55. String clientId;
  56. // connection settings
  57. uint16_t keepAlive;
  58. bool cleanSession;
  59. uint16_t maxTopicLength;
  60. static void serialize(MqttSettings& settings, JsonObject& root) {
  61. root["enabled"] = settings.enabled;
  62. root["host"] = settings.host;
  63. root["port"] = settings.port;
  64. root["username"] = settings.username;
  65. root["password"] = settings.password;
  66. root["client_id"] = settings.clientId;
  67. root["keep_alive"] = settings.keepAlive;
  68. root["clean_session"] = settings.cleanSession;
  69. root["max_topic_length"] = settings.maxTopicLength;
  70. }
  71. static void deserialize(JsonObject& root, MqttSettings& settings) {
  72. settings.enabled = root["enabled"] | FACTORY_MQTT_ENABLED;
  73. settings.host = root["host"] | FACTORY_MQTT_HOST;
  74. settings.port = root["port"] | FACTORY_MQTT_PORT;
  75. settings.username = root["username"] | FACTORY_MQTT_USERNAME;
  76. settings.password = root["password"] | FACTORY_MQTT_PASSWORD;
  77. settings.clientId = root["client_id"] | FACTORY_MQTT_CLIENT_ID;
  78. settings.keepAlive = root["keep_alive"] | FACTORY_MQTT_KEEP_ALIVE;
  79. settings.cleanSession = root["clean_session"] | FACTORY_MQTT_CLEAN_SESSION;
  80. settings.maxTopicLength = root["max_topic_length"] | FACTORY_MQTT_MAX_TOPIC_LENGTH;
  81. }
  82. };
  83. class MqttSettingsService : public StatefulService<MqttSettings> {
  84. public:
  85. MqttSettingsService(AsyncWebServer* server, FS* fs, SecurityManager* securityManager);
  86. ~MqttSettingsService();
  87. void begin();
  88. void loop();
  89. bool isEnabled();
  90. bool isConnected();
  91. const char* getClientId();
  92. AsyncMqttClientDisconnectReason getDisconnectReason();
  93. AsyncMqttClient* getMqttClient();
  94. protected:
  95. void onConfigUpdated();
  96. private:
  97. HttpEndpoint<MqttSettings> _httpEndpoint;
  98. FSPersistence<MqttSettings> _fsPersistence;
  99. // Pointers to hold retained copies of the mqtt client connection strings.
  100. // Required as AsyncMqttClient holds refrences to the supplied connection strings.
  101. char* _retainedHost = nullptr;
  102. char* _retainedClientId = nullptr;
  103. char* _retainedUsername = nullptr;
  104. char* _retainedPassword = nullptr;
  105. AsyncMqttClient _mqttClient;
  106. bool _reconfigureMqtt;
  107. unsigned long _disconnectedAt;
  108. // connection status
  109. AsyncMqttClientDisconnectReason _disconnectReason;
  110. #ifdef ESP32
  111. void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
  112. void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
  113. #elif defined(ESP8266)
  114. WiFiEventHandler _onStationModeDisconnectedHandler;
  115. WiFiEventHandler _onStationModeGotIPHandler;
  116. void onStationModeGotIP(const WiFiEventStationModeGotIP& event);
  117. void onStationModeDisconnected(const WiFiEventStationModeDisconnected& event);
  118. #endif
  119. void onMqttConnect(bool sessionPresent);
  120. void onMqttDisconnect(AsyncMqttClientDisconnectReason reason);
  121. void configureMqtt();
  122. };
  123. #endif // end MqttSettingsService_h