Added json broadcasting
This commit is contained in:
parent
df27b622a0
commit
3cbe947408
37 changed files with 514 additions and 487 deletions
144
src/mainobject.cpp
Normal file
144
src/mainobject.cpp
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
#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()),
|
||||
settingsPath(settingsPathIn),
|
||||
sunSensorSource(49.884450, 8.650536),
|
||||
items(&sensors),
|
||||
powerItem(&sensors),
|
||||
rgbItem(new RgbItem(&sensors, µ, 5487422, "Rgb Lights")),
|
||||
auxItem(new AuxItem(&sensors, µ, 5487421, "Desk Light"))
|
||||
|
||||
{
|
||||
qDebug()<<"Is master:"<<master;
|
||||
//connect sensors subsystem
|
||||
QObject::connect(µ, &Microcontroller::gotSensorState, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&livingroomSpeakerSensorSource, &SpeakerSensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&sunSensorSource, &SunSensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
QObject::connect(&sensors, &SensorStore::sensorChangedState, &ocupancySensor, &OcupancySensorSource::sensorEvent);
|
||||
QObject::connect(&ocupancySensor, &OcupancySensorSource::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
|
||||
QMetaObject::invokeMethod(&livingroomSpeakerSensorSource, "run", Qt::QueuedConnection);
|
||||
sunSensorSource.run();
|
||||
|
||||
//connect item store
|
||||
QObject::connect(µ, &Microcontroller::gotRelayList, &items, &ItemStore::addItems);
|
||||
QObject::connect(µ, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged);
|
||||
|
||||
//special items
|
||||
QObject::connect(&powerItem, &PowerItem::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
powerItem.emmitSensor();
|
||||
items.addItem(rgbItem);
|
||||
items.addItem(auxItem);
|
||||
|
||||
connect(&broadCast, &BroadCast::gotJson, this, &MainObject::load);
|
||||
connect(&broadCast, &BroadCast::jsonRequested, this, &MainObject::sendJson);
|
||||
|
||||
if(master) load(getJsonObjectFromDisk(settingsPath));
|
||||
else
|
||||
{
|
||||
broadCast.requestJson();
|
||||
}
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
Item::secondaryFlag = !master;
|
||||
#endif
|
||||
}
|
||||
|
||||
MainObject::~MainObject()
|
||||
{
|
||||
if(master)
|
||||
{
|
||||
QJsonObject json;
|
||||
store(json);
|
||||
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();
|
||||
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::sendJson()
|
||||
{
|
||||
QJsonObject json;
|
||||
store(json);
|
||||
broadCast.sendJson(json);
|
||||
}
|
||||
|
||||
QJsonObject MainObject::getJsonObjectFromDisk(const QString& filePath)
|
||||
{
|
||||
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
|
||||
{
|
||||
QJsonDocument document(QJsonDocument::fromJson(file.readAll()));
|
||||
file.close();
|
||||
return document.object();
|
||||
}
|
||||
return QJsonObject();
|
||||
}
|
||||
|
||||
bool MainObject::storeJsonObjectToDisk(const QJsonObject& json, const QString& filePath)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(filePath.size() > 0) file.setFileName(filePath);
|
||||
else
|
||||
#endif
|
||||
{
|
||||
file.setFileName(QStandardPaths::writableLocation(QStandardPaths::ConfigLocation) + "/shinterface.json");
|
||||
}
|
||||
std::cout<<"config file: "<<file.fileName().toLatin1().data()<<std::endl;
|
||||
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();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Add table
Add a link
Reference in a new issue