swimtracker-firmware/firmware/src/WifiAPI.cpp

47 lines
1.3 KiB
C++
Raw Normal View History

2021-06-06 10:49:09 +02:00
#include "WifiAPI.h"
#include "WifiManager.h"
#include "WebsocketServer.h"
bool WifiAPI::handleMessage(websockets::WebsocketsClient &client, MessageCode code, const char *payload, size_t size)
{
if (code == MessageCode::WIFI_STATE_GET)
{
StaticJsonDocument<128> data;
data["state"] = wifiManager_.stateStr();
sendToClient<64>(client, MessageCode::WIFI_STATE_GET, data);
return true;
}
else if (code == MessageCode::WIFI_STATE_SET)
{
StaticJsonDocument<1024> json;
DeserializationError err = deserializeJson(json, payload, size);
if (err)
{
sendErrorToClient(client, "Deserialization Error");
return true;
}
if (json.containsKey("reset_to_provisioning") && json["reset_to_provisioning"].as<bool>())
{
wifiManager_.resetToApProvisioning();
restartScheduled_ = true;
return true;
}
else if (json.containsKey("ap_password"))
{
wifiManager_.setApCredentials(json["ap_password"].as<const char *>());
restartScheduled_ = true;
return true;
}
else if (json.containsKey("sta_ssid") && json.containsKey("sta_password"))
{
wifiManager_.setStaCredentials(json["sta_ssid"].as<const char *>(), //
json["sta_password"].as<const char *>());
restartScheduled_ = true;
return true;
}
}
return false;
}