add QSettings for config parsing

This commit is contained in:
Anika Raemer 2021-09-25 15:11:02 +02:00
parent f9af4c662a
commit 893a2990af
5 changed files with 71 additions and 31 deletions

View file

@ -1,14 +1,19 @@
#include "EnergySaver.h" #include "EnergySaver.h"
#include <QDebug> #include <QDebug>
#include <QFileInfo>
#include <QProcess> #include <QProcess>
#include <iostream> #include <iostream>
void EnergySaver::init(int interval) void EnergySaver::init(int interval, const QString &shutdownScript)
{ {
EnergySaver* saver = instance(); QFileInfo script(shutdownScript);
saver->initTimer(interval*1000); if(script.exists()){
saver->restartTimer(); auto saver = instance();
saver->setShutdownScript(shutdownScript);
saver->initTimer(interval*1000);
saver->restartTimer();
}
} }
@ -39,13 +44,18 @@ void EnergySaver::initTimer(int interval)
mTimer.setSingleShot(true); mTimer.setSingleShot(true);
} }
void EnergySaver::setShutdownScript(const QString &shutdownScript)
{
mShutdownScript = shutdownScript;
}
void EnergySaver::onTimeout() void EnergySaver::onTimeout()
{ {
std::cout << "Shutting down."; std::cout << "Shutting down.";
#ifndef _DEBUG #ifndef _DEBUG
QProcess p; QProcess p;
p.start("/usr/local/sbin/do_shutdown.sh"); p.start(mShutdownScript);
p.waitForFinished(); p.waitForFinished();
#endif #endif
} }

View file

