2018-01-01 18:56:07 +00:00
|
|
|
/*
|
|
|
|
* 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)
|
|
|
|
{
|
2018-01-07 22:12:42 +00:00
|
|
|
return fromJson(data, nullptr);
|
2018-01-01 18:56:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool ConnectionSettings::fromJson(char* data, bool* changed)
|
|
|
|
{
|
2018-01-07 22:12:42 +00:00
|
|
|
if (changed != nullptr)
|
2018-01-01 18:56:07 +00:00
|
|
|
*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"];
|
|
|
|
|
2018-01-07 22:12:42 +00:00
|
|
|
if (jsonIPText == nullptr || !jsonIP.fromString(jsonIPText)) jsonIP = emptyIP;
|
|
|
|
if (jsonSubnetMaskText == nullptr || !jsonSubnetMask.fromString(jsonSubnetMaskText)) jsonSubnetMask = emptyIP;
|
|
|
|
if (jsonGatewayText == nullptr || !jsonGateway.fromString(jsonGatewayText)) jsonGateway = emptyIP;
|
2018-01-01 18:56:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
2018-01-07 22:12:42 +00:00
|
|
|
if (changed != nullptr)
|
2018-01-01 18:56:07 +00:00
|
|
|
*changed = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|