first working version
parent
94635a8977
commit
8a0d30605b
|
|
@ -24,6 +24,7 @@
|
||||||
#include "devicepluginhttpcommander.h"
|
#include "devicepluginhttpcommander.h"
|
||||||
#include "network/networkaccessmanager.h"
|
#include "network/networkaccessmanager.h"
|
||||||
#include "plugininfo.h"
|
#include "plugininfo.h"
|
||||||
|
#include <QNetworkInterface>
|
||||||
|
|
||||||
DevicePluginHttpCommander::DevicePluginHttpCommander()
|
DevicePluginHttpCommander::DevicePluginHttpCommander()
|
||||||
{
|
{
|
||||||
|
|
@ -71,9 +72,20 @@ void DevicePluginHttpCommander::setupDevice(DeviceSetupInfo *info)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (device->deviceClassId() == httpServerDeviceClassId) {
|
if (device->deviceClassId() == httpServerDeviceClassId) {
|
||||||
//TODO create a simple server
|
|
||||||
HttpSimpleServer *httpSimpleServer = new HttpSimpleServer(this);
|
HttpSimpleServer *httpSimpleServer = new HttpSimpleServer(this);
|
||||||
|
connect(httpSimpleServer, &HttpSimpleServer::requestReceived, this, &DevicePluginHttpCommander::onHttpSimpleServerRequestReceived);
|
||||||
m_httpSimpleServer.insert(device, httpSimpleServer);
|
m_httpSimpleServer.insert(device, httpSimpleServer);
|
||||||
|
|
||||||
|
QString interfaces;
|
||||||
|
foreach (const QNetworkInterface &interface, QNetworkInterface::allInterfaces()) {
|
||||||
|
foreach (QNetworkAddressEntry entry, interface.addressEntries()) {
|
||||||
|
if (entry.ip().protocol() == QAbstractSocket::IPv4Protocol && entry.ip().toString() != "127.0.0.1") {
|
||||||
|
interfaces.append(entry.ip().toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
device->setStateValue(httpServerServerAddressStateTypeId, interfaces);
|
||||||
return info->finish(Device::DeviceErrorNoError);
|
return info->finish(Device::DeviceErrorNoError);
|
||||||
}
|
}
|
||||||
info->finish(Device::DeviceErrorNoError);
|
info->finish(Device::DeviceErrorNoError);
|
||||||
|
|
@ -135,7 +147,7 @@ void DevicePluginHttpCommander::makeGetCall(Device *device)
|
||||||
url.setPort(device->paramValue(httpGetCommanderDevicePortParamTypeId).toInt());
|
url.setPort(device->paramValue(httpGetCommanderDevicePortParamTypeId).toInt());
|
||||||
QNetworkRequest request;
|
QNetworkRequest request;
|
||||||
request.setUrl(url);
|
request.setUrl(url);
|
||||||
request.setRawHeader("User-Agent", "nymea 1.0");
|
request.setRawHeader("User-Agent", "nymea http commander");
|
||||||
|
|
||||||
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
QNetworkReply *reply = hardwareManager()->networkManager()->get(request);
|
||||||
connect(reply, &QNetworkReply::finished, this, &DevicePluginHttpCommander::onGetRequestFinished);
|
connect(reply, &QNetworkReply::finished, this, &DevicePluginHttpCommander::onGetRequestFinished);
|
||||||
|
|
@ -221,6 +233,20 @@ void DevicePluginHttpCommander::onPutRequestFinished()
|
||||||
reply->deleteLater();
|
reply->deleteLater();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void DevicePluginHttpCommander::onHttpSimpleServerRequestReceived(const QString &type, const QString &path, const QString &body)
|
||||||
|
{
|
||||||
|
//qCDebug(dcHttpCommander()) << "Request recieved" << type << body;
|
||||||
|
HttpSimpleServer *httpServer = static_cast<HttpSimpleServer *>(sender());
|
||||||
|
Device *device = m_httpSimpleServer.key(httpServer);
|
||||||
|
Event ev = Event(httpServerTriggeredEventTypeId, device->id());
|
||||||
|
ParamList params;
|
||||||
|
params.append(Param(httpServerTriggeredEventRequestTypeParamTypeId, type));
|
||||||
|
params.append(Param(httpServerTriggeredEventPathParamTypeId, path));
|
||||||
|
params.append(Param(httpServerTriggeredEventBodyParamTypeId, body));
|
||||||
|
ev.setParams(params);
|
||||||
|
emit emitEvent(ev);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void DevicePluginHttpCommander::deviceRemoved(Device *device)
|
void DevicePluginHttpCommander::deviceRemoved(Device *device)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -40,6 +40,7 @@ class DevicePluginHttpCommander : public DevicePlugin
|
||||||
Q_INTERFACES(DevicePlugin)
|
Q_INTERFACES(DevicePlugin)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
||||||
explicit DevicePluginHttpCommander();
|
explicit DevicePluginHttpCommander();
|
||||||
|
|
||||||
void setupDevice(DeviceSetupInfo *info) override;
|
void setupDevice(DeviceSetupInfo *info) override;
|
||||||
|
|
@ -60,6 +61,8 @@ private slots:
|
||||||
void onGetRequestFinished();
|
void onGetRequestFinished();
|
||||||
void onPostRequestFinished();
|
void onPostRequestFinished();
|
||||||
void onPutRequestFinished();
|
void onPutRequestFinished();
|
||||||
|
|
||||||
|
void onHttpSimpleServerRequestReceived(const QString &type, const QString &path, const QString &body);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // DEVICEPLUGINHTTPCOMMANDER_H
|
#endif // DEVICEPLUGINHTTPCOMMANDER_H
|
||||||
|
|
|
||||||
|
|
@ -182,6 +182,16 @@
|
||||||
"defaultValue": "8000"
|
"defaultValue": "8000"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
"stateTypes": [
|
||||||
|
{
|
||||||
|
"id": "f4458008-67f2-4ba1-bbca-e85771a30ddc",
|
||||||
|
"name": "serverAddress",
|
||||||
|
"displayName": "Server address",
|
||||||
|
"displayNameEvent": "Server address changed",
|
||||||
|
"type": "QString",
|
||||||
|
"defaultValue": "127.0.0.1"
|
||||||
|
}
|
||||||
|
],
|
||||||
"eventTypes": [
|
"eventTypes": [
|
||||||
{
|
{
|
||||||
"id": "86f794c6-31ad-40a8-928f-4b8802506ce1",
|
"id": "86f794c6-31ad-40a8-928f-4b8802506ce1",
|
||||||
|
|
|
||||||
|
|
@ -36,11 +36,13 @@
|
||||||
HttpSimpleServer::HttpSimpleServer(QObject *parent):
|
HttpSimpleServer::HttpSimpleServer(QObject *parent):
|
||||||
QTcpServer(parent)
|
QTcpServer(parent)
|
||||||
{
|
{
|
||||||
|
listen(QHostAddress::Any, 7777);
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpSimpleServer::~HttpSimpleServer()
|
HttpSimpleServer::~HttpSimpleServer()
|
||||||
{
|
{
|
||||||
close();
|
close();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void HttpSimpleServer::incomingConnection(qintptr socket)
|
void HttpSimpleServer::incomingConnection(qintptr socket)
|
||||||
|
|
@ -49,10 +51,10 @@ void HttpSimpleServer::incomingConnection(qintptr socket)
|
||||||
// communication with the client is done over this QTcpSocket. QTcpSocket
|
// communication with the client is done over this QTcpSocket. QTcpSocket
|
||||||
// works asynchronously, this means that all the communication is done
|
// works asynchronously, this means that all the communication is done
|
||||||
// in the two slots readClient() and discardClient().
|
// in the two slots readClient() and discardClient().
|
||||||
QTcpSocket* s = new QTcpSocket(this);
|
QTcpSocket* tcpSocket = new QTcpSocket(this);
|
||||||
connect(s, SIGNAL(readyRead()), this, SLOT(readClient()));
|
connect(tcpSocket, SIGNAL(readyRead()), this, SLOT(readClient()));
|
||||||
connect(s, SIGNAL(disconnected()), this, SLOT(discardClient()));
|
connect(tcpSocket, SIGNAL(disconnected()), this, SLOT(discardClient()));
|
||||||
s->setSocketDescriptor(socket);
|
tcpSocket->setSocketDescriptor(socket);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -61,27 +63,56 @@ void HttpSimpleServer::readClient()
|
||||||
// This slot is called when the client sent data to the server. The
|
// This slot is called when the client sent data to the server. The
|
||||||
// server looks if it was a get request and sends a very simple HTML
|
// server looks if it was a get request and sends a very simple HTML
|
||||||
// document back.
|
// document back.
|
||||||
QTcpSocket* socket = static_cast<QTcpSocket*>(sender());
|
QTcpSocket* tcpSocket = static_cast<QTcpSocket*>(sender());
|
||||||
if (socket->canReadLine()) {
|
if (tcpSocket->canReadLine()) {
|
||||||
QByteArray data = socket->readLine();
|
|
||||||
|
QByteArray data = tcpSocket->readAll();
|
||||||
QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*"));
|
QStringList tokens = QString(data).split(QRegExp("[ \r\n][ \r\n]*"));
|
||||||
QUrl url("http://foo.bar" + tokens[1]);
|
//QUrl url("http://foo.bar" + tokens[1]);
|
||||||
QUrlQuery query(url);
|
//QUrlQuery query(url);
|
||||||
|
qCDebug(dcHttpCommander()) << "Http Request, type" << tokens[0] << "path" << tokens[1] << "body" << tokens.last();
|
||||||
|
|
||||||
if (tokens[0] == "GET") {
|
if (tokens[0] == "GET") {
|
||||||
QTextStream os(socket);
|
QTextStream os(tcpSocket);
|
||||||
os.setAutoDetectUnicode(true);
|
os.setAutoDetectUnicode(true);
|
||||||
os << generateHeader();
|
os << generateHeader();
|
||||||
socket->close();
|
tcpSocket->close();
|
||||||
|
|
||||||
if (socket->state() == QTcpSocket::UnconnectedState)
|
if (tcpSocket->state() == QTcpSocket::UnconnectedState)
|
||||||
delete socket;
|
delete tcpSocket;
|
||||||
|
|
||||||
|
emit requestReceived(tokens[0], tokens[1], tokens.last());
|
||||||
} else if (tokens[0] == "PUT") {
|
} else if (tokens[0] == "PUT") {
|
||||||
|
QTextStream os(tcpSocket);
|
||||||
|
os.setAutoDetectUnicode(true);
|
||||||
|
os << generateHeader();
|
||||||
|
tcpSocket->close();
|
||||||
|
|
||||||
|
if (tcpSocket->state() == QTcpSocket::UnconnectedState)
|
||||||
|
delete tcpSocket;
|
||||||
|
|
||||||
|
emit requestReceived(tokens[0], tokens[1], tokens.last());
|
||||||
|
|
||||||
} else if (tokens[0] == "POST") {
|
} else if (tokens[0] == "POST") {
|
||||||
|
QTextStream os(tcpSocket);
|
||||||
|
os.setAutoDetectUnicode(true);
|
||||||
|
os << generateHeader();
|
||||||
|
tcpSocket->close();
|
||||||
|
|
||||||
|
if (tcpSocket->state() == QTcpSocket::UnconnectedState)
|
||||||
|
delete tcpSocket;
|
||||||
|
emit requestReceived(tokens[0], tokens[1], tokens.last());
|
||||||
|
|
||||||
} else if (tokens[0] == "DELETE") {
|
} else if (tokens[0] == "DELETE") {
|
||||||
|
QTextStream os(tcpSocket);
|
||||||
|
os.setAutoDetectUnicode(true);
|
||||||
|
os << generateHeader();
|
||||||
|
tcpSocket->close();
|
||||||
|
|
||||||
|
if (tcpSocket->state() == QTcpSocket::UnconnectedState)
|
||||||
|
delete tcpSocket;
|
||||||
|
|
||||||
|
emit requestReceived(tokens[0], tokens[1], tokens.last());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,14 +22,15 @@
|
||||||
* *
|
* *
|
||||||
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
#ifndef HTTPSIMPLESERVER_H
|
#ifndef HTTPSIMPLESERVER1_H
|
||||||
#define HTTPSIMPLESERVER_H
|
#define HTTPSIMPLESERVER1_H
|
||||||
|
|
||||||
#include "typeutils.h"
|
#include "typeutils.h"
|
||||||
|
|
||||||
#include <QTcpServer>
|
#include <QTcpServer>
|
||||||
#include <QUuid>
|
#include <QUuid>
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QUrl>
|
||||||
|
|
||||||
class Device;
|
class Device;
|
||||||
class DevicePlugin;
|
class DevicePlugin;
|
||||||
|
|
@ -38,6 +39,7 @@ class HttpSimpleServer : public QTcpServer
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
|
|
||||||
HttpSimpleServer(QObject* parent = nullptr);
|
HttpSimpleServer(QObject* parent = nullptr);
|
||||||
~HttpSimpleServer() override;
|
~HttpSimpleServer() override;
|
||||||
void incomingConnection(qintptr socket) override;
|
void incomingConnection(qintptr socket) override;
|
||||||
|
|
@ -45,7 +47,9 @@ public:
|
||||||
signals:
|
signals:
|
||||||
void disappear();
|
void disappear();
|
||||||
void reconfigureAutodevice();
|
void reconfigureAutodevice();
|
||||||
void getRequestReceied(QUrl url);
|
//void getRequestReceied(QUrl url);
|
||||||
|
|
||||||
|
void requestReceived(const QString &type, const QString &path, const QString &body);
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void readClient();
|
void readClient();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue