mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-08 00:39:50 +03:00
move all mqtt from statusservice to mqtt.start()
This commit is contained in:
@@ -5,16 +5,51 @@
|
||||
|
||||
namespace emsesp {
|
||||
|
||||
EMSESPStatusService::EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager, AsyncMqttClient * mqttClient)
|
||||
: _mqttClient(mqttClient) {
|
||||
_mqttClient->onConnect(std::bind(&EMSESPStatusService::init_mqtt, this)); // configure MQTT callback
|
||||
|
||||
EMSESPStatusService::EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager) {
|
||||
// rest endpoint for web page
|
||||
server->on(EMSESP_STATUS_SERVICE_PATH,
|
||||
HTTP_GET,
|
||||
securityManager->wrapRequest(std::bind(&EMSESPStatusService::emsespStatusService, this, std::placeholders::_1),
|
||||
AuthenticationPredicates::IS_AUTHENTICATED));
|
||||
|
||||
// trigger on wifi connects
|
||||
#ifdef ESP32
|
||||
WiFi.onEvent(onStationModeConnected, WiFiEvent_t::SYSTEM_EVENT_STA_CONNECTED);
|
||||
WiFi.onEvent(onStationModeDisconnected, WiFiEvent_t::SYSTEM_EVENT_STA_DISCONNECTED);
|
||||
WiFi.onEvent(onStationModeGotIP, WiFiEvent_t::SYSTEM_EVENT_STA_GOT_IP);
|
||||
#elif defined(ESP8266)
|
||||
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
|
||||
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
|
||||
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef ESP32
|
||||
void EMSESPStatusService::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
EMSESP::logger().debug(F("Wifi Connected"));
|
||||
}
|
||||
|
||||
void EMSESPStatusService::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
EMSESP::logger().debug(F("WiFi Disconnected. Reason code=%d"), info.disconnected.reason);
|
||||
}
|
||||
|
||||
void EMSESPStatusService::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
||||
EMSESP::logger().debug(F("WiFi Got IP. IP=%s, hostname=%s"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
|
||||
}
|
||||
#elif defined(ESP8266)
|
||||
void EMSESPStatusService::onStationModeConnected(const WiFiEventStationModeConnected & event) {
|
||||
EMSESP::logger().debug(F("Wifi connected with SSID %s"), event.ssid);
|
||||
}
|
||||
|
||||
void EMSESPStatusService::onStationModeDisconnected(const WiFiEventStationModeDisconnected & event) {
|
||||
EMSESP::logger().debug(F("WiFi Disconnected. Reason code=%d"), event.reason);
|
||||
}
|
||||
|
||||
void EMSESPStatusService::onStationModeGotIP(const WiFiEventStationModeGotIP & event) {
|
||||
EMSESP::logger().debug(F("WiFi Got IP. IP=%s, hostname=%s"), event.ip.toString().c_str(), WiFi.hostname().c_str());
|
||||
}
|
||||
#endif
|
||||
|
||||
void EMSESPStatusService::emsespStatusService(AsyncWebServerRequest * request) {
|
||||
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_STATUS_SIZE);
|
||||
JsonObject root = response->getRoot();
|
||||
@@ -34,12 +69,4 @@ void EMSESPStatusService::emsespStatusService(AsyncWebServerRequest * request) {
|
||||
request->send(response);
|
||||
}
|
||||
|
||||
void EMSESPStatusService::init_mqtt() {
|
||||
if (!_mqttClient->connected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
Mqtt::on_connect();
|
||||
}
|
||||
|
||||
} // namespace emsesp
|
||||
@@ -14,13 +14,23 @@ namespace emsesp {
|
||||
|
||||
class EMSESPStatusService {
|
||||
public:
|
||||
EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager, AsyncMqttClient * mqttClient);
|
||||
EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||
|
||||
private:
|
||||
void emsespStatusService(AsyncWebServerRequest * request);
|
||||
AsyncMqttClient * _mqttClient;
|
||||
void emsespStatusService(AsyncWebServerRequest * request);
|
||||
|
||||
void init_mqtt();
|
||||
#ifdef ESP32
|
||||
static void onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
static void onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
static void onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info);
|
||||
#elif defined(ESP8266)
|
||||
WiFiEventHandler _onStationModeConnectedHandler;
|
||||
WiFiEventHandler _onStationModeDisconnectedHandler;
|
||||
WiFiEventHandler _onStationModeGotIPHandler;
|
||||
static void onStationModeConnected(const WiFiEventStationModeConnected & event);
|
||||
static void onStationModeDisconnected(const WiFiEventStationModeDisconnected & event);
|
||||
static void onStationModeGotIP(const WiFiEventStationModeGotIP & event);
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace emsesp
|
||||
|
||||
@@ -35,8 +35,7 @@ ESP8266React EMSESP::esp8266React(&webServer, &LittleFS);
|
||||
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||
#endif
|
||||
|
||||
EMSESPStatusService EMSESP::emsespStatusService =
|
||||
EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager(), EMSESP::esp8266React.getMqttClient());
|
||||
EMSESPStatusService EMSESP::emsespStatusService = EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||
|
||||
|
||||
@@ -255,9 +255,11 @@ void Mqtt::start(AsyncMqttClient * mqttClient) {
|
||||
});
|
||||
|
||||
#ifndef EMSESP_STANDALONE
|
||||
mqttClient_->onConnect([this](bool sessionPresent) { on_connect(); });
|
||||
mqttClient_->setWill(make_topic(will_topic_, "status"), 1, true, "offline"); // with qos 1, retain true
|
||||
mqttClient_->onMessage([this](char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||
on_message(topic, payload, len);
|
||||
mqttClient_->onPublish([this](uint16_t packetId) { on_publish(packetId); });
|
||||
});
|
||||
#endif
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user