swimtracker-firmware/firmware/src/WifiAPI.h

87 lines
2.0 KiB
C
Raw Normal View History

2021-06-06 10:49:09 +02:00
#include "Dtypes.h"
#include "MessageCodes.h"
#include "SwimTrackerConfig.h"
#include <ArduinoWebsockets.h>
#include <ArduinoJson.h>
class WifiManager;
class WifiAPI
{
public:
WifiAPI(WifiManager &wifiManager)
: wifiManager_(wifiManager), restartScheduled_(false)
{
}
2021-06-06 15:56:30 +02:00
void onClientConnect(websockets::WebsocketsClient &client);
2021-06-06 10:49:09 +02:00
bool handleMessage(websockets::WebsocketsClient &client, MessageCode code, const char *payload, size_t size);
template <typename TServer>
2021-06-06 15:56:30 +02:00
void iteration(TServer &server);
2021-06-06 10:49:09 +02:00
private:
2021-06-06 15:56:30 +02:00
void sendWifiState(websockets::WebsocketsClient &client);
template <typename TServer>
void reportScanResultIfAvailable(TServer &server);
2021-06-06 10:49:09 +02:00
WifiManager &wifiManager_;
bool restartScheduled_;
};
2021-06-06 15:56:30 +02:00
template <typename TServer>
void WifiAPI::iteration(TServer &server)
{
if (restartScheduled_)
{
Serial.print("Restart triggered by WifiAPI");
ESP.restart();
}
reportScanResultIfAvailable(server);
}
template <typename TServer>
void WifiAPI::reportScanResultIfAvailable(TServer &server)
{
auto numNetworks = WiFi.scanComplete();
if (numNetworks >= 0)
{
DynamicJsonDocument response(192 * numNetworks);
for (uint16_t i = 0; i < numNetworks; ++i)
{
JsonObject wifiObj = response.createNestedObject();
wifiObj["ssid"] = WiFi.SSID(i);
wifiObj["rssi"] = WiFi.RSSI(i);
wifiObj["channel"] = WiFi.channel(i);
switch (WiFi.encryptionType(i))
{
case WIFI_AUTH_OPEN:
wifiObj["sec"] = "open";
break;
case WIFI_AUTH_WEP:
wifiObj["sec"] = "WEP";
break;
case WIFI_AUTH_WPA_PSK:
wifiObj["sec"] = "WPA_PSK";
break;
case WIFI_AUTH_WPA2_PSK:
wifiObj["sec"] = "WPA2_PSK";
break;
case WIFI_AUTH_WPA_WPA2_PSK:
wifiObj["sec"] = "WPA_WPA2_PSK";
break;
case WIFI_AUTH_WPA2_ENTERPRISE:
wifiObj["sec"] = "WPA2_ENTP";
break;
default:
wifiObj["sec"] = "?";
}
}
server.sendToAll(MessageCode::WIFI_SCAN_RESPONSE, response);
WiFi.scanDelete();
}
}