@ -20,18 +20,22 @@ class EnergySaver : public QObject
Q_OBJECT Q_OBJECT
protected: protected:
explicit EnergySaver(QObject *parent = nullptr) : QObject(parent) {}; explicit EnergySaver(QObject *parent = nullptr) : QObject(parent) {}
public: public:
/** /**
* @brief Create instance if necessary and set timeout interval. * @brief Create instance if necessary, configure it and start timer.
* @param interval Timer interval in seconds * @param interval Timer interval in seconds
* @param shutdownScript Path to shutdown script file
* @see EnergySaver::instance * @see EnergySaver::instance
*
* Instance is initialized with timeout interval and shutdown script
* and started if the provided script exists.
*/ */
static void init(int interval); static void init(int interval, const QString& shutdownScript);
/** /**
* @brief Implements the singleton pattern. * @brief Implements the singleton pattern.
* @return Instance * @return Instance of EnergySaver
* *
* If no instance has been created yet, create new instance. * If no instance has been created yet, create new instance.
*/ */
@ -49,7 +53,9 @@ private:
* @param interval Timeout interval in millisecond. * @param interval Timeout interval in millisecond.
*/ */
void initTimer(int interval); void initTimer(int interval);
void setShutdownScript(const QString& shutdownScript);
QTimer mTimer; QTimer mTimer;
QString mShutdownScript;
private slots: private slots:
/** /**

View file

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.8.2, 2021-03-14T14:48:57. --> <!-- Written by QtCreator 4.8.2, 2021-09-25T15:10:11. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
@ -294,8 +294,8 @@
<value type="int" key="PE.EnvironmentAspect.Base">2</value> <value type="int" key="PE.EnvironmentAspect.Base">2</value>
<valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/> <valuelist type="QVariantList" key="PE.EnvironmentAspect.Changes"/>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">LenaPi</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">LenaPi</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName"></value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">LenaPi2</value>
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/home/araemer/source/LenaPi/LenaPi/LenaPi.pro</value> <value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">Qt4ProjectManager.Qt4RunConfiguration:/home/ar/source/LenaPi/LenaPi/LenaPi.pro</value>
<value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">LenaPi.pro</value> <value type="QString" key="Qt4ProjectManager.Qt4RunConfiguration.ProFile">LenaPi.pro</value>
<value type="QString" key="RunConfiguration.Arguments"></value> <value type="QString" key="RunConfiguration.Arguments"></value>
<value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value> <value type="uint" key="RunConfiguration.QmlDebugServerPort">3768</value>

View file

@ -1,39 +1,46 @@
#include <QGuiApplication> #include <QGuiApplication>
#include <QQmlApplicationEngine> #include <QQmlApplicationEngine>
#include <QSettings>
#include "controllers/NavigationController.h" #include "controllers/NavigationController.h"
#include "MouseEventSpy.h" #include "MouseEventSpy.h"
#include "EnergySaver.h" #include "EnergySaver.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
/** /*
* @todo Add command line parser to specify a custom config file * @todo Add command line parser to specify a custom config file
* https://doc.qt.io/qt-5/qcommandlineparser.html * https://doc.qt.io/qt-5/qcommandlineparser.html
* Add config handling https://doc.qt.io/qt-5/qsettings.html#QSettings-4
* - set root path
* ui mode -> pass to NavigationController and store in UiStateModel
* - set mode
* -- RasPi -> no volumeControl or exit Button
* -- Desktop -> volumeControl and exit Button
* -- in future probably also MobileApp
* Energy saving options -> hand over to energy saver on initialization
* - enable energy saving
* - set energy saver timeout
* - set shutdown script path
*/ */
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
/* Read Settings */
QSettings settings("me", "LenaPi");
const auto rootPath = settings.value("rootPath", "/home/ar/source/lenaMusic/").toString(); // path to music files
const auto profile = settings.value("profile", "RasPiTouch").toString(); // known modes are "RasPiTouch" and "Desktop"
const auto isEnergySavingEnabled = settings.value("enableEnergySaver", true).toBool(); // enable/disable energy saver
const auto energySaverTimeout = settings.value("timeout", 60).toInt(); //timeout in seconds
const auto shutdownScript = settings.value("shutdownScript", "/usr/local/sbin/do_shutdown.sh").toString();
/* @todo Hand over profile to UiStateModel via NavigationController
* Add properties to UiStateModel for showing volume controls and close button.
* Set those properties according to profile and use them to hide/show elements in qml
*/
NavigationController navController; NavigationController navController;
navController.setContext(engine.rootContext()); navController.setContext(engine.rootContext());
navController.init("/home/ar/source/lenaMusic/"); navController.init(rootPath);
// install MouseEventSpy and energy saver used for auto if(isEnergySavingEnabled){
// shut down of RaspberryPi if not used for a predefined time. /* install MouseEventSpy and energy saver used for auto shut down of device
MouseEventSpy::init(); * if not used for a predefined time.
EnergySaver::init(10); */
QObject::connect(MouseEventSpy::instance(), &MouseEventSpy::mouseEventDetected, MouseEventSpy::init();
EnergySaver::instance(), &EnergySaver::restartTimer); EnergySaver::init(energySaverTimeout, shutdownScript);
QObject::connect(MouseEventSpy::instance(), &MouseEventSpy::mouseEventDetected,
EnergySaver::instance(), &EnergySaver::restartTimer);
}
// load GUI
engine.load(QUrl("qrc:/main.qml")); engine.load(QUrl("qrc:/main.qml"));

View file

@ -20,6 +20,23 @@ signals:
void sourceChanged(); void sourceChanged();
public: public:
/**
* @brief Provides the possibility to configure UI according to profile.
*/
enum EProfile : uint{
/**
* @brief Profile designed for running LenaPi as window manager on Raspberry Pi Touch
*
* Start fullscreen and do NOT show close button or volume controls
*/
Profile_RasPiTouch = 0,
/**
* @brief Profiled designed for running LenaPi as a desktop app.
*
* Start fullscreen, show close button and volume controls.
*/
Profile_Desktop
};
explicit UiStateModel(QObject *parent = nullptr); explicit UiStateModel(QObject *parent = nullptr);
QUrl getSource() const; QUrl getSource() const;