149 lines
3.6 KiB
C++
149 lines
3.6 KiB
C++
|
/*
|
||
|
* Stairs
|
||
|
* Copyright 2017 (c) Mark van Renswoude
|
||
|
*
|
||
|
* https://git.x2software.net/pub/Stairs
|
||
|
*/
|
||
|
#include "connection.h"
|
||
|
#include <ArduinoJson.h>
|
||
|
#include <FS.h>
|
||
|
#include "../debug.h"
|
||
|
#include "../global.h"
|
||
|
#include "../config.h"
|
||
|
|
||
|
|
||
|
|
||
|
void ConnectionSettings::read()
|
||
|
{
|
||
|
_dln("ConnectionSettings :: opening file");
|
||
|
File settingsFile = SPIFFS.open(ConnectionSettingsFile, "r");
|
||
|
if (!settingsFile)
|
||
|
{
|
||
|
_dln("ConnectionSettings :: failed to open file");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
size_t size = settingsFile.size();
|
||
|
if (size > 1024)
|
||
|
{
|
||
|
_dln("ConnectionSettings :: file size is too large");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (size == 0)
|
||
|
{
|
||
|
_dln("ConnectionSettings :: zero size file");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
std::unique_ptr<char[]> buf(new char[size]);
|
||
|
settingsFile.readBytes(buf.get(), size);
|
||
|
|
||
|
_dln(buf.get());
|
||
|
|
||
|
if (fromJson(buf.get()))
|
||
|
_dln("ConnectionSettings :: read from file");
|
||
|
else
|
||
|
_dln("ConnectionSettings :: failed to parse file");
|
||
|
}
|
||
|
|
||
|
|
||
|
void ConnectionSettings::write()
|
||
|
{
|
||
|
_dln("ConnectionSettings :: opening file for writing");
|
||
|
File settingsFile = SPIFFS.open(ConnectionSettingsFile, "w");
|
||
|
if (!settingsFile)
|
||
|
{
|
||
|
_dln("ConnectionSettings:: failed to open file for writing");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
toJson(settingsFile);
|
||
|
_dln("ConnectionSettings:: written to file");
|
||
|
}
|
||
|
|
||
|
|
||
|
void ConnectionSettings::toJson(Print &print)
|
||
|
{
|
||
|
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(9));
|
||
|
|
||
|
JsonObject& root = jsonBuffer.createObject();
|
||
|
root["hostname"] = hostname();
|
||
|
root["accesspoint"] = flag(AccessPoint);
|
||
|
root["station"] = flag(StationMode);
|
||
|
root["ssid"] = ssid();
|
||
|
root["password"] = password();
|
||
|
root["dhcp"] = flag(DHCP);
|
||
|
root["ip"] = ip() != 0 ? ip().toString() : "";
|
||
|
root["subnetmask"] = subnetMask() != 0 ? subnetMask().toString() : "";
|
||
|
root["gateway"] = gateway() != 0 ? gateway().toString() : "";
|
||
|
|
||
|
root.printTo(print);
|
||
|
}
|
||
|
|
||
|
|
||
|
bool ConnectionSettings::fromJson(char* data)
|
||
|
{
|
||
|
return fromJson(data, NULL);
|
||
|
}
|
||
|
|
||
|
|
||
|
bool ConnectionSettings::fromJson(char* data, bool* changed)
|
||
|
{
|
||
|
if (changed != NULL)
|
||
|
*changed = false;
|
||
|
|
||
|
DynamicJsonBuffer jsonBuffer(JSON_OBJECT_SIZE(9) + 250);
|
||
|
JsonObject& root = jsonBuffer.parseObject(data);
|
||
|
|
||
|
if (!root.success())
|
||
|
return false;
|
||
|
|
||
|
IPAddress jsonIP;
|
||
|
IPAddress jsonSubnetMask;
|
||
|
IPAddress jsonGateway;
|
||
|
|
||
|
const char* jsonHostname = root["hostname"];
|
||
|
bool jsonAccessPoint = root["accesspoint"];
|
||
|
bool jsonStation = root["station"];
|
||
|
const char* jsonSSID = root["ssid"];
|
||
|
const char* jsonPassword = root["password"];
|
||
|
bool jsonDHCP = root["dhcp"];
|
||
|
const char* jsonIPText = root["ip"];
|
||
|
const char* jsonSubnetMaskText = root["subnetmask"];
|
||
|
const char* jsonGatewayText = root["gateway"];
|
||
|
|
||
|
if (jsonIPText == NULL || !jsonIP.fromString(jsonIPText)) jsonIP = emptyIP;
|
||
|
if (jsonSubnetMaskText == NULL || !jsonSubnetMask.fromString(jsonSubnetMaskText)) jsonSubnetMask = emptyIP;
|
||
|
if (jsonGatewayText == NULL || !jsonGateway.fromString(jsonGatewayText)) jsonGateway = emptyIP;
|
||
|
|
||
|
|
||
|
if (!(jsonAccessPoint || jsonStation))
|
||
|
jsonAccessPoint = true;
|
||
|
|
||
|
if ((jsonHostname != hostname()) ||
|
||
|
(jsonAccessPoint != flag(AccessPoint)) ||
|
||
|
(jsonStation != flag(StationMode)) ||
|
||
|
(jsonSSID != ssid()) ||
|
||
|
(jsonPassword != password()) ||
|
||
|
(jsonDHCP != flag(DHCP)) ||
|
||
|
(jsonIP != ip()) ||
|
||
|
(jsonSubnetMask != subnetMask()) ||
|
||
|
(jsonGateway != gateway()))
|
||
|
{
|
||
|
hostname(jsonHostname);
|
||
|
flag(AccessPoint, jsonAccessPoint);
|
||
|
flag(StationMode, jsonStation);
|
||
|
ssid(jsonSSID);
|
||
|
password(jsonPassword);
|
||
|
flag(DHCP, jsonDHCP);
|
||
|
ip(jsonIP);
|
||
|
subnetMask(jsonSubnetMask);
|
||
|
gateway(jsonGateway);
|
||
|
|
||
|
if (changed != NULL)
|
||
|
*changed = true;
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|