Implémentation Watchdog & NTP

This commit is contained in:
JFMartel 2017-07-14 14:55:47 -04:00 committed by zonetest
parent dc75fc9313
commit 31ce874693
3 changed files with 98 additions and 2 deletions

View File

@ -1,7 +1,8 @@
#ifndef PROTOCOLDEFS_H
#define PROTOCOLDEFS_H
#define MODBUS_ZT_WATCHDOG_TIMEOUT 3000
#define MODBUS_CC_WATCHDOG_TIMEOUT 5000
//ZT - CC modbus table masks

View File

@ -1,6 +1,8 @@
#include "ModbusCCMgr.h"
#include "ZTLog.h"
#include "EngLog.h"
#include "ModbusCCDefs.h"
#include <QDateTime>
CModbusCCMgr::CModbusCCMgr(CModbusRepository *Repo, int ModbusPort, int DevID) :
@ -12,11 +14,28 @@ CModbusCCMgr::CModbusCCMgr(CModbusRepository *Repo, int ModbusPort, int DevID) :
connect(mModbusServer,SIGNAL(newConnection()),this,SLOT(NewModbusConnection()));
mModbusMode = MODBUS_SLAVE_MODE;
mZTWatchdogTimer = new QTimer;
mZTWatchdogTimer->setSingleShot(false);
mZTWatchdogTimer->setInterval(MODBUS_ZT_WATCHDOG_TIMEOUT);
connect(mZTWatchdogTimer,SIGNAL(timeout()),this,SLOT(ModbusZTWatchdogTimeout()));
mCCWatchdogTimer = new QTimer;
mCCWatchdogTimer->setSingleShot(true);
mCCWatchdogTimer->setInterval(MODBUS_CC_WATCHDOG_TIMEOUT);
connect(mCCWatchdogTimer,SIGNAL(timeout()),this,SLOT(ModbusCCWatchdogTimeout()));
mZTWatchdog = 0;
mCCWatchdogState = 0;
mCCLinkState = false;
}
CModbusCCMgr::~CModbusCCMgr()
{
delete mModbusServer;
delete mZTWatchdogTimer;
delete mCCWatchdogTimer;
}
int CModbusCCMgr::StartModbusCCServer()
@ -36,6 +55,9 @@ void CModbusCCMgr::NewModbusConnection()
connect(mModbusTCPSocketHandle,SIGNAL(disconnected()),this,SLOT(ConnectionLost()));
//qDebug("ModbusCC: Connection with CC established");
CZTLog::instance()->AddLogString(QString("Connection Modbus avec la CC établie. IP[%1]").arg(mModbusTCPSocketHandle->peerAddress().toString()),true);
mZTWatchdogTimer->start();
mCCWatchdogTimer->start(MODBUS_CC_WATCHDOG_TIMEOUT*2); //Allow twice the time for the first watchdog reset.
mCCLinkState = true;
emit ModbusCCConnected();
}
@ -44,8 +66,10 @@ void CModbusCCMgr::NewModbusConnection()
void CModbusCCMgr::ConnectionLost()
{
ModbusLinkDisconnected();
mZTWatchdogTimer->stop();
mCCWatchdogTimer->stop();
emit ModbusCCDisconnected();
CZTLog::instance()->AddLogString(QString("Connection Modbus avec la CC rompue."),true);
CZTLog::instance()->AddLogString(QString("Connection Modbus (Ethernet) avec la CC rompue."),true);
}
bool CModbusCCMgr::IsModbusConnected()
@ -57,6 +81,48 @@ void CModbusCCMgr::RegistersDatabaseUpdated(quint16 StartAddress, quint16 Length
{
Q_UNUSED(StartAddress)
Q_UNUSED(Length)
qint16 WD = mModbusRepo->GetSingleReg(MODBUS_CC_WATCHDOG_BASE_REG_ADD);
if(WD != mCCWatchdogState)
{
mCCWatchdogTimer->start(MODBUS_CC_WATCHDOG_TIMEOUT); //The watchdog has toggled. Kick the timer.
mCCWatchdogState = WD;
if(mCCLinkState == false)
{
mCCLinkState = true;
CZTLog::instance()->AddLogString("Lien de communication (Watchdog) avec la CC rétabli",true);
}
}
qint16 NTPWrite = mModbusRepo->GetSingleReg(MODBUS_CC_CLK_UPDATE_BASE_REG_ADD);
if(NTPWrite == 1)
{
//Update date & time...
bool OK;
QList<qint16> DateTime = mModbusRepo->GetRegs(MODUBS_CC_CLK_SEC_BASE_REG_ADD,4,&OK);
if(OK)
{
qint8 Secs, Minutes, Hours, Month, Day;
Secs = (qint8)(DateTime.at(0) >> 8);
Minutes = (qint8)(DateTime.at(1) & 0x00FF);
Hours = (qint8)(DateTime.at(1) >> 8);
Day = (qint8)(DateTime.at(2) & 0x00FF);
Month = (qint8)(DateTime.at(2) >> 8);
qint16 Year = DateTime.at(3);
QDateTime NetworkTime;
NetworkTime.setTimeSpec(Qt::UTC);
NetworkTime.setDate(QDate(Year,Month,Day));
NetworkTime.setTime(QTime(Hours,Minutes,Secs));
QDateTime MyTime = NetworkTime.toLocalTime();
qDebug("Date & Heure reçue du SACL: %s",MyTime.toString("yyyy-MM-dd hh:mm:ss").toAscii().data());
mModbusRepo->WriteSingleReg(MODBUS_CC_CLK_UPDATE_BASE_REG_ADD,0);
}
}
emit RepoHasChanged();
}
@ -65,3 +131,23 @@ void CModbusCCMgr::ModbusRequestException(quint8 ExceptionCode, quint8 FctCode)
//qDebug("Modbus CC: Request exception occured. ExceptCode: [%d], FctCode: [%d]",ExceptionCode,FctCode);
CEngLog::instance()->AddLogString(QString("Modbus CC: Exception de requête. ExceptionCode[%1], FctCode[%2]").arg(ExceptionCode).arg(FctCode));
}
void CModbusCCMgr::ModbusCCWatchdogTimeout()
{
CZTLog::instance()->AddLogString("Perte du lien de communication avec la CC: Watchdog CC Expiré",true);
mCCLinkState = false;
}
void CModbusCCMgr::ModbusZTWatchdogTimeout()
{
qint16 WD = mZTWatchdog;
mModbusRepo->WriteSingleReg(MODBUS_ZT_WATCHDOG_REG_ADD,WD);
if(mZTWatchdog == 1)
{
mZTWatchdog = 0;
}
else
{
mZTWatchdog = 1;
}
}

View File

@ -5,6 +5,7 @@
#include "ModbusBackend.h"
#include <QTcpServer>
#include "ModbusRepository.h"
#include <QTimer>
class CModbusCCMgr : public CModbusBackend
{
@ -24,15 +25,23 @@ public:
private:
QTcpServer *mModbusServer;
int mModbusPort;
QTimer *mZTWatchdogTimer, *mCCWatchdogTimer;
qint16 mZTWatchdog;
qint16 mCCWatchdogState;
bool mCCLinkState;
signals:
void RepoHasChanged();
void ModbusCCConnected();
void ModbusCCDisconnected();
void ModbusCCLinkLost();
void ModbusCCLinkRecovered();
public slots:
void NewModbusConnection();
void ConnectionLost();
void ModbusZTWatchdogTimeout();
void ModbusCCWatchdogTimeout();
};
#endif // CMODBUSSLAVE_H