155 lines
4.6 KiB
C++
155 lines
4.6 KiB
C++
#include "mainobject.h"
|
|
|
|
MainObject::MainObject(QIODevice* ioDevice, const QString& settingsPathIn, const bool masterIn, QObject *parent) :
|
|
QObject(parent),
|
|
master(masterIn),
|
|
masterIODevice(ioDevice),
|
|
ioMultiplexer(masterIODevice),
|
|
micro(ioMultiplexer.getIoDevice()),
|
|
broadCast(ioMultiplexer.getIoDevice(), masterIn),
|
|
settingsPath(settingsPathIn),
|
|
sunSensorSource(49.884450, 8.650536),
|
|
rgbItem(new RgbItem(µ, 5487422, "Rgb Lights")),
|
|
auxItem(new AuxItem(µ, 5487421, "Desk Light"))
|
|
|
|
{
|
|
qDebug()<<"Is master:"<<master;
|
|
//connect sensors subsystem
|
|
QObject::connect(µ, &Microcontroller::gotSensorState, &globalSensors, &SensorStore::sensorGotState);
|
|
QObject::connect(&sunSensorSource, &SunSensorSource::stateChanged, &globalSensors, &SensorStore::sensorGotState);
|
|
QObject::connect(&globalSensors, &SensorStore::sensorChangedState, &ocupancySensor, &OcupancySensorSource::sensorEvent);
|
|
QObject::connect(&ocupancySensor, &OcupancySensorSource::stateChanged, &globalSensors, &SensorStore::sensorGotState);
|
|
|
|
//connect item store
|
|
QObject::connect(µ, &Microcontroller::gotRelayList, &items, &ItemStore::addItems);
|
|
QObject::connect(µ, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged);
|
|
|
|
//special items
|
|
QObject::connect(&powerItem, &PowerItem::stateChanged, &globalSensors, &SensorStore::sensorGotState);
|
|
powerItem.emmitSensor();
|
|
items.addItem(rgbItem);
|
|
items.addItem(auxItem);
|
|
|
|
connect(&broadCast, &BroadCast::gotJson, this, &MainObject::recivedJson);
|
|
QObject::connect(&broadCast, &BroadCast::gotSensorState, &globalSensors, &SensorStore::sensorGotState);
|
|
if(master)connect(&broadCast, &BroadCast::jsonRequested, this, &MainObject::sendJson);
|
|
|
|
if(master) load(getJsonObjectFromDisk(settingsPath, &noSave));
|
|
else
|
|
{
|
|
broadCast.requestJson();
|
|
}
|
|
|
|
#ifndef Q_OS_ANDROID
|
|
Item::secondaryFlag = !master;
|
|
#endif
|
|
}
|
|
|
|
MainObject::~MainObject()
|
|
{
|
|
if(master)
|
|
{
|
|
QJsonObject json;
|
|
store(json);
|
|
if(!noSave)storeJsonObjectToDisk(json, settingsPath);
|
|
}
|
|
}
|
|
|
|
void MainObject::store(QJsonObject &json)
|
|
{
|
|
items.store(json);
|
|
QJsonObject powerObject;
|
|
powerItem.store(powerObject);
|
|
json.insert("Power", powerObject);
|
|
}
|
|
|
|
void MainObject::load(const QJsonObject& json)
|
|
{
|
|
items.clear();
|
|
rgbItem->removeAllActors();
|
|
auxItem->removeAllActors();
|
|
powerItem.removeAllActors();
|
|
items.addItem(rgbItem);
|
|
items.addItem(auxItem);
|
|
items.load(json, µ);
|
|
powerItem.load(json["Power"].toObject());
|
|
qDebug()<<"aray size: "<<json.isEmpty();
|
|
if(json["Items"].toArray().size() >= 2)
|
|
{
|
|
rgbItem->load(json["Items"].toArray()[0].toObject());
|
|
auxItem->load(json["Items"].toArray()[1].toObject());
|
|
}
|
|
micro.requestState();
|
|
}
|
|
|
|
void MainObject::recivedJson(const QJsonObject json)
|
|
{
|
|
if(master)storeJsonObjectToDisk(json, settingsPath);
|
|
load(json);
|
|
}
|
|
|
|
void MainObject::sendJson()
|
|
{
|
|
QJsonObject json;
|
|
store(json);
|
|
broadCast.sendJson(json);
|
|
}
|
|
|
|
QJsonObject MainObject::getJsonObjectFromDisk(const QString& filePath, bool* error)
|
|
{
|
|
QFile file;
|
|
|
|
#ifndef Q_OS_ANDROID
|
|
if(filePath.size() > 0) file.setFileName(filePath);
|
|
else
|
|
#endif
|
|
{
|
|
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json");
|
|
}
|
|
|
|
file.open(QIODevice::ReadOnly);
|
|
if(!file.isOpen()) 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 MainObject::storeJsonObjectToDisk(const QJsonObject& json, QString filePath)
|
|
{
|
|
#ifndef Q_OS_ANDROID
|
|
if(filePath.size() == 0)
|
|
#endif
|
|
{
|
|
filePath = QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json";
|
|
}
|
|
QFile file(filePath + ".out");
|
|
|
|
qDebug()<<"config file: "<<filePath;
|
|
file.open(QIODevice::WriteOnly);
|
|
if(!file.isOpen())
|
|
{
|
|
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;
|
|
}
|
|
}
|
|
|