From 858ae955525195b7656e7e3f17d7229ec120e1c9 Mon Sep 17 00:00:00 2001 From: Alex Spataru Date: Sun, 28 Feb 2021 21:24:39 -0500 Subject: [PATCH] Add SS -> MQTT data support --- src/JSON/FrameInfo.cpp | 2 ++ src/MQTT/Publisher.cpp | 63 +++++++++++++++++++++++++++++++++++++++++- src/MQTT/Publisher.h | 2 ++ 3 files changed, 66 insertions(+), 1 deletion(-) diff --git a/src/JSON/FrameInfo.cpp b/src/JSON/FrameInfo.cpp index 381a187a..358a4cd9 100644 --- a/src/JSON/FrameInfo.cpp +++ b/src/JSON/FrameInfo.cpp @@ -38,6 +38,8 @@ bool JFI_Valid(const JFI_Object &info) void JFI_SortList(QList *list) { Q_ASSERT(list); + if (list->count() <= 1) + return; for (int i = 0; i < list->count() - 1; ++i) { diff --git a/src/MQTT/Publisher.cpp b/src/MQTT/Publisher.cpp index 8b146ab8..07828c68 100644 --- a/src/MQTT/Publisher.cpp +++ b/src/MQTT/Publisher.cpp @@ -22,6 +22,9 @@ #include "Publisher.h" +#include +#include +#include #include #include @@ -34,10 +37,20 @@ Client::Client() m_lookupActive = false; m_clientMode = MQTTClientMode::ClientPublisher; + // MQTT signals/slots connect(&m_client, &QMQTT::Client::connected, this, &Client::connectedChanged); connect(&m_client, &QMQTT::Client::disconnected, this, &Client::connectedChanged); connect(&m_client, &QMQTT::Client::error, this, &Client::onError); + // Send data @ 1 Hz & reset statistics when disconnected/connected to a device + auto io = IO::Manager::getInstance(); + auto ge = JSON::Generator::getInstance(); + auto te = Misc::TimerEvents::getInstance(); + connect(te, &Misc::TimerEvents::timeout42Hz, this, &Client::sendData); + connect(io, &IO::Manager::connectedChanged, this, &Client::resetStatistics); + connect(ge, &JSON::Generator::jsonChanged, this, &Client::registerJsonFrame); + + // Set default port/host setPort(defaultPort()); setHost(defaultHost()); } @@ -212,11 +225,53 @@ void Client::sendData() JFI_SortList(&m_jfiList); // Send data in CSV format + QString csv; + JSON::Frame frame; + JSON::Group *group; + JSON::Dataset *dataset; + for (int i = 0; i < m_jfiList.count(); ++i) + { + // Try to read frame + auto jfi = m_jfiList.at(i); + if (!frame.read(jfi.jsonDocument.object())) + continue; + + // Write dataset values + QString str; + for (int j = 0; j < frame.groupCount(); ++j) + { + group = frame.getGroup(j); + for (int k = 0; k < group->datasetCount(); ++k) + { + dataset = group->getDataset(k); + str.append(dataset->value()); + str.append(","); + } + } + + // Remove last "," character & add data to CSV list + str.chop(1); + csv.append(str + "\n"); + } + + // Create & send MQTT message + if (!csv.isEmpty()) + { + QMQTT::Message message(m_sentMessages, topic(), csv.toUtf8()); + m_client.publish(message); + ++m_sentMessages; + } // Clear JFI list m_jfiList.clear(); } +void Client::resetStatistics() +{ + m_sentMessages = 0; + m_jfiList.clear(); +} + /** * Sets the host IP address when the lookup finishes. * If the lookup fails, the error code/string shall be shown to the user in a messagebox. @@ -343,5 +398,11 @@ void Client::onError(const QMQTT::ClientError error) void Client::registerJsonFrame(const JFI_Object &frameInfo) { - m_jfiList.append(frameInfo); + // Ignore if device is not connected + if (!IO::Manager::getInstance()->connected()) + return; + + // Validate JFI & register it + if (JFI_Valid(frameInfo)) + m_jfiList.append(frameInfo); } diff --git a/src/MQTT/Publisher.h b/src/MQTT/Publisher.h index d07c9a18..b676a771 100644 --- a/src/MQTT/Publisher.h +++ b/src/MQTT/Publisher.h @@ -141,6 +141,7 @@ private: private slots: void sendData(); + void resetStatistics(); void lookupFinished(const QHostInfo &info); void onError(const QMQTT::ClientError error); void registerJsonFrame(const JFI_Object &frameInfo); @@ -149,6 +150,7 @@ private: QString m_topic; bool m_lookupActive; QMQTT::Client m_client; + quint16 m_sentMessages; QList m_jfiList; MQTTClientMode m_clientMode; };