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

@ -20,17 +20,61 @@ void MainObject::refresh()
globalItems.refresh();
}
PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, QJsonObject* settings, QString host, int port, QObject *parent) :
QJsonObject MainObject::getJsonObjectFromDisk(const QString& filename, bool* error)
{
QFile file;
file.setFileName(filename);
bool ret = file.open(QIODevice::ReadOnly);
if(!file.isOpen() || !ret)
{
std::cerr<<"Can not open config file: "<<filename.toLatin1().data()<<std::endl;
}
else
{
QJsonParseError qerror;
QJsonDocument document(QJsonDocument::fromJson(file.readAll(), &qerror));
file.close();
if(qerror.error != QJsonParseError::NoError)
{
qDebug()<<filename<<" "<<qerror.errorString();
if(error)
(*error) = true;
}
return document.object();
}
return QJsonObject();
}
bool MainObject::storeJsonObjectToDisk(const QString& filename, const QJsonObject& json)
{
QFile file(filename + ".out");
qDebug()<<"config file: "<<filename;
bool ret = file.open(QIODevice::WriteOnly);
if(!file.isOpen() || !ret)
{
std::cerr<<"Can not open config file: "<<filename.toLatin1().data()<<std::endl;
return false;
}
else
{
QJsonDocument document(json);
file.write(document.toJson());
file.close();
QFile::remove(filename);
file.rename(filename);
return true;
}
}
PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, const QString& settingsPath, const QString& host, int port, QObject *parent) :
MainObject(parent),
settings(settings),
microDevice(microDevice),
ioMultiplexer(microDevice),
settingsPath(settingsPath),
micro(microDevice),
tcpServer(new TcpServer),
sunSensorSource(49.824972, 8.702194),
powerItem(new PowerItem),
rgbItem(new RgbItem(&micro, 5487422, "Rgb Lights")),
auxItem(new AuxItem(&micro, 5487421, "Desk Light"))
fixedItems(&micro)
{
//connect sensors subsystem
connect(&globalSensors, &SensorStore::sensorChangedState, tcpServer, &TcpServer::sensorEvent);
@ -38,17 +82,24 @@ PrimaryMainObject::PrimaryMainObject(QIODevice* microDevice, QJsonObject* settin
connect(&sunSensorSource, &SunSensorSource::stateChanged, &globalSensors, &SensorStore::sensorGotState);
connect(&micro, &Microcontroller::gotSensorState, &globalSensors, &SensorStore::sensorGotState);
sunSensorSource.run();
globalItems.registerItemSource(&fixedItems);
globalItems.registerItemSource(tcpServer);
globalItems.registerItemSource(&micro);
globalItems.registerItemSource(&itemLoader);
load(*settings);
Relay::setMicrocontroller(&micro);
loadFromDisk(settingsPath);
tcpServer->launch(QHostAddress(host), port);
connect(&globalItems, &ItemStore::itemUpdated, tcpServer, &TcpServer::itemUpdated);
}
PrimaryMainObject::~PrimaryMainObject()
{
store(*settings);
storeToDisk(settingsPath);
}
void PrimaryMainObject::store(QJsonObject &json)
@ -58,22 +109,28 @@ void PrimaryMainObject::store(QJsonObject &json)
void PrimaryMainObject::load(const QJsonObject& json)
{
settings = json;
itemLoader.updateJson(json);
globalItems.clear();
rgbItem->removeAllActors();
auxItem->removeAllActors();
powerItem->removeAllActors();
globalItems.addItem(rgbItem);
globalItems.addItem(auxItem);
globalItems.addItem(powerItem);
globalItems.load(json);
if(json["Items"].toArray().size() >= 2)
{
rgbItem->load(json["Items"].toArray()[0].toObject());
auxItem->load(json["Items"].toArray()[1].toObject());
}
globalItems.refresh();
}
bool PrimaryMainObject::storeToDisk(const QString& filename)
{
store(settings);
itemLoader.updateJson(settings);
return storeJsonObjectToDisk(filename, settings);
}
bool PrimaryMainObject::loadFromDisk(const QString& filename)
{
bool error = false;
QJsonObject json = getJsonObjectFromDisk(filename, &error);
if(!error)
load(json);
return error;
}
SecondaryMainObject::SecondaryMainObject(QString host, int port, QObject *parent) :
MainObject(parent),
tcpClient(new TcpClient)
@ -84,8 +141,12 @@ SecondaryMainObject::SecondaryMainObject(QString host, int port, QObject *parent
if(!tcpClient->launch(QHostAddress(host), port))
{
QMessageBox::critical(nullptr, "Error", "Could not connect to "+host+":"+QString::number(port));
exit(1);
QMetaObject::invokeMethod(this, [](){exit(1);}, Qt::QueuedConnection);
}
connect(&globalItems, &ItemStore::itemUpdated, tcpClient, &TcpClient::itemUpdated);
globalItems.refresh();
}
SecondaryMainObject::~SecondaryMainObject()