Migrate plugintimer to libguh-core

This commit is contained in:
Simon Stürz 2017-12-19 12:02:54 +01:00 committed by Michael Zanetti
parent 81c6dc77e7
commit 967d3fe5ff
14 changed files with 579 additions and 383 deletions

View File

@ -432,8 +432,11 @@ void GuhCore::init() {
qCDebug(dcApplication) << "Creating Log Engine";
m_logger = new LogEngine(GuhSettings::logPath(), this);
qCDebug(dcApplication) << "Creating Hardware Manager";
m_hardwareManager = new HardwareManagerImplementation(this);
qCDebug(dcApplication) << "Creating Device Manager (locale:" << m_configuration->locale() << ")";
m_deviceManager = new DeviceManager(m_configuration->locale(), this);
m_deviceManager = new DeviceManager(m_hardwareManager, m_configuration->locale(), this);
qCDebug(dcApplication) << "Creating Rule Engine";
m_ruleEngine = new RuleEngine(this);

View File

@ -36,6 +36,7 @@
#include "cloudmanager.h"
#include "time/timemanager.h"
#include "hardwaremanagerimplementation.h"
#include <QObject>
@ -113,6 +114,7 @@ private:
LogEngine *m_logger;
TimeManager *m_timeManager;
CloudManager *m_cloudManager;
HardwareManagerImplementation *m_hardwareManager;
NetworkManager *m_networkManager;
UserManager *m_userManager;

View File

@ -0,0 +1,207 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "plugintimermanagerimplementation.h"
#include "loggingcategories.h"
namespace guhserver {
PluginTimerImplementation::PluginTimerImplementation(int intervall, QObject *parent) :
PluginTimer(parent),
m_interval(intervall)
{
}
int PluginTimerImplementation::interval() const
{
return m_interval;
}
int PluginTimerImplementation::currentTick() const
{
return m_currentTick;
}
bool PluginTimerImplementation::running() const
{
return m_running;
}
void PluginTimerImplementation::setRunning(const bool &running)
{
if (m_running != running) {
m_running = running;
emit runningChanged(m_running);
}
}
void PluginTimerImplementation::setPaused(const bool &paused)
{
if (m_paused != paused) {
m_paused = paused;
emit pausedChanged(m_paused);
}
}
void PluginTimerImplementation::setCurrentTick(const int &tick)
{
if (m_currentTick != tick) {
m_currentTick = tick;
emit currentTickChanged(m_currentTick);
}
}
void PluginTimerImplementation::tick()
{
if (m_paused)
return;
if (!m_running)
return;
setCurrentTick(m_currentTick += 1);
if (m_currentTick >= m_interval) {
emit timeout();
reset();
}
}
void PluginTimerImplementation::reset()
{
setCurrentTick(0);
}
void PluginTimerImplementation::start()
{
setPaused(false);
setRunning(true);
}
void PluginTimerImplementation::stop()
{
setPaused(false);
setRunning(false);
}
void PluginTimerImplementation::pause()
{
m_paused = true;
}
void PluginTimerImplementation::resume()
{
m_paused = false;
}
PluginTimerManagerImplementation::PluginTimerManagerImplementation(QObject *parent) :
PluginTimerManager(parent)
{
setAvailable(true);
qCDebug(dcHardware()) << "-->" << name() << "created successfully.";
}
PluginTimer *PluginTimerManagerImplementation::registerTimer(int seconds)
{
QPointer<PluginTimerImplementation> pluginTimer = new PluginTimerImplementation(seconds, this);
qCDebug(dcHardware()) << "Register timer" << pluginTimer->interval();
// TODO: schedule timer for load balancing
// // Get min/max timer interval
// PluginTimer *shortestTimer = nullptr;
// PluginTimer *longestTimer = nullptr;
// foreach (PluginTimer *timer, m_timers) {
// if (!shortestTimer && !longestTimer) {
// shortestTimer = timer;
// longestTimer = timer;
// continue;
// }
// if (timer->interval() < shortestTimer->interval()) {
// shortestTimer = timer;
// continue;
// }
// if (timer->interval() > longestTimer->interval()) {
// longestTimer = timer;
// continue;
// }
// }
m_timers.append(pluginTimer);
return pluginTimer.data();
}
void PluginTimerManagerImplementation::unregisterTimer(PluginTimer *timer)
{
QPointer<PluginTimer> timerPointer(timer);
if (timerPointer.isNull()) {
qCWarning(dcHardware()) << name() << "Cannot unregister timer. Looks like the timer is already unregistered.";
return;
}
qCDebug(dcHardware()) << "Unregister timer" << timer->interval();
foreach (QPointer<PluginTimerImplementation> tPointer, m_timers) {
if (timerPointer.data() == tPointer.data()) {
m_timers.removeAll(tPointer);
tPointer->deleteLater();
}
}
}
void PluginTimerManagerImplementation::timeTick()
{
// If timer resource is not enabled do nothing
if (!enabled()) {
return;
}
foreach (PluginTimerImplementation *timer, m_timers) {
timer->tick();
}
}
bool PluginTimerManagerImplementation::enable()
{
if (!available())
return false;
setEnabled(true);
return true;
}
bool PluginTimerManagerImplementation::disable()
{
if (!available())
return false;
setEnabled(false);
return true;
}
}

View File

@ -0,0 +1,100 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef PLUGINTIMERIMPLEMENTATION_H
#define PLUGINTIMERIMPLEMENTATION_H
#include <QTimer>
#include <QObject>
#include <QPointer>
#include "plugintimer.h"
namespace guhserver {
class PluginTimerImplementation : public PluginTimer
{
Q_OBJECT
friend class PluginTimerManagerImplementation;
public:
explicit PluginTimerImplementation(int intervall, QObject *parent = nullptr);
int interval() const;
int currentTick() const;
bool running() const;
signals:
void timeout();
void currentTickChanged(const int &currentTick);
void runningChanged(const bool &running);
void pausedChanged(const bool &paused);
private:
int m_interval;
int m_currentTick = 0;
bool m_paused = false;
bool m_running = true;
void setRunning(const bool &running);
void setPaused(const bool &paused);
void setCurrentTick(const int &tick);
void tick();
public slots:
void reset();
void start();
void stop();
void pause();
void resume();
};
class PluginTimerManagerImplementation : public PluginTimerManager
{
Q_OBJECT
friend class HardwareManagerImplementation;
public:
explicit PluginTimerManagerImplementation(QObject *parent = nullptr);
PluginTimer *registerTimer(int seconds = 60);
void unregisterTimer(PluginTimer *timer = nullptr);
private:
QList<QPointer<PluginTimerImplementation> > m_timers;
void timeTick();
public slots:
bool enable();
bool disable();
};
}
#endif // PLUGINTIMERIMPLEMENTATION_H

View File

@ -0,0 +1,139 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "hardwaremanagerimplementation.h"
#include "hardware/plugintimermanagerimplementation.h"
#include "plugintimer.h"
#include "loggingcategories.h"
#include "hardware/radio433/radio433.h"
#include "bluetooth/bluetoothlowenergymanager.h"
#include "network/networkaccessmanager.h"
#include "network/upnp/upnpdiscovery.h"
#include "network/upnp/upnpdevicedescriptor.h"
#include "network/avahi/qtavahiservicebrowser.h"
namespace guhserver {
HardwareManagerImplementation::HardwareManagerImplementation(QObject *parent) :
HardwareManager(parent)
{
// Init hardware resources
m_pluginTimerManager = new PluginTimerManagerImplementation(this);
m_pluginTimerManager->enable();
m_hardwareResources.append(m_pluginTimerManager);
m_radio433 = new Radio433(this);
m_hardwareResources.append(m_radio433);
m_radio433->enable();
// Create network access manager for all resources, centralized
// Note: configuration and proxy settings could be implemented here
m_networkAccessManager = new QNetworkAccessManager(this);
// Network manager
m_networkManager = new NetworkAccessManager(m_networkAccessManager, this);
m_hardwareResources.append(m_networkManager);
if (m_networkManager->available())
m_networkManager->enable();
// UPnP discovery
m_upnpDiscovery = new UpnpDiscovery(m_networkAccessManager, this);
m_hardwareResources.append(m_upnpDiscovery);
m_upnpDiscovery->enable();
// Avahi Browser
m_avahiBrowser = new QtAvahiServiceBrowser(this);
m_hardwareResources.append(m_avahiBrowser);
m_avahiBrowser->enable();
// Bluetooth LE
m_bluetoothLowEnergyManager = new BluetoothLowEnergyManager(m_pluginTimerManager->registerTimer(10), this);
m_hardwareResources.append(m_bluetoothLowEnergyManager);
if (m_networkManager->available())
m_networkManager->enable();
qCDebug(dcHardware()) << "Hardware manager initialized successfully";
// Register D-Bus interface for enable/disable hardware resources
bool status = QDBusConnection::systemBus().registerService("io.guh.nymead");
if (!status) {
qCWarning(dcHardware()) << "Failed to register HardwareManager D-Bus service. HardwareManager D-Bus control will not work.";
return;
}
status = QDBusConnection::systemBus().registerObject("/io/guh/nymead/HardwareManager", this, QDBusConnection::ExportScriptableContents);
if (!status) {
qCWarning(dcHardware()) << "Failed to register HardwareManager D-Bus object. HardwareManager D-Bus control will not work.";
return;
}
qCDebug(dcHardware()) << "HardwareManager D-Bus service set up.";
}
Radio433 *HardwareManagerImplementation::radio433()
{
return m_radio433;
}
PluginTimerManager *HardwareManagerImplementation::pluginTimerManager()
{
return m_pluginTimerManager;
}
NetworkAccessManager *HardwareManagerImplementation::networkManager()
{
return m_networkManager;
}
UpnpDiscovery *HardwareManagerImplementation::upnpDiscovery()
{
return m_upnpDiscovery;
}
QtAvahiServiceBrowser *HardwareManagerImplementation::avahiBrowser()
{
return m_avahiBrowser;
}
BluetoothLowEnergyManager *HardwareManagerImplementation::bluetoothLowEnergyManager()
{
return m_bluetoothLowEnergyManager;
}
void HardwareManagerImplementation::EnableBluetooth(const bool &enabled)
{
qCDebug(dcHardware()) << "Bluetooth hardware resource" << (enabled ? "enabled" : "disabled");
if (enabled) {
m_bluetoothLowEnergyManager->enable();
} else {
m_bluetoothLowEnergyManager->disable();
}
}
void HardwareManagerImplementation::timeTick()
{
m_pluginTimerManager->timeTick();
}
}

View File

@ -0,0 +1,81 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* *
* Copyright (C) 2017 Simon Stürz <simon.stuerz@guh.io> *
* *
* This file is part of guh. *
* *
* This library is free software; you can redistribute it and/or *
* modify it under the terms of the GNU Lesser General Public *
* License as published by the Free Software Foundation; either *
* version 2.1 of the License, or (at your option) any later version. *
* *
* This library is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library; If not, see *
* <http://www.gnu.org/licenses/>. *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifndef HARDWAREMANAGERIMPLEMENTATION_H
#define HARDWAREMANAGERIMPLEMENTATION_H
#include <QObject>
#include <QDBusConnection>
#include <QNetworkAccessManager>
#include "hardwaremanager.h"
class Radio433;
class PluginTimer;
class UpnpDiscovery;
class PluginTimerManagerImplementation;
class NetworkAccessManager;
class UpnpDeviceDescriptor;
class QtAvahiServiceBrowser;
class BluetoothLowEnergyManager;
namespace guhserver {
class HardwareManagerImplementation : public HardwareManager
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "io.guh.nymead")
public:
explicit HardwareManagerImplementation(QObject *parent = nullptr);
Radio433 *radio433();
PluginTimerManager *pluginTimerManager();
NetworkAccessManager *networkManager();
UpnpDiscovery *upnpDiscovery();
QtAvahiServiceBrowser *avahiBrowser();
BluetoothLowEnergyManager *bluetoothLowEnergyManager();
// D-Bus method for enable/disable bluetooth support
Q_SCRIPTABLE void EnableBluetooth(const bool &enabled);
private:
QNetworkAccessManager *m_networkAccessManager;
// Hardware Resources
PluginTimerManagerImplementation *m_pluginTimerManager = nullptr;
Radio433 *m_radio433 = nullptr;
NetworkAccessManager *m_networkManager = nullptr;
UpnpDiscovery *m_upnpDiscovery = nullptr;
QtAvahiServiceBrowser *m_avahiBrowser = nullptr;
BluetoothLowEnergyManager *m_bluetoothLowEnergyManager = nullptr;
bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType);
bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType);
void timeTick();
};
}
#endif // HARDWAREMANAGERIMPLEMENTATION_H

