Skip to content

Commit

Permalink
WiFi management functions
Browse files Browse the repository at this point in the history
  • Loading branch information
snakeye committed Mar 5, 2019
1 parent 8423e29 commit 22403fb
Show file tree
Hide file tree
Showing 2 changed files with 147 additions and 67 deletions.
188 changes: 129 additions & 59 deletions src/ConfigManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ const char mimeHTML[] PROGMEM = "text/html";
const char mimeJSON[] PROGMEM = "application/json";
const char mimePlain[] PROGMEM = "text/plain";

void ConfigParameterGroup::toJson(JsonObject *json)
{
JsonObject &obj = json->createNestedObject(this->name);

std::list<BaseParameter *>::iterator it;
for (it = parameters.begin(); it != parameters.end(); ++it)
{
(*it)->toJson(&obj);
}
}

void ConfigParameterGroup::toJsonSchema(JsonObject *json)
{
json->set("name", name);
Expand All @@ -21,11 +32,6 @@ void ConfigParameterGroup::toJsonSchema(JsonObject *json)
}
}

Mode ConfigManager::getMode()
{
return this->mode;
}

void ConfigManager::setAPName(const char *name)
{
this->apName = (char *)name;
Expand Down Expand Up @@ -70,7 +76,7 @@ void ConfigManager::loop()
{
if (mode == ap && apTimeout > 0 && ((millis() - apStart) / 1000) > apTimeout)
{
ESP.restart();
// ESP.restart();
}

if (dnsServer)
Expand Down Expand Up @@ -108,54 +114,85 @@ JsonObject &ConfigManager::decodeJson(String jsonString)
return obj;
}

void ConfigManager::handleGetSettings()
/**
* Return index.html
*/
void ConfigManager::handleGetRoot()
{
// build json with settings values
SPIFFS.begin();

File f = SPIFFS.open(apFilename, "r");
if (!f)
{
Serial.println(F("file open failed"));
server->send(404, FPSTR(mimeHTML), F("File not found"));
return;
}

server->streamFile(f, FPSTR(mimeHTML));

f.close();
}

/**
* Return JSON with data schema
* Return current WiFi mode
*/
void ConfigManager::handleGetSettingsSchema()
void ConfigManager::handleGetWifi()
{
DynamicJsonBuffer jsonBuffer;
JsonArray &res = jsonBuffer.createArray();
JsonObject &res = jsonBuffer.createObject();

std::list<ConfigParameterGroup *>::iterator it;
for (it = groups.begin(); it != groups.end(); ++it)
{
JsonObject &obj = res.createNestedObject();
(*it)->toJsonSchema(&obj);
}
WiFiMode_t mode = WiFi.getMode();

res.set("mode", mode);
res.set("connected", WiFi.isConnected());

String body;
res.printTo(body);

server->send(200, FPSTR(mimeJSON), body);
}

void ConfigManager::handleGetScan()
/**
* Scan networks
*/
void ConfigManager::handleGetWifiScan()
{
}
DynamicJsonBuffer jsonBuffer;
JsonArray &res = jsonBuffer.createArray();

void ConfigManager::handleAPGet()
{
SPIFFS.begin();
static long lastScan = 0;
const long scanPeriod = 5000;
long now = millis();

File f = SPIFFS.open(apFilename, "r");
if (!f)
if (now > lastScan + scanPeriod)
{
Serial.println(F("file open failed"));
server->send(404, FPSTR(mimeHTML), F("File not found"));
return;
WiFi.scanDelete();
WiFi.scanNetworks(true);
lastScan = now;
}

server->streamFile(f, FPSTR(mimeHTML));
int n = WiFi.scanComplete();
if (n >= 0)
{
for (int i = 0; i < n; i++)
{
JsonObject &obj = res.createNestedObject();

f.close();
obj.set("ssid", WiFi.SSID(i));
obj.set("channel", WiFi.channel(i));
obj.set("rssi", WiFi.RSSI(i));
obj.set("open", WiFi.encryptionType(i) == ENC_TYPE_NONE);
}
}

String body;
res.printTo(body);

server->send(200, FPSTR(mimeJSON), body);
}

void ConfigManager::handleAPPost()
void ConfigManager::handlePostConnect()
{
bool isJson = server->header("Content-Type") == FPSTR(mimeJSON);
String ssid;
Expand Down Expand Up @@ -191,50 +228,80 @@ void ConfigManager::handleAPPost()
EEPROM.commit();

server->send(204, FPSTR(mimePlain), F("Saved. Will attempt to reboot."));
}

void ConfigManager::handlePostDisconnect()
{
WiFi.mode(WIFI_STA);
WiFi.disconnect();

ESP.restart();
}

void ConfigManager::handleRESTGet()
/**
* Return JSON with data schema
*/
void ConfigManager::handleGetSettingsSchema()
{
// DynamicJsonBuffer jsonBuffer;
// JsonObject& obj = jsonBuffer.createObject();
DynamicJsonBuffer jsonBuffer;
JsonArray &res = jsonBuffer.createArray();

// std::list<BaseParameter*>::iterator it;
// for (it = parameters.begin(); it != parameters.end(); ++it) {
// if ((*it)->getMode() == set) {
// continue;
// }
std::list<ConfigParameterGroup *>::iterator it;
for (it = groups.begin(); it != groups.end(); ++it)
{
JsonObject &obj = res.createNestedObject();
(*it)->toJsonSchema(&obj);
}

// (*it)->toJson(&obj);
// }
String body;
res.printTo(body);

// String body;
// obj.printTo(body);
server->send(200, FPSTR(mimeJSON), body);
}

// server->send(200, FPSTR(mimeJSON), body);
/**
* Return JSON with settings values
*/
void ConfigManager::handleGetSettings()
{
DynamicJsonBuffer jsonBuffer;
JsonObject &res = jsonBuffer.createObject();

std::list<ConfigParameterGroup *>::iterator it;
for (it = groups.begin(); it != groups.end(); ++it)
{
(*it)->toJson(&res);
}

String body;
res.printTo(body);

server->send(200, FPSTR(mimeJSON), body);
}

void ConfigManager::handleRESTPut()
void ConfigManager::handlePostSettings()
{
// JsonObject& obj = this->decodeJson(server->arg("plain"));
// if (!obj.success()) {
// server->send(400, FPSTR(mimeJSON), "");
// return;
// }
JsonObject &obj = this->decodeJson(server->arg("plain"));
if (!obj.success())
{
server->send(400, FPSTR(mimeJSON), "");
return;
}

// std::list<BaseParameter*>::iterator it;
// for (it = parameters.begin(); it != parameters.end(); ++it) {
// if ((*it)->getMode() == get) {
// std::list<BaseParameter *>::iterator it;
// for (it = parameters.begin(); it != parameters.end(); ++it)
// {
// if ((*it)->getMode() == get)
// {
// continue;
// }

// (*it)->fromJson(&obj);
// }

// writeConfig();
writeConfig();

// server->send(204, FPSTR(mimeJSON), "");
server->send(204, FPSTR(mimeJSON), "");
}

void ConfigManager::handleNotFound()
Expand Down Expand Up @@ -327,15 +394,18 @@ void ConfigManager::startWebServer()

server->collectHeaders(headerKeys, headerKeysSize);

server->on("/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleAPGet, this));
server->on("/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleGetRoot, this));

// wifi parameters
server->on("/wifi/scan/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleGetScan, this));
server->on("/wifi", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleGetWifi, this));
server->on("/wifi/scan", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleGetWifiScan, this));
server->on("/wifi/connect", HTTPMethod::HTTP_POST, std::bind(&ConfigManager::handlePostConnect, this));
server->on("/wifi/disconnect", HTTPMethod::HTTP_POST, std::bind(&ConfigManager::handlePostDisconnect, this));

// configuration settings
server->on("/settings/", HTTPMethod::HTTP_OPTIONS, std::bind(&ConfigManager::handleGetSettingsSchema, this));
server->on("/settings/", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleGetSettings, this));
server->on("/settings/", HTTPMethod::HTTP_POST, std::bind(&ConfigManager::handleAPPost, this));
server->on("/settings", HTTPMethod::HTTP_OPTIONS, std::bind(&ConfigManager::handleGetSettingsSchema, this));
server->on("/settings", HTTPMethod::HTTP_GET, std::bind(&ConfigManager::handleGetSettings, this));
server->on("/settings", HTTPMethod::HTTP_POST, std::bind(&ConfigManager::handlePostSettings, this));

// not found handling
server->onNotFound(std::bind(&ConfigManager::handleNotFound, this));
Expand Down
26 changes: 18 additions & 8 deletions src/ConfigManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,14 @@ class ConfigParameterGroup
return *this;
}

