64 lines
1.9 KiB
C++
64 lines
1.9 KiB
C++
#include "PIHistorianManager.h"
|
|
#include "ZTLog.h"
|
|
|
|
CPIHistorianManager::CPIHistorianManager(CModbusRepository *PIHistorianRepo, int ModbusPort)
|
|
{
|
|
mModbusPort = ModbusPort;
|
|
mModbusServer = new QTcpServer();
|
|
connect(mModbusServer,SIGNAL(newConnection()),this,SLOT(NewModbusConnection()));
|
|
mPIHistorianRepo = PIHistorianRepo;
|
|
}
|
|
|
|
CPIHistorianManager::~CPIHistorianManager()
|
|
{
|
|
StopPIHistorian();
|
|
delete mModbusServer;
|
|
}
|
|
|
|
void CPIHistorianManager::NewModbusConnection()
|
|
{
|
|
QTcpSocket* SessionSocket = mModbusServer->nextPendingConnection();
|
|
if(SessionSocket != 0)
|
|
{
|
|
CPIHistorianSession *NewSession = new CPIHistorianSession(mPIHistorianRepo,PI_HISTORIAN_MODBUS_DEVICE_ID);
|
|
connect(NewSession,SIGNAL(PIHistorianSessionClosed(CPIHistorianSession*)),this,SLOT(HistorianSessionClosed(CPIHistorianSession*)));
|
|
mHistorianSessionsList.append(NewSession);
|
|
NewSession->OpenSession(SessionSocket);
|
|
}
|
|
|
|
}
|
|
|
|
void CPIHistorianManager::HistorianSessionClosed(CPIHistorianSession *SessionPtr)
|
|
{
|
|
for(int i = 0; i < mHistorianSessionsList.size(); i++)
|
|
{
|
|
if(mHistorianSessionsList.at(i) == SessionPtr)
|
|
{
|
|
CPIHistorianSession *Session = mHistorianSessionsList.takeAt(i);
|
|
delete Session;
|
|
return;
|
|
}
|
|
}
|
|
CZTLog::instance()->AddLogString(QString("Erreur de logique dans PIHistorianManager::HistorianSessionClosed. [Session fermée inconnue] "));
|
|
}
|
|
|
|
int CPIHistorianManager::StartPIHistorian()
|
|
{
|
|
mModbusServer->listen(QHostAddress::Any,mModbusPort);
|
|
CZTLog::instance()->AddLogString(QString("Gestionnaire de l'historien PI démarré sur le port %1").arg(mModbusPort),true);
|
|
return 1;
|
|
}
|
|
|
|
int CPIHistorianManager::StopPIHistorian()
|
|
{
|
|
mModbusServer->close();
|
|
for(int i = 0; i < mHistorianSessionsList.size(); i++)
|
|
{
|
|
CPIHistorianSession *Session = mHistorianSessionsList.takeFirst();
|
|
Session->CloseSession();
|
|
delete Session;
|
|
|
|
}
|
|
|
|
}
|