diff --git a/sma/integrationpluginsma.cpp b/sma/integrationpluginsma.cpp index 0e880ff6..0a79e200 100644 --- a/sma/integrationpluginsma.cpp +++ b/sma/integrationpluginsma.cpp @@ -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 devices) +void IntegrationPluginSma::onDevicesReceived(const QUuid &messageId, QList devices) { if (m_asyncActions.contains(messageId)) { ThingActionInfo *info = m_asyncActions.value(messageId); @@ -221,7 +221,7 @@ void IntegrationPluginSma::onDevicesReceived(int messageId, QList &channels) +void IntegrationPluginSma::onProcessDataReceived(const QUuid &messageId, const QString &deviceKey, const QHash &channels) { Q_UNUSED(messageId) Thing *thing = m_sunnyWebBoxes.key(static_cast(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) diff --git a/sma/integrationpluginsma.h b/sma/integrationpluginsma.h index 7bcf1695..01185063 100644 --- a/sma/integrationpluginsma.h +++ b/sma/integrationpluginsma.h @@ -59,16 +59,16 @@ public: private slots: void onRefreshTimer(); - void onPlantOverviewReceived(int messageId, SunnyWebBox::Overview overview); - void onDevicesReceived(int messageId, QList devices); - void onProcessDataReceived(int messageId, const QString &deviceKey, const QHash &channels); - void onParameterChannelsReceived(int messageId, const QString &deviceKey, QStringList parameterChannels); + void onPlantOverviewReceived(const QUuid &messageId, SunnyWebBox::Overview overview); + void onDevicesReceived(const QUuid &messageId, QList devices); + void onProcessDataReceived(const QUuid &messageId, const QString &deviceKey, const QHash &channels); + void onParameterChannelsReceived(const QUuid &messageId, const QString &deviceKey, QStringList parameterChannels); private: PluginTimer *m_refreshTimer = nullptr; QHash m_sunnyWebBoxes; - QHash m_asyncSetup; - QHash m_asyncActions; + QHash m_asyncSetup; + QHash m_asyncActions; SunnyWebBoxCommunication *m_sunnyWebBoxCommunication = nullptr; SunnyWebBox *createSunnyWebBoxConnection(Thing *thing); diff --git a/sma/sunnywebbox.cpp b/sma/sunnywebbox.cpp index 85da7f9b..ad4871c4 100644 --- a/sma/sunnywebbox.cpp +++ b/sma/sunnywebbox.cpp @@ -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 &channels) +QUuid SunnyWebBox::setParameters(const QString &deviceKey, const QHash &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; diff --git a/sma/sunnywebbox.h b/sma/sunnywebbox.h index aae27eb0..608771d2 100644 --- a/sma/sunnywebbox.h +++ b/sma/sunnywebbox.h @@ -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 &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 &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 devices); - void processDataChannelsReceived(int messageId, const QString &deviceKey, QStringList processDataChanels); - void processDataReceived(int messageId, const QString &deviceKey, const QHash &channels); - void parameterChannelsReceived(int messageId, const QString &deviceKey, QStringList parameterChannels); - void parametersReceived(int messageId, const QString &deviceKey, const QList ¶meters); + void plantOverviewReceived(const QUuid &messageId, Overview overview); + void devicesReceived(const QUuid &messageId, QList devices); + void processDataChannelsReceived(const QUuid &messageId, const QString &deviceKey, QStringList processDataChanels); + void processDataReceived(const QUuid &messageId, const QString &deviceKey, const QHash &channels); + void parameterChannelsReceived(const QUuid &messageId, const QString &deviceKey, QStringList parameterChannels); + void parametersReceived(const QUuid &messageId, const QString &deviceKey, const QList ¶meters); }; #endif // SUNNYWEBBOX_H diff --git a/sma/sunnywebboxcommunication.cpp b/sma/sunnywebboxcommunication.cpp index 0d1c7d54..de7ef31d 100644 --- a/sma/sunnywebboxcommunication.cpp +++ b/sma/sunnywebboxcommunication.cpp @@ -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 ¶ms) +QUuid SunnyWebBoxCommunication::sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms) { - 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 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 { diff --git a/sma/sunnywebboxcommunication.h b/sma/sunnywebboxcommunication.h index cec4175c..8ceb6ee9 100644 --- a/sma/sunnywebboxcommunication.h +++ b/sma/sunnywebboxcommunication.h @@ -33,6 +33,7 @@ #include #include +#include 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 ¶ms); + QUuid sendMessage(const QHostAddress &address, const QString &procedure); + QUuid sendMessage(const QHostAddress &address, const QString &procedure, const QJsonObject ¶ms); 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); };