MQTT client fonctionnel
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
CCANDataLogger::CCANDataLogger():
|
||||
mTopicDeviceString("")
|
||||
{
|
||||
|
||||
mMQTTCLient = 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ int CCANDataLogger::LogNewData(const QList<CCANMessage *> *MsgList)
|
||||
|
||||
//First, clear the current list
|
||||
mMQTTMsgList.clear();
|
||||
qDebug("\n\n\n");
|
||||
//qDebug("\n\n\n");
|
||||
|
||||
for(int i = 0; i < MsgList->size(); i++)
|
||||
{
|
||||
@@ -25,7 +25,7 @@ int CCANDataLogger::LogNewData(const QList<CCANMessage *> *MsgList)
|
||||
QString MsgPayload = "{";
|
||||
QString MsgTopic = mTopicDeviceString;
|
||||
MsgTopic.append(Msg->mCANMsgName);
|
||||
MsgTopic.append("/");
|
||||
// MsgTopic.append("/");
|
||||
|
||||
//For each signal with new data, insert an entry in the JSon payload string
|
||||
for(int signal = 0; signal < Msg->mSignalsList.size(); signal++)
|
||||
@@ -54,7 +54,7 @@ int CCANDataLogger::LogNewData(const QList<CCANMessage *> *MsgList)
|
||||
}
|
||||
MsgPayload.append("}");
|
||||
|
||||
qDebug("%s",qPrintable(MsgPayload));
|
||||
// qDebug("%s",qPrintable(MsgPayload));
|
||||
|
||||
CMQTTMessage NewMessage(MsgTopic, MsgPayload);
|
||||
mMQTTMsgList.append(NewMessage);
|
||||
@@ -62,6 +62,10 @@ int CCANDataLogger::LogNewData(const QList<CCANMessage *> *MsgList)
|
||||
} //if(MsgList->at(i)->mPendingData == true)
|
||||
}
|
||||
|
||||
if(mMQTTCLient != 0)
|
||||
{
|
||||
mMQTTCLient->SendMQTTMessages(mMQTTMsgList);
|
||||
}
|
||||
return RET_OK;
|
||||
|
||||
}
|
||||
@@ -72,3 +76,8 @@ int CCANDataLogger::SetMQTTTopicDevice(QString DeviceString)
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CCANDataLogger::SetMQTTClient(CMQTTClientWrapper *MQTTClient)
|
||||
{
|
||||
mMQTTCLient = MQTTClient;
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include "MQTTMessage.h"
|
||||
#include <QList>
|
||||
#include "CANMessage.h"
|
||||
#include "MQTTClientWrapper.h"
|
||||
|
||||
class CCANDataLogger
|
||||
{
|
||||
@@ -16,8 +17,10 @@ public:
|
||||
//MQTT logging
|
||||
QString mTopicDeviceString;
|
||||
QList<CMQTTMessage> mMQTTMsgList;
|
||||
CMQTTClientWrapper *mMQTTCLient;
|
||||
|
||||
int SetMQTTTopicDevice(QString DeviceString);
|
||||
int SetMQTTClient(CMQTTClientWrapper *MQTTClient);
|
||||
|
||||
|
||||
};
|
||||
|
||||
@@ -19,7 +19,7 @@ CCANDevice::CCANDevice(QObject *parent)
|
||||
// mDevicePollPeriod = 0;
|
||||
}
|
||||
|
||||
CCANDevice::CCANDevice(CCANDeviceConfig &SysConfig)
|
||||
CCANDevice::CCANDevice(CCANDeviceConfig &SysConfig, CMQTTClientWrapper *MQTTClient)
|
||||
{
|
||||
mMessageList.clear();
|
||||
mMessagesListLoaded = false;
|
||||
@@ -27,6 +27,7 @@ CCANDevice::CCANDevice(CCANDeviceConfig &SysConfig)
|
||||
mProgramPtr = 0;
|
||||
|
||||
mDeviceConfigInfo = SysConfig;
|
||||
mCANMQTTClient = MQTTClient;
|
||||
|
||||
// mCANDeviceID = SysConfig.mCANDeviceID;
|
||||
// mCANDeviceBaudrate = SysConfig.mCANDeviceBaudrate;
|
||||
@@ -73,6 +74,7 @@ int CCANDevice::Init()
|
||||
}
|
||||
|
||||
mCANDataLogger.SetMQTTTopicDevice(QString("CANBus/%1/").arg(mDeviceConfigInfo.mDeviceName));
|
||||
mCANDataLogger.SetMQTTClient(mCANMQTTClient);
|
||||
|
||||
CGeneralMessagesLogDispatcher::instance()->AddLogMessage(QString("Dispositif [%1] initialisé avec succès!").arg(mDeviceConfigInfo.mDeviceName),true,CGeneralMessagesLogDispatcher::GEN_MSG_TXT_SUCCESS_STATUS);
|
||||
return RET_OK;
|
||||
|
||||
@@ -11,7 +11,9 @@
|
||||
#include "PCANBasic.h"
|
||||
#include "CANDeviceConfig.h"
|
||||
#include "CANDataLogger.h"
|
||||
//#include "MQTTClientWrapper.h"
|
||||
|
||||
class CMQTTClientWrapper;
|
||||
class COtarcikCan;
|
||||
|
||||
class CCANDevice : public QObject
|
||||
@@ -19,7 +21,7 @@ class CCANDevice : public QObject
|
||||
Q_OBJECT
|
||||
public:
|
||||
explicit CCANDevice(QObject *parent = 0);
|
||||
CCANDevice(CCANDeviceConfig &SysConfig);
|
||||
CCANDevice(CCANDeviceConfig &SysConfig, CMQTTClientWrapper* MQTTClient = 0);
|
||||
~CCANDevice();
|
||||
|
||||
int Init(QString DatabaseFileName, TPCANHandle CANDeviceID, TPCANBaudrate CANDeviceBaudRate, QString DevDescription, QString DeviceName, unsigned int DevicePollPeriod);
|
||||
@@ -33,6 +35,7 @@ public:
|
||||
CCANAnalyzer mCANAnalyzer; //The module that handles the USB puck and decodes the data
|
||||
CCANDatabase mCANDatabase; //The device's database loaded from dbc file
|
||||
CCANDataLogger mCANDataLogger;
|
||||
CMQTTClientWrapper *mCANMQTTClient;
|
||||
|
||||
QList<CCANMessage*> mMessageList;
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#include "MQTTClientWrapper.h"
|
||||
|
||||
|
||||
CMQTTClientWrapper::CMQTTClientWrapper()
|
||||
{
|
||||
connect(&mMQTTClient,&QMqttClient::stateChanged,this,&CMQTTClientWrapper::StateChanged);
|
||||
}
|
||||
|
||||
int CMQTTClientWrapper::SetMQTTParams(CCloudParams *Params)
|
||||
{
|
||||
mMQTTParams = *Params;
|
||||
mMQTTClientID = "Otarcik_Station_Ville_Emard";
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CMQTTClientWrapper::ConnectToBroker()
|
||||
{
|
||||
//Setup the client before connecting.
|
||||
mMQTTClient.setAutoKeepAlive(true);
|
||||
mMQTTClient.setClientId(mMQTTClientID);
|
||||
mMQTTClient.setHostname(mMQTTParams.mMQTTBrokerHostName);
|
||||
mMQTTClient.setPort(mMQTTParams.mMQTTBrokerPort);
|
||||
mMQTTClient.setPassword(mMQTTParams.mMQTTBrokerPassword);
|
||||
mMQTTClient.setUsername(mMQTTParams.mMQTTBrokerUserName);
|
||||
|
||||
mMQTTClient.connectToHost();
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CMQTTClientWrapper::DisconnectFromBroker()
|
||||
{
|
||||
mMQTTClient.disconnectFromHost();
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
int CMQTTClientWrapper::SendMQTTMessages(QList<CMQTTMessage> MessagesList)
|
||||
{
|
||||
if(mMQTTClient.state() != QMqttClient::Connected)
|
||||
{
|
||||
return RET_GENERAL_ERROR;
|
||||
}
|
||||
if(MessagesList.isEmpty())
|
||||
{
|
||||
return RET_GENERAL_ERROR;
|
||||
}
|
||||
|
||||
for(int i = 0; i < MessagesList.size(); i++)
|
||||
{
|
||||
qint32 res = mMQTTClient.publish(MessagesList.at(i).mMessageTopic,MessagesList.at(i).mMessagePayload.toLocal8Bit(),0,true);
|
||||
}
|
||||
|
||||
|
||||
return RET_OK;
|
||||
}
|
||||
|
||||
void CMQTTClientWrapper::StateChanged()
|
||||
{
|
||||
switch(mMQTTClient.state())
|
||||
{
|
||||
case QMqttClient::Disconnected:
|
||||
{
|
||||
qDebug("MQTT client Disconnected");
|
||||
break;
|
||||
}
|
||||
case QMqttClient::Connected:
|
||||
{
|
||||
qDebug("MQTT client Connected");
|
||||
break;
|
||||
}
|
||||
case QMqttClient::Connecting:
|
||||
{
|
||||
qDebug("MQTT client Connecting...");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
#ifndef MQTTCLIENTWRAPPER_H
|
||||
#define MQTTCLIENTWRAPPER_H
|
||||
|
||||
#include "defines.h"
|
||||
#include <QtMqtt/QtMqtt>
|
||||
#include <QMqttClient>
|
||||
#include "CloudParams.h"
|
||||
#include "MQTTMessage.h"
|
||||
#include <QList>
|
||||
|
||||
class CMQTTClientWrapper : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
CMQTTClientWrapper();
|
||||
int SetMQTTParams(CCloudParams *Params);
|
||||
int ConnectToBroker();
|
||||
int SendMQTTMessages(QList<CMQTTMessage> MessagesList);
|
||||
int DisconnectFromBroker();
|
||||
|
||||
QString mMQTTClientID;
|
||||
|
||||
private:
|
||||
QMqttClient mMQTTClient;
|
||||
CCloudParams mMQTTParams;
|
||||
|
||||
public slots:
|
||||
void StateChanged();
|
||||
|
||||
};
|
||||
|
||||
#endif // MQTTCLIENTWRAPPER_H
|
||||
@@ -204,6 +204,37 @@
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLabel" name="mCloudMQTTClientID">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>190</y>
|
||||
<width>71</width>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="font">
|
||||
<font>
|
||||
<pointsize>10</pointsize>
|
||||
</font>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>Client ID</string>
|
||||
</property>
|
||||
<property name="alignment">
|
||||
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
|
||||
</property>
|
||||
</widget>
|
||||
<widget class="QLineEdit" name="mCloudMQTTTopicPrefixEdit_2">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>90</x>
|
||||
<y>190</y>
|
||||
<width>321</width>
|
||||
<height>20</height>
|
||||
</rect>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<widget class="QLabel" name="mGeneralLogParamsLbl">
|
||||
<property name="geometry">
|
||||
|
||||
@@ -31,6 +31,8 @@ COtarcikCan::~COtarcikCan()
|
||||
}
|
||||
mCANDevicesList.clear();
|
||||
|
||||
mCANBusMQTTClient.DisconnectFromBroker();
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -63,6 +65,9 @@ int COtarcikCan::Start()
|
||||
mCloudLoggingParams = *mSystemConfig.GetCloudParams();
|
||||
mMainWindow.mDataLoggingSettingsPage->SetCloudParams(&mCloudLoggingParams);
|
||||
|
||||
mCANBusMQTTClient.SetMQTTParams(&mCloudLoggingParams);
|
||||
mCANBusMQTTClient.ConnectToBroker();
|
||||
|
||||
}
|
||||
// CCANDevice *NewCANDevice = new CCANDevice();
|
||||
// mCANDevicesList.append(NewCANDevice);
|
||||
@@ -143,7 +148,7 @@ int COtarcikCan::PopulateCANDevicesList(QList<CCANDeviceConfig *> *CANDeviceConf
|
||||
|
||||
for(int i = 0; i < CANDeviceConfigList->size(); i++)
|
||||
{
|
||||
CCANDevice *NewDevice = new CCANDevice(*CANDeviceConfigList->at(i));
|
||||
CCANDevice *NewDevice = new CCANDevice(*CANDeviceConfigList->at(i),&mCANBusMQTTClient);
|
||||
NewDevice->mProgramPtr = this;
|
||||
NewDevice->Init();
|
||||
mCANDevicesList.append(NewDevice);
|
||||
|
||||
@@ -8,9 +8,8 @@
|
||||
#include "CANDevice.h"
|
||||
#include <QList>
|
||||
#include "SystemConfig.h"
|
||||
#include "MQTTClientWrapper.h"
|
||||
|
||||
#include <QtMqtt/QtMqtt>
|
||||
#include <QMqttClient>
|
||||
|
||||
class COtarcikCan : public QObject
|
||||
{
|
||||
@@ -20,6 +19,7 @@ public:
|
||||
~COtarcikCan();
|
||||
CMainWindow mMainWindow;
|
||||
CSystemConfig mSystemConfig;
|
||||
CMQTTClientWrapper mCANBusMQTTClient;
|
||||
|
||||
int Start();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user