#ifndef ENERGYSAVER_H #define ENERGYSAVER_H #include #include #ifdef ANDROID #include #endif /** * @brief Class handling energy saving options. * * On Android devices, it locks the cpu shutdown while active. On other devices, it * will shut down device on timeout if the options are set accordingly and a shutdown script * is provided. * * In the context of the LenaPi application, it will prevent cpu shutdown on android devices while playing * music. On other devices, it will shutdown the device if no music has been playing and no mouse input was * detected for a certain time intervall. */ class EnergySaver : public QObject { Q_OBJECT protected: using QObject::QObject; public: ~EnergySaver(); /** * @brief Create instance if necessary. The instance will have auto shutdown disabeld. * * Used to prevent sleep on android devices. */ static void init(); /** * @brief Create instance if necessary, configure it and start timer. * @param enabledAutoShutdown Defines whether device will shutdown on inactivity using shutdownScript * @param interval Timer interval in seconds * @param shutdownScript Path to shutdown script file * @see EnergySaver::instance * * Instance is initialized with timeout interval and shutdown script * and started if the provided script exists. */ static void init(int interval, const QString& shutdownScript); /** * @brief Implements the singleton pattern. * @return Instance of EnergySaver * * If no instance has been created yet, create new instance. */ static EnergySaver *instance(); public slots: /** * @brief Deactivate energy saver, e.g., as music is currently playing * * Sets locks on adroid devics and stops shutdown-timer. */ void deactivate(); /** * @brief Active energy saver * * Releases locks on android devices and restarts shutdown-timer if shutdown option is set. */ void activate(); /** * @brief Restart shutdown timer, e.g. because of music player activity */ void restartTimer(); private: /** * @brief Initialize and connect timer. * @param interval Timeout interval in millisecond. */ void initTimer(int interval); void setShutdownScript(const QString& shutdownScript); /** * @brief Initializes locking or Android devices */ void initAdroidLocks(); /** * @brief Sets locks on Android devices to prevent sleep. * * Used to prevent cpu sleep as otherwise music will stop. */ void setAndroidLock(); /** * @brief Releases locks on Android devices to allow the device's cpu to go into sleep mode. */ void releaseAndroidLock(); QTimer mTimer; QString mShutdownScript; bool mIsActive{false}; bool mIsAutoShutDownEnabled{false}; #ifdef ANDROID /** * @brief Prevent energy saving for Android devices */ QAndroidJniObject m_wakeLock; #endif private slots: /** * @brief Behavior on timeout: shut down RaspberryPi. */ void onTimeout(); }; #endif // ENERGYSAVER_H