Finish lerge refactor of systems

This commit is contained in:
Carl Philipp Klemm 2026-03-22 23:23:18 +01:00
parent 6d742e60db
commit 913d7df56d
36 changed files with 614 additions and 634 deletions

View file

@ -12,54 +12,6 @@
#include "mainobject.h"
#include "programmode.h"
QJsonObject getJsonObjectFromDisk(const QString& filePath, bool* error = nullptr)
{
QFile file;
file.setFileName(filePath);
bool ret = file.open(QIODevice::ReadOnly);
if(!file.isOpen() || !ret)
{
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
}
else
{
QJsonParseError qerror;
QJsonDocument document(QJsonDocument::fromJson(file.readAll(), &qerror));
file.close();
if(qerror.error != QJsonParseError::NoError)
{
qDebug()<<filePath<<" "<<qerror.errorString();
if(error) (*error) = true;
}
return document.object();
}
return QJsonObject();
}
bool storeJsonObjectToDisk(const QJsonObject& json, QString filePath)
{
QFile file(filePath + ".out");
qDebug()<<"config file: "<<filePath;
bool ret = file.open(QIODevice::WriteOnly);
if(!file.isOpen() || !ret)
{
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
return false;
}
else
{
QJsonDocument document(json);
file.write(document.toJson());
file.close();
QFile::remove(filePath);
file.rename(filePath);
return true;
}
}
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
@ -103,7 +55,8 @@ int main(int argc, char *argv[])
}
if(programMode == PROGRAM_MODE_PRIMARY || programMode == PROGRAM_MODE_HEADLESS_PRIMARY)
{
QJsonObject json = getJsonObjectFromDisk(parser.value(settingsPathOption));
QString settingsPath = parser.value(settingsPathOption);
QJsonObject json = MainObject::getJsonObjectFromDisk(settingsPath);
bool tcpMicro = json["MicroTcp"].toBool(true);
json["MicroTcp"] = tcpMicro;
@ -123,7 +76,7 @@ int main(int argc, char *argv[])
if(!microSocket->waitForConnected(1000))
{
qCritical()<<"Can not connect to tcp micro";
storeJsonObjectToDisk(json, parser.value(settingsPathOption));
MainObject::storeJsonObjectToDisk(settingsPath, json);
if(programMode == PROGRAM_MODE_PRIMARY)
QMessageBox::critical(nullptr, "Error", "Can not connect to tcp micro");
return 1;
@ -145,21 +98,23 @@ int main(int argc, char *argv[])
if(!microPort->isOpen())
{
qCritical()<<"Can not open serial port"<<port;
storeJsonObjectToDisk(json, parser.value(settingsPathOption));
MainObject::storeJsonObjectToDisk(settingsPath, json);
if(programMode == PROGRAM_MODE_PRIMARY)
QMessageBox::critical(nullptr, "Error", "Can not open serial port " + port);
return 1;
}
microDevice = microPort;
}
PrimaryMainObject mainObject(microDevice, &json, parser.value(hostOption), parser.value(portOption).toInt());
PrimaryMainObject mainObject(microDevice, settingsPath, parser.value(hostOption), parser.value(portOption).toInt());
QObject::connect(mainObject.tcpServer, &TcpServer::sigRequestSave, &mainObject, [&mainObject, settingsPath](){mainObject.storeToDisk(settingsPath);});
MainWindow* w = nullptr;
if(programMode != PROGRAM_MODE_HEADLESS_PRIMARY)
{
w = new MainWindow(&mainObject);
QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), w, SLOT(changeHeaderLableText(QString)));
QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), w, SLOT(changeHeaderLableText(QString)));
//QObject::connect(&w, &MainWindow::sigSave, &mainObject, &MainObject::storeToDisk);
QObject::connect(w, &MainWindow::sigSetRgb, &mainObject.micro, &Microcontroller::changeRgbColor);
QObject::connect(w, &MainWindow::sigSave, &mainObject, [&mainObject, settingsPath](){mainObject.storeToDisk(settingsPath);});
QObject::connect(w, &MainWindow::createdItem, &globalItems, [](std::shared_ptr<Item> item){globalItems.addItem(item, false);});
w->show();
}
@ -167,14 +122,13 @@ int main(int argc, char *argv[])
delete w;
delete microDevice;
mainObject.store(json);
storeJsonObjectToDisk(json, parser.value(settingsPathOption));
}
else
{
SecondaryMainObject mainObject(parser.value(hostOption), parser.value(portOption).toInt());
MainWindow w(&mainObject);
//QObject::connect(&w, &MainWindow::sigSave, &mainObject, &MainObject::sendJson);
QObject::connect(&w, &MainWindow::createdItem, &globalItems, [](std::shared_ptr<Item> item){globalItems.addItem(item, false);});
QObject::connect(&w, &MainWindow::sigSave, mainObject.tcpClient, &TcpClient::sendItems);
w.show();
retVal = a.exec();