diff --git a/libguh-core/guhcore.cpp b/libguh-core/guhcore.cpp index 94d5ee0d..86da9c6c 100644 --- a/libguh-core/guhcore.cpp +++ b/libguh-core/guhcore.cpp @@ -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); diff --git a/libguh-core/guhcore.h b/libguh-core/guhcore.h index 9ac34d84..12d06b26 100644 --- a/libguh-core/guhcore.h +++ b/libguh-core/guhcore.h @@ -36,6 +36,7 @@ #include "cloudmanager.h" #include "time/timemanager.h" +#include "hardwaremanagerimplementation.h" #include @@ -113,6 +114,7 @@ private: LogEngine *m_logger; TimeManager *m_timeManager; CloudManager *m_cloudManager; + HardwareManagerImplementation *m_hardwareManager; NetworkManager *m_networkManager; UserManager *m_userManager; diff --git a/libguh-core/hardware/plugintimermanagerimplementation.cpp b/libguh-core/hardware/plugintimermanagerimplementation.cpp new file mode 100644 index 00000000..ae17cfe1 --- /dev/null +++ b/libguh-core/hardware/plugintimermanagerimplementation.cpp @@ -0,0 +1,207 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * 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 * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#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 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 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 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; +} + +} + + diff --git a/libguh-core/hardware/plugintimermanagerimplementation.h b/libguh-core/hardware/plugintimermanagerimplementation.h new file mode 100644 index 00000000..18d74746 --- /dev/null +++ b/libguh-core/hardware/plugintimermanagerimplementation.h @@ -0,0 +1,100 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * 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 * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef PLUGINTIMERIMPLEMENTATION_H +#define PLUGINTIMERIMPLEMENTATION_H + +#include +#include +#include + +#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 ¤tTick); + 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 > m_timers; + void timeTick(); + +public slots: + bool enable(); + bool disable(); + +}; + +} + +#endif // PLUGINTIMERIMPLEMENTATION_H diff --git a/libguh-core/hardwaremanagerimplementation.cpp b/libguh-core/hardwaremanagerimplementation.cpp new file mode 100644 index 00000000..4e29091b --- /dev/null +++ b/libguh-core/hardwaremanagerimplementation.cpp @@ -0,0 +1,139 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * 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 * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#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(); +} + +} diff --git a/libguh-core/hardwaremanagerimplementation.h b/libguh-core/hardwaremanagerimplementation.h new file mode 100644 index 00000000..54ab44ce --- /dev/null +++ b/libguh-core/hardwaremanagerimplementation.h @@ -0,0 +1,81 @@ +/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + * * + * Copyright (C) 2017 Simon Stürz * + * * + * 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 * + * . * + * * + * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ + +#ifndef HARDWAREMANAGERIMPLEMENTATION_H +#define HARDWAREMANAGERIMPLEMENTATION_H + +#include +#include +#include + +#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 diff --git a/libguh-core/jsonrpc/jsonrpcserver.h b/libguh-core/jsonrpc/jsonrpcserver.h index 25a00908..184d9529 100644 --- a/libguh-core/jsonrpc/jsonrpcserver.h +++ b/libguh-core/jsonrpc/jsonrpcserver.h @@ -33,9 +33,9 @@ #include #include #include +#include class Device; -class QSslConfiguration; namespace guhserver { diff --git a/libguh-core/libguh-core.pro b/libguh-core/libguh-core.pro index a9d2371d..73bbebec 100644 --- a/libguh-core/libguh-core.pro +++ b/libguh-core/libguh-core.pro @@ -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 \ diff --git a/libguh/devicemanager.cpp b/libguh/devicemanager.cpp index 68102cfe..7842edc0 100644 --- a/libguh/devicemanager.cpp +++ b/libguh/devicemanager.cpp @@ -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(); qRegisterMetaType(); - 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() diff --git a/libguh/devicemanager.h b/libguh/devicemanager.h index 73bbae75..8e5f41d7 100644 --- a/libguh/devicemanager.h +++ b/libguh/devicemanager.h @@ -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 m_supportedVendors; QHash > m_vendorDeviceMap; @@ -186,8 +188,6 @@ private: QHash m_devicePlugins; - HardwareManager *m_hardwareManager; - QHash m_pairingsJustAdd; QHash m_pairingsDiscovery; diff --git a/libguh/hardwaremanager.cpp b/libguh/hardwaremanager.cpp index 8be0e148..a8042623 100644 --- a/libguh/hardwaremanager.cpp +++ b/libguh/hardwaremanager.cpp @@ -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(); -} - diff --git a/libguh/hardwaremanager.h b/libguh/hardwaremanager.h index 0feb5adf..e1a060c5 100644 --- a/libguh/hardwaremanager.h +++ b/libguh/hardwaremanager.h @@ -24,65 +24,42 @@ #define HARDWAREMANAGER_H #include -#include -#include #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 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 diff --git a/libguh/plugintimer.cpp b/libguh/plugintimer.cpp index 5f4b0540..29360b5d 100644 --- a/libguh/plugintimer.cpp +++ b/libguh/plugintimer.cpp @@ -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 = 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 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 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; -} - - diff --git a/libguh/plugintimer.h b/libguh/plugintimer.h index f920f7b8..8e695868 100644 --- a/libguh/plugintimer.h +++ b/libguh/plugintimer.h @@ -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 ¤tTick); 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 > 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