Skip to content

Commit

Permalink
start implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vcloarec committed Mar 1, 2023
1 parent 39bd01a commit b318fc9
Show file tree
Hide file tree
Showing 7 changed files with 262 additions and 60 deletions.
5 changes: 3 additions & 2 deletions Tests/simulationEngines/hecras/test_hecras.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,7 @@ void ReosHecrasTesting::importAndLaunchStructure()
if ( !versions.isEmpty() )
{
ReosHecRasController controller( versions.last() );
controller.initialize();

QVERIFY( controller.isValid() );

Expand All @@ -928,8 +929,8 @@ void ReosHecrasTesting::importAndLaunchStructure()
QCOMPARE( plans.at( 0 ), QStringLiteral( "plan_test" ) );
QCOMPARE( plans.at( 1 ), QStringLiteral( "plan_test_2" ) );

QVERIFY( controller.setCurrentPlan( plans.at( 1 ) ) );
QVERIFY( controller.setCurrentPlan( plans.at( 0 ) ) );
QVERIFY( controller.setCurrentPlanPrivate( plans.at( 1 ) ) );
QVERIFY( controller.setCurrentPlanPrivate( plans.at( 0 ) ) );

QVERIFY( !controller.computeCurrentPlan().isEmpty() );
}
Expand Down
2 changes: 2 additions & 0 deletions src/dataProviders/hec-dss/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
SET(REOS_HEC_DSS_SOURCES
reosdssutils.cpp
reosdssfile.cpp
reosdsswatcher.cpp
)

SET(REOS_HEC_DSS_HEADERS
reosdssutils.h
reosdssfile.h
reosdsswatcher.h
)

SET(REOS_PROVIDER_HEC_DSS_SOURCES
Expand Down
8 changes: 8 additions & 0 deletions src/dataProviders/hec-dss/reosdsswatcher.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include "reosdsswatcher.h"



void ReosDssWatcher::setTimeWatchingTimeWindow( const ReosTimeWindow &timeWatchingTimeWindow )
{
mTimeWatchingTimeWindow = timeWatchingTimeWindow;
}
124 changes: 124 additions & 0 deletions src/dataProviders/hec-dss/reosdsswatcher.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
#ifndef REOSDSSWATCHER_H
#define REOSDSSWATCHER_H

#include <QObject>
#include <QThread>
#include <QTimer>

#include "reosdssfile.h"

class ReosDssWatcher : public QObject
{
Q_OBJECT
public:
ReosDssWatcher( const QString &filePath )
: mFilePath( filePath )
{
connect( &mTimer, &QTimer::timeout, this, &ReosDssWatcher::watch );
}

void startWatch()
{
mFile.reset( new ReosDssFile( mFilePath ) );
if ( !mFile->isValid() )
return;
mTimer.start( mWatchInterval );
}

void setTimeWatchingTimeWindow( const ReosTimeWindow &newTimeWatchingTimeWindow );

signals:
void sendValues( QString path, QList<double> values, qint64 timeStepMillisec );

private slots:
void watch()
{

}

private:
QString mFilePath;
std::unique_ptr<ReosDssFile> mFile;
ReosTimeWindow mTimeWatchingTimeWindow;
QList<ReosDssPath> mPaths;
QTimer mTimer;
int mWatchInterval = 500;

QHash<QString, QDateTime> mLastObtainedTime;

void watchForTimeSerie( const ReosDssPath &path )
{
QVector<double> values;
ReosDuration timeStep;
QDateTime startTime;
mFile->getSeries( path, mTimeWatchingTimeWindow, values, timeStep, startTime );

if ( values.count() > 0 )
{
const QDateTime lastTimeRecorded = mLastObtainedTime.value( path.string() );
const QDateTime lastTimeValue = startTime.addMSecs( ( values.count() - 1 ) * timeStep.valueMilliSecond() );
if ( !lastTimeRecorded.isValid() || lastTimeValue > lastTimeRecorded )
{
int timeStepCount;
if ( lastTimeRecorded.isValid() )
{
timeStepCount = values.count();
}
else
{
timeStepCount = lastTimeRecorded.msecsTo( lastTimeValue ) / timeStep.valueMilliSecond();
}

mLastObtainedTime.insert( path.string(), lastTimeValue );

QList<double> valuesToSend;
valuesToSend.reserve( timeStepCount );
for ( int i = values.count() - timeStepCount; i < values.count(); ++i )
{
valuesToSend.append( values.at( i ) );
}

emit sendValues( path.string(), valuesToSend, timeStep.valueMilliSecond() );
}
}
}

};

class ReosDssWatcherControler : public QObject
{
Q_OBJECT
public:
ReosDssWatcherControler( const QString &dssFilePath )
: mFilePath( dssFilePath )
{

}

~ReosDssWatcherControler()
{
mThread.quit();
mThread.wait();
mWatcher->deleteLater();
}

void startWatching()
{
mWatcher = new ReosDssWatcher( mFilePath );
QObject::connect( &mThread, &QThread::started, mWatcher, &ReosDssWatcher::startWatch );
mWatcher->moveToThread( &mThread );
}

private slots:
void receiveFlowValues( QString &path, QList<double> values, qint64 timeStepMillisec )
{

}

private:
QThread mThread;
QString mFilePath;
ReosDssWatcher *mWatcher = nullptr;
};

#endif // REOSDSSWATCHER_H
70 changes: 66 additions & 4 deletions src/simulationEngines/hecras/reoshecrascontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,14 @@ QStringList ReosHecRasController::availableVersion()
return ret;
}

