WaterLevelSensor/configinterface.cpp

168 lines
4.2 KiB
C++
Raw Normal View History

2026-04-01 18:33:19 +05:30
#include "configinterface.h"
ConfigInterface::ConfigInterface(QObject *parent)
: QObject{parent}
{
auto paths = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
qDebug() << "Got writable path as: " << paths;
std::filesystem::path dir = paths.toStdString();
if(!QDir(dir).exists()) {
QDir().mkpath(paths);
}
std::filesystem::path path = dir / "config.json";
QFile config(path);
if(config.exists()) {
qDebug() << "Loading config file....";
bool ok = config.open(QFile::ReadOnly);
if(!ok) {
qDebug() << config.errorString();
return;
}
auto data = config.readAll();
config.close();
QJsonObject top = QJsonDocument::fromJson(data).object();
2026-04-13 17:49:08 +05:30
QString max, min, alert, mode, url;
2026-04-03 18:46:34 +05:30
bool flipbits;
2026-04-16 17:18:02 +05:30
max = top["max_threshold"].toString("110");
min = top["min_threshold"].toString("10");
alert = top["alert_threshold"].toString("30");
2026-04-13 19:34:36 +05:30
flipbits = top["flip_bits"].toBool(true);
mode = top["mode"].toString("http");
url = top["url"].toString("https://unrealdragon.in/server/waterlevel");
2026-04-01 18:33:19 +05:30
setMax(max);
setMin(min);
setAlert(alert);
2026-04-03 18:46:34 +05:30
setFlipBits(flipbits);
2026-04-13 17:49:08 +05:30
setMode(mode);
setUrl(url);
2026-04-01 18:33:19 +05:30
}
else {
qDebug() << "File does not exist creating one...";
QJsonObject top;
2026-04-16 17:18:02 +05:30
top["max_threshold"] = "110";
top["min_threshold"] = "10";
top["alert_threshold"] = "30";
2026-04-13 17:49:08 +05:30
top["flip_bits"] = true;
top["mode"] = "http";
top["url"] = "https://unrealdragon.in/server/waterlevel";
2026-04-01 18:33:19 +05:30
QByteArray data = QJsonDocument(top).toJson();
bool ok = config.open(QFile::WriteOnly);
if(!ok) {
qDebug() << config.errorString();
return;
}
config.write(data);
config.flush();
config.close();
2026-04-16 17:18:02 +05:30
setMax("110");
setMin("10");
setAlert("30");
2026-04-13 17:49:08 +05:30
setFlipBits(true);
setMode("http");
setUrl("https://unrealdragon.in/server/waterlevel");
qDebug() << "Config set initial values completed";
2026-04-01 18:33:19 +05:30
}
}
2026-04-13 17:49:08 +05:30
void ConfigInterface::saveConfig(QString max, QString min, QString alert, bool flipbits, QString mode, QString url)
2026-04-01 18:33:19 +05:30
{
auto paths = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
qDebug() << "Got writable path as: " << paths;
std::filesystem::path path = paths.toStdString();
QFile config(path / "config.json");
qDebug() << "Writing to config file..." << max << min << alert;
QJsonObject top;
top["max_threshold"] = max;
top["min_threshold"] = min;
top["alert_threshold"] = alert;
2026-04-03 18:46:34 +05:30
top["flip_bits"] = flipbits;
2026-04-13 17:49:08 +05:30
top["mode"] = mode;
top["url"] = url;
2026-04-01 18:33:19 +05:30
setMax(max);
setMin(min);
setAlert(alert);
2026-04-13 17:49:08 +05:30
setMode(mode);
2026-04-01 18:33:19 +05:30
QByteArray data = QJsonDocument(top).toJson();
config.open(QIODevice::WriteOnly);
config.write(data);
config.flush();
config.close();
}
QString ConfigInterface::alert() const
{
return m_alert;
}
void ConfigInterface::setAlert(const QString &newAlert)
{
if (m_alert == newAlert)
return;
m_alert = newAlert;
emit alertChanged();
}
QString ConfigInterface::max() const
{
return m_max;
}
void ConfigInterface::setMax(const QString &newMax)
{
if (m_max == newMax)
return;
m_max = newMax;
emit maxChanged();
}
QString ConfigInterface::min() const
{
return m_min;
}
void ConfigInterface::setMin(const QString &newMin)
{
if (m_min == newMin)
return;
m_min = newMin;
emit minChanged();
}
2026-04-03 18:46:34 +05:30
bool ConfigInterface::flipBits() const
{
return m_flipBits;
}
void ConfigInterface::setFlipBits(bool newFlipBits)
{
if (m_flipBits == newFlipBits)
return;
m_flipBits = newFlipBits;
emit flipBitsChanged();
}
2026-04-13 17:49:08 +05:30
QString ConfigInterface::mode() const
{
return m_mode;
}
void ConfigInterface::setMode(const QString &newMode)
{
if (m_mode == newMode)
return;
m_mode = newMode;
emit modeChanged();
}
QString ConfigInterface::url() const
{
return m_url;
}
void ConfigInterface::setUrl(const QString &newUrl)
{
if (m_url == newUrl)
return;
m_url = newUrl;
emit urlChanged();
}