Added json broadcasting
This commit is contained in:
parent
df27b622a0
commit
3cbe947408
37 changed files with 514 additions and 487 deletions
187
src/main.cpp
187
src/main.cpp
|
|
@ -1,14 +1,8 @@
|
|||
#include <QtWidgets/QApplication>
|
||||
#include <stdio.h>
|
||||
#include <QDir>
|
||||
#include <QDebug>
|
||||
#include <QString>
|
||||
#include <QSettings>
|
||||
#include <QSignalMapper>
|
||||
#include <QTcpSocket>
|
||||
#include <QFileInfo>
|
||||
#include <QJsonDocument>
|
||||
#include <QStandardPaths>
|
||||
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
#include <QtSerialPort/QtSerialPort>
|
||||
|
|
@ -28,96 +22,11 @@
|
|||
#include "items/auxitem.h"
|
||||
#include "items/rgbitem.h"
|
||||
#include "items/poweritem.h"
|
||||
#include "mainobject.h"
|
||||
|
||||
|
||||
#define BAUD QSerialPort::Baud38400
|
||||
|
||||
QJsonDocument* createJsonDocument(const bool pathOption, const QString& filePath)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(pathOption && 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 = new QJsonDocument(QJsonDocument::fromJson(file.readAll()));
|
||||
file.close();
|
||||
return document;
|
||||
}
|
||||
return new QJsonDocument;
|
||||
}
|
||||
|
||||
void saveJsonDocument(const QJsonDocument * const document, const bool pathOption, const QString& filePath)
|
||||
{
|
||||
QFile file;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(pathOption && 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;
|
||||
else
|
||||
{
|
||||
file.write(document->toJson());
|
||||
file.close();
|
||||
}
|
||||
}
|
||||
|
||||
QSettings* createQSettings(const bool pathOption, const QString& filePath )
|
||||
{
|
||||
QSettings* settings;
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(pathOption && filePath.size() > 0)
|
||||
{
|
||||
settings = new QSettings(filePath, QSettings::IniFormat);
|
||||
std::cout<<"Config file: "<<settings->fileName().toLatin1().data()<<std::endl;
|
||||
}
|
||||
else if(pathOption)
|
||||
{
|
||||
std::cerr<<"Can not open config file: "<<filePath.toLatin1().data()<<std::endl;
|
||||
settings = new QSettings;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
settings = new QSettings;
|
||||
}
|
||||
return settings;
|
||||
}
|
||||
|
||||
void loadItemState(const QJsonDocument * const jsonDocument, QSettings * const settings, Microcontroller* micro, ItemStore* items, PowerItem* power, RgbItem* rgb, AuxItem* aux )
|
||||
{
|
||||
if(jsonDocument)
|
||||
{
|
||||
const QJsonObject jsonObject = jsonDocument->object();
|
||||
items->load(jsonObject, micro);
|
||||
power->load(jsonObject["Power"].toObject());
|
||||
if(jsonObject["Items"].toArray().size() >= 2)
|
||||
{
|
||||
rgb->load(jsonObject["Items"].toArray()[0].toObject());
|
||||
aux->load(jsonObject["Items"].toArray()[1].toObject());
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
items->load("Items", settings, micro);
|
||||
power->load("Power", settings);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
|
|
@ -155,14 +64,7 @@ int main(int argc, char *argv[])
|
|||
parser.process(a);
|
||||
#endif
|
||||
|
||||
QSettings* settings = nullptr;
|
||||
QJsonDocument* jsonDocument = nullptr;
|
||||
if(parser.isSet(qsettingsOption)) settings = createQSettings(parser.isSet(settingsPathOption), parser.value(settingsPathOption));
|
||||
else jsonDocument = createJsonDocument(parser.isSet(settingsPathOption), parser.value(settingsPathOption));
|
||||
|
||||
|
||||
//connect to microcontoler
|
||||
Microcontroller micro;
|
||||
QIODevice* masterIODevice = nullptr;
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(parser.isSet(tcpOption))
|
||||
|
|
@ -181,7 +83,7 @@ int main(int argc, char *argv[])
|
|||
std::cout<<"Can not connect to to Server.\n";
|
||||
return 1;
|
||||
}
|
||||
micro.setIODevice(microSocket);
|
||||
masterIODevice = microSocket;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -193,7 +95,7 @@ int main(int argc, char *argv[])
|
|||
else microPort->setBaudRate(BAUD);
|
||||
|
||||
if(!microPort->open(QIODevice::ReadWrite)) std::cout<<"Can not open serial port "<<microPort->portName().toStdString()<<". Continueing in demo mode"<<'\n';
|
||||
else micro.setIODevice(microPort);
|
||||
masterIODevice = microPort;
|
||||
}
|
||||
#else
|
||||
QTcpSocket* microSocket = new QTcpSocket;
|
||||
|
|
@ -203,87 +105,22 @@ int main(int argc, char *argv[])
|
|||
std::cout<<"Can not connect to to Server.\n";
|
||||
return 1;
|
||||
}
|
||||
micro.setIODevice(microSocket);
|
||||
masterIODevice = microSocket;
|
||||
#endif
|
||||
|
||||
|
||||
AlarmActions alarmActions(&a, µ);
|
||||
|
||||
//sensors
|
||||
SpeakerSensorSource livingroomSpeakerSensorSource("Livingroom Speakers");
|
||||
SunSensorSource sunSensorSource(49.884450, 8.650536);
|
||||
OcupancySensorSource ocupancySensor;
|
||||
|
||||
SensorStore sensors;
|
||||
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();
|
||||
|
||||
//item store
|
||||
ItemStore items(&sensors);
|
||||
QObject::connect(µ, &Microcontroller::gotRelayList, &items, &ItemStore::addItems);
|
||||
QObject::connect(µ, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged);
|
||||
|
||||
//Power Item
|
||||
PowerItem powerItem(&sensors, &a, 5487423, "Power");
|
||||
QObject::connect(&powerItem, &PowerItem::stateChanged, &sensors, &SensorStore::sensorGotState);
|
||||
powerItem.emmitSensor();
|
||||
MainObject mainObject(masterIODevice, parser.isSet(settingsPathOption) ? parser.value(settingsPathOption) : "", !parser.isSet(secondaryOption));
|
||||
|
||||
//mainwindow
|
||||
MainWindow w(µ, &powerItem, &items, &sensors);
|
||||
QObject::connect(µ, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
|
||||
if(!micro.connected()) w.changeHeaderLableText("No io debug only!");
|
||||
|
||||
//special items
|
||||
std::shared_ptr<RgbItem> rgbItem(new RgbItem(&sensors, µ, 5487422, "Rgb Lights"));
|
||||
items.addItem(rgbItem);
|
||||
|
||||
std::shared_ptr<AuxItem> auxItem(new AuxItem(&sensors, µ, 5487421, "Desk Light"));
|
||||
items.addItem(auxItem);
|
||||
|
||||
QMetaObject::invokeMethod(µ, "run", Qt::QueuedConnection );
|
||||
|
||||
loadItemState(jsonDocument, settings, µ, &items, &powerItem, rgbItem.get(), auxItem.get());
|
||||
|
||||
#ifndef Q_OS_ANDROID
|
||||
if(!parser.isSet(secondaryOption))
|
||||
{
|
||||
Item::secondaryFlag = false;
|
||||
}
|
||||
else
|
||||
{
|
||||
Item::secondaryFlag = true;
|
||||
}
|
||||
#endif
|
||||
MainWindow w(&mainObject.micro, &mainObject.powerItem, &mainObject.items, &mainObject.sensors, !parser.isSet(secondaryOption));
|
||||
QObject::connect(&mainObject.micro, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
|
||||
QObject::connect(&w, &MainWindow::sigBrodcast, &mainObject, &MainObject::sendJson);
|
||||
if(!mainObject.micro.connected()) w.changeHeaderLableText("No io debug only!");
|
||||
|
||||
w.show();
|
||||
|
||||
//micro.requestState();
|
||||
int retVal = a.exec();
|
||||
|
||||
if(settings)
|
||||
{
|
||||
items.store("Items", settings);
|
||||
powerItem.store("Power/", settings);
|
||||
settings->sync();
|
||||
}
|
||||
else
|
||||
{
|
||||
QJsonObject itemsJsonObject;
|
||||
items.store(itemsJsonObject);
|
||||
QJsonObject powerObject;
|
||||
powerItem.store(powerObject);
|
||||
itemsJsonObject.insert("Power", powerObject);
|
||||
jsonDocument->setObject(itemsJsonObject);
|
||||
saveJsonDocument(jsonDocument, parser.isSet(settingsPathOption), parser.value(settingsPathOption));
|
||||
}
|
||||
if(settings) delete settings;
|
||||
else delete jsonDocument;
|
||||
if(masterIODevice) delete masterIODevice;
|
||||
return retVal;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue