Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / streamservice / streamserviceplugin.cpp
blob652acebd7e87f82e011e8707bb493bda19212d1f
1 /**
2 ******************************************************************************
4 * @file streamserviceplugin.h
5 * @author The LibrePilot Project, http://www.librepilot.org Copyright (C) 2016.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup StreamServicePlugin Plugin
9 * @{
10 * @brief Data UAV Data objects TCP/IP stream service
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "streamserviceplugin.h"
29 #include <QJsonObject>
30 #include <QJsonDocument>
31 #include <QMessageBox>
32 #include <QDateTime>
33 #include <QTcpServer>
34 #include <QTcpSocket>
36 #include "extensionsystem/pluginmanager.h"
37 #include "../uavobjects/uavobjectmanager.h"
38 #include "../uavobjects/uavdataobject.h"
40 StreamServicePlugin::StreamServicePlugin() :
41 port(7891),
42 isSubscribed(false) {}
44 StreamServicePlugin::~StreamServicePlugin()
46 if (pServer == Q_NULLPTR) {
47 return;
49 if (pServer->isListening()) {
50 foreach(QTcpSocket * client, activeClients) {
51 /* Disconnect the client discarding pending
52 * bytes */
53 if (client->isOpen()) {
54 client->close();
57 pServer->close();
61 bool StreamServicePlugin::initialize(const QStringList &arguments, QString *errorString)
63 Q_UNUSED(arguments);
64 Q_UNUSED(errorString);
66 pServer = new QTcpServer();
68 if (!pServer->listen(QHostAddress::Any, port)) {
69 *errorString = tr("Couldn't start StreamService: ") + pServer->errorString();
70 return false;
73 connect(pServer, &QTcpServer::newConnection, this, &StreamServicePlugin::clientConnected);
75 return true;
78 void StreamServicePlugin::extensionsInitialized()
80 if (pServer == Q_NULLPTR) {
81 return;
84 pServer->resumeAccepting();
87 void StreamServicePlugin::shutdown()
89 if (pServer != Q_NULLPTR) {
90 pServer->pauseAccepting();
93 foreach(QTcpSocket * pClient, activeClients) {
94 pClient->disconnectFromHost();
98 void StreamServicePlugin::objectUpdated(UAVObject *pObj)
100 QJsonObject qtjson;
102 pObj->toJson(qtjson);
104 // Adds timestamp: Milliseconds from epoch
105 qtjson.insert("gcs_timestamp_ms", QJsonValue(QDateTime::currentMSecsSinceEpoch()));
107 QJsonDocument jsonDoc(qtjson);
108 QString strJson = QString(jsonDoc.toJson(QJsonDocument::Compact)) + "\n";
110 foreach(QTcpSocket * pClient, activeClients) {
111 if (pClient->isOpen()) {
112 if (pClient->write(strJson.toUtf8().constData(), strJson.length())) {
113 pClient->flush();
119 void StreamServicePlugin::clientConnected()
121 QTcpSocket *pending = pServer->nextPendingConnection();
123 if (pending == Q_NULLPTR) {
124 return;
126 makeSureIsSubscribed();
128 connect(pending, &QTcpSocket::disconnected, this, &StreamServicePlugin::clientDisconnected);
129 activeClients.append(pending);
132 void StreamServicePlugin::clientDisconnected()
134 disconnect(sender());
135 activeClients.removeAll((QTcpSocket *)sender());
138 inline void StreamServicePlugin::makeSureIsSubscribed()
140 if (isSubscribed) {
141 return;
144 ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance();
145 UAVObjectManager *objManager = pm->getObject<UAVObjectManager>();
146 Q_ASSERT(objManager);
148 QList< QList<UAVDataObject *> > objList = objManager->getDataObjects();
149 foreach(QList<UAVDataObject *> list, objList) {
150 foreach(UAVDataObject * obj, list) {
151 connect(obj, &UAVDataObject::objectUpdated, this, &StreamServicePlugin::objectUpdated);
154 isSubscribed = true;