Add plugin timer manager
This commit is contained in:
parent
19a4223906
commit
f7cf13aba5
65
libguh/bluetooth/bluetoothdiscoveryreply.cpp
Normal file
65
libguh/bluetooth/bluetoothdiscoveryreply.cpp
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* *
|
||||||
|
* 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 "bluetoothdiscoveryreply.h"
|
||||||
|
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
|
bool BluetoothDiscoveryReply::isFinished() const
|
||||||
|
{
|
||||||
|
return m_finished;
|
||||||
|
}
|
||||||
|
|
||||||
|
BluetoothDiscoveryReply::BluetoothDiscoveryReplyError BluetoothDiscoveryReply::error() const
|
||||||
|
{
|
||||||
|
return m_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<QBluetoothDeviceInfo> BluetoothDiscoveryReply::discoveredDevices() const
|
||||||
|
{
|
||||||
|
return m_discoveredDevices;
|
||||||
|
}
|
||||||
|
|
||||||
|
BluetoothDiscoveryReply::BluetoothDiscoveryReply(QObject *parent) : QObject(parent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothDiscoveryReply::setError(const BluetoothDiscoveryReply::BluetoothDiscoveryReplyError &error)
|
||||||
|
{
|
||||||
|
m_error = error;
|
||||||
|
if (m_error != BluetoothDiscoveryReplyErrorNoError) {
|
||||||
|
emit errorOccured(m_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothDiscoveryReply::setDiscoveredDevices(const QList<QBluetoothDeviceInfo> &discoveredDevices)
|
||||||
|
{
|
||||||
|
m_discoveredDevices = discoveredDevices;
|
||||||
|
}
|
||||||
|
|
||||||
|
void BluetoothDiscoveryReply::setFinished()
|
||||||
|
{
|
||||||
|
m_finished = true;
|
||||||
|
// Note: this makes sure the finished signal will be processed in the next event loop
|
||||||
|
QTimer::singleShot(0, this, &BluetoothDiscoveryReply::finished);
|
||||||
|
}
|
||||||
65
libguh/bluetooth/bluetoothdiscoveryreply.h
Normal file
65
libguh/bluetooth/bluetoothdiscoveryreply.h
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* *
|
||||||
|
* 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 BLUETOOTHDISCOVERYREPLY_H
|
||||||
|
#define BLUETOOTHDISCOVERYREPLY_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QBluetoothDeviceInfo>
|
||||||
|
|
||||||
|
class BluetoothDiscoveryReply : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
friend class BluetoothLowEnergyManager;
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum BluetoothDiscoveryReplyError {
|
||||||
|
BluetoothDiscoveryReplyErrorNoError,
|
||||||
|
BluetoothDiscoveryReplyErrorNotAvailable,
|
||||||
|
BluetoothDiscoveryReplyErrorNotEnabled,
|
||||||
|
BluetoothDiscoveryReplyErrorBusy
|
||||||
|
};
|
||||||
|
Q_ENUM(BluetoothDiscoveryReplyError)
|
||||||
|
|
||||||
|
bool isFinished() const;
|
||||||
|
BluetoothDiscoveryReplyError error() const;
|
||||||
|
QList<QBluetoothDeviceInfo> discoveredDevices() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit BluetoothDiscoveryReply(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
bool m_finished = false;
|
||||||
|
BluetoothDiscoveryReplyError m_error = BluetoothDiscoveryReplyErrorNoError;
|
||||||
|
QList<QBluetoothDeviceInfo> m_discoveredDevices;
|
||||||
|
|
||||||
|
void setError(const BluetoothDiscoveryReplyError &error);
|
||||||
|
void setDiscoveredDevices(const QList<QBluetoothDeviceInfo> &discoveredDevices);
|
||||||
|
void setFinished();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void finished();
|
||||||
|
void errorOccured(const BluetoothDiscoveryReplyError &error);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // BLUETOOTHDISCOVERYREPLY_H
|
||||||
@ -23,33 +23,45 @@
|
|||||||
#include "bluetoothlowenergymanager.h"
|
#include "bluetoothlowenergymanager.h"
|
||||||
#include "loggingcategories.h"
|
#include "loggingcategories.h"
|
||||||
|
|
||||||
bool BluetoothLowEnergyManager::discoverDevices(QPointer<QObject> caller, const QString &callbackMethod)
|
BluetoothDiscoveryReply *BluetoothLowEnergyManager::discoverDevices(const int &interval)
|
||||||
{
|
{
|
||||||
|
// Create the reply for this discovery request
|
||||||
|
QPointer<BluetoothDiscoveryReply> reply = new BluetoothDiscoveryReply(this);
|
||||||
if (!available()) {
|
if (!available()) {
|
||||||
qCWarning(dcHardware()) << name() << "is not avilable.";
|
qCWarning(dcHardware()) << name() << "is not avilable.";
|
||||||
return false;
|
reply->setError(BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNotAvailable);
|
||||||
|
reply->setFinished();
|
||||||
|
return reply.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!enabled()) {
|
if (!enabled()) {
|
||||||
qCWarning(dcHardware()) << name() << "is not enabled.";
|
qCWarning(dcHardware()) << name() << "is not enabled.";
|
||||||
return false;
|
reply->setError(BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNotEnabled);
|
||||||
|
reply->setFinished();
|
||||||
|
return reply.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_timer->isActive()) {
|
if (!m_currentReply.isNull()) {
|
||||||
qCWarning(dcHardware()) << name() << "discovery already running.";
|
qCWarning(dcHardware()) << name() << "resource busy. There is already a discovery running.";
|
||||||
return false;
|
reply->setError(BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorBusy);
|
||||||
|
reply->setFinished();
|
||||||
|
return reply.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
qCDebug(dcHardware) << name() << "start discovering";
|
||||||
|
m_currentReply = reply;
|
||||||
m_discoveredDevices.clear();
|
m_discoveredDevices.clear();
|
||||||
m_currentReply.caller = caller;
|
|
||||||
m_currentReply.callbackMethod = callbackMethod;
|
|
||||||
|
|
||||||
// Start discovery on all adapters
|
// Start discovery on all adapters
|
||||||
qCDebug(dcHardware()) << name() << "Start bluetooth discovery";
|
qCDebug(dcHardware()) << name() << "Start bluetooth discovery";
|
||||||
foreach (QBluetoothDeviceDiscoveryAgent *discoveryAgent, m_bluetoothDiscoveryAgents) {
|
foreach (QBluetoothDeviceDiscoveryAgent *discoveryAgent, m_bluetoothDiscoveryAgents) {
|
||||||
discoveryAgent->start();
|
discoveryAgent->start();
|
||||||
}
|
}
|
||||||
return true;
|
|
||||||
|
// TODO: limit interval to prevent resource blocking from a plugin
|
||||||
|
m_timer->start(interval);
|
||||||
|
|
||||||
|
return reply.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothLowEnergyManager::BluetoothLowEnergyManager(QObject *parent) :
|
BluetoothLowEnergyManager::BluetoothLowEnergyManager(QObject *parent) :
|
||||||
@ -80,7 +92,6 @@ BluetoothLowEnergyManager::BluetoothLowEnergyManager(QObject *parent) :
|
|||||||
// Discovery timer
|
// Discovery timer
|
||||||
m_timer = new QTimer(this);
|
m_timer = new QTimer(this);
|
||||||
m_timer->setSingleShot(true);
|
m_timer->setSingleShot(true);
|
||||||
m_timer->setInterval(5000);
|
|
||||||
|
|
||||||
connect(m_timer, &QTimer::timeout, this, &BluetoothLowEnergyManager::discoveryTimeout);
|
connect(m_timer, &QTimer::timeout, this, &BluetoothLowEnergyManager::discoveryTimeout);
|
||||||
|
|
||||||
@ -90,21 +101,25 @@ BluetoothLowEnergyManager::BluetoothLowEnergyManager(QObject *parent) :
|
|||||||
|
|
||||||
void BluetoothLowEnergyManager::discoveryTimeout()
|
void BluetoothLowEnergyManager::discoveryTimeout()
|
||||||
{
|
{
|
||||||
// Start discovery on all adapters
|
// Stop discovery on all adapters
|
||||||
qCDebug(dcHardware()) << name() << "Stop bluetooth discovery";
|
qCDebug(dcHardware()) << name() << "Stop bluetooth discovery";
|
||||||
foreach (QBluetoothDeviceDiscoveryAgent *discoveryAgent, m_bluetoothDiscoveryAgents) {
|
foreach (QBluetoothDeviceDiscoveryAgent *discoveryAgent, m_bluetoothDiscoveryAgents) {
|
||||||
discoveryAgent->stop();
|
discoveryAgent->stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(dcHardware()) << name() << "Discovery finished. Found"<< m_discoveredDevices.count() << "Bluetooth devices.";
|
qCDebug(dcHardware()) << name() << "Discovery finished. Found" << m_discoveredDevices.count() << "bluetooth devices.";
|
||||||
|
|
||||||
if (m_currentReply.caller.isNull()) {
|
if (m_currentReply.isNull()) {
|
||||||
qCWarning(dcHardware()) << name() << "The reciver of the discovery request does not exist any more.";
|
qCWarning(dcHardware()) << name() << "Reply does not exist any more. Please don't delete the reply before it has finished.";
|
||||||
} else {
|
m_currentReply.clear();
|
||||||
// Invoke the method containing the discovered devicelist
|
return;
|
||||||
// FIXME: check the callback method paramters during compililation
|
|
||||||
QMetaObject::invokeMethod(m_currentReply.caller.data(), m_currentReply.callbackMethod.toLatin1().data(), Q_ARG(QList<QBluetoothDeviceInfo>, m_discoveredDevices));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
m_currentReply->setError(BluetoothDiscoveryReply::BluetoothDiscoveryReplyErrorNoError);
|
||||||
|
m_currentReply->setDiscoveredDevices(m_discoveredDevices);
|
||||||
|
m_currentReply->setFinished();
|
||||||
|
|
||||||
|
m_currentReply.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
void BluetoothLowEnergyManager::onDeviceDiscovered(const QBluetoothDeviceInfo &deviceInfo)
|
void BluetoothLowEnergyManager::onDeviceDiscovered(const QBluetoothDeviceInfo &deviceInfo)
|
||||||
|
|||||||
@ -31,12 +31,7 @@
|
|||||||
#include <QBluetoothDeviceDiscoveryAgent>
|
#include <QBluetoothDeviceDiscoveryAgent>
|
||||||
|
|
||||||
#include "hardwareresource.h"
|
#include "hardwareresource.h"
|
||||||
|
#include "bluetoothdiscoveryreply.h"
|
||||||
struct BluetoothDiscoveryReply
|
|
||||||
{
|
|
||||||
QPointer<QObject> caller;
|
|
||||||
QString callbackMethod;
|
|
||||||
};
|
|
||||||
|
|
||||||
class BluetoothLowEnergyManager : public HardwareResource
|
class BluetoothLowEnergyManager : public HardwareResource
|
||||||
{
|
{
|
||||||
@ -45,7 +40,7 @@ class BluetoothLowEnergyManager : public HardwareResource
|
|||||||
friend class HardwareManager;
|
friend class HardwareManager;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool discoverDevices(QPointer<QObject> caller = QPointer<QObject>(), const QString &callbackMethod = QString());
|
BluetoothDiscoveryReply *discoverDevices(const int &interval = 5000);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
explicit BluetoothLowEnergyManager(QObject *parent = nullptr);
|
explicit BluetoothLowEnergyManager(QObject *parent = nullptr);
|
||||||
@ -54,7 +49,7 @@ private:
|
|||||||
QList<QBluetoothDeviceDiscoveryAgent *> m_bluetoothDiscoveryAgents;
|
QList<QBluetoothDeviceDiscoveryAgent *> m_bluetoothDiscoveryAgents;
|
||||||
QList<QBluetoothDeviceInfo> m_discoveredDevices;
|
QList<QBluetoothDeviceInfo> m_discoveredDevices;
|
||||||
|
|
||||||
BluetoothDiscoveryReply m_currentReply;
|
QPointer<BluetoothDiscoveryReply> m_currentReply;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|
||||||
|
|||||||
@ -973,7 +973,7 @@ DeviceManager::DeviceError DeviceManager::executeAction(const Action &action)
|
|||||||
/*! Centralized time tick for the GuhTimer resource. Ticks every second. */
|
/*! Centralized time tick for the GuhTimer resource. Ticks every second. */
|
||||||
void DeviceManager::timeTick()
|
void DeviceManager::timeTick()
|
||||||
{
|
{
|
||||||
|
m_hardwareManager->timeTick();
|
||||||
}
|
}
|
||||||
|
|
||||||
void DeviceManager::loadPlugins()
|
void DeviceManager::loadPlugins()
|
||||||
|
|||||||
@ -25,6 +25,7 @@
|
|||||||
#include "plugintimer.h"
|
#include "plugintimer.h"
|
||||||
#include "loggingcategories.h"
|
#include "loggingcategories.h"
|
||||||
#include "hardware/radio433/radio433.h"
|
#include "hardware/radio433/radio433.h"
|
||||||
|
#include "bluetooth/bluetoothlowenergymanager.h"
|
||||||
#include "network/networkaccessmanager.h"
|
#include "network/networkaccessmanager.h"
|
||||||
#include "network/upnp/upnpdiscovery.h"
|
#include "network/upnp/upnpdiscovery.h"
|
||||||
#include "network/upnp/upnpdevicedescriptor.h"
|
#include "network/upnp/upnpdevicedescriptor.h"
|
||||||
@ -33,8 +34,9 @@
|
|||||||
HardwareManager::HardwareManager(QObject *parent) : QObject(parent)
|
HardwareManager::HardwareManager(QObject *parent) : QObject(parent)
|
||||||
{
|
{
|
||||||
// Init hardware resources
|
// Init hardware resources
|
||||||
m_pluginTimer = new PluginTimer(10000, this);
|
m_pluginTimerManager = new PluginTimerManager(this);
|
||||||
m_hardwareResources.append(m_pluginTimer);
|
m_pluginTimerManager->enable();
|
||||||
|
m_hardwareResources.append(m_pluginTimerManager);
|
||||||
|
|
||||||
m_radio433 = new Radio433(this);
|
m_radio433 = new Radio433(this);
|
||||||
m_hardwareResources.append(m_radio433);
|
m_hardwareResources.append(m_radio433);
|
||||||
@ -60,12 +62,11 @@ HardwareManager::HardwareManager(QObject *parent) : QObject(parent)
|
|||||||
m_hardwareResources.append(m_avahiBrowser);
|
m_hardwareResources.append(m_avahiBrowser);
|
||||||
m_avahiBrowser->enable();
|
m_avahiBrowser->enable();
|
||||||
|
|
||||||
// // Bluetooth LE
|
// Bluetooth LE
|
||||||
// m_bluetoothScanner = new BluetoothScanner(this);
|
m_bluetoothLowEnergyManager = new BluetoothLowEnergyManager(this);
|
||||||
// if (!m_bluetoothScanner->isAvailable()) {
|
m_hardwareResources.append(m_bluetoothLowEnergyManager);
|
||||||
// delete m_bluetoothScanner;
|
if (m_networkManager->available())
|
||||||
// m_bluetoothScanner = nullptr;
|
m_networkManager->enable();
|
||||||
// }
|
|
||||||
|
|
||||||
qCDebug(dcHardware()) << "Hardware manager initialized successfully";
|
qCDebug(dcHardware()) << "Hardware manager initialized successfully";
|
||||||
}
|
}
|
||||||
@ -75,9 +76,9 @@ Radio433 *HardwareManager::radio433()
|
|||||||
return m_radio433;
|
return m_radio433;
|
||||||
}
|
}
|
||||||
|
|
||||||
PluginTimer *HardwareManager::pluginTimer()
|
PluginTimerManager *HardwareManager::pluginTimerManager()
|
||||||
{
|
{
|
||||||
return m_pluginTimer;
|
return m_pluginTimerManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
NetworkAccessManager *HardwareManager::networkManager()
|
NetworkAccessManager *HardwareManager::networkManager()
|
||||||
@ -95,9 +96,9 @@ QtAvahiServiceBrowser *HardwareManager::avahiBrowser()
|
|||||||
return m_avahiBrowser;
|
return m_avahiBrowser;
|
||||||
}
|
}
|
||||||
|
|
||||||
BluetoothScanner *HardwareManager::bluetoothScanner()
|
BluetoothLowEnergyManager *HardwareManager::bluetoothLowEnergyManager()
|
||||||
{
|
{
|
||||||
return m_bluetoothScanner;
|
return m_bluetoothLowEnergyManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HardwareManager::isAvailable(const HardwareResource::Type &hardwareResourceType) const
|
bool HardwareManager::isAvailable(const HardwareResource::Type &hardwareResourceType) const
|
||||||
@ -140,3 +141,8 @@ bool HardwareManager::disableHardwareReource(const HardwareResource::Type &hardw
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void HardwareManager::timeTick()
|
||||||
|
{
|
||||||
|
m_pluginTimerManager->timeTick();
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@ -28,8 +28,8 @@
|
|||||||
|
|
||||||
#include "hardwareresource.h"
|
#include "hardwareresource.h"
|
||||||
|
|
||||||
class PluginTimer;
|
class PluginTimerManager;
|
||||||
class BluetoothScanner;
|
class BluetoothLowEnergyManager;
|
||||||
class Radio433;
|
class Radio433;
|
||||||
class NetworkAccessManager;
|
class NetworkAccessManager;
|
||||||
class UpnpDiscovery;
|
class UpnpDiscovery;
|
||||||
@ -46,11 +46,11 @@ public:
|
|||||||
explicit HardwareManager(QObject *parent = nullptr);
|
explicit HardwareManager(QObject *parent = nullptr);
|
||||||
|
|
||||||
Radio433 *radio433();
|
Radio433 *radio433();
|
||||||
PluginTimer *pluginTimer();
|
PluginTimerManager *pluginTimerManager();
|
||||||
NetworkAccessManager *networkManager();
|
NetworkAccessManager *networkManager();
|
||||||
UpnpDiscovery *upnpDiscovery();
|
UpnpDiscovery *upnpDiscovery();
|
||||||
QtAvahiServiceBrowser *avahiBrowser();
|
QtAvahiServiceBrowser *avahiBrowser();
|
||||||
BluetoothScanner *bluetoothScanner();
|
BluetoothLowEnergyManager *bluetoothLowEnergyManager();
|
||||||
|
|
||||||
bool isAvailable(const HardwareResource::Type &hardwareResourceType) const;
|
bool isAvailable(const HardwareResource::Type &hardwareResourceType) const;
|
||||||
bool isEnabled(const HardwareResource::Type &hardwareResourceType) const;
|
bool isEnabled(const HardwareResource::Type &hardwareResourceType) const;
|
||||||
@ -62,15 +62,17 @@ private:
|
|||||||
|
|
||||||
// Hardware Resources
|
// Hardware Resources
|
||||||
Radio433 *m_radio433 = nullptr;
|
Radio433 *m_radio433 = nullptr;
|
||||||
PluginTimer *m_pluginTimer = nullptr;
|
PluginTimerManager *m_pluginTimerManager = nullptr;
|
||||||
NetworkAccessManager *m_networkManager = nullptr;
|
NetworkAccessManager *m_networkManager = nullptr;
|
||||||
UpnpDiscovery *m_upnpDiscovery = nullptr;
|
UpnpDiscovery *m_upnpDiscovery = nullptr;
|
||||||
QtAvahiServiceBrowser *m_avahiBrowser = nullptr;
|
QtAvahiServiceBrowser *m_avahiBrowser = nullptr;
|
||||||
BluetoothScanner *m_bluetoothScanner = nullptr;
|
BluetoothLowEnergyManager *m_bluetoothLowEnergyManager = nullptr;
|
||||||
|
|
||||||
bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType);
|
bool enableHardwareReource(const HardwareResource::Type &hardwareResourceType);
|
||||||
bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType);
|
bool disableHardwareReource(const HardwareResource::Type &hardwareResourceType);
|
||||||
|
|
||||||
|
void timeTick();
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void hardwareResourceAvailableChanged(const HardwareResource::Type &hardwareResourceType, const bool &available);
|
void hardwareResourceAvailableChanged(const HardwareResource::Type &hardwareResourceType, const bool &available);
|
||||||
void hardwareResourceEnabledChanged(const HardwareResource::Type &hardwareResourceType, const bool &enabled);
|
void hardwareResourceEnabledChanged(const HardwareResource::Type &hardwareResourceType, const bool &enabled);
|
||||||
|
|||||||
@ -70,7 +70,9 @@ HEADERS += devicemanager.h \
|
|||||||
hardwareresource.h \
|
hardwareresource.h \
|
||||||
plugintimer.h \
|
plugintimer.h \
|
||||||
hardwaremanager.h \
|
hardwaremanager.h \
|
||||||
bluetooth/bluetoothlowenergymanager.h
|
bluetooth/bluetoothlowenergymanager.h \
|
||||||
|
network/upnp/upnpdiscoveryreply.h \
|
||||||
|
bluetooth/bluetoothdiscoveryreply.h
|
||||||
|
|
||||||
SOURCES += devicemanager.cpp \
|
SOURCES += devicemanager.cpp \
|
||||||
loggingcategories.cpp \
|
loggingcategories.cpp \
|
||||||
@ -126,7 +128,9 @@ SOURCES += devicemanager.cpp \
|
|||||||
hardwareresource.cpp \
|
hardwareresource.cpp \
|
||||||
plugintimer.cpp \
|
plugintimer.cpp \
|
||||||
hardwaremanager.cpp \
|
hardwaremanager.cpp \
|
||||||
bluetooth/bluetoothlowenergymanager.cpp
|
bluetooth/bluetoothlowenergymanager.cpp \
|
||||||
|
network/upnp/upnpdiscoveryreply.cpp \
|
||||||
|
bluetooth/bluetoothdiscoveryreply.cpp
|
||||||
|
|
||||||
# install plugininfo python script for libguh-dev
|
# install plugininfo python script for libguh-dev
|
||||||
generateplugininfo.files = $$top_srcdir/plugins/guh-generateplugininfo
|
generateplugininfo.files = $$top_srcdir/plugins/guh-generateplugininfo
|
||||||
|
|||||||
@ -73,8 +73,6 @@ QtAvahiServiceBrowser::~QtAvahiServiceBrowser()
|
|||||||
delete d_ptr;
|
delete d_ptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/*! Returns the current \l{AvahiServiceEntry} list of this \l{QtAvahiServiceBrowser}. */
|
/*! Returns the current \l{AvahiServiceEntry} list of this \l{QtAvahiServiceBrowser}. */
|
||||||
QList<AvahiServiceEntry> QtAvahiServiceBrowser::serviceEntries() const
|
QList<AvahiServiceEntry> QtAvahiServiceBrowser::serviceEntries() const
|
||||||
{
|
{
|
||||||
|
|||||||
@ -82,12 +82,12 @@ QNetworkReply *NetworkAccessManager::put(const QNetworkRequest &request, QIODevi
|
|||||||
|
|
||||||
QNetworkReply *NetworkAccessManager::put(const QNetworkRequest &request, const QByteArray &data)
|
QNetworkReply *NetworkAccessManager::put(const QNetworkRequest &request, const QByteArray &data)
|
||||||
{
|
{
|
||||||
return m_manager->post(request, data);
|
return m_manager->put(request, data);
|
||||||
}
|
}
|
||||||
|
|
||||||
QNetworkReply *NetworkAccessManager::put(const QNetworkRequest &request, QHttpMultiPart *multiPart)
|
QNetworkReply *NetworkAccessManager::put(const QNetworkRequest &request, QHttpMultiPart *multiPart)
|
||||||
{
|
{
|
||||||
return m_manager->post(request, multiPart);
|
return m_manager->put(request, multiPart);
|
||||||
}
|
}
|
||||||
|
|
||||||
QNetworkReply *NetworkAccessManager::sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QIODevice *data)
|
QNetworkReply *NetworkAccessManager::sendCustomRequest(const QNetworkRequest &request, const QByteArray &verb, QIODevice *data)
|
||||||
|
|||||||
@ -80,40 +80,35 @@ UpnpDiscovery::~UpnpDiscovery()
|
|||||||
m_socket->close();
|
m_socket->close();
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! Returns false, if the \l{UpnpDiscovery} resource is not available. Returns true, if a device with
|
UpnpDiscoveryReply *UpnpDiscovery::discoverDevices(const QString &searchTarget, const QString &userAgent, const int &timeout)
|
||||||
* the given \a searchTarget, \a userAgent and \a pluginId can be discovered.*/
|
|
||||||
bool UpnpDiscovery::discoverDevices(QPointer<QObject> caller, const QString &callbackMethod, const QString &searchTarget, const QString &userAgent)
|
|
||||||
{
|
{
|
||||||
|
// Create the reply for this discovery request
|
||||||
|
QPointer<UpnpDiscoveryReply> reply = new UpnpDiscoveryReply(searchTarget, userAgent, this);
|
||||||
|
|
||||||
if (!available()) {
|
if (!available()) {
|
||||||
qCWarning(dcHardware()) << name() << "not available.";
|
qCWarning(dcHardware()) << name() << "is not avilable.";
|
||||||
return false;
|
reply->setError(UpnpDiscoveryReply::UpnpDiscoveryReplyErrorNotAvailable);
|
||||||
|
reply->setFinished();
|
||||||
|
return reply.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!enabled()) {
|
if (!enabled()) {
|
||||||
qCWarning(dcHardware()) << name() << "disabled.";
|
qCWarning(dcHardware()) << name() << "is not enabled.";
|
||||||
return false;
|
reply->setError(UpnpDiscoveryReply::UpnpDiscoveryReplyErrorNotEnabled);
|
||||||
}
|
reply->setFinished();
|
||||||
|
return reply.data();
|
||||||
if (!m_socket)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
if(m_socket->state() != QUdpSocket::BoundState){
|
|
||||||
qCWarning(dcHardware) << "UPnP not bound to port 1900";
|
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
qCDebug(dcHardware) << name() << "discover" << searchTarget << userAgent;
|
qCDebug(dcHardware) << name() << "discover" << searchTarget << userAgent;
|
||||||
|
|
||||||
|
// Looks good so far, lets start a request
|
||||||
// Create a new request
|
UpnpDiscoveryRequest *request = new UpnpDiscoveryRequest(this, reply);
|
||||||
UpnpDiscoveryRequest *request = new UpnpDiscoveryRequest(this, caller, callbackMethod, searchTarget, userAgent);
|
|
||||||
connect(request, &UpnpDiscoveryRequest::discoveryTimeout, this, &UpnpDiscovery::discoverTimeout);
|
connect(request, &UpnpDiscoveryRequest::discoveryTimeout, this, &UpnpDiscovery::discoverTimeout);
|
||||||
request->discover();
|
request->discover(timeout);
|
||||||
m_discoverRequests.append(request);
|
m_discoverRequests.append(request);
|
||||||
return true;
|
return reply.data();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void UpnpDiscovery::requestDeviceInformation(const QNetworkRequest &networkRequest, const UpnpDeviceDescriptor &upnpDeviceDescriptor)
|
void UpnpDiscovery::requestDeviceInformation(const QNetworkRequest &networkRequest, const UpnpDeviceDescriptor &upnpDeviceDescriptor)
|
||||||
{
|
{
|
||||||
QNetworkReply *replay = m_networkAccessManager->get(networkRequest);
|
QNetworkReply *replay = m_networkAccessManager->get(networkRequest);
|
||||||
@ -420,13 +415,14 @@ void UpnpDiscovery::sendAliveMessage()
|
|||||||
void UpnpDiscovery::discoverTimeout()
|
void UpnpDiscovery::discoverTimeout()
|
||||||
{
|
{
|
||||||
UpnpDiscoveryRequest *discoveryRequest = static_cast<UpnpDiscoveryRequest*>(sender());
|
UpnpDiscoveryRequest *discoveryRequest = static_cast<UpnpDiscoveryRequest*>(sender());
|
||||||
|
QPointer<UpnpDiscoveryReply> reply = discoveryRequest->reply();
|
||||||
|
|
||||||
if (discoveryRequest->caller().isNull()) {
|
if (reply.isNull()) {
|
||||||
qCWarning(dcHardware()) << name() << "The reciver of the discovery request does not exist any more.";
|
qCWarning(dcHardware()) << name() << "Reply does not exist any more. Please don't delete the reply before it has finished.";
|
||||||
} else {
|
} else {
|
||||||
// Invoke the method containing the discovered devicelist
|
reply->setDeviceDescriptors(discoveryRequest->deviceList());
|
||||||
// FIXME: check the callback method paramters during compililation
|
reply->setError(UpnpDiscoveryReply::UpnpDiscoveryReplyErrorNoError);
|
||||||
QMetaObject::invokeMethod(discoveryRequest->caller().data(), discoveryRequest->callbackMethod().toLatin1().data(), Q_ARG(QList<UpnpDeviceDescriptor>, discoveryRequest->deviceList()));
|
reply->setFinished();
|
||||||
}
|
}
|
||||||
|
|
||||||
m_discoverRequests.removeOne(discoveryRequest);
|
m_discoverRequests.removeOne(discoveryRequest);
|
||||||
|
|||||||
@ -34,6 +34,7 @@
|
|||||||
#include "libguh.h"
|
#include "libguh.h"
|
||||||
#include "devicemanager.h"
|
#include "devicemanager.h"
|
||||||
#include "hardwareresource.h"
|
#include "hardwareresource.h"
|
||||||
|
#include "upnpdiscoveryreply.h"
|
||||||
#include "upnpdiscoveryrequest.h"
|
#include "upnpdiscoveryrequest.h"
|
||||||
#include "upnpdevicedescriptor.h"
|
#include "upnpdevicedescriptor.h"
|
||||||
|
|
||||||
@ -49,7 +50,7 @@ class LIBGUH_EXPORT UpnpDiscovery : public HardwareResource
|
|||||||
friend class HardwareManager;
|
friend class HardwareManager;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
bool discoverDevices(QPointer<QObject> caller = QPointer<QObject>(), const QString &callbackMethod = QString(), const QString &searchTarget = "ssdp:all", const QString &userAgent = QString());
|
UpnpDiscoveryReply *discoverDevices(const QString &searchTarget = "ssdp:all", const QString &userAgent = QString(), const int &timeout = 5000);
|
||||||
void sendToMulticast(const QByteArray &data);
|
void sendToMulticast(const QByteArray &data);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@ -65,7 +66,7 @@ private:
|
|||||||
QNetworkAccessManager *m_networkAccessManager = nullptr;
|
QNetworkAccessManager *m_networkAccessManager = nullptr;
|
||||||
|
|
||||||
QList<UpnpDiscoveryRequest *> m_discoverRequests;
|
QList<UpnpDiscoveryRequest *> m_discoverRequests;
|
||||||
QHash<QNetworkReply*,UpnpDeviceDescriptor> m_informationRequestList;
|
QHash<QNetworkReply*, UpnpDeviceDescriptor> m_informationRequestList;
|
||||||
|
|
||||||
void requestDeviceInformation(const QNetworkRequest &networkRequest, const UpnpDeviceDescriptor &upnpDeviceDescriptor);
|
void requestDeviceInformation(const QNetworkRequest &networkRequest, const UpnpDeviceDescriptor &upnpDeviceDescriptor);
|
||||||
void respondToSearchRequest(QHostAddress host, int port);
|
void respondToSearchRequest(QHostAddress host, int port);
|
||||||
|
|||||||
78
libguh/network/upnp/upnpdiscoveryreply.cpp
Normal file
78
libguh/network/upnp/upnpdiscoveryreply.cpp
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* *
|
||||||
|
* 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 "upnpdiscoveryreply.h"
|
||||||
|
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
|
QString UpnpDiscoveryReply::searchTarget() const
|
||||||
|
{
|
||||||
|
return m_searchTarget;
|
||||||
|
}
|
||||||
|
|
||||||
|
QString UpnpDiscoveryReply::userAgent() const
|
||||||
|
{
|
||||||
|
return m_userAgent;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpnpDiscoveryReply::UpnpDiscoveryReplyError UpnpDiscoveryReply::error() const
|
||||||
|
{
|
||||||
|
return m_error;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool UpnpDiscoveryReply::isFinished() const
|
||||||
|
{
|
||||||
|
return m_finished;
|
||||||
|
}
|
||||||
|
|
||||||
|
QList<UpnpDeviceDescriptor> UpnpDiscoveryReply::deviceDescriptors() const
|
||||||
|
{
|
||||||
|
return m_deviceDescriptors;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpnpDiscoveryReply::UpnpDiscoveryReply(const QString &searchTarget, const QString &userAgent, QObject *parent) :
|
||||||
|
QObject(parent),
|
||||||
|
m_searchTarget(searchTarget),
|
||||||
|
m_userAgent(userAgent)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpnpDiscoveryReply::setDeviceDescriptors(const QList<UpnpDeviceDescriptor> &deviceDescriptors)
|
||||||
|
{
|
||||||
|
m_deviceDescriptors = deviceDescriptors;
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpnpDiscoveryReply::setError(const UpnpDiscoveryReply::UpnpDiscoveryReplyError &error)
|
||||||
|
{
|
||||||
|
m_error = error;
|
||||||
|
if (m_error != UpnpDiscoveryReplyErrorNoError) {
|
||||||
|
emit errorOccured(m_error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void UpnpDiscoveryReply::setFinished()
|
||||||
|
{
|
||||||
|
m_finished = true;
|
||||||
|
// Note: this makes sure the finished signal will be processed in the next event loop
|
||||||
|
QTimer::singleShot(0, this, &UpnpDiscoveryReply::finished);
|
||||||
|
}
|
||||||
74
libguh/network/upnp/upnpdiscoveryreply.h
Normal file
74
libguh/network/upnp/upnpdiscoveryreply.h
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
* *
|
||||||
|
* 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 UPNPDISCOVERYREPLY_H
|
||||||
|
#define UPNPDISCOVERYREPLY_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
|
||||||
|
#include "upnpdevicedescriptor.h"
|
||||||
|
|
||||||
|
class UpnpDiscoveryReply : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
|
||||||
|
friend class UpnpDiscovery;
|
||||||
|
|
||||||
|
public:
|
||||||
|
enum UpnpDiscoveryReplyError {
|
||||||
|
UpnpDiscoveryReplyErrorNoError,
|
||||||
|
UpnpDiscoveryReplyErrorNotAvailable,
|
||||||
|
UpnpDiscoveryReplyErrorNotEnabled,
|
||||||
|
UpnpDiscoveryReplyErrorResourceBusy
|
||||||
|
};
|
||||||
|
Q_ENUM(UpnpDiscoveryReplyError)
|
||||||
|
|
||||||
|
QString searchTarget() const;
|
||||||
|
QString userAgent() const;
|
||||||
|
|
||||||
|
UpnpDiscoveryReplyError error() const;
|
||||||
|
bool isFinished() const;
|
||||||
|
|
||||||
|
QList<UpnpDeviceDescriptor> deviceDescriptors() const;
|
||||||
|
|
||||||
|
private:
|
||||||
|
explicit UpnpDiscoveryReply(const QString &searchTarget, const QString &userAgent, QObject *parent = nullptr);
|
||||||
|
|
||||||
|
QString m_searchTarget;
|
||||||
|
QString m_userAgent;
|
||||||
|
|
||||||
|
QList<UpnpDeviceDescriptor> m_deviceDescriptors;
|
||||||
|
UpnpDiscoveryReplyError m_error = UpnpDiscoveryReplyErrorNoError;
|
||||||
|
bool m_finished = false;
|
||||||
|
|
||||||
|
// Methods for UpnpDiscovery
|
||||||
|
void setDeviceDescriptors(const QList<UpnpDeviceDescriptor> &deviceDescriptors);
|
||||||
|
void setError(const UpnpDiscoveryReplyError &error);
|
||||||
|
void setFinished();
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void finished();
|
||||||
|
void errorOccured(const UpnpDiscoveryReplyError &error);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // UPNPDISCOVERYREPLY_H
|
||||||
@ -23,35 +23,31 @@
|
|||||||
#include "upnpdiscoveryrequest.h"
|
#include "upnpdiscoveryrequest.h"
|
||||||
#include "loggingcategories.h"
|
#include "loggingcategories.h"
|
||||||
|
|
||||||
UpnpDiscoveryRequest::UpnpDiscoveryRequest(UpnpDiscovery *upnpDiscovery, QPointer<QObject> caller, const QString &callbackMethod, QString searchTarget, QString userAgent):
|
UpnpDiscoveryRequest::UpnpDiscoveryRequest(UpnpDiscovery *upnpDiscovery, QPointer<UpnpDiscoveryReply> reply):
|
||||||
QObject(upnpDiscovery),
|
QObject(upnpDiscovery),
|
||||||
m_upnpDiscovery(upnpDiscovery),
|
m_upnpDiscovery(upnpDiscovery),
|
||||||
m_caller(caller),
|
m_reply(reply)
|
||||||
m_callbackMethod(callbackMethod),
|
|
||||||
m_searchTarget(searchTarget),
|
|
||||||
m_userAgent(userAgent)
|
|
||||||
{
|
{
|
||||||
m_timer = new QTimer(this);
|
m_timer = new QTimer(this);
|
||||||
m_timer->setSingleShot(true);
|
m_timer->setSingleShot(true);
|
||||||
connect(m_timer, &QTimer::timeout, this, &UpnpDiscoveryRequest::discoveryTimeout);
|
connect(m_timer, &QTimer::timeout, this, &UpnpDiscoveryRequest::discoveryTimeout);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpnpDiscoveryRequest::discover()
|
void UpnpDiscoveryRequest::discover(const int &timeout)
|
||||||
{
|
{
|
||||||
QByteArray ssdpSearchMessage = QByteArray("M-SEARCH * HTTP/1.1\r\n"
|
QByteArray ssdpSearchMessage = QByteArray("M-SEARCH * HTTP/1.1\r\n"
|
||||||
"HOST:239.255.255.250:1900\r\n"
|
"HOST:239.255.255.250:1900\r\n"
|
||||||
"MAN:\"ssdp:discover\"\r\n"
|
"MAN:\"ssdp:discover\"\r\n"
|
||||||
"MX:4\r\n"
|
"MX:4\r\n"
|
||||||
"ST: " + m_searchTarget.toUtf8() + "\r\n"
|
"ST: " + reply()->searchTarget().toUtf8() + "\r\n"
|
||||||
"USR-AGENT: " + m_userAgent.toUtf8() + "\r\n\r\n");
|
"USR-AGENT: " + reply()->userAgent().toUtf8() + "\r\n\r\n");
|
||||||
|
|
||||||
m_upnpDiscovery->sendToMulticast(ssdpSearchMessage);
|
m_upnpDiscovery->sendToMulticast(ssdpSearchMessage);
|
||||||
|
|
||||||
// TODO: call in 500ms steps
|
// TODO: call in 500ms steps
|
||||||
|
|
||||||
qCDebug(dcHardware) << "--> UPnP discovery called.";
|
qCDebug(dcHardware) << "--> UPnP discovery called.";
|
||||||
|
m_timer->start(timeout);
|
||||||
m_timer->start(5000);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void UpnpDiscoveryRequest::addDeviceDescriptor(const UpnpDeviceDescriptor &deviceDescriptor)
|
void UpnpDiscoveryRequest::addDeviceDescriptor(const UpnpDeviceDescriptor &deviceDescriptor)
|
||||||
@ -73,7 +69,7 @@ QNetworkRequest UpnpDiscoveryRequest::createNetworkRequest(UpnpDeviceDescriptor
|
|||||||
QNetworkRequest deviceRequest;
|
QNetworkRequest deviceRequest;
|
||||||
deviceRequest.setUrl(deviveDescriptor.location());
|
deviceRequest.setUrl(deviveDescriptor.location());
|
||||||
deviceRequest.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml"));
|
deviceRequest.setHeader(QNetworkRequest::ContentTypeHeader,QVariant("text/xml"));
|
||||||
deviceRequest.setHeader(QNetworkRequest::UserAgentHeader,QVariant(m_userAgent));
|
deviceRequest.setHeader(QNetworkRequest::UserAgentHeader,QVariant(reply()->userAgent()));
|
||||||
|
|
||||||
return deviceRequest;
|
return deviceRequest;
|
||||||
}
|
}
|
||||||
@ -83,22 +79,7 @@ QList<UpnpDeviceDescriptor> UpnpDiscoveryRequest::deviceList() const
|
|||||||
return m_deviceList;
|
return m_deviceList;
|
||||||
}
|
}
|
||||||
|
|
||||||
QPointer<QObject> UpnpDiscoveryRequest::caller() const
|
QPointer<UpnpDiscoveryReply> UpnpDiscoveryRequest::reply()
|
||||||
{
|
{
|
||||||
return m_caller;
|
return m_reply;
|
||||||
}
|
|
||||||
|
|
||||||
QString UpnpDiscoveryRequest::callbackMethod() const
|
|
||||||
{
|
|
||||||
return m_callbackMethod;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString UpnpDiscoveryRequest::searchTarget() const
|
|
||||||
{
|
|
||||||
return m_searchTarget;
|
|
||||||
}
|
|
||||||
|
|
||||||
QString UpnpDiscoveryRequest::userAgent() const
|
|
||||||
{
|
|
||||||
return m_userAgent;
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -28,6 +28,7 @@
|
|||||||
#include <QMetaObject>
|
#include <QMetaObject>
|
||||||
|
|
||||||
#include "upnpdiscovery.h"
|
#include "upnpdiscovery.h"
|
||||||
|
#include "upnpdiscoveryreply.h"
|
||||||
#include "upnpdevicedescriptor.h"
|
#include "upnpdevicedescriptor.h"
|
||||||
#include "libguh.h"
|
#include "libguh.h"
|
||||||
#include "typeutils.h"
|
#include "typeutils.h"
|
||||||
@ -38,26 +39,20 @@ class LIBGUH_EXPORT UpnpDiscoveryRequest : public QObject
|
|||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
explicit UpnpDiscoveryRequest(UpnpDiscovery *upnpDiscovery, QPointer<QObject> caller, const QString &callbackMethod, QString searchTarget, QString userAgent);
|
explicit UpnpDiscoveryRequest(UpnpDiscovery *upnpDiscovery, QPointer<UpnpDiscoveryReply> reply);
|
||||||
|
|
||||||
void discover();
|
void discover(const int &timeout);
|
||||||
void addDeviceDescriptor(const UpnpDeviceDescriptor &deviceDescriptor);
|
void addDeviceDescriptor(const UpnpDeviceDescriptor &deviceDescriptor);
|
||||||
QNetworkRequest createNetworkRequest(UpnpDeviceDescriptor deviveDescriptor);
|
QNetworkRequest createNetworkRequest(UpnpDeviceDescriptor deviveDescriptor);
|
||||||
QList<UpnpDeviceDescriptor> deviceList() const;
|
QList<UpnpDeviceDescriptor> deviceList() const;
|
||||||
|
|
||||||
QPointer<QObject> caller() const;
|
QPointer<UpnpDiscoveryReply> reply();
|
||||||
QString callbackMethod() const;
|
|
||||||
QString searchTarget() const;
|
|
||||||
QString userAgent() const;
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
UpnpDiscovery *m_upnpDiscovery;
|
UpnpDiscovery *m_upnpDiscovery;
|
||||||
QTimer *m_timer;
|
QPointer<UpnpDiscoveryReply> m_reply;
|
||||||
QPointer<QObject> m_caller;
|
|
||||||
QString m_callbackMethod;
|
|
||||||
QString m_searchTarget;
|
|
||||||
QString m_userAgent;
|
|
||||||
|
|
||||||
|
QTimer *m_timer = nullptr;
|
||||||
QList<UpnpDeviceDescriptor> m_deviceList;
|
QList<UpnpDeviceDescriptor> m_deviceList;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
|
|||||||
@ -23,37 +23,154 @@
|
|||||||
#include "plugintimer.h"
|
#include "plugintimer.h"
|
||||||
#include "loggingcategories.h"
|
#include "loggingcategories.h"
|
||||||
|
|
||||||
PluginTimer::PluginTimer(int intervall, QObject *parent) :
|
int PluginTimer::interval() const
|
||||||
HardwareResource(HardwareResource::TypeTimer, "Plugin timer", parent),
|
|
||||||
m_intervall(intervall)
|
|
||||||
{
|
{
|
||||||
// FIXME: the timer should be able to emit timerEvents with different resolutions
|
return m_interval;
|
||||||
m_timer = new QTimer(this);
|
}
|
||||||
m_timer->setSingleShot(false);
|
|
||||||
m_timer->setInterval(m_intervall);
|
|
||||||
|
|
||||||
connect(m_timer, &QTimer::timeout, this, &PluginTimer::timerEvent);
|
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);
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
{
|
||||||
setAvailable(true);
|
setAvailable(true);
|
||||||
|
|
||||||
qCDebug(dcHardware()) << "-->" << name() << "created successfully.";
|
qCDebug(dcHardware()) << "-->" << name() << "created successfully.";
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PluginTimer::enable()
|
void PluginTimerManager::timeTick()
|
||||||
|
{
|
||||||
|
// If timer resource is not enabled do nothing
|
||||||
|
if (!enabled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (PluginTimer *timer, m_timers) {
|
||||||
|
timer->tick();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool PluginTimerManager::enable()
|
||||||
{
|
{
|
||||||
if (!available())
|
if (!available())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_timer->start();
|
|
||||||
setEnabled(true);
|
setEnabled(true);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool PluginTimer::disable()
|
bool PluginTimerManager::disable()
|
||||||
{
|
{
|
||||||
if (!available())
|
if (!available())
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
m_timer->stop();
|
|
||||||
setEnabled(false);
|
setEnabled(false);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -23,24 +23,69 @@
|
|||||||
#ifndef PLUGINTIMER_H
|
#ifndef PLUGINTIMER_H
|
||||||
#define PLUGINTIMER_H
|
#define PLUGINTIMER_H
|
||||||
|
|
||||||
#include <QObject>
|
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
#include <QObject>
|
||||||
|
#include <QPointer>
|
||||||
|
|
||||||
#include "hardwareresource.h"
|
#include "hardwareresource.h"
|
||||||
|
|
||||||
class PluginTimer : public HardwareResource
|
class PluginTimer : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
|
||||||
explicit PluginTimer(int intervall, QObject *parent = nullptr);
|
|
||||||
|
|
||||||
private:
|
friend class PluginTimerManager;
|
||||||
QTimer *m_timer = nullptr;
|
|
||||||
int m_intervall = 10000;
|
public:
|
||||||
|
int interval() const;
|
||||||
|
|
||||||
|
void pause();
|
||||||
|
void resume();
|
||||||
|
int currentTick() const;
|
||||||
|
|
||||||
|
bool running() const;
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
// TODO: emit different resolutions
|
void timeout();
|
||||||
void timerEvent();
|
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();
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
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:
|
public slots:
|
||||||
bool enable();
|
bool enable();
|
||||||
|
|||||||
Reference in New Issue
Block a user