#include "LANDeviceMonitor.h" #include #include "GeneralMessagesLogDispatcher.h" CLANDeviceMonitor::CLANDeviceMonitor(QString IPAddress, unsigned int CANBit, bool Active, QString Description) { mDeviceIPAddress = IPAddress; mCANStatusBit = CANBit; mIsDetectionActive = Active; mDeviceDescription = Description; mIsDeviceOnline = false; mDevMonitorTimer = 0; mPingProcess = 0; mIsPingInProgress = false; if(mIsDetectionActive == true) { mDevMonitorTimer = new QTimer(); mDevMonitorTimer->setSingleShot(true); connect(mDevMonitorTimer,&QTimer::timeout,this,&CLANDeviceMonitor::MonitorTimeExpired); mPingProcess = new QProcess(this); connect(mPingProcess,SIGNAL(finished(int,QProcess::ExitStatus)),this,SLOT(PingProcessFinished(int,QProcess::ExitStatus))); PingDevice(); } else { } } int CLANDeviceMonitor::PingDevice() { int PingTimeout = LAN_DEVICE_MONITOR_PING_TIMEOUT - 500; QString Cmd = QString("ping /n 1 /w %1 %2").arg(PingTimeout).arg(mDeviceIPAddress); mPingProcess->start(Cmd); //mDevMonitorTimer->start(4000); return RET_OK; } CLANDeviceMonitor::~CLANDeviceMonitor() { if(mPingProcess != 0) { mPingProcess->terminate(); delete mPingProcess; } if(mDevMonitorTimer != 0) { mDevMonitorTimer->stop(); mDevMonitorTimer = 0; delete mDevMonitorTimer; } } void CLANDeviceMonitor::MonitorTimeExpired() { PingDevice(); } bool CLANDeviceMonitor::IsDeviceOnline() { return mIsDeviceOnline; } void CLANDeviceMonitor::PingProcessFinished(int exitCode, QProcess::ExitStatus exitStatus) { QString Result = mPingProcess->readAllStandardOutput(); if(Result.toLower().contains("ttl=")) { //Online if(mIsDeviceOnline == false) { QString Msg = QString("LAN Device %1 is now ONLINE on network").arg(mDeviceIPAddress); CGeneralMessagesLogDispatcher::instance()->AddLogMessage(Msg,"CLANDeviceMonitor"); } mIsDeviceOnline = true; } else { //Offline if(mIsDeviceOnline == true) { QString Msg = QString("LAN Device %1 is now OFFLINE on network").arg(mDeviceIPAddress); CGeneralMessagesLogDispatcher::instance()->AddLogMessage(Msg,"CLANDeviceMonitor"); } mIsDeviceOnline = false; } GetDeviceCANStatusMask(); if(mDevMonitorTimer != 0) mDevMonitorTimer->start(LAN_DEVICE_MONITOR_PRESENCE_CHECK_TIMEOUT); } quint64 CLANDeviceMonitor::GetDeviceCANStatusMask() { quint64 Mask = 0; if(mIsDetectionActive == true && mIsDeviceOnline == false) { Mask = 1; Mask <<= mCANStatusBit; } return Mask; }