switched from qsettings to json added editng of actors

This commit is contained in:
Carl Klemm 2019-06-06 21:19:12 +02:00
parent b04fbfb5bc
commit df27b622a0
141 changed files with 4402 additions and 5068 deletions

View file

@ -6,21 +6,118 @@
#include <QSettings>
#include <QSignalMapper>
#include <QTcpSocket>
#include <QFileInfo>
#include <QJsonDocument>
#include <QStandardPaths>
#ifndef Q_OS_ANDROID
#include <QtSerialPort/QtSerialPort>
#include <QtSerialPort/QSerialPortInfo>
#include <QCommandLineParser>
#endif
#include "alarmtime.h"
#include "actors/alarmtime.h"
#include "microcontroller.h"
#include "mainwindow.h"
#include "ampmanager.h"
#include "ui/mainwindow.h"
#include "sensors/speakersensor.h"
#include "sensors/sunsensor.h"
#include "sensors/ocupancysensor.h"
#include "alarmactions.h"
#include "alarmsettingsdialog.h"
#include "sensors/sensor.h"
#include "items/itemstore.h"
#include "items/auxitem.h"
#include "items/rgbitem.h"
#include "items/poweritem.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);
@ -29,11 +126,12 @@ int main(int argc, char *argv[])
QCoreApplication::setOrganizationName("UVOS");
QCoreApplication::setOrganizationDomain("uvos.xyz");
QCoreApplication::setApplicationName("SHinterface");
QCoreApplication::setApplicationVersion("0.5");
QCoreApplication::setApplicationVersion("0.6");
QDir::setCurrent(a.applicationDirPath());
//parse comand line
#ifndef Q_OS_ANDROID
QCommandLineParser parser;
parser.setApplicationDescription("Smart Home Interface");
parser.addHelpOption();
@ -48,14 +146,25 @@ int main(int argc, char *argv[])
parser.addOption(serialOption);
QCommandLineOption baudOption(QStringList() << "b" << "baud", QCoreApplication::translate("main", "Set Baud Rate"));
parser.addOption(baudOption);
QCommandLineOption settingsPathOption(QStringList() << "c" << "config", QCoreApplication::translate("main", "Set config file"), "configFilePath");
parser.addOption(settingsPathOption);
QCommandLineOption secondaryOption(QStringList() << "e" << "secondary", QCoreApplication::translate("main", "Set if instance is not main instance"));
parser.addOption(secondaryOption);
QCommandLineOption qsettingsOption(QStringList() << "q" << "qsettigns", QCoreApplication::translate("main", "Set if jsettings file format should be used."));
parser.addOption(secondaryOption);
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));
QSettings settings;
//connect to microcontoler
Microcontroller micro;
#ifndef Q_OS_ANDROID
if(parser.isSet(tcpOption))
{
QTcpSocket* microSocket = new QTcpSocket;
@ -86,55 +195,95 @@ int main(int argc, char *argv[])
if(!microPort->open(QIODevice::ReadWrite)) std::cout<<"Can not open serial port "<<microPort->portName().toStdString()<<". Continueing in demo mode"<<'\n';
else micro.setIODevice(microPort);
}
#else
QTcpSocket* microSocket = new QTcpSocket;
microSocket->connectToHost("10.0.0.1", 6856, QIODevice::ReadWrite);
if(!microSocket->waitForConnected(1000))
{
std::cout<<"Can not connect to to Server.\n";
return 1;
}
micro.setIODevice(microSocket);
#endif
AlarmActions alarmActions(&settings, &micro);
AmpManager amp(&micro, 0);
AlarmActions alarmActions(&a, &micro);
//Alarms
AlarmTime almNight(settings.value("nightTime").toTime());
AlarmTime almAlarm(settings.value("alarmTime").toTime());
//sensors
SpeakerSensorSource livingroomSpeakerSensorSource("Livingroom Speakers");
SunSensorSource sunSensorSource(49.884450, 8.650536);
OcupancySensorSource ocupancySensor;
SensorStore sensors;
QObject::connect(&micro, &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(&micro, &Microcontroller::gotRelayList, &items, &ItemStore::addItems);
QObject::connect(&micro, &Microcontroller::itemChanged, &items, &ItemStore::itemStateChanged);
//Power Item
PowerItem powerItem(&sensors, &a, 5487423, "Power");
QObject::connect(&powerItem, &PowerItem::stateChanged, &sensors, &SensorStore::sensorGotState);
powerItem.emmitSensor();
//mainwindow
MainWindow w(&micro, parser.isSet(secondaryOption));
MainWindow w(&micro, &powerItem, &items, &sensors);
QObject::connect(&micro, SIGNAL(textRecived(QString)), &w, SLOT(changeHeaderLableText(QString)));
QObject::connect(&micro, SIGNAL(relayStateChanged(std::vector<bool>)), &w, SLOT(relayStateChanged(std::vector<bool>)));
QObject::connect(&micro, SIGNAL(auxStateChanged(int)), &w, SLOT(auxStateChanged(int)));
if(!micro.connected()) w.changeHeaderLableText("No io debug only!");
//dialogs
AlarmSettingsDialog alarmDialog(&almNight, &almAlarm, &settings, &w);
//special items
std::shared_ptr<RgbItem> rgbItem(new RgbItem(&sensors, &micro, 5487422, "Rgb Lights"));
items.addItem(rgbItem);
if(!parser.isSet(secondaryOption))
{
QObject::connect(&almNight, SIGNAL(trigger()), &alarmActions, SLOT(syncoff()));
QObject::connect(&w, SIGNAL(signalAlmNightChanged(QTime)), &almNight, SLOT(changeTime(QTime)));
QObject::connect(&w, SIGNAL(signalAlmNightStateChanged(int)), &almNight, SLOT(runOrAbort(int)));
almNight.run();
QObject::connect(&almAlarm, &AlarmTime::trigger, &micro, &Microcontroller::startSunrise);
QObject::connect(&w, SIGNAL(signalAlmAlarmChanged(QTime)), &almAlarm, SLOT(changeTime(QTime)));
QObject::connect(&w, SIGNAL(signalAlmAlarmStateChanged(int)), &almAlarm, SLOT(runOrAbort(int)));
almAlarm.run();
//Amplifyer
QObject::connect(&w, SIGNAL(signalAmpOn()), &amp, SLOT(run()));
QObject::connect(&w, SIGNAL(signalAmpOff()), &amp, SLOT(abort()));
QMetaObject::invokeMethod(&amp, "run", Qt::QueuedConnection );
}
//show dialogs
QObject::connect(&w, &MainWindow::showAlmSettingsDialog, &alarmDialog, &AlarmSettingsDialog::show);
std::shared_ptr<AuxItem> auxItem(new AuxItem(&sensors, &micro, 5487421, "Desk Light"));
items.addItem(auxItem);
QMetaObject::invokeMethod(&micro, "run", Qt::QueuedConnection );
loadItemState(jsonDocument, settings, &micro, &items, &powerItem, rgbItem.get(), auxItem.get());
#ifndef Q_OS_ANDROID
if(!parser.isSet(secondaryOption))
{
Item::secondaryFlag = false;
}
else
{
Item::secondaryFlag = true;
}
#endif
w.show();
micro.requestRelayList();
micro.requestState();
//micro.requestState();
int retVal = a.exec();
micro.requestRelayList();
micro.requestState();
return 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;
return retVal;
}