mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-19 04:52:12 +01:00
LP-269 StreamService plugin which enables a TCP/IP stream whereby any connection to 7891 port gets served with all asynchronous changes on UAV data objects in JSON format.
This commit is contained in:
parent
3498f3c981
commit
53c32251f2
@ -238,3 +238,10 @@ plugin_usagetracker.depends += plugin_uavobjects
|
||||
plugin_usagetracker.depends += plugin_uavtalk
|
||||
plugin_setupwizard.depends += plugin_uavobjectutil
|
||||
SUBDIRS += plugin_usagetracker
|
||||
|
||||
# Stream Service Plugin
|
||||
plugin_streamservice.subdir = streamservice
|
||||
plugin_streamservice.depends = plugin_coreplugin
|
||||
plugin_streamservice.depends += plugin_uavobjects
|
||||
plugin_streamservice.depends += plugin_uavtalk
|
||||
SUBDIRS += plugin_streamservice
|
||||
|
@ -0,0 +1,12 @@
|
||||
<plugin name="StreamServicePlugin" version="1.0.0" compatVersion="1.0.0">
|
||||
<vendor>The LibrePilot Project</vendor>
|
||||
<copyright>(C) 2016 LibrePilot Project; (C) 2010 OpenPilot Project and Nokia</copyright>
|
||||
<license>The GNU Public License (GPL) Version 3</license>
|
||||
<description>UAV Objects listener publishing changes in socket</description>
|
||||
<url>http://www.librepilot.org</url>
|
||||
<dependencyList>
|
||||
<dependency name="Core" version="1.0.0"/>
|
||||
<dependency name="UAVTalk" version="1.0.0"/>
|
||||
<dependency name="UAVObjects" version="1.0.0"/>
|
||||
</dependencyList>
|
||||
</plugin>
|
18
ground/gcs/src/plugins/streamservice/streamservice.pro
Normal file
18
ground/gcs/src/plugins/streamservice/streamservice.pro
Normal file
@ -0,0 +1,18 @@
|
||||
TEMPLATE = lib
|
||||
TARGET = StreamServicePlugin
|
||||
|
||||
QT += network widgets
|
||||
|
||||
include(../../plugin.pri)
|
||||
include(../../plugins/coreplugin/coreplugin.pri)
|
||||
include(../../plugins/uavtalk/uavtalk.pri)
|
||||
include(../../plugins/uavobjects/uavobjects.pri)
|
||||
|
||||
SOURCES += streamserviceplugin.cpp
|
||||
|
||||
HEADERS += streamserviceplugin.h
|
||||
|
||||
OTHER_FILES +=
|
||||
|
||||
DISTFILES += \
|
||||
StreamServicePlugin.pluginspec
|
155
ground/gcs/src/plugins/streamservice/streamserviceplugin.cpp
Normal file
155
ground/gcs/src/plugins/streamservice/streamserviceplugin.cpp
Normal file
@ -0,0 +1,155 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file streamserviceplugin.h
|
||||
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
||||
* @addtogroup GCSPlugins GCS Plugins
|
||||
* @{
|
||||
* @addtogroup StreamServicePlugin Plugin
|
||||
* @{
|
||||
* @brief Data UAV Data objects TCP/IP stream service
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
#include "streamserviceplugin.h"
|
||||
|
||||
#include <QJsonObject>
|
||||
#include <QJsonDocument>
|
||||
#include <QMessageBox>
|
||||
#include <QDateTime>
|
||||
#include <QTcpServer>
|
||||
#include <QTcpSocket>
|
||||
|
||||
#include "extensionsystem/pluginmanager.h"
|
||||
#include "../uavobjects/uavobjectmanager.h"
|
||||
#include "../uavobjects/uavdataobject.h"
|
||||
|
||||
StreamServicePlugin::StreamServicePlugin() :
|
||||
port(7891),
|
||||
isSubscribed(false) {}
|
||||
|
||||
StreamServicePlugin::~StreamServicePlugin()
|
||||
{
|
||||
if (pServer == Q_NULLPTR) {
|
||||
return;
|
||||
}
|
||||
if (pServer->isListening()) {
|
||||
foreach(QTcpSocket * client, activeClients) {
|
||||
/* Disconnect the client discarding pending
|
||||
* bytes */
|
||||
if (client->isOpen()) {
|
||||
client->close();
|
||||
}
|
||||
}
|
||||
pServer->close();
|
||||
}
|
||||
}
|
||||
|
||||
bool StreamServicePlugin::initialize(const QStringList &arguments, QString *errorString)
|
||||
{
|
||||
Q_UNUSED(arguments);
|
||||
Q_UNUSED(errorString);
|
||||
|
||||
pServer = new QTcpServer();
|
||||
|
||||
if (!pServer->listen(QHostAddress::Any, port)) {
|
||||
*errorString = tr("Couldn't start StreamService: ") + pServer->errorString();
|
||||
return false;
|
||||
}
|
||||
|
||||
connect(pServer, &QTcpServer::newConnection, this, &StreamServicePlugin::clientConnected);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void StreamServicePlugin::extensionsInitialized()
|
||||
{
|
||||
if (pServer == Q_NULLPTR) {
|
||||
return;
|
||||
}
|
||||
|
||||
pServer->resumeAccepting();
|
||||
}
|
||||
|
||||
void StreamServicePlugin::shutdown()
|
||||
{
|
||||
if (pServer != Q_NULLPTR) {
|
||||
pServer->pauseAccepting();
|
||||
}
|
||||
|
||||
foreach(QTcpSocket * pClient, activeClients) {
|
||||
pClient->disconnectFromHost();
|
||||
}
|
||||
}
|
||||
|
||||
void StreamServicePlugin::objectUpdated(UAVObject *pObj)
|
||||
{
|
||||
QJsonObject qtjson;
|
||||
|
||||
pObj->toJson(qtjson);
|
||||
|
||||
// Adds timestamp: Milliseconds from epoch
|
||||
qtjson.insert("gcs_timestamp_ms", QJsonValue(QDateTime::currentMSecsSinceEpoch()));
|
||||
|
||||
QJsonDocument jsonDoc(qtjson);
|
||||
QString strJson = QString(jsonDoc.toJson(QJsonDocument::Compact)) + "\n";
|
||||
|
||||
foreach(QTcpSocket * pClient, activeClients) {
|
||||
if (pClient->isOpen()) {
|
||||
if (pClient->write(strJson.toUtf8().constData(), strJson.length())) {
|
||||
pClient->flush();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void StreamServicePlugin::clientConnected()
|
||||
{
|
||||
QTcpSocket *pending = pServer->nextPendingConnection();
|
||||
|
||||
if (pending == Q_NULLPTR) {
|
||||
return;
|
||||
}
|
||||
makeSureIsSubscribed();
|
||||
|
||||
connect(pending, &QTcpSocket::disconnected, this, &StreamServicePlugin::clientDisconnected);
|
||||
activeClients.append(pending);
|
||||
}
|
||||
|
||||
void StreamServicePlugin::clientDisconnected()
|
||||
{
|
||||
disconnect(sender());
|
||||
activeClients.removeAll((QTcpSocket *)sender());
|
||||
}
|
||||
|
||||
inline void StreamServicePlugin::makeSureIsSubscribed()
|
||||
{
|
||||
if (isSubscribed) {
|
||||
return;
|
||||
}
|
||||
|
||||
ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
|
||||
UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
|
||||
Q_ASSERT(objManager);
|
||||
|
||||
QList< QList<UAVDataObject *> > objList = objManager->getDataObjects();
|
||||
foreach(QList<UAVDataObject *> list, objList) {
|
||||
foreach(UAVDataObject * obj, list) {
|
||||
connect(obj, &UAVDataObject::objectUpdated, this, &StreamServicePlugin::objectUpdated);
|
||||
}
|
||||
}
|
||||
isSubscribed = true;
|
||||
}
|
67
ground/gcs/src/plugins/streamservice/streamserviceplugin.h
Normal file
67
ground/gcs/src/plugins/streamservice/streamserviceplugin.h
Normal file
@ -0,0 +1,67 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file streamserviceplugin.h
|
||||
* @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
|
||||
* @addtogroup GCSPlugins GCS Plugins
|
||||
* @{
|
||||
* @addtogroup StreamServicePlugin Plugin
|
||||
* @{
|
||||
* @brief Data UAV Data objects TCP/IP stream service
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
#ifndef STREAMSERVICEPLUGIN_H
|
||||
#define STREAMSERVICEPLUGIN_H
|
||||
|
||||
#include <extensionsystem/iplugin.h>
|
||||
#include "../uavobjects/uavobject.h"
|
||||
|
||||
#include <QtPlugin>
|
||||
|
||||
class QTcpServer;
|
||||
class QTcpSocket;
|
||||
|
||||
class StreamServicePlugin : public ExtensionSystem::IPlugin {
|
||||
Q_OBJECT
|
||||
Q_PLUGIN_METADATA(IID "Openpilot.StreamService")
|
||||
|
||||
public:
|
||||
StreamServicePlugin();
|
||||
~StreamServicePlugin();
|
||||
|
||||
bool initialize(const QStringList &arguments, QString *errorString);
|
||||
void extensionsInitialized();
|
||||
void shutdown();
|
||||
|
||||
public slots:
|
||||
void objectUpdated(UAVObject *pObj);
|
||||
|
||||
private slots:
|
||||
void clientConnected();
|
||||
void clientDisconnected();
|
||||
|
||||
private:
|
||||
quint16 port;
|
||||
|
||||
QTcpServer *pServer;
|
||||
QList<QTcpSocket *> activeClients;
|
||||
bool isSubscribed;
|
||||
|
||||
inline void makeSureIsSubscribed();
|
||||
};
|
||||
|
||||
#endif // STREAMSERVICEPLUGIN_H
|
Loading…
x
Reference in New Issue
Block a user