mirror of
https://github.com/Serial-Studio/Serial-Studio.git
synced 2025-01-15 05:22:53 +08:00
Add SS -> MQTT data support
This commit is contained in:
parent
978819ef96
commit
858ae95552
@ -38,6 +38,8 @@ bool JFI_Valid(const JFI_Object &info)
|
|||||||
void JFI_SortList(QList<JFI_Object> *list)
|
void JFI_SortList(QList<JFI_Object> *list)
|
||||||
{
|
{
|
||||||
Q_ASSERT(list);
|
Q_ASSERT(list);
|
||||||
|
if (list->count() <= 1)
|
||||||
|
return;
|
||||||
|
|
||||||
for (int i = 0; i < list->count() - 1; ++i)
|
for (int i = 0; i < list->count() - 1; ++i)
|
||||||
{
|
{
|
||||||
|
@ -22,6 +22,9 @@
|
|||||||
|
|
||||||
#include "Publisher.h"
|
#include "Publisher.h"
|
||||||
|
|
||||||
|
#include <JSON/Frame.h>
|
||||||
|
#include <IO/Manager.h>
|
||||||
|
#include <JSON/Generator.h>
|
||||||
#include <Misc/Utilities.h>
|
#include <Misc/Utilities.h>
|
||||||
#include <Misc/TimerEvents.h>
|
#include <Misc/TimerEvents.h>
|
||||||
|
|
||||||
@ -34,10 +37,20 @@ Client::Client()
|
|||||||
m_lookupActive = false;
|
m_lookupActive = false;
|
||||||
m_clientMode = MQTTClientMode::ClientPublisher;
|
m_clientMode = MQTTClientMode::ClientPublisher;
|
||||||
|
|
||||||
|
// MQTT signals/slots
|
||||||
connect(&m_client, &QMQTT::Client::connected, this, &Client::connectedChanged);
|
connect(&m_client, &QMQTT::Client::connected, this, &Client::connectedChanged);
|
||||||
connect(&m_client, &QMQTT::Client::disconnected, this, &Client::connectedChanged);
|
connect(&m_client, &QMQTT::Client::disconnected, this, &Client::connectedChanged);
|
||||||
connect(&m_client, &QMQTT::Client::error, this, &Client::onError);
|
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());
|
setPort(defaultPort());
|
||||||
setHost(defaultHost());
|
setHost(defaultHost());
|
||||||
}
|
}
|
||||||
@ -212,11 +225,53 @@ void Client::sendData()
|
|||||||
JFI_SortList(&m_jfiList);
|
JFI_SortList(&m_jfiList);
|
||||||
|
|
||||||
// Send data in CSV format
|
// 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
|
// Clear JFI list
|
||||||
m_jfiList.clear();
|
m_jfiList.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Client::resetStatistics()
|
||||||
|
{
|
||||||
|
m_sentMessages = 0;
|
||||||
|
m_jfiList.clear();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the host IP address when the lookup finishes.
|
* 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.
|
* 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)
|
void Client::registerJsonFrame(const JFI_Object &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);
|
m_jfiList.append(frameInfo);
|
||||||
}
|
}
|
||||||
|
@ -141,6 +141,7 @@ private:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void sendData();
|
void sendData();
|
||||||
|
void resetStatistics();
|
||||||
void lookupFinished(const QHostInfo &info);
|
void lookupFinished(const QHostInfo &info);
|
||||||
void onError(const QMQTT::ClientError error);
|
void onError(const QMQTT::ClientError error);
|
||||||
void registerJsonFrame(const JFI_Object &frameInfo);
|
void registerJsonFrame(const JFI_Object &frameInfo);
|
||||||
@ -149,6 +150,7 @@ private:
|
|||||||
QString m_topic;
|
QString m_topic;
|
||||||
bool m_lookupActive;
|
bool m_lookupActive;
|
||||||
QMQTT::Client m_client;
|
QMQTT::Client m_client;
|
||||||
|
quint16 m_sentMessages;
|
||||||
QList<JFI_Object> m_jfiList;
|
QList<JFI_Object> m_jfiList;
|
||||||
MQTTClientMode m_clientMode;
|
MQTTClientMode m_clientMode;
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user