UvosSmartHomeInterface/src/mainobject.cpp

166 lines
4.3 KiB
C++

#include "mainobject.h"
#include<QJsonObject>
#include<QJsonArray>
#include<QMessageBox>
#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: "<<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),
settingsPath(settingsPath),
micro(microDevice),
tcpServer(new TcpServer(this)),
webServer(new WebSocketServer("shinterface", this)),
sunSensorSource(49.824972, 8.702194),
fixedItems(&micro)
{
//connect sensors subsystem
connect(&globalSensors, &SensorStore::sensorChangedState, tcpServer, &TcpServer::sensorEvent);
connect(tcpServer, &TcpServer::gotSensor, &globalSensors, &SensorStore::sensorGotState);
connect(webServer, &WebSocketServer::gotSensor, &globalSensors, &SensorStore::sensorGotState);
connect(&sunSensorSource, &SunSensorSource::stateChanged, &globalSensors, &SensorStore::sensorGotState);
connect(&micro, &Microcontroller::gotSensorState, &globalSensors, &SensorStore::sensorGotState);
connect(&mqttSensorSource, &MqttSensorSource::stateChanged, &globalSensors, &SensorStore::sensorGotState);
sunSensorSource.run();
globalItems.registerItemSource(&fixedItems);
globalItems.registerItemSource(tcpServer);
globalItems.registerItemSource(&micro);
globalItems.registerItemSource(&itemLoader);
Relay::setMicrocontroller(&micro);
loadFromDisk(settingsPath);
QJsonObject mqttJson = settings["Mqtt"].toObject();
mqttSensorSource.start(mqttJson);
tcpServer->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()
{
}