restructured to proper message allocation
This commit is contained in:
parent
f8df49845b
commit
f0310205ad
@ -36,12 +36,25 @@ IntegrationPluginSma::IntegrationPluginSma()
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginSma::discoverThings(ThingDiscoveryInfo *info)
|
||||||
|
{
|
||||||
|
if (info->thingClassId() == sunnyWebBoxThingClassId) {
|
||||||
|
|
||||||
|
info->finish(Thing::ThingErrorNoError);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
|
void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
|
||||||
{
|
{
|
||||||
Thing *thing = info->thing();
|
Thing *thing = info->thing();
|
||||||
|
|
||||||
if (!m_udpSocket) {
|
if (!m_sunnyWebBoxCommunication) {
|
||||||
m_udpSocket = new QUdpSocket(this);
|
m_sunnyWebBoxCommunication = new SunnyWebBoxCommunication(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!m_refreshTimer) {
|
||||||
|
m_refreshTimer = hardwareManager()->pluginTimerManager()->registerTimer(10);
|
||||||
|
connect(m_refreshTimer, &PluginTimer::timeout, this, &IntegrationPluginSma::onRefreshTimer);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (thing->thingClassId() == sunnyWebBoxThingClassId) {
|
if (thing->thingClassId() == sunnyWebBoxThingClassId) {
|
||||||
@ -54,6 +67,10 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
SunnyWebBox *sunnyWebBox = new SunnyWebBox(m_sunnyWebBoxCommunication, QHostAddress(thing->paramValue(sunnyWebBoxThingHostParamTypeId).toString()), this);
|
||||||
|
m_sunnyWebBoxes.insert(thing, sunnyWebBox);
|
||||||
|
//TODO m_asyncSetup
|
||||||
|
return info->finish(Thing::ThingErrorNoError);
|
||||||
|
|
||||||
} else if (thing->thingClassId() == inverterThingClassId) {
|
} else if (thing->thingClassId() == inverterThingClassId) {
|
||||||
Thing *parentThing = myThings().findById(thing->parentId());
|
Thing *parentThing = myThings().findById(thing->parentId());
|
||||||
@ -84,6 +101,26 @@ void IntegrationPluginSma::postSetupThing(Thing *thing)
|
|||||||
return;
|
return;
|
||||||
sunnyWebBox->getDevices();
|
sunnyWebBox->getDevices();
|
||||||
} else if (thing->thingClassId() == inverterThingClassId) {
|
} else if (thing->thingClassId() == inverterThingClassId) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginSma::executeAction(ThingActionInfo *info)
|
||||||
|
{
|
||||||
|
Thing *thing = info->thing();
|
||||||
|
Action action = info->action();
|
||||||
|
|
||||||
|
if (thing->thingClassId() == sunnyWebBoxThingClassId) {
|
||||||
|
SunnyWebBox *sunnyWebBox = m_sunnyWebBoxes.value(thing);
|
||||||
|
if (!sunnyWebBox)
|
||||||
|
return;
|
||||||
|
if (action.actionTypeId() == sunnyWebBoxSearchDevicesActionTypeId) {
|
||||||
|
sunnyWebBox->getDevices();
|
||||||
|
} else {
|
||||||
|
//Unhandled actionTypeId
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
Q_ASSERT_X(false, "executeAction", QString("Unhandled thingClassId: %1").arg(thing->thingClassId().toString()).toUtf8());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,16 +131,58 @@ void IntegrationPluginSma::thingRemoved(Thing *thing)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (myThings().filterByThingClassId(sunnyWebBoxThingClassId).isEmpty()) {
|
if (myThings().filterByThingClassId(sunnyWebBoxThingClassId).isEmpty()) {
|
||||||
m_udpSocket->deleteLater();
|
m_sunnyWebBoxCommunication->deleteLater();
|
||||||
m_udpSocket = nullptr;
|
m_sunnyWebBoxCommunication = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginSma::onRefreshTimer()
|
||||||
|
{
|
||||||
|
Q_FOREACH(SunnyWebBox *sunnyWebBox, m_sunnyWebBoxes) {
|
||||||
|
sunnyWebBox->getPlantOverview();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginSma::onPlantOverviewReceived(int messageId, SunnyWebBox::Overview overview)
|
||||||
|
{
|
||||||
|
Q_UNUSED(messageId)
|
||||||
|
|
||||||
|
Thing *thing = m_sunnyWebBoxes.key(static_cast<SunnyWebBox *>(sender()));
|
||||||
|
if (!thing)
|
||||||
|
return;
|
||||||
|
|
||||||
|
thing->setStateValue(sunnyWebBoxConnectedStateTypeId, true);
|
||||||
|
thing->setStateValue(sunnyWebBoxCurrentPowerStateTypeId, overview.power);
|
||||||
|
thing->setStateValue(sunnyWebBoxDayEnergyStateTypeId, overview.dailyYield);
|
||||||
|
thing->setStateValue(sunnyWebBoxTotalEnergyStateTypeId, overview.totalYield);
|
||||||
|
thing->setStateValue(sunnyWebBoxModeStateTypeId, overview.status);
|
||||||
|
if (!overview.error.isEmpty()){
|
||||||
|
qCDebug(dcSma()) << "Received error" << overview.error;
|
||||||
|
thing->setStateValue(sunnyWebBoxErrorStateTypeId, overview.error);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void IntegrationPluginSma::onDevicesReceived(int messageId, QList<SunnyWebBox::Device> devices)
|
||||||
|
{
|
||||||
|
Q_UNUSED(messageId)
|
||||||
|
|
||||||
|
Thing *thing = m_sunnyWebBoxes.key(static_cast<SunnyWebBox *>(sender()));
|
||||||
|
if (!thing)
|
||||||
|
return;
|
||||||
|
|
||||||
|
ThingDescriptors descriptors;
|
||||||
|
Q_FOREACH(SunnyWebBox::Device device, devices){
|
||||||
|
ThingDescriptor descriptor(inverterThingClassId, device.name, device.key ,thing->id());
|
||||||
|
descriptors.append(descriptor);
|
||||||
|
}
|
||||||
|
emit autoThingsAppeared(descriptors);
|
||||||
|
}
|
||||||
|
|
||||||
SunnyWebBox * IntegrationPluginSma::createSunnyWebBoxConnection(Thing *thing)
|
SunnyWebBox * IntegrationPluginSma::createSunnyWebBoxConnection(Thing *thing)
|
||||||
{
|
{
|
||||||
SunnyWebBox *sunnyWebBox = new SunnyWebBox(m_udpSocket, this);
|
SunnyWebBox *sunnyWebBox = new SunnyWebBox(m_sunnyWebBoxCommunication, QHostAddress(thing->paramValue(sunnyWebBoxThingHostParamTypeId).toString()), this);
|
||||||
m_sunnyWebBoxes.insert(thing, sunnyWebBox);
|
m_sunnyWebBoxes.insert(thing, sunnyWebBox);
|
||||||
//connect();
|
connect(sunnyWebBox, &SunnyWebBox::plantOverviewReceived, this, &IntegrationPluginSma::onPlantOverviewReceived);
|
||||||
return sunnyWebBox;
|
return sunnyWebBox;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -34,6 +34,7 @@
|
|||||||
#include "integrations/integrationplugin.h"
|
#include "integrations/integrationplugin.h"
|
||||||
#include "plugintimer.h"
|
#include "plugintimer.h"
|
||||||
#include "sunnywebbox.h"
|
#include "sunnywebbox.h"
|
||||||
|
#include "sunnywebboxcommunication.h"
|
||||||
|
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
@ -48,18 +49,23 @@ class IntegrationPluginSma: public IntegrationPlugin {
|
|||||||
public:
|
public:
|
||||||
explicit IntegrationPluginSma();
|
explicit IntegrationPluginSma();
|
||||||
|
|
||||||
|
void discoverThings(ThingDiscoveryInfo *info) override;
|
||||||
void setupThing(ThingSetupInfo *info) override;
|
void setupThing(ThingSetupInfo *info) override;
|
||||||
void postSetupThing(Thing *thing) override;
|
void postSetupThing(Thing *thing) override;
|
||||||
|
void executeAction(ThingActionInfo *info) override;
|
||||||
void thingRemoved(Thing *thing) override;
|
void thingRemoved(Thing *thing) override;
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void onRefreshTimer();
|
void onRefreshTimer();
|
||||||
|
|
||||||
|
void onPlantOverviewReceived(int messageId, SunnyWebBox::Overview overview);
|
||||||
|
void onDevicesReceived(int messageId, QList<SunnyWebBox::Device> devices);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
PluginTimer *m_refreshTimer = nullptr;
|
PluginTimer *m_refreshTimer = nullptr;
|
||||||
QHash<Thing *, SunnyWebBox *> m_sunnyWebBoxes;
|
QHash<Thing *, SunnyWebBox *> m_sunnyWebBoxes;
|
||||||
QHash<Thing *, ThingSetupInfo *> m_asyncSetup;
|
QHash<Thing *, ThingSetupInfo *> m_asyncSetup;
|
||||||
QUdpSocket *m_udpSocket = nullptr;
|
SunnyWebBoxCommunication *m_sunnyWebBoxCommunication = nullptr;
|
||||||
|
|
||||||
SunnyWebBox * createSunnyWebBoxConnection(Thing *thing);
|
SunnyWebBox * createSunnyWebBoxConnection(Thing *thing);
|
||||||
void setupChild(ThingSetupInfo *info, Thing *parentThing);
|
void setupChild(ThingSetupInfo *info, Thing *parentThing);
|
||||||
|
|||||||
@ -6,7 +6,9 @@ QT += \
|
|||||||
SOURCES += \
|
SOURCES += \
|
||||||
integrationpluginsma.cpp \
|
integrationpluginsma.cpp \
|
||||||
sunnywebbox.cpp \
|
sunnywebbox.cpp \
|
||||||
|
sunnywebboxcommunication.cpp \
|
||||||
|
|
||||||
HEADERS += \
|
HEADERS += \
|
||||||
integrationpluginsma.h \
|
integrationpluginsma.h \
|
||||||
sunnywebbox.h \
|
sunnywebbox.h \
|
||||||
|
sunnywebboxcommunication.h \
|
||||||
|
|||||||
@ -34,98 +34,38 @@
|
|||||||
#include "QJsonDocument"
|
#include "QJsonDocument"
|
||||||
#include "QJsonObject"
|
#include "QJsonObject"
|
||||||
|
|
||||||
SunnyWebBox::SunnyWebBox(QUdpSocket *udpSocket, QObject *parrent) :
|
SunnyWebBox::SunnyWebBox(SunnyWebBoxCommunication *communication, const QHostAddress &hostAddress, QObject *parrent) :
|
||||||
QObject(parrent),
|
QObject(parrent),
|
||||||
m_udpSocket(udpSocket)
|
m_hostAddresss(hostAddress),
|
||||||
|
m_communication(communication)
|
||||||
{
|
{
|
||||||
connect(m_udpSocket, &QUdpSocket::stateChanged, this, [this](QAbstractSocket::SocketState state) {
|
//TODO connect communication with socket state;
|
||||||
emit connectedChanged((state == QAbstractSocket::SocketState::ConnectedState));
|
connect(m_communication, &SunnyWebBoxCommunication::messageReceived, this, &SunnyWebBox::onMessageReceived);
|
||||||
});
|
|
||||||
|
|
||||||
connect(m_udpSocket, &QUdpSocket::readyRead, this, [this] {
|
|
||||||
//m_udpSocket->readDatagram(QByteArray())
|
|
||||||
qCDebug(dcSma()) << "Received datagram" << m_udpSocket->readAll();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int SunnyWebBox::getPlantOverview()
|
int SunnyWebBox::getPlantOverview()
|
||||||
{
|
{
|
||||||
return sendMessage("GetPlantOverview");
|
return m_communication->sendMessage(m_hostAddresss, "GetPlantOverview");
|
||||||
}
|
}
|
||||||
|
|
||||||
int SunnyWebBox::getDevices()
|
int SunnyWebBox::getDevices()
|
||||||
{
|
{
|
||||||
return sendMessage("GetDevices");
|
return m_communication->sendMessage(m_hostAddresss, "GetDevices");
|
||||||
}
|
}
|
||||||
|
|
||||||
int SunnyWebBox::getProcessDataChannels(const QString &deviceId)
|
int SunnyWebBox::getProcessDataChannels(const QString &deviceId)
|
||||||
{
|
{
|
||||||
QJsonObject params;
|
QJsonObject params;
|
||||||
params["device"] = deviceId;
|
params["device"] = deviceId;
|
||||||
return sendMessage("GetProcessDataChannels", params);
|
return m_communication->sendMessage(m_hostAddresss, "GetProcessDataChannels", params);
|
||||||
}
|
}
|
||||||
|
|
||||||
int SunnyWebBox::sendMessage(const QString &procedure)
|
void SunnyWebBox::onMessageReceived(const QHostAddress &address, int messageId, const QString &messageType, const QVariantMap &result)
|
||||||
{
|
{
|
||||||
int requestId = qrand();
|
if (address != m_hostAddresss) {
|
||||||
|
|
||||||
QJsonDocument doc;
|
|
||||||
QJsonObject obj;
|
|
||||||
obj["version"] = "1.0";
|
|
||||||
obj["proc"] = procedure;
|
|
||||||
obj["id"] = requestId;
|
|
||||||
obj["format"] = "JSON";
|
|
||||||
m_udpSocket->writeDatagram(doc.toJson(), m_hostAddresss, m_port);
|
|
||||||
return requestId;
|
|
||||||
}
|
|
||||||
|
|
||||||
int SunnyWebBox::sendMessage(const QString &procedure, const QJsonObject ¶ms)
|
|
||||||
{
|
|
||||||
int requestId = qrand();
|
|
||||||
|
|
||||||
QJsonDocument doc;
|
|
||||||
QJsonObject obj;
|
|
||||||
obj["version"] = "1.0";
|
|
||||||
obj["proc"] = procedure;
|
|
||||||
obj["id"] = requestId;
|
|
||||||
obj["format"] = "JSON";
|
|
||||||
if (!params.isEmpty()) {
|
|
||||||
obj.insert("params", params);
|
|
||||||
}
|
|
||||||
m_udpSocket->writeDatagram(doc.toJson(), m_hostAddresss, m_port);
|
|
||||||
return requestId;
|
|
||||||
}
|
|
||||||
|
|
||||||
void SunnyWebBox::onDatagramReceived(const QByteArray &data)
|
|
||||||
{
|
|
||||||
QJsonParseError error;
|
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
|
||||||
if (error.error != QJsonParseError::NoError) {
|
|
||||||
qCWarning(dcSma()) << "Could not parse JSON" << error.errorString();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!doc.isObject()) {
|
|
||||||
qCWarning(dcSma()) << "JSON is not an Object";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
QVariantMap map = doc.toVariant().toMap();
|
|
||||||
if (map["version"] != "1.0") {
|
|
||||||
qCWarning(dcSma()) << "API version not supported" << map["version"];
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (map.contains("proc") && map.contains("result")) {
|
|
||||||
QString requestType = map["proc"].toString();
|
|
||||||
int requestId = map["id"].toInt();
|
|
||||||
QVariantMap result = map.value("result").toMap();
|
|
||||||
emit messageResponseReceived(requestId, requestType, result);
|
|
||||||
} else {
|
|
||||||
qCWarning(dcSma()) << "Missing proc or result value";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SunnyWebBox::parseMessageReponse(int messageId, const QString &messageType, const QVariantMap &result)
|
|
||||||
{
|
|
||||||
if (messageType == "GetPlantOverview") {
|
if (messageType == "GetPlantOverview") {
|
||||||
Overview overview;
|
Overview overview;
|
||||||
QVariantList overviewList = result.value("overview").toList();
|
QVariantList overviewList = result.value("overview").toList();
|
||||||
|
|||||||
@ -32,6 +32,7 @@
|
|||||||
#define SUNNYWEBBOX_H
|
#define SUNNYWEBBOX_H
|
||||||
|
|
||||||
#include "integrations/thing.h"
|
#include "integrations/thing.h"
|
||||||
|
#include "sunnywebboxcommunication.h"
|
||||||
|
|
||||||
#include <QObject>
|
#include <QObject>
|
||||||
#include <QHostAddress>
|
#include <QHostAddress>
|
||||||
@ -56,7 +57,7 @@ public:
|
|||||||
QList<Device> childrens;
|
QList<Device> childrens;
|
||||||
};
|
};
|
||||||
|
|
||||||
explicit SunnyWebBox(QUdpSocket *udpSocket, QObject *parrent = 0);
|
explicit SunnyWebBox(SunnyWebBoxCommunication *communication, const QHostAddress &hostAddress, QObject *parrent = 0);
|
||||||
|
|
||||||
int getPlantOverview();
|
int getPlantOverview();
|
||||||
int getDevices();
|
int getDevices();
|
||||||
@ -69,20 +70,17 @@ public:
|
|||||||
QHostAddress hostAddress();
|
QHostAddress hostAddress();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_port = 34268;
|
|
||||||
QHostAddress m_hostAddresss;
|
|
||||||
QUdpSocket *m_udpSocket = nullptr;
|
|
||||||
|
|
||||||
int sendMessage(const QString &procedure);
|
QHostAddress m_hostAddresss;
|
||||||
int sendMessage(const QString &procedure, const QJsonObject ¶ms);
|
SunnyWebBoxCommunication *m_communication = nullptr;
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void onDatagramReceived(const QByteArray &data);
|
void onDatagramReceived(const QByteArray &data);
|
||||||
void parseMessageReponse(int messageId, const QString &messageType, const QVariantMap &result);
|
void onMessageReceived(const QHostAddress &address, int messageId, const QString &messageType, const QVariantMap &result);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void connectedChanged(bool connected);
|
void connectedChanged(bool connected);
|
||||||
void messageResponseReceived(int messageId, const QString &messageType, const QVariantMap &result);
|
|
||||||
void plantOverviewReceived(int messageId, Overview overview);
|
void plantOverviewReceived(int messageId, Overview overview);
|
||||||
void devicesReceived(int messageId, QList<Device> devices);
|
void devicesReceived(int messageId, QList<Device> devices);
|
||||||
};
|
};
|
||||||
|
|||||||
118
sma/sunnywebboxcommunication.cpp
Normal file
118
sma/sunnywebboxcommunication.cpp
Normal file
@ -0,0 +1,118 @@
|
|||||||
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
*
|
||||||
|
* Copyright 2013 - 2020, nymea GmbH
|
||||||
|
* Contact: contact@nymea.io
|
||||||
|
*
|
||||||
|
* This file is part of nymea.
|
||||||
|
* This project including source code and documentation is protected by
|
||||||
|
* copyright law, and remains the property of nymea GmbH. All rights, including
|
||||||
|
* reproduction, publication, editing and translation, are reserved. The use of
|
||||||
|
* this project is subject to the terms of a license agreement to be concluded
|
||||||
|
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
|
||||||
|
* under https://nymea.io/license
|
||||||
|
*
|
||||||
|
* GNU Lesser General Public License Usage
|
||||||
|
* Alternatively, this project may be redistributed and/or modified under the
|
||||||
|
* terms of the GNU Lesser General Public License as published by the Free
|
||||||
|
* Software Foundation; version 3. This project 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 project. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* For any further details and any questions please contact us under
|
||||||
|
* contact@nymea.io or see our FAQ/Licensing Information on
|
||||||
|
* https://nymea.io/license/faq
|
||||||
|
*
|
||||||
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
|
#include "sunnywebboxcommunication.h"
|
||||||
|
#include "extern-plugininfo.h"
|
||||||
|
|
||||||
|
#include "QJsonDocument"
|
||||||
|
#include "QJsonObject"
|
||||||
|
|
||||||
|
SunnyWebBoxCommunication::SunnyWebBoxCommunication(QObject *parent) : QObject(parent)
|
||||||
|
{
|
||||||
|
m_udpSocket = new QUdpSocket(this);
|
||||||
|
m_udpSocket->bind(QHostAddress::LocalHost, m_port);
|
||||||
|
|
||||||
|
connect(m_udpSocket, &QUdpSocket::stateChanged, this, [this](QAbstractSocket::SocketState state) {
|
||||||
|
emit socketConnected(state == QAbstractSocket::SocketState::ConnectedState);
|
||||||
|
});
|
||||||
|
|
||||||
|
connect(m_udpSocket, &QUdpSocket::readyRead, this, [this] {
|
||||||
|
|
||||||
|
QHostAddress address;
|
||||||
|
quint16 port;
|
||||||
|
QByteArray data;
|
||||||
|
while (m_udpSocket->hasPendingDatagrams()) {
|
||||||
|
qCDebug(dcSma()) << "Received datagram";
|
||||||
|
int receivedBytes = m_udpSocket->readDatagram(data.data(), 1000, &address, &port);
|
||||||
|
if (receivedBytes == -1) {
|
||||||
|
qCWarning(dcSma()) << "Error reading pending datagram";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
int SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure)
|
||||||
|
{
|
||||||
|
int requestId = qrand();
|
||||||
|
|
||||||
|
QJsonDocument doc;
|
||||||
|
QJsonObject obj;
|
||||||
|
obj["version"] = "1.0";
|
||||||
|
obj["proc"] = procedure;
|
||||||
|
obj["id"] = requestId;
|
||||||
|
obj["format"] = "JSON";
|
||||||
|
m_udpSocket->writeDatagram(doc.toJson(), address, m_port);
|
||||||
|
return requestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
int SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms)
|
||||||
|
{
|
||||||
|
int requestId = qrand();
|
||||||
|
|
||||||
|
QJsonDocument doc;
|
||||||
|
QJsonObject obj;
|
||||||
|
obj["version"] = "1.0";
|
||||||
|
obj["proc"] = procedure;
|
||||||
|
obj["id"] = requestId;
|
||||||
|
obj["format"] = "JSON";
|
||||||
|
if (!params.isEmpty()) {
|
||||||
|
obj.insert("params", params);
|
||||||
|
}
|
||||||
|
m_udpSocket->writeDatagram(doc.toJson(), address, m_port);
|
||||||
|
return requestId;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SunnyWebBoxCommunication::datagramReceived(const QHostAddress &address, const QByteArray &data)
|
||||||
|
{
|
||||||
|
QJsonParseError error;
|
||||||
|
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
|
||||||
|
if (error.error != QJsonParseError::NoError) {
|
||||||
|
qCWarning(dcSma()) << "Could not parse JSON" << error.errorString();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!doc.isObject()) {
|
||||||
|
qCWarning(dcSma()) << "JSON is not an Object";
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
QVariantMap map = doc.toVariant().toMap();
|
||||||
|
if (map["version"] != "1.0") {
|
||||||
|
qCWarning(dcSma()) << "API version not supported" << map["version"];
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (map.contains("proc") && map.contains("result")) {
|
||||||
|
QString requestType = map["proc"].toString();
|
||||||
|
int requestId = map["id"].toInt();
|
||||||
|
QVariantMap result = map.value("result").toMap();
|
||||||
|
emit messageReceived(address, requestId, requestType, result);
|
||||||
|
} else {
|
||||||
|
qCWarning(dcSma()) << "Missing proc or result value";
|
||||||
|
}
|
||||||
|
}
|
||||||
58
sma/sunnywebboxcommunication.h
Normal file
58
sma/sunnywebboxcommunication.h
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
|
||||||
|
*
|
||||||
|
* Copyright 2013 - 2020, nymea GmbH
|
||||||
|
* Contact: contact@nymea.io
|
||||||
|
*
|
||||||
|
* This file is part of nymea.
|
||||||
|
* This project including source code and documentation is protected by
|
||||||
|
* copyright law, and remains the property of nymea GmbH. All rights, including
|
||||||
|
* reproduction, publication, editing and translation, are reserved. The use of
|
||||||
|
* this project is subject to the terms of a license agreement to be concluded
|
||||||
|
* with nymea GmbH in accordance with the terms of use of nymea GmbH, available
|
||||||
|
* under https://nymea.io/license
|
||||||
|
*
|
||||||
|
* GNU Lesser General Public License Usage
|
||||||
|
* Alternatively, this project may be redistributed and/or modified under the
|
||||||
|
* terms of the GNU Lesser General Public License as published by the Free
|
||||||
|
* Software Foundation; version 3. This project 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 project. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*
|
||||||
|
* For any further details and any questions please contact us under
|
||||||
|
* contact@nymea.io or see our FAQ/Licensing Information on
|
||||||
|
* https://nymea.io/license/faq
|
||||||
|
*
|
||||||
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
|
||||||
|
|
||||||
|
#ifndef SUNNYWEBBOXCOMMUNICATION_H
|
||||||
|
#define SUNNYWEBBOXCOMMUNICATION_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QUdpSocket>
|
||||||
|
|
||||||
|
class SunnyWebBoxCommunication : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
explicit SunnyWebBoxCommunication(QObject *parent = nullptr);
|
||||||
|
|
||||||
|
int sendMessage(const QHostAddress &address, const QString &procedure);
|
||||||
|
int sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms);
|
||||||
|
|
||||||
|
private:
|
||||||
|
int m_port = 34268;
|
||||||
|
QUdpSocket *m_udpSocket;
|
||||||
|
|
||||||
|
void datagramReceived(const QHostAddress &address, const QByteArray &data);
|
||||||
|
|
||||||
|
signals:
|
||||||
|
void socketConnected(bool connected);
|
||||||
|
void messageReceived(const QHostAddress &address, int messageId, const QString &messageType, const QVariantMap &result);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif // SUNNYWEBBOXCOMMUNICATION_H
|
||||||
Loading…
x
Reference in New Issue
Block a user