rgbwifi/src/settings/connection.cpp

86 lines
2.5 KiB
C++

/*
* ESP8266 RGBW controller
* Copyright 2020 (c) Mark van Renswoude
*
* https://git.x2software.net/pub/RGBWifi
*/
#include "./connection.h"
#include <ArduinoJson.h>
#include "./abstractjson.h"
#include "../debug.h"
#include "../config.h"
#include "../global.h"
void ConnectionSettings::toJson(Print &print)
{
DynamicJsonDocument jsonDocument(1024);
jsonDocument["hostname"] = hostname();
jsonDocument["accesspoint"] = flag(AccessPoint);
jsonDocument["station"] = flag(StationMode);
jsonDocument["ssid"] = ssid();
jsonDocument["password"] = password();
jsonDocument["dhcp"] = flag(DHCP);
jsonDocument["ip"] = ip() != emptyIP ? ip().toString() : "";
jsonDocument["subnetmask"] = subnetMask() != emptyIP ? subnetMask().toString() : "";
jsonDocument["gateway"] = gateway() != emptyIP ? gateway().toString() : "";
serializeJson(jsonDocument, print);
}
bool ConnectionSettings::fromJson(JsonVariant &jsonDocument, bool* changed)
{
if (changed != nullptr)
*changed = false;
IPAddress jsonIP;
IPAddress jsonSubnetMask;
IPAddress jsonGateway;
const char* jsonHostname = jsonDocument["hostname"];
bool jsonAccessPoint = jsonDocument["accesspoint"];
bool jsonStation = jsonDocument["station"];
const char* jsonSSID = jsonDocument["ssid"];
const char* jsonPassword = jsonDocument["password"];
bool jsonDHCP = jsonDocument["dhcp"];
const char* jsonIPText = jsonDocument["ip"];
const char* jsonSubnetMaskText = jsonDocument["subnetmask"];
const char* jsonGatewayText = jsonDocument["gateway"];
if (jsonIPText == nullptr || !jsonIP.fromString(jsonIPText)) jsonIP = emptyIP;
if (jsonSubnetMaskText == nullptr || !jsonSubnetMask.fromString(jsonSubnetMaskText)) jsonSubnetMask = emptyIP;
if (jsonGatewayText == nullptr || !jsonGateway.fromString(jsonGatewayText)) jsonGateway = emptyIP;
if (!(jsonAccessPoint || jsonStation))
jsonAccessPoint = true;
if ((!sameStr(jsonHostname, hostname())) ||
(jsonAccessPoint != flag(AccessPoint)) ||
(jsonStation != flag(StationMode)) ||
(!sameStr(jsonSSID, ssid())) ||
(!sameStr(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 != nullptr)
*changed = true;
}
return true;
}