ReosHecRasController::ReosHecRasController( const QString &version )
: mVersion( version )
void ReosHecRasController::initialize()
{
#ifdef _WIN32
if ( !SUCCEEDED( CoInitializeEx( nullptr, COINIT_APARTMENTTHREADED ) ) )
return;

CLSID ClassID;
QString controllerName = version + QStringLiteral( ".HECRASController" );
QString controllerName = mVersion + QStringLiteral( ".HECRASController" );

std::wstring controllerString = qStringToWideString( controllerName );

Expand Down Expand Up @@ -196,6 +195,10 @@ ReosHecRasController::ReosHecRasController( const QString &version )
#endif
}

ReosHecRasController::ReosHecRasController( const QString &version )
: mVersion( version )
{}

ReosHecRasController::~ReosHecRasController()
{
#ifdef _WIN32
Expand Down Expand Up @@ -340,7 +343,12 @@ QStringList ReosHecRasController::planNames() const
return ret;
}

bool ReosHecRasController::setCurrentPlan( const QString &planName )
void ReosHecRasController::setCurrentPlan( const QString &currentPlan )
{
mCurrentPlan = currentPlan;
}

bool ReosHecRasController::setCurrentPlanPrivate( const QString &planName )
{
bool ok = false;
#ifdef _WIN32
Expand Down Expand Up @@ -376,6 +384,8 @@ bool ReosHecRasController::setCurrentPlan( const QString &planName )

QStringList ReosHecRasController::computeCurrentPlan()
{
setCurrentPlanPrivate( mCurrentPlan );

QStringList ret;
#ifdef _WIN32
DISPID id = mFunctionNames.value( QStringLiteral( "Compute_CurrentPlan" ) );
Expand Down Expand Up @@ -671,3 +681,55 @@ bool ReosHecRasController::hideComputationWindow() const
#endif
return false;
}

void ReosHecRasController::setProjectFileName( const QString &newProjectFileName )
{
mProjectFileName = newProjectFileName;
}

void ReosHecRasController::startComputation()
{
initialize();

if ( !isValid() )
{
emit sendInformation( tr( "Controller of HEC-RAS found is not valid.\nCalculation cancelled." ) );
return;
}

if ( !openHecrasProject( mProjectFileName ) )
{
emit sendInformation( tr( "UNable to open HEC-RAS project file \"%1\".\nCalculation cancelled." ).arg( mProjectFileName ) );
return;
}

QStringList plans = planNames();

if ( !plans.contains( mCurrentPlan ) )
{
emit sendInformation( tr( "Plan \"%1\" not found.\nCalculation cancelled." ).arg( mCurrentPlan ) );
return;
}

if ( !setCurrentPlanPrivate( mCurrentPlan ) )
{
emit sendInformation( tr( "Unable to set plan \"%1\" as current plan.\nCalculation cancelled." ).arg( mCurrentPlan ) );
return;
}

showComputationWindow();

const QStringList returnedMessages = computeCurrentPlan();

for ( const QString &mes : returnedMessages )
{
emit sendInformation( mes );
}

mIsSuccessful = !returnedMessages.isEmpty() && returnedMessages.last() == QStringLiteral( "Computations Completed" );
}

bool ReosHecRasController::isSuccessful() const
{
return mIsSuccessful;
}
68 changes: 47 additions & 21 deletions src/simulationEngines/hecras/reoshecrascontroller.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@

#include <QPolygon>
#include <QMap>
#include <QObject>

class ReosHecRasController

class ReosHecRasController : public QObject
{
Q_OBJECT
public:
//! Constructor with \a version of HecRas, \see availableVersion()
explicit ReosHecRasController( const QString &version );
Expand All @@ -38,42 +41,65 @@ class ReosHecRasController
//! Returns whether the controller is valid
bool isValid() const;

//! Returns the user-friendly string of the controller version of this instance
QString version() const;
//! Sets the current plan
void setCurrentPlan( const QString &currentPlan );

//! Opens a project with path \a projFileName
bool openHecrasProject( const QString &projFileName );
//! Sets the current project file name
void setProjectFileName( const QString &newProjectFileName );

//! Returns the plan names of the currently opened project
QStringList planNames() const;
bool isSuccessful() const;

//! Set the current plan
bool setCurrentPlan( const QString &planName );
public slots:
void startComputation();

//! Starts computation of the current plan
QStringList computeCurrentPlan();
signals:
void sendInformation( const QString &info );

//! Returns the flow 2D area names of the currently opened project
QStringList flowAreas2D() const;
private:

QString mVersion;
QString mProjectFileName;
QString mCurrentPlan;
bool mIsValid = false;
bool mIsSuccessful = false;
#ifdef _WIN32
IDispatch *mDispatch = nullptr;
QMap<QString, DISPID> mFunctionNames;
#endif

bool exitRas() const;

bool setCurrentPlanPrivate( const QString &planName );

//! Initializes the controller before using it. ONce initialized, the controller must be used in the thread where is has been initializd.
void initialize();

//! Returns the domain of the flow area with \a areaName
QPolygonF flow2DAreasDomain( const QString &areaName ) const;

//! Returns the flow 2D area names of the currently opened project
QStringList flowAreas2D() const;

//! Returns the plan names of the currently opened project
QStringList planNames() const;

//! Returns the user-friendly string of the controller version of this instance
QString version() const;

//! Opens a project with path \a projFileName
bool openHecrasProject( const QString &projFileName );

//! Starts computation of the current plan
QStringList computeCurrentPlan();

bool showRas() const;

bool showComputationWindow() const;

bool hideComputationWindow() const;

private:
QString mVersion;
bool mIsValid = false;
#ifdef _WIN32
IDispatch *mDispatch = nullptr;
QMap<QString, DISPID> mFunctionNames;
#endif
friend class ReosHecrasTesting;

bool exitRas() const;
};


Expand Down
Loading

0 comments on commit b318fc9

Please sign in to comment.