2021-06-06 10:49:09 +02:00
|
|
|
#include "WifiAPI.h"
|
|
|
|
#include "WifiManager.h"
|
|
|
|
|
|
|
|
#include "WebsocketServer.h"
|
|
|
|
|
2021-06-06 15:56:30 +02:00
|
|
|
void WifiAPI::sendWifiState(websockets::WebsocketsClient &client)
|
|
|
|
{
|
|
|
|
StaticJsonDocument<128> data;
|
|
|
|
data["state"] = wifiManager_.stateStr();
|
|
|
|
sendToClient<64>(client, MessageCode::WIFI_STATE_RESPONSE, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
void WifiAPI::onClientConnect(websockets::WebsocketsClient &client)
|
|
|
|
{
|
|
|
|
sendWifiState(client);
|
|
|
|
}
|
|
|
|
|
2021-06-06 10:49:09 +02:00
|
|
|
bool WifiAPI::handleMessage(websockets::WebsocketsClient &client, MessageCode code, const char *payload, size_t size)
|
|
|
|
{
|
|
|
|
if (code == MessageCode::WIFI_STATE_GET)
|
|
|
|
{
|
2021-06-06 15:56:30 +02:00
|
|
|
sendWifiState(client);
|
2021-06-06 10:49:09 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (code == MessageCode::WIFI_STATE_SET)
|
|
|
|
{
|
|
|
|
StaticJsonDocument<1024> json;
|
|
|
|
|
2021-07-22 18:38:28 +02:00
|
|
|
DeserializationError err = deserializeMsgPack(json, payload, size);
|
2021-06-06 10:49:09 +02:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
sendErrorToClient(client, "Deserialization Error");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (json.containsKey("reset_to_provisioning") && json["reset_to_provisioning"].as<bool>())
|
|
|
|
{
|
2021-07-22 18:38:28 +02:00
|
|
|
Serial.println("wifi: reset_to_provisioning");
|
2021-06-06 10:49:09 +02:00
|
|
|
wifiManager_.resetToApProvisioning();
|
|
|
|
restartScheduled_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (json.containsKey("ap_password"))
|
|
|
|
{
|
2021-07-22 18:38:28 +02:00
|
|
|
Serial.println("wifi: ap_password");
|
2021-06-06 10:49:09 +02:00
|
|
|
wifiManager_.setApCredentials(json["ap_password"].as<const char *>());
|
|
|
|
restartScheduled_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else if (json.containsKey("sta_ssid") && json.containsKey("sta_password"))
|
|
|
|
{
|
2021-07-22 18:38:28 +02:00
|
|
|
Serial.println("wifi: sta_ssid");
|
2021-06-06 10:49:09 +02:00
|
|
|
wifiManager_.setStaCredentials(json["sta_ssid"].as<const char *>(), //
|
|
|
|
json["sta_password"].as<const char *>());
|
|
|
|
restartScheduled_ = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2021-06-06 15:56:30 +02:00
|
|
|
else if (code == MessageCode::WIFI_TRIGGER_SCAN)
|
|
|
|
{
|
|
|
|
WiFi.scanNetworks(true);
|
|
|
|
return true;
|
|
|
|
}
|
2021-06-06 10:49:09 +02:00
|
|
|
return false;
|
|
|
|
}
|