2018-02-26 00:11:31 +00:00
|
|
|
#include <WiFiStatus.h>
|
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
WiFiStatus::WiFiStatus(AsyncWebServer* server, SecurityManager* securityManager) {
|
|
|
|
server->on(WIFI_STATUS_SERVICE_PATH,
|
|
|
|
HTTP_GET,
|
|
|
|
securityManager->wrapRequest(std::bind(&WiFiStatus::wifiStatus, this, std::placeholders::_1),
|
|
|
|
AuthenticationPredicates::IS_AUTHENTICATED));
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
2019-12-03 23:16:06 +00:00
|
|
|
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);
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
_onStationModeConnectedHandler = WiFi.onStationModeConnected(onStationModeConnected);
|
|
|
|
_onStationModeDisconnectedHandler = WiFi.onStationModeDisconnected(onStationModeDisconnected);
|
|
|
|
_onStationModeGotIPHandler = WiFi.onStationModeGotIP(onStationModeGotIP);
|
2018-11-11 17:47:44 +00:00
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
#ifdef ESP32
|
|
|
|
void WiFiStatus::onStationModeConnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.println(F("WiFi Connected."));
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void WiFiStatus::onStationModeDisconnected(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.print(F("WiFi Disconnected. Reason code="));
|
2019-12-24 11:19:19 +00:00
|
|
|
Serial.println(info.disconnected.reason);
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void WiFiStatus::onStationModeGotIP(WiFiEvent_t event, WiFiEventInfo_t info) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.printf_P(
|
|
|
|
PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), WiFi.localIP().toString().c_str(), WiFi.getHostname());
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
2019-12-24 11:19:19 +00:00
|
|
|
#elif defined(ESP8266)
|
|
|
|
void WiFiStatus::onStationModeConnected(const WiFiEventStationModeConnected& event) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.print(F("WiFi Connected. SSID="));
|
2019-12-24 11:19:19 +00:00
|
|
|
Serial.println(event.ssid);
|
2018-11-11 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void WiFiStatus::onStationModeDisconnected(const WiFiEventStationModeDisconnected& event) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.print(F("WiFi Disconnected. Reason code="));
|
2019-12-24 11:19:19 +00:00
|
|
|
Serial.println(event.reason);
|
2018-11-11 17:47:44 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 11:19:19 +00:00
|
|
|
void WiFiStatus::onStationModeGotIP(const WiFiEventStationModeGotIP& event) {
|
2020-05-21 07:42:21 +00:00
|
|
|
Serial.printf_P(
|
|
|
|
PSTR("WiFi Got IP. localIP=%s, hostName=%s\r\n"), event.ip.toString().c_str(), WiFi.hostname().c_str());
|
2018-11-11 17:47:44 +00:00
|
|
|
}
|
|
|
|
#endif
|
2018-02-26 00:11:31 +00:00
|
|
|
|
2019-12-03 23:16:06 +00:00
|
|
|
void WiFiStatus::wifiStatus(AsyncWebServerRequest* request) {
|
|
|
|
AsyncJsonResponse* response = new AsyncJsonResponse(false, MAX_WIFI_STATUS_SIZE);
|
2019-04-14 07:52:40 +00:00
|
|
|
JsonObject root = response->getRoot();
|
2018-02-26 00:11:31 +00:00
|
|
|
wl_status_t status = WiFi.status();
|
2019-12-03 23:16:06 +00:00
|
|
|
root["status"] = (uint8_t)status;
|
|
|
|
if (status == WL_CONNECTED) {
|
2018-02-26 00:11:31 +00:00
|
|
|
root["local_ip"] = WiFi.localIP().toString();
|
2019-04-29 21:15:07 +00:00
|
|
|
root["mac_address"] = WiFi.macAddress();
|
2018-02-26 00:11:31 +00:00
|
|
|
root["rssi"] = WiFi.RSSI();
|
|
|
|
root["ssid"] = WiFi.SSID();
|
|
|
|
root["bssid"] = WiFi.BSSIDstr();
|
|
|
|
root["channel"] = WiFi.channel();
|
|
|
|
root["subnet_mask"] = WiFi.subnetMask().toString();
|
|
|
|
root["gateway_ip"] = WiFi.gatewayIP().toString();
|
|
|
|
IPAddress dnsIP1 = WiFi.dnsIP(0);
|
|
|
|
IPAddress dnsIP2 = WiFi.dnsIP(1);
|
2021-03-09 09:17:03 +00:00
|
|
|
if (IPUtils::isSet(dnsIP1)) {
|
2018-02-26 00:11:31 +00:00
|
|
|
root["dns_ip_1"] = dnsIP1.toString();
|
|
|
|
}
|
2021-03-09 09:17:03 +00:00
|
|
|
if (IPUtils::isSet(dnsIP2)) {
|
2018-02-26 00:11:31 +00:00
|
|
|
root["dns_ip_2"] = dnsIP2.toString();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
response->setLength();
|
|
|
|
request->send(response);
|
|
|
|
}
|