YULTek/Otarcik_CAN/Sources/LANDeviceMonitor.cpp

91 lines
2.0 KiB
C++

#include "LANDeviceMonitor.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(mDevMonitorTimer != 0)
delete mDevMonitorTimer;
if(mPingProcess != 0)
delete mPingProcess;
}
void CLANDeviceMonitor::MonitorTimeExpired()
{
PingDevice();
}
void CLANDeviceMonitor::PingProcessFinished(int exitCode, QProcess::ExitStatus exitStatus)
{
QString Result = mPingProcess->readAllStandardOutput();
if(Result.toLower().contains("ttl="))
{
//Online
mIsDeviceOnline = true;
qDebug("Device Online");
}
else
{
//Offline
mIsDeviceOnline = false;
qDebug("Device Offline");
}
GetDeviceCANStatusMask();
mDevMonitorTimer->start(LAN_DEVICE_MONITOR_PRESENCE_CHECK_TIMEOUT);
// qDebug("%s",qPrintable(Result));
}
quint64 CLANDeviceMonitor::GetDeviceCANStatusMask()
{
quint64 Mask = 0;
if(mIsDeviceOnline == false)
{
Mask = 1;
Mask <<= mCANStatusBit;
}
}