View File

@ -33,9 +33,9 @@
#include <QObject>
#include <QVariantMap>
#include <QString>
#include <QSslConfiguration>
class Device;
class QSslConfiguration;
namespace guhserver {

View File

@ -76,7 +76,9 @@ HEADERS += guhcore.h \
MbedTLS/MbedTLSConnection.hpp \
janusconnector.h \
pushbuttondbusservice.h \
guhdbusservice.h
guhdbusservice.h \
hardwaremanagerimplementation.h \
hardware/plugintimermanagerimplementation.h \
SOURCES += guhcore.cpp \
@ -136,4 +138,6 @@ SOURCES += guhcore.cpp \
MbedTLS/MbedTLSConnection.cpp \
janusconnector.cpp \
pushbuttondbusservice.cpp \
guhdbusservice.cpp
guhdbusservice.cpp \
hardwaremanagerimplementation.cpp \
hardware/plugintimermanagerimplementation.cpp \

View File

@ -198,15 +198,14 @@
/*! Constructs the DeviceManager with the given \a locale and \a parent. There should only be one DeviceManager in the system created by \l{guhserver::GuhCore}.
* Use \c guhserver::GuhCore::instance()->deviceManager() instead to access the DeviceManager. */
DeviceManager::DeviceManager(const QLocale &locale, QObject *parent) :
DeviceManager::DeviceManager(HardwareManager *hardwareManager, const QLocale &locale, QObject *parent) :
QObject(parent),
m_hardwareManager(hardwareManager),
m_locale(locale)
{
qRegisterMetaType<DeviceClassId>();
qRegisterMetaType<DeviceDescriptor>();
m_hardwareManager = new HardwareManager(this);
// Give hardware a chance to start up before loading plugins etc.
QMetaObject::invokeMethod(this, "loadPlugins", Qt::QueuedConnection);
QMetaObject::invokeMethod(this, "loadConfiguredDevices", Qt::QueuedConnection);
@ -973,7 +972,7 @@ DeviceManager::DeviceError DeviceManager::executeAction(const Action &action)
/*! Centralized time tick for the GuhTimer resource. Ticks every second. */
void DeviceManager::timeTick()
{
m_hardwareManager->timeTick();
}
void DeviceManager::loadPlugins()

View File

@ -89,7 +89,7 @@ public:
};
Q_ENUM(DeviceSetupStatus)
explicit DeviceManager(const QLocale &locale, QObject *parent = nullptr);
explicit DeviceManager(HardwareManager *hardwareManager, const QLocale &locale, QObject *parent = nullptr);
~DeviceManager();
static QStringList pluginSearchDirs();
@ -177,6 +177,8 @@ private:
private:
HardwareManager *m_hardwareManager;
QLocale m_locale;
QHash<VendorId, Vendor> m_supportedVendors;
QHash<VendorId, QList<DeviceClassId> > m_vendorDeviceMap;
@ -186,8 +188,6 @@ private:
QHash<PluginId, DevicePlugin*> m_devicePlugins;
HardwareManager *m_hardwareManager;
QHash<QUuid, DevicePairingInfo> m_pairingsJustAdd;
QHash<QUuid, DevicePairingInfo> m_pairingsDiscovery;

View File

@ -21,97 +21,11 @@
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "hardwaremanager.h"
#include "plugintimer.h"
#include "loggingcategories.h"
#include "hardware/radio433/radio433.h"
#include "bluetooth/bluetoothlowenergymanager.h"
#include "network/networkaccessmanager.h"
#include "network/upnp/upnpdiscovery.h"
#include "network/upnp/upnpdevicedescriptor.h"
#include "network/avahi/qtavahiservicebrowser.h"
HardwareManager::HardwareManager(QObject *parent) : QObject(parent)
HardwareManager::HardwareManager(QObject *parent) :
QObject(parent)
{
// Init hardware resources
m_pluginTimerManager = new PluginTimerManager(this);
m_pluginTimerManager->enable();
m_hardwareResources.append(m_pluginTimerManager);
m_radio433 = new Radio433(this);
m_hardwareResources.append(m_radio433);
m_radio433->enable();
// Create network access manager for all resources, centralized
// Note: configuration and proxy settings could be implemented here
m_networkAccessManager = new QNetworkAccessManager(this);
// Network manager
m_networkManager = new NetworkAccessManager(m_networkAccessManager, this);
m_hardwareResources.append(m_networkManager);
if (m_networkManager->available())
m_networkManager->enable();
// UPnP discovery
m_upnpDiscovery = new UpnpDiscovery(m_networkAccessManager, this);
m_hardwareResources.append(m_upnpDiscovery);
m_upnpDiscovery->enable();
// Avahi Browser
m_avahiBrowser = new QtAvahiServiceBrowser(this);
m_hardwareResources.append(m_avahiBrowser);
m_avahiBrowser->enable();
// Bluetooth LE
m_bluetoothLowEnergyManager = new BluetoothLowEnergyManager(m_pluginTimerManager->registerTimer(10), this);
m_hardwareResources.append(m_bluetoothLowEnergyManager);
if (m_networkManager->available())
m_networkManager->enable();
qCDebug(dcHardware()) << "Hardware manager initialized successfully";
// Register D-Bus interface for enable/disable hardware resources
bool status = QDBusConnection::systemBus().registerService("io.guh.nymead");
if (!status) {
qCWarning(dcHardware()) << "Failed to register HardwareManager D-Bus service. HardwareManager D-Bus control will not work.";
return;
}
status = QDBusConnection::systemBus().registerObject("/io/guh/nymead/HardwareManager", this, QDBusConnection::ExportScriptableContents);
if (!status) {
qCWarning(dcHardware()) << "Failed to register HardwareManager D-Bus object. HardwareManager D-Bus control will not work.";
return;
}
qCDebug(dcHardware()) << "HardwareManager D-Bus service set up.";
}
Radio433 *HardwareManager::radio433()
{
return m_radio433;
}
PluginTimerManager *HardwareManager::pluginTimerManager()
{
return m_pluginTimerManager;
}
NetworkAccessManager *HardwareManager::networkManager()
{
return m_networkManager;
}
UpnpDiscovery *HardwareManager::upnpDiscovery()
{
return m_upnpDiscovery;
}
QtAvahiServiceBrowser *HardwareManager::avahiBrowser()
{
return m_avahiBrowser;
}
BluetoothLowEnergyManager *HardwareManager::bluetoothLowEnergyManager()
{
return m_bluetoothLowEnergyManager;
}
bool HardwareManager::isAvailable(const HardwareResource::Type &hardwareResourceType) const
@ -134,40 +48,3 @@ bool HardwareManager::isEnabled(const HardwareResource::Type &hardwareResourceTy
return false;
}
void HardwareManager::EnableBluetooth(const bool &enabled)
{
qCDebug(dcHardware()) << "Bluetooth hardware resource" << (enabled ? "enabled" : "disabled");
if (enabled) {
m_bluetoothLowEnergyManager->enable();
} else {
m_bluetoothLowEnergyManager->disable();
}
}
bool HardwareManager::enableHardwareReource(const HardwareResource::Type &hardwareResourceType)
{
foreach (HardwareResource *resource, m_hardwareResources) {
if (resource->hardwareReourceType() == hardwareResourceType) {
return resource->enable();
}
}
return false;
}
bool HardwareManager::disableHardwareReource(const HardwareResource::Type &hardwareResourceType)
{
foreach (HardwareResource *resource, m_hardwareResources) {
if (resource->hardwareReourceType() == hardwareResourceType) {
return resource->disable();
}
}
return false;
}
void HardwareManager::timeTick()
{
m_pluginTimerManager->timeTick();
}

View File

@ -24,65 +24,42 @@
#define HARDWAREMANAGER_H
#include <QObject>
#include <QDBusConnection>
#include <QNetworkAccessManager>
#include "hardwareresource.h"
class PluginTimerManager;
class BluetoothLowEnergyManager;
class Radio433;
class NetworkAccessManager;
class UpnpDiscovery;
class PluginTimerManager;
class NetworkAccessManager;
class UpnpDeviceDescriptor;
class QtAvahiServiceBrowser;
class BluetoothLowEnergyManager;
class HardwareManager : public QObject
{
Q_OBJECT
Q_CLASSINFO("D-Bus Interface", "io.guh.nymead")
friend class DeviceManager;
public:
explicit HardwareManager(QObject *parent = nullptr);
HardwareManager(QObject *parent = nullptr);
virtual ~HardwareManager() = default;
Radio433 *radio433();
PluginTimerManager *pluginTimerManager();
NetworkAccessManager *networkManager();
UpnpDiscovery *upnpDiscovery();
QtAvahiServiceBrowser *avahiBrowser();
BluetoothLowEnergyManager *bluetoothLowEnergyManager();
virtual Radio433 *radio433() = 0;
virtual PluginTimerManager *pluginTimerManager() = 0;
virtual NetworkAccessManager *networkManager() = 0;
virtual UpnpDiscovery *upnpDiscovery() = 0;
virtual QtAvahiServiceBrowser *avahiBrowser() = 0;
virtual BluetoothLowEnergyManager *bluetoothLowEnergyManager() = 0;
bool isAvailable(const HardwareResource::Type &hardwareResourceType) const;
bool isEnabled(const HardwareResource::Type &hardwareResourceType) const;
// D-Bus method for enable/disable bluetooth support
Q_SCRIPTABLE void EnableBluetooth(const bool &enabled);
private:
protected:
QList<HardwareResource *> m_hardwareResources;
QNetworkAccessManager *m_networkAccessManager;
// Hardware Resources
Radio433 *m_radio433 = nullptr;
PluginTimerManager *m_pluginTimerManager = nullptr;
NetworkAccessManager *m_networkManager = nullptr;
UpnpDiscovery *m_upnpDiscovery = nullptr;
QtAvahiServiceBrowser *m_avahiBrowser = nullptr;
BluetoothLowEnergyManager *m_bluetoothLowEnergyManager = nullptr;
bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType);
bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType);
void timeTick();
signals:
void hardwareResourceAvailableChanged(const HardwareResource::Type &hardwareResourceType, const bool &available);
void hardwareResourceEnabledChanged(const HardwareResource::Type &hardwareResourceType, const bool &enabled);
};
#endif // HARDWAREMANAGER_H

