133 lines
3.8 KiB
C++
133 lines
3.8 KiB
C++
#include "CANDataLogger.h"
|
|
#include "defines.h"
|
|
|
|
CCANDataLogger::CCANDataLogger():
|
|
mTopicDeviceString("")
|
|
{
|
|
mMQTTCLient = 0;
|
|
mCANMsgList.clear();
|
|
|
|
CCANMessage toto;
|
|
toto.mCANMsgID = 234;
|
|
CCANSignal test;
|
|
test.mSignalName = "TOTORO";
|
|
toto.mSignalsList.append(test);
|
|
|
|
CCANMessage Bleh;
|
|
Bleh = toto;
|
|
|
|
|
|
}
|
|
|
|
|
|
int CCANDataLogger::LogNewData(const QList<CCANMessage *> *MsgList)
|
|
{
|
|
|
|
//Since the MQTT msg timeout is different from the CAN read timeout (much faster), and also each CAN device can have it's own timeout we need
|
|
//store the messages and update the values until it's ready to send to the MQTT broker.
|
|
|
|
for(int i = 0; i < MsgList->size(); i++)
|
|
{
|
|
bool found = false;
|
|
|
|
int j = 0;
|
|
while(j < mCANMsgList.size() && found == false)
|
|
{
|
|
if(MsgList->at(i)->mCANMsgID == mCANMsgList.at(j).mCANMsgID &&
|
|
MsgList->at(i)->mPendingData == true)
|
|
{
|
|
//We already had this msg in the list, just update it's value
|
|
found = true;
|
|
mCANMsgList[j] = *MsgList->at(i);
|
|
}
|
|
j++;
|
|
}
|
|
if(found == false && MsgList->at(i)->mPendingData == true)
|
|
{
|
|
//Message is not in our list, we must add it.
|
|
CCANMessage NewMsg = *MsgList->at(i);
|
|
mCANMsgList.append(NewMsg);
|
|
}
|
|
}
|
|
|
|
|
|
return RET_OK;
|
|
|
|
}
|
|
|
|
int CCANDataLogger::SetMQTTTopicDevice(QString DeviceString)
|
|
{
|
|
mTopicDeviceString = DeviceString;
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
int CCANDataLogger::SetMQTTClient(CMQTTClientWrapper *MQTTClient)
|
|
{
|
|
mMQTTCLient = MQTTClient;
|
|
}
|
|
|
|
QList<CMQTTMessage> *CCANDataLogger::GetMQTTMessagesList()
|
|
{
|
|
//MQTT
|
|
//The MQTT client wants to send the messages
|
|
//For each CAN message in the list, we create a MQTT Message
|
|
|
|
//First, clear the current list
|
|
mMQTTMsgList.clear();
|
|
//qDebug("\n\n\n");
|
|
|
|
for(int i = 0; i < mCANMsgList.size(); i++)
|
|
{
|
|
// if(mCANMsgList.size() != 0)
|
|
{
|
|
const CCANMessage Msg = mCANMsgList.at(i);
|
|
QString MsgPayload = "{";
|
|
QString MsgTopic = mTopicDeviceString;
|
|
MsgTopic.append(Msg.mCANMsgName);
|
|
// 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++)
|
|
{
|
|
QString SignalData;
|
|
CCANSignal Signal = Msg.mSignalsList.at(signal);
|
|
if(Signal.mValueType == CCANSignal::CAN_SIGNAL_TYPE_UNSIGNED_INT)
|
|
{
|
|
SignalData = QString("\"%1\":%2").arg(Signal.mSignalName).arg((quint64)Signal.mPhysicalValue);
|
|
}
|
|
else if(Signal.mValueType == CCANSignal::CAN_SIGNAL_TYPE_SIGNED_INT)
|
|
{
|
|
SignalData = QString("\"%1\":%2").arg(Signal.mSignalName).arg((qint64)Signal.mPhysicalValue);
|
|
}
|
|
else if(Signal.mValueType == CCANSignal::CAN_SIGNAL_TYPE_32_BIT_FLOAT ||
|
|
Signal.mValueType == CCANSignal::CAN_SIGNAL_TYPE_64_BIT_DOUBLE)
|
|
{
|
|
SignalData = QString("\"%1\":%2").arg(Signal.mSignalName).arg(Signal.mPhysicalValue,0,'f',1);
|
|
}
|
|
MsgPayload.append(SignalData);
|
|
|
|
if(signal < Msg.mSignalsList.size()-1)
|
|
{
|
|
MsgPayload.append(",");
|
|
}
|
|
}
|
|
MsgPayload.append("}");
|
|
|
|
// qDebug("%s",qPrintable(MsgPayload));
|
|
|
|
CMQTTMessage NewMessage(MsgTopic, MsgPayload);
|
|
mMQTTMsgList.append(NewMessage);
|
|
|
|
} //if(MsgList->at(i)->mPendingData == true)
|
|
}
|
|
|
|
mCANMsgList.clear();
|
|
|
|
// if(mMQTTCLient != 0)
|
|
// {
|
|
// mMQTTCLient->NewMQTTMessages(mMQTTMsgList);
|
|
// }
|
|
return &mMQTTMsgList;
|
|
}
|