#include "mainobject.h" #include #include #include #include "items/itemstore.h" MainObject::MainObject(QObject *parent) : QObject(parent) { } MainObject::~MainObject() { } void MainObject::refresh() { globalItems.refresh(); } 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: "<launch(QHostAddress(host), port); webServer->launch(QHostAddress(host), port+1); connect(&globalItems, &ItemStore::itemUpdated, tcpServer, &TcpServer::itemUpdated); connect(&globalItems, &ItemStore::itemUpdated, webServer, &WebSocketServer::itemUpdated); } PrimaryMainObject::~PrimaryMainObject() { storeToDisk(settingsPath); } void PrimaryMainObject::store(QJsonObject &json) { globalItems.store(json); QJsonObject mqttJson = json["Mqtt"].toObject(); mqttSensorSource.store(mqttJson); json["Mqtt"] = mqttJson; } void PrimaryMainObject::load(const QJsonObject& json) { settings = json; itemLoader.updateJson(json); globalItems.clear(); 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) { connect(tcpClient, &TcpClient::gotSensor, &globalSensors, &SensorStore::sensorGotState); globalItems.registerItemSource(tcpClient); if(!tcpClient->launch(QHostAddress(host), port)) { QMessageBox::critical(nullptr, "Error", "Could not connect to "+host+":"+QString::number(port)); QMetaObject::invokeMethod(this, [](){exit(1);}, Qt::QueuedConnection); } connect(&globalItems, &ItemStore::itemUpdated, tcpClient, &TcpClient::itemUpdated); globalItems.refresh(); } SecondaryMainObject::~SecondaryMainObject() { }