void toJson(JsonObject *json);
void toJsonSchema(JsonObject *json);

const char *getName()
{
return name;
}

private:
const char *name;
const char *label;
Expand All @@ -217,8 +223,6 @@ class ConfigManager
public:
ConfigManager() {}

Mode getMode();

void setAPName(const char *name);
void setAPPassword(const char *password);
void setAPFilename(const char *filename);
Expand Down Expand Up @@ -276,28 +280,34 @@ class ConfigManager
std::function<void(WebServer *)> apiCallback;

private:
JsonObject &decodeJson(String jsonString);
void handleGetRoot();

void handleGetWifi();
void handleGetWifiScan();
void handlePostConnect();
void handlePostDisconnect();

void handleGetSettings();
void handleGetSettingsSchema();
void handleGetScan();
void handlePostSettings();

void handleAPGet();
void handleAPPost();
void handleRESTGet();
void handleRESTPut();
void handleNotFound();

private:
JsonObject &decodeJson(String jsonString);

void readConfig();
void writeConfig();
bool wifiConnected();
void setup();
void startAP();
void startApi();

void startWebServer();

void readConfig();
void writeConfig();

boolean isIp(String str);
String toStringIP(IPAddress ip);
};
Expand Down

0 comments on commit 22403fb

Please sign in to comment.