testing http instead of udp
This commit is contained in:
parent
8a5f32c479
commit
1459ab4d8f
@ -75,11 +75,11 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
|
||||
Thing *thing = info->thing();
|
||||
|
||||
if (!m_sunnyWebBoxCommunication) {
|
||||
m_sunnyWebBoxCommunication = new SunnyWebBoxCommunication(this);
|
||||
m_sunnyWebBoxCommunication = new SunnyWebBoxCommunication(hardwareManager()->networkManager(), this);
|
||||
}
|
||||
|
||||
if (!m_refreshTimer) {
|
||||
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
|
||||
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(30);
|
||||
connect(m_refreshTimer, &PluginTimer::timeout, this, &IntegrationPluginSma::onRefreshTimer);
|
||||
}
|
||||
|
||||
|
||||
@ -35,8 +35,12 @@
|
||||
#include "QJsonObject"
|
||||
#include <QUuid>
|
||||
#include <QTimer>
|
||||
#include <QNetworkReply>
|
||||
#include <QNetworkRequest>
|
||||
|
||||
SunnyWebBoxCommunication::SunnyWebBoxCommunication(QObject *parent) : QObject(parent)
|
||||
SunnyWebBoxCommunication::SunnyWebBoxCommunication(NetworkAccessManager *networkAccessManager, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_networkManager(networkAccessManager)
|
||||
{
|
||||
m_udpSocket = new QUdpSocket(this);
|
||||
m_udpSocket->bind(QHostAddress::Any, m_port);
|
||||
@ -65,25 +69,43 @@ QString SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const
|
||||
|
||||
QJsonDocument doc;
|
||||
QJsonObject obj;
|
||||
obj.insert("params", QJsonObject());
|
||||
obj["format"] = "JSON";
|
||||
obj["id"] = requestId;
|
||||
obj["proc"] = procedure;
|
||||
obj["version"] = "1.0";
|
||||
doc.setObject(obj);
|
||||
|
||||
QUrl url;
|
||||
url.setHost(address.toString());
|
||||
url.setPath("/rpc");
|
||||
url.setScheme("http");
|
||||
QNetworkRequest request(url);
|
||||
request.setHeader(QNetworkRequest::KnownHeaders::ContentTypeHeader, "application/json");
|
||||
QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact);
|
||||
qCDebug(dcSma()) << "Send message" << data << address << m_port;
|
||||
if(m_messageResponsePending) {
|
||||
|
||||
QNetworkReply *reply = m_networkManager->post(request, data);
|
||||
connect(reply, &QNetworkReply::finished, reply, &QNetworkReply::deleteLater);
|
||||
connect(reply, &QNetworkReply::finished, this, [this, address, requestId, reply]{
|
||||
|
||||
//int status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
|
||||
QByteArray data = reply->readAll();
|
||||
qCDebug(dcSma()) << "Received reply" << data;
|
||||
datagramReceived(address, data);
|
||||
});
|
||||
|
||||
/* if(!m_messageResponsePending) {
|
||||
qCDebug(dcSma()) << "Send message" << data << address << m_port;
|
||||
m_udpSocket->writeDatagram(data, address, m_port);
|
||||
m_messageResponsePending = true;
|
||||
} else {
|
||||
if (m_messageQueue[address].length() < 40) {
|
||||
qCDebug(dcSma()) << "Adding message to queue" << data << address << m_port;
|
||||
m_messageQueue[address].append(data);
|
||||
} else {
|
||||
qCWarning(dcSma()) << "Message queue overflow";
|
||||
return "";
|
||||
}
|
||||
}
|
||||
}*/
|
||||
return requestId;
|
||||
}
|
||||
|
||||
@ -101,12 +123,13 @@ QString SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const
|
||||
obj["version"] = "1.0";
|
||||
doc.setObject(obj);
|
||||
QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact);
|
||||
qCDebug(dcSma()) << "Send message" << data << address << m_port;
|
||||
if(m_messageResponsePending) {
|
||||
if(!m_messageResponsePending) {
|
||||
qCDebug(dcSma()) << "Send message" << data << address << m_port;
|
||||
m_udpSocket->writeDatagram(data, address, m_port);
|
||||
m_messageResponsePending = true;
|
||||
} else {
|
||||
if (m_messageQueue[address].length() < 40) {
|
||||
qCDebug(dcSma()) << "Adding message to queue" << data << address << m_port;
|
||||
m_messageQueue[address].append(data);
|
||||
} else {
|
||||
qCDebug(dcSma()) << "Message queue overflow";
|
||||
|
||||
@ -34,16 +34,19 @@
|
||||
#include <QObject>
|
||||
#include <QUdpSocket>
|
||||
|
||||
#include "network/networkaccessmanager.h"
|
||||
|
||||
class SunnyWebBoxCommunication : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit SunnyWebBoxCommunication(QObject *parent = nullptr);
|
||||
explicit SunnyWebBoxCommunication(NetworkAccessManager *networkAccessManager, QObject *parent = nullptr);
|
||||
|
||||
QString sendMessage(const QHostAddress &address, const QString &procedure);
|
||||
QString sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms);
|
||||
|
||||
private:
|
||||
NetworkAccessManager *m_networkManager;
|
||||
int m_port = 34268;
|
||||
bool m_messageResponsePending = false;
|
||||
QUdpSocket *m_udpSocket;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user