153 lines
4.8 KiB
C++
153 lines
4.8 KiB
C++
|
|
#include "CANWatchdog.h"
|
|
#include "GeneralMessagesLogDispatcher.h"
|
|
#include <QDate>
|
|
#include <QTime>
|
|
#include <QFloat16>
|
|
#include "MQTTClientWrapper.h"
|
|
#include "InternetMonitor.h"
|
|
#include "LANDevicesPresenceMonitor.h"
|
|
#include "DeviceDetectionConfig.h"
|
|
|
|
CCANWatchdog::CCANWatchdog()
|
|
{
|
|
//mCANDriverIF = new CPCANInterface;
|
|
mIsCANInitialized = false;
|
|
|
|
mWatchdogTimer = new QTimer;
|
|
mWatchdogTimer->setSingleShot(false);
|
|
mCANDriverIF = 0;
|
|
mWDTInitialized = false;
|
|
// mMQTTPresenceMonitor = 0;
|
|
// mLANDevicesMonitor = 0;
|
|
// mInternetMonitor = 0;
|
|
mDevicesMonitorConfig = 0;
|
|
|
|
connect(mWatchdogTimer,&QTimer::timeout,this,&CCANWatchdog::WatchdogTimeoutTimerExpired);
|
|
}
|
|
|
|
int CCANWatchdog::Init(CPCANInterface *CANDriverIF, TPCANHandle CANDeviceHandle, unsigned int WDTPeriod, CDeviceDetectionConfig *DevicesMonitorConfig)
|
|
{
|
|
if(WDTPeriod < 200)
|
|
{
|
|
WDTPeriod = 200;
|
|
qDebug("CANWatchdog:: Trying to init with WDT period lower tan 200ms");
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
|
|
mDevicesMonitorConfig = DevicesMonitorConfig;
|
|
mCANDeviceHandle = CANDeviceHandle;
|
|
mWDTPeriod = WDTPeriod;
|
|
mCANDriverIF = CANDriverIF;
|
|
CGeneralMessagesLogDispatcher::instance()->AddLogMessage("Démarrage du Watchdog CAN...","CCANWatchdog");
|
|
|
|
mWatchdogTimer->setInterval(mWDTPeriod);
|
|
mWatchdogTimer->setSingleShot(false);
|
|
|
|
mWDTCANMsg.mCANMsgID = DevicesMonitorConfig->mReportingCANMsgID;
|
|
mWDTCANMsg.mCANMsgLength = 8;
|
|
mWDTCANMsg.mCANMsgData.clear();
|
|
mWDTCANMsg.mCANMsgData.append(8,(char)0x00);
|
|
mWDTCANMsg.mCANMsgName = "Watchdog";
|
|
|
|
mWDTInitialized = true;
|
|
|
|
return RET_OK;
|
|
}
|
|
|
|
void CCANWatchdog::WatchdogTimeoutTimerExpired()
|
|
{
|
|
quint64 BigData = 0;
|
|
|
|
// int seconds = (QTime::currentTime().minute() * 60) + QTime::currentTime().second();
|
|
// float Hours32bits = (float)((float)seconds / 3600);
|
|
// Hours32bits += QTime::currentTime().hour();
|
|
|
|
// quint16 Hours = static_cast<quint16>(Hours32bits*1000);
|
|
// quint16 buffer;
|
|
// memcpy(&buffer,&Hours,2);
|
|
// BigData |= buffer;
|
|
// BigData <<= 16;
|
|
// quint16 DaysSinceALongTimeAgo;
|
|
// DaysSinceALongTimeAgo = -QDate::currentDate().daysTo(QDate(1899,12,30));
|
|
// BigData |= DaysSinceALongTimeAgo;
|
|
// BigData |= 0x100000000;
|
|
|
|
BigData = GetCANDateTimeBuffer();
|
|
if(mDevicesMonitorConfig != 0)
|
|
{
|
|
if(mDevicesMonitorConfig->mInternetMonitor != 0)
|
|
{
|
|
BigData |= mDevicesMonitorConfig->mInternetMonitor->GetCANInternetStatusMask();
|
|
}
|
|
}
|
|
if(mDevicesMonitorConfig != 0)
|
|
{
|
|
if(mDevicesMonitorConfig->mMQTTPresenceMonitor != 0)
|
|
{
|
|
BigData |= mDevicesMonitorConfig->mMQTTPresenceMonitor->GetMQTTServerPresenceCANMask();
|
|
}
|
|
}
|
|
if(mDevicesMonitorConfig != 0)
|
|
{
|
|
if(mDevicesMonitorConfig->mLANDevicesMonitor != 0)
|
|
{
|
|
|
|
BigData |= mDevicesMonitorConfig->mLANDevicesMonitor->GetDevicesPresenceCANMask();
|
|
}
|
|
}
|
|
|
|
mWDTCANMsg.mCANMsgID = mDevicesMonitorConfig->mReportingCANMsgID;
|
|
mWDTCANMsg.SetRawData(BigData,CCANSignal::CAN_SIGNAL_ENCODING_INTEL);
|
|
mCANDriverIF->SendCANMessage(mCANDeviceHandle,mWDTCANMsg);
|
|
|
|
// qDebug("Sending 0x%X to CAN",data);
|
|
//QString LogMsg = QString("Écriture du watchdog. CAN ID %1 Payload: 0x%2 0x%3 0x%4 0x%5 0x%6 0x%7 0x%8 0x%9").arg(mWDTCANMsg.mCANMsgID,0,16).arg(mWDTCANMsg.mCANMsgData.at(7),0,16).arg(mWDTCANMsg.mCANMsgData.at(6),0,16).arg(mWDTCANMsg.mCANMsgData.at(5),0,16).arg(mWDTCANMsg.mCANMsgData.at(4),0,16).arg(mWDTCANMsg.mCANMsgData.at(3),0,16).arg(mWDTCANMsg.mCANMsgData.at(2),0,16).arg(mWDTCANMsg.mCANMsgData.at(1),0,16).arg(mWDTCANMsg.mCANMsgData[0],0,16);
|
|
// QString LogMsg = QString("Écriture du watchdog. CAN ID 0x%1").arg(mWDTCANMsg.mCANMsgID,0,16);
|
|
// CGeneralMessagesLogDispatcher::instance()->AddLogMessage(LogMsg,"CCANWatchdog",true,3);
|
|
}
|
|
|
|
quint64 CCANWatchdog::GetCANDateTimeBuffer()
|
|
{
|
|
quint64 OutputBuffer = 0;
|
|
|
|
int seconds = (QTime::currentTime().minute() * 60) + QTime::currentTime().second();
|
|
float Hours32bits = (float)((float)seconds / 3600);
|
|
Hours32bits += QTime::currentTime().hour();
|
|
quint16 Hours = static_cast<quint16>(Hours32bits*1000);
|
|
OutputBuffer |= Hours;
|
|
OutputBuffer <<= 16;
|
|
|
|
quint16 DaysSinceALongTimeAgo;
|
|
DaysSinceALongTimeAgo = -QDate::currentDate().daysTo(QDate(1899,12,30));
|
|
OutputBuffer |= DaysSinceALongTimeAgo;
|
|
|
|
return OutputBuffer;
|
|
}
|
|
|
|
int CCANWatchdog::SetDevicesDetectionConfig(CDeviceDetectionConfig *DeviceMonitoringConfig)
|
|
{
|
|
mDevicesMonitorConfig = DeviceMonitoringConfig;
|
|
}
|
|
|
|
int CCANWatchdog::StartWDT()
|
|
{
|
|
if(mWDTInitialized == false)
|
|
{
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
|
|
mWatchdogTimer->start();
|
|
return RET_OK;
|
|
}
|
|
|
|
int CCANWatchdog::StopWDT()
|
|
{
|
|
if(mWDTInitialized == false)
|
|
{
|
|
return RET_GENERAL_ERROR;
|
|
}
|
|
mWatchdogTimer->stop();
|
|
return RET_OK;
|
|
}
|