View File

@ -23,183 +23,14 @@
#include "plugintimer.h"
#include "loggingcategories.h"
int PluginTimer::interval() const
{
return m_interval;
}
void PluginTimer::pause()
{
m_paused = true;
}
void PluginTimer::resume()
{
m_paused = false;
}
int PluginTimer::currentTick() const
{
return m_currentTick;
}
bool PluginTimer::running() const
{
return m_running;
}
PluginTimer::PluginTimer(int intervall, QObject *parent) :
QObject(parent),
m_interval(intervall)
{
}
void PluginTimer::setRunning(const bool &running)
{
if (m_running != running) {
m_running = running;
emit runningChanged(m_running);
}
}
void PluginTimer::setPaused(const bool &paused)
{
if (m_paused != paused) {
m_paused = paused;
emit pausedChanged(m_paused);
}
}
void PluginTimer::setCurrentTick(const int &tick)
{
if (m_currentTick != tick) {
m_currentTick = tick;
emit currentTickChanged(m_currentTick);
}
}
void PluginTimer::tick()
{
if (m_paused)
return;
if (!m_running)
return;
setCurrentTick(m_currentTick += 1);
if (m_currentTick >= m_interval) {
emit timeout();
reset();
}
}
void PluginTimer::reset()
{
setCurrentTick(0);
}
void PluginTimer::start()
{
setPaused(false);
setRunning(true);
}
void PluginTimer::stop()
{
setPaused(false);
setRunning(false);
}
PluginTimer *PluginTimerManager::registerTimer(int seconds)
{
QPointer<PluginTimer> pluginTimer = new PluginTimer(seconds, this);
qCDebug(dcHardware()) << "Register timer" << pluginTimer->interval();
// TODO: schedule timer for load balancing
// // Get min/max timer interval
// PluginTimer *shortestTimer = nullptr;
// PluginTimer *longestTimer = nullptr;
// foreach (PluginTimer *timer, m_timers) {
// if (!shortestTimer && !longestTimer) {
// shortestTimer = timer;
// longestTimer = timer;
// continue;
// }
// if (timer->interval() < shortestTimer->interval()) {
// shortestTimer = timer;
// continue;
// }
// if (timer->interval() > longestTimer->interval()) {
// longestTimer = timer;
// continue;
// }
// }
m_timers.append(pluginTimer);
return pluginTimer.data();
}
void PluginTimerManager::unregisterTimer(PluginTimer *timer)
{
QPointer<PluginTimer> timerPointer(timer);
if (timerPointer.isNull()) {
qCWarning(dcHardware()) << name() << "Cannot unregister timer. Looks like the timer is already unregistered.";
return;
}
qCDebug(dcHardware()) << "Unregister timer" << timer->interval();
foreach (QPointer<PluginTimer> tPointer, m_timers) {
if (timerPointer.data() == tPointer.data()) {
m_timers.removeAll(tPointer);
tPointer->deleteLater();
}
}
}
PluginTimerManager::PluginTimerManager(QObject *parent) :
HardwareResource(HardwareResource::TypeTimer, "Plugin timer manager", parent)
HardwareResource(HardwareResource::TypeTimer, "PluginTimerManager", parent)
{
setAvailable(true);
qCDebug(dcHardware()) << "-->" << name() << "created successfully.";
}
void PluginTimerManager::timeTick()
PluginTimer::PluginTimer(QObject *parent) :
QObject(parent)
{
// If timer resource is not enabled do nothing
if (!enabled()) {
return;
}
foreach (PluginTimer *timer, m_timers) {
timer->tick();
}
}
bool PluginTimerManager::enable()
{
if (!available())
return false;
setEnabled(true);
return true;
}
bool PluginTimerManager::disable()
{
if (!available())
return false;
setEnabled(false);
return true;
}

