mirror of
https://github.com/emsesp/EMS-ESP32.git
synced 2025-12-07 00:09:51 +03:00
move all mqtt from statusservice to mqtt.start()
This commit is contained in:
@@ -5,16 +5,51 @@
|
|||||||
|
|
||||||
namespace emsesp {
|
namespace emsesp {
|
||||||
|
|
||||||
EMSESPStatusService::EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager, AsyncMqttClient * mqttClient)
|
EMSESPStatusService::EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager) {
|
||||||
: _mqttClient(mqttClient) {
|
// rest endpoint for web page
|
||||||
_mqttClient->onConnect(std::bind(&EMSESPStatusService::init_mqtt, this)); // configure MQTT callback
|
|
||||||
|
|
||||||
server->on(EMSESP_STATUS_SERVICE_PATH,
|
server->on(EMSESP_STATUS_SERVICE_PATH,
|
||||||
HTTP_GET,
|
HTTP_GET,
|
||||||
securityManager->wrapRequest(std::bind(&EMSESPStatusService::emsespStatusService, this, std::placeholders::_1),
|
securityManager->wrapRequest(std::bind(&EMSESPStatusService::emsespStatusService, this, std::placeholders::_1),
|
||||||
AuthenticationPredicates::IS_AUTHENTICATED));
|
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) {
|
void EMSESPStatusService::emsespStatusService(AsyncWebServerRequest * request) {
|
||||||
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_STATUS_SIZE);
|
AsyncJsonResponse * response = new AsyncJsonResponse(false, MAX_EMSESP_STATUS_SIZE);
|
||||||
JsonObject root = response->getRoot();
|
JsonObject root = response->getRoot();
|
||||||
@@ -34,12 +69,4 @@ void EMSESPStatusService::emsespStatusService(AsyncWebServerRequest * request) {
|
|||||||
request->send(response);
|
request->send(response);
|
||||||
}
|
}
|
||||||
|
|
||||||
void EMSESPStatusService::init_mqtt() {
|
|
||||||
if (!_mqttClient->connected()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Mqtt::on_connect();
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace emsesp
|
} // namespace emsesp
|
||||||
@@ -14,13 +14,23 @@ namespace emsesp {
|
|||||||
|
|
||||||
class EMSESPStatusService {
|
class EMSESPStatusService {
|
||||||
public:
|
public:
|
||||||
EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager, AsyncMqttClient * mqttClient);
|
EMSESPStatusService(AsyncWebServer * server, SecurityManager * securityManager);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void emsespStatusService(AsyncWebServerRequest * request);
|
void emsespStatusService(AsyncWebServerRequest * request);
|
||||||
AsyncMqttClient * _mqttClient;
|
|
||||||
|
|
||||||
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
|
} // namespace emsesp
|
||||||
|
|||||||
@@ -35,8 +35,7 @@ ESP8266React EMSESP::esp8266React(&webServer, &LittleFS);
|
|||||||
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
EMSESPSettingsService EMSESP::emsespSettingsService = EMSESPSettingsService(&webServer, &LittleFS, EMSESP::esp8266React.getSecurityManager());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
EMSESPStatusService EMSESP::emsespStatusService =
|
EMSESPStatusService EMSESP::emsespStatusService = EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||||
EMSESPStatusService(&webServer, EMSESP::esp8266React.getSecurityManager(), EMSESP::esp8266React.getMqttClient());
|
|
||||||
|
|
||||||
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
EMSESPDevicesService EMSESP::emsespDevicesService = EMSESPDevicesService(&webServer, EMSESP::esp8266React.getSecurityManager());
|
||||||
|
|
||||||
|
|||||||
@@ -255,9 +255,11 @@ void Mqtt::start(AsyncMqttClient * mqttClient) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
#ifndef EMSESP_STANDALONE
|
#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_->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) {
|
mqttClient_->onMessage([this](char * topic, char * payload, AsyncMqttClientMessageProperties properties, size_t len, size_t index, size_t total) {
|
||||||
on_message(topic, payload, len);
|
on_message(topic, payload, len);
|
||||||
|
mqttClient_->onPublish([this](uint16_t packetId) { on_publish(packetId); });
|
||||||
});
|
});
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user