moved from qrand to Quuid

This commit is contained in:
bernhard.trinnes 2020-08-18 14:58:29 +02:00 committed by Simon Stürz
parent f3481b84d3
commit 837751c06c
6 changed files with 66 additions and 56 deletions

View File

@ -100,7 +100,7 @@ void IntegrationPluginSma::setupThing(ThingSetupInfo *info)
connect(sunnyWebBox, &SunnyWebBox::parameterChannelsReceived, this, &IntegrationPluginSma::onParameterChannelsReceived);
m_sunnyWebBoxes.insert(thing, sunnyWebBox);
connect(info, &ThingSetupInfo::aborted, this, [thing, this] { m_sunnyWebBoxes.remove(thing);});
int requestId = sunnyWebBox->getPlantOverview();
QUuid requestId = sunnyWebBox->getPlantOverview();
m_asyncSetup.insert(requestId, info);
return info->finish(Thing::ThingErrorNoError);
@ -147,7 +147,7 @@ void IntegrationPluginSma::executeAction(ThingActionInfo *info)
if (!sunnyWebBox)
return;
if (action.actionTypeId() == sunnyWebBoxSearchDevicesActionTypeId) {
int requestId = sunnyWebBox->getDevices();
QUuid requestId = sunnyWebBox->getDevices();
m_asyncActions.insert(requestId, info);
connect(info, &ThingActionInfo::aborted, info, [requestId, this] {m_asyncActions.remove(requestId);});
} else {
@ -177,7 +177,7 @@ void IntegrationPluginSma::onRefreshTimer()
}
}
void IntegrationPluginSma::onPlantOverviewReceived(int messageId, SunnyWebBox::Overview overview)
void IntegrationPluginSma::onPlantOverviewReceived(const QUuid &messageId, SunnyWebBox::Overview overview)
{
if (m_asyncSetup.contains(messageId)) {
ThingSetupInfo *info = m_asyncSetup.value(messageId);
@ -200,7 +200,7 @@ void IntegrationPluginSma::onPlantOverviewReceived(int messageId, SunnyWebBox::O
}
}
void IntegrationPluginSma::onDevicesReceived(int messageId, QList<SunnyWebBox::Device> devices)
void IntegrationPluginSma::onDevicesReceived(const QUuid &messageId, QList<SunnyWebBox::Device> devices)
{
if (m_asyncActions.contains(messageId)) {
ThingActionInfo *info = m_asyncActions.value(messageId);
@ -221,7 +221,7 @@ void IntegrationPluginSma::onDevicesReceived(int messageId, QList<SunnyWebBox::D
emit autoThingsAppeared(descriptors);
}
void IntegrationPluginSma::onProcessDataReceived(int messageId, const QString &deviceKey, const QHash<QString, QVariant> &channels)
void IntegrationPluginSma::onProcessDataReceived(const QUuid &messageId, const QString &deviceKey, const QHash<QString, QVariant> &channels)
{
Q_UNUSED(messageId)
Thing *thing = m_sunnyWebBoxes.key(static_cast<SunnyWebBox *>(sender()));
@ -240,7 +240,7 @@ void IntegrationPluginSma::onProcessDataReceived(int messageId, const QString &d
}
}
void IntegrationPluginSma::onParameterChannelsReceived(int messageId, const QString &deviceKey, QStringList parameterChannels)
void IntegrationPluginSma::onParameterChannelsReceived(const QUuid &messageId, const QString &deviceKey, QStringList parameterChannels)
{
Q_UNUSED(messageId)

View File

@ -59,16 +59,16 @@ public:
private slots:
void onRefreshTimer();
void onPlantOverviewReceived(int messageId, SunnyWebBox::Overview overview);
void onDevicesReceived(int messageId, QList<SunnyWebBox::Device> devices);
void onProcessDataReceived(int messageId, const QString &deviceKey, const QHash<QString, QVariant> &channels);
void onParameterChannelsReceived(int messageId, const QString &deviceKey, QStringList parameterChannels);
void onPlantOverviewReceived(const QUuid &messageId, SunnyWebBox::Overview overview);
void onDevicesReceived(const QUuid &messageId, QList<SunnyWebBox::Device> devices);
void onProcessDataReceived(const QUuid &messageId, const QString &deviceKey, const QHash<QString, QVariant> &channels);
void onParameterChannelsReceived(const QUuid &messageId, const QString &deviceKey, QStringList parameterChannels);
private:
PluginTimer *m_refreshTimer = nullptr;
QHash<Thing *, SunnyWebBox *> m_sunnyWebBoxes;
QHash<int, ThingSetupInfo *> m_asyncSetup;
QHash<int, ThingActionInfo *> m_asyncActions;
QHash<QUuid, ThingSetupInfo *> m_asyncSetup;
QHash<QUuid, ThingActionInfo *> m_asyncActions;
SunnyWebBoxCommunication *m_sunnyWebBoxCommunication = nullptr;
SunnyWebBox *createSunnyWebBoxConnection(Thing *thing);

View File

@ -44,31 +44,31 @@ SunnyWebBox::SunnyWebBox(SunnyWebBoxCommunication *communication, const QHostAdd
connect(m_communication, &SunnyWebBoxCommunication::messageReceived, this, &SunnyWebBox::onMessageReceived);
}
int SunnyWebBox::getPlantOverview()
QUuid SunnyWebBox::getPlantOverview()
{
return m_communication->sendMessage(m_hostAddresss, "GetPlantOverview");
}
int SunnyWebBox::getDevices()
QUuid SunnyWebBox::getDevices()
{
return m_communication->sendMessage(m_hostAddresss, "GetDevices");
}
int SunnyWebBox::getProcessDataChannels(const QString &deviceId)
QUuid SunnyWebBox::getProcessDataChannels(const QString &deviceId)
{
QJsonObject params;
params["device"] = deviceId;
return m_communication->sendMessage(m_hostAddresss, "GetProcessDataChannels", params);
}
int SunnyWebBox::getProcessData(const QStringList &deviceKeys)
QUuid SunnyWebBox::getProcessData(const QStringList &deviceKeys)
{
QJsonObject params;
params["device"] = deviceKeys.first(); //TODO
params["device"] = deviceKeys.first();
return m_communication->sendMessage(m_hostAddresss, "GetProcessData", params);
}
int SunnyWebBox::getParameterChannels(const QString &deviceKey)
QUuid SunnyWebBox::getParameterChannels(const QString &deviceKey)
{
QJsonObject paramsObj;
QJsonArray devicesArray;
@ -79,7 +79,7 @@ int SunnyWebBox::getParameterChannels(const QString &deviceKey)
return m_communication->sendMessage(m_hostAddresss, "GetParameterChannels", paramsObj);
}
int SunnyWebBox::getParameters(const QStringList &deviceKeys)
QUuid SunnyWebBox::getParameters(const QStringList &deviceKeys)
{
QJsonObject paramsObj;
QJsonArray devicesArray;
@ -90,7 +90,7 @@ int SunnyWebBox::getParameters(const QStringList &deviceKeys)
return m_communication->sendMessage(m_hostAddresss, "GetParameter", paramsObj);
}
int SunnyWebBox::setParameters(const QString &deviceKey, const QHash<QString, QVariant> &channels)
QUuid SunnyWebBox::setParameters(const QString &deviceKey, const QHash<QString, QVariant> &channels)
{
QJsonObject paramsObj;
QJsonArray devicesArray;
@ -119,7 +119,7 @@ QHostAddress SunnyWebBox::hostAddress()
return m_hostAddresss;
}
void SunnyWebBox::onMessageReceived(const QHostAddress &address, int messageId, const QString &messageType, const QVariantMap &result)
void SunnyWebBox::onMessageReceived(const QHostAddress &address, const QUuid &messageId, const QString &messageType, const QVariantMap &result)
{
if (address != m_hostAddresss) {
return;

View File

@ -75,13 +75,13 @@ public:
explicit SunnyWebBox(SunnyWebBoxCommunication *communication, const QHostAddress &hostAddress, QObject *parrent = 0);
int getPlantOverview(); // Returns an object with the following plant data: PAC, E-TODAY, E-TOTAL, MODE, ERROR
int getDevices(); //Returns a hierarchical list of all detected plant devices.
int getProcessDataChannels(const QString &deviceKey); //Returns a list with the meta names of the available process data channels for a particular device type.
int getProcessData(const QStringList &deviceKeys); //Returns process data for up to 5 devices per request.
int getParameterChannels(const QString &deviceKey); //Returns a list with the meta names of the available parameter channels for a particular device type
int getParameters(const QStringList &deviceKeys); //Returns the parameter values of up to 5 devices
int setParameters(const QString &deviceKeys, const QHash<QString, QVariant> &channels); //Sets parameter values
QUuid getPlantOverview(); // Returns an object with the following plant data: PAC, E-TODAY, E-TOTAL, MODE, ERROR
QUuid getDevices(); //Returns a hierarchical list of all detected plant devices.
QUuid getProcessDataChannels(const QString &deviceKey); //Returns a list with the meta names of the available process data channels for a particular device type.
QUuid getProcessData(const QStringList &deviceKeys); //Returns process data for up to 5 devices per request.
QUuid getParameterChannels(const QString &deviceKey); //Returns a list with the meta names of the available parameter channels for a particular device type
QUuid getParameters(const QStringList &deviceKeys); //Returns the parameter values of up to 5 devices
QUuid setParameters(const QString &deviceKeys, const QHash<QString, QVariant> &channels); //Sets parameter values
void setHostAddress(const QHostAddress &address);
QHostAddress hostAddress();
@ -92,17 +92,17 @@ private:
SunnyWebBoxCommunication *m_communication = nullptr;
public slots:
void onMessageReceived(const QHostAddress &address, int messageId, const QString &messageType, const QVariantMap &result);
void onMessageReceived(const QHostAddress &address, const QUuid &messageId, const QString &messageType, const QVariantMap &result);
signals:
void connectedChanged(bool connected);
void plantOverviewReceived(int messageId, Overview overview);
void devicesReceived(int messageId, QList<Device> devices);
void processDataChannelsReceived(int messageId, const QString &deviceKey, QStringList processDataChanels);
void processDataReceived(int messageId, const QString &deviceKey, const QHash<QString, QVariant> &channels);
void parameterChannelsReceived(int messageId, const QString &deviceKey, QStringList parameterChannels);
void parametersReceived(int messageId, const QString &deviceKey, const QList<Parameter> &parameters);
void plantOverviewReceived(const QUuid &messageId, Overview overview);
void devicesReceived(const QUuid &messageId, QList<Device> devices);
void processDataChannelsReceived(const QUuid &messageId, const QString &deviceKey, QStringList processDataChanels);
void processDataReceived(const QUuid &messageId, const QString &deviceKey, const QHash<QString, QVariant> &channels);
void parameterChannelsReceived(const QUuid &messageId, const QString &deviceKey, QStringList parameterChannels);
void parametersReceived(const QUuid &messageId, const QString &deviceKey, const QList<Parameter> &parameters);
};
#endif // SUNNYWEBBOX_H

View File

@ -57,46 +57,55 @@ SunnyWebBoxCommunication::SunnyWebBoxCommunication(QObject *parent) : QObject(pa
});
}
int SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure)
QUuid SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure)
{
int requestId = qrand();
QUuid requestId = QUuid::createUuid();
QJsonDocument doc;
QJsonObject obj;
obj["version"] = "1.0";
obj["proc"] = procedure;
obj["id"] = requestId;
obj["format"] = "JSON";
obj["id"] = requestId.toString().remove('{').remove('}');
obj["proc"] = procedure;
obj["version"] = "1.0";
doc.setObject(obj);
qCDebug(dcSma()) << "Send message" << doc.toJson() << address << m_port;
m_udpSocket->writeDatagram(doc.toJson(), address, m_port);
QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact);
qCDebug(dcSma()) << "Send message" << data << address << m_port;
m_udpSocket->writeDatagram(data, address, m_port);
return requestId;
}
int SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params)
QUuid SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params)
{
int requestId = qrand();
QUuid requestId = QUuid::createUuid();
QJsonDocument doc;
QJsonObject obj;
obj["version"] = "1.0";
obj["proc"] = procedure;
obj["id"] = requestId;
obj["format"] = "JSON";
if (!params.isEmpty()) {
obj.insert("params", params);
}
obj["format"] = "JSON";
obj["id"] = requestId.toString().remove('{').remove('}');
obj["proc"] = procedure;
obj["version"] = "1.0";
doc.setObject(obj);
qCDebug(dcSma()) << "Send message" << doc.toJson() << address << m_port;
m_udpSocket->writeDatagram(doc.toJson(), address, m_port);
QByteArray data = doc.toJson(QJsonDocument::JsonFormat::Compact);
qCDebug(dcSma()) << "Send message" << data << address << m_port;
m_udpSocket->writeDatagram(data, address, m_port);
return requestId;
}
void SunnyWebBoxCommunication::datagramReceived(const QHostAddress &address, const QByteArray &data)
{
qCDebug(dcSma()) << "Datagram received" << data;
QList<QByteArray> arrayList = data.split('\x00');
QByteArray cleanData;
Q_FOREACH(QByteArray i, arrayList) {
//Removing all '\0' characters
cleanData.append(i);
}
qCDebug(dcSma()) << "Datagram received" << cleanData;
QJsonParseError error;
QJsonDocument doc = QJsonDocument::fromJson(data, &error);
QJsonDocument doc = QJsonDocument::fromJson(cleanData, &error);
if (error.error != QJsonParseError::NoError) {
qCWarning(dcSma()) << "Could not parse JSON" << error.errorString();
return;
@ -113,7 +122,7 @@ void SunnyWebBoxCommunication::datagramReceived(const QHostAddress &address, con
if (map.contains("proc") && map.contains("result")) {
QString requestType = map["proc"].toString();
int requestId = map["id"].toInt();
QUuid requestId = QUuid(map["id"].toString());
QVariantMap result = map.value("result").toMap();
emit messageReceived(address, requestId, requestType, result);
} else {

View File

@ -33,6 +33,7 @@
#include <QObject>
#include <QUdpSocket>
#include <QUuid>
class SunnyWebBoxCommunication : public QObject
{
@ -40,8 +41,8 @@ class SunnyWebBoxCommunication : public QObject
public:
explicit SunnyWebBoxCommunication(QObject *parent = nullptr);
int sendMessage(const QHostAddress &address, const QString &procedure);
int sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params);
QUuid sendMessage(const QHostAddress &address, const QString &procedure);
QUuid sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject &params);
private:
int m_port = 34268;
@ -51,7 +52,7 @@ private:
signals:
void socketConnected(bool connected);
void messageReceived(const QHostAddress &address, int messageId, const QString &messageType, const QVariantMap &result);
void messageReceived(const QHostAddress &address, QUuid messageId, const QString &messageType, const QVariantMap &result);
};