KDEAmbi/wled_config_tool.cpp

73 lines
2.3 KiB
C++

#include <QCoreApplication>
#include <QCommandLineParser>
#include <QDebug>
#include <QJsonDocument>
#include <QFile>
#include "wledconfigclient.h"
int main(int argc, char *argv[])
{
QCoreApplication app(argc, argv);
QCoreApplication::setApplicationName("wled_config_tool");
QCoreApplication::setApplicationVersion("1.0");
QCommandLineParser parser;
parser.setApplicationDescription("WLED Configuration Tool");
parser.addHelpOption();
parser.addVersionOption();
QCommandLineOption addressOption(QStringList() << "a" << "address",
"WLED device IP address or hostname.",
"address");
parser.addOption(addressOption);
QCommandLineOption saveOption(QStringList() << "s" << "save",
"Save configuration to a file.",
"filepath");
parser.addOption(saveOption);
parser.process(app);
if (!parser.isSet(addressOption)) {
qCritical() << "Error: WLED address not specified. Use -a or --address.";
parser.showHelp(1);
}
QString wledAddress = parser.value(addressOption);
QString saveFilePath = parser.value(saveOption);
WledConfigClient client(wledAddress);
QObject::connect(&client, &WledConfigClient::infoReceived, [&](const QJsonObject &info) {
QJsonDocument doc(info);
QFile stdoutFile;
stdoutFile.open(stdout, QFile::WriteOnly);
QTextStream ts(&stdoutFile);
ts << "WLED Configuration Info:\n" << doc.toJson(QJsonDocument::Indented) << Qt::endl;
stdoutFile.close();
QByteArray jsonBytes = doc.toJson(QJsonDocument::Indented); // Keep this for saving to file
if (!saveFilePath.isEmpty()) {
QFile file(saveFilePath);
if (file.open(QIODevice::WriteOnly)) {
file.write(jsonBytes);
file.close();
qDebug() << "Configuration saved to" << saveFilePath;
} else {
qWarning() << "Failed to save configuration to" << saveFilePath << ":" << file.errorString();
}
}
app.quit();
});
QObject::connect(&client, &WledConfigClient::error, [&](const QString &message) {
qCritical() << "Error:" << message;
app.quit();
});
client.getInfo();
return app.exec();
}