View File

@ -33,41 +33,27 @@ class PluginTimer : public QObject
{
Q_OBJECT
friend class PluginTimerManager;
public:
int interval() const;
PluginTimer(QObject *parent = nullptr);
virtual ~PluginTimer() = default;
void pause();
void resume();
int currentTick() const;
bool running() const;
virtual int interval() const = 0;
virtual int currentTick() const = 0;
virtual bool running() const = 0;
signals:
void timeout();
void currentTickChanged(const int &currentTick);
void runningChanged(const bool &running);
void pausedChanged(const bool &paused);
private:
explicit PluginTimer(int intervall, QObject *parent = nullptr);
int m_interval;
int m_currentTick = 0;
bool m_paused = false;
bool m_running = true;
void setRunning(const bool &running);
void setPaused(const bool &paused);
void setCurrentTick(const int &tick);
void tick();
public slots:
void reset();
void start();
void stop();
virtual void reset() = 0;
virtual void start() = 0;
virtual void stop() = 0;
virtual void pause() = 0;
virtual void resume() = 0;
};
@ -76,22 +62,12 @@ class PluginTimerManager : public HardwareResource
{
Q_OBJECT
friend class HardwareManager;
public:
PluginTimer *registerTimer(int seconds = 60);
void unregisterTimer(PluginTimer *timer = nullptr);
private:
explicit PluginTimerManager(QObject *parent = nullptr);
QList<QPointer<PluginTimer> > m_timers;
void timeTick();
public slots:
bool enable();
bool disable();
PluginTimerManager(QObject *parent = nullptr);
virtual ~PluginTimerManager() = default;
virtual PluginTimer *registerTimer(int seconds = 60) = 0;
virtual void unregisterTimer(PluginTimer *timer = nullptr) = 0;
};
#endif // PLUGINTIMER_H