319 lines
9.4 KiB
C++
Executable File
319 lines
9.4 KiB
C++
Executable File
/**
|
|
*@RDE_HEADER@
|
|
* GUI Plugin for Building - Info
|
|
* $Id$
|
|
* $HeadURL$
|
|
* $Author$
|
|
* Created by hjl, SLSE, 2017-04-05
|
|
*/
|
|
|
|
#include "egcu_operational.h"
|
|
#include "ui_egcu_operational.h"
|
|
#include "bld_config_dialog.h"
|
|
|
|
// Lts
|
|
#include <LtsWidgets/lts_wait_dialog.h>
|
|
|
|
// DDS
|
|
#include <SituationMapPlugin/situation_map_gui_plugin_interface.h>
|
|
#include <InfrastructureManagement/ism_dtc.h>
|
|
|
|
// Qt
|
|
#include <QNetworkReply>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
#include <QEventLoop>
|
|
#include <QTimer>
|
|
|
|
namespace Buildings
|
|
{
|
|
|
|
EgcuOperationalDialog::EgcuOperationalDialog(int mountPointId, const QString &menuGroup, QWidget *parent)
|
|
: InfoDialogBase(mountPointId, menuGroup, QStringLiteral("EgcuOperationalDialog"), LtsWidgets::ButtonCombination::OkApplyCancel, parent)
|
|
, m_ui(new Ui::EgcuOperationalDialog)
|
|
, m_mountPointId(mountPointId)
|
|
{
|
|
m_ui->setupUi(this);
|
|
|
|
m_ui->m_lblIcon->setPixmap(QPixmap(QStringLiteral(":/icons/svg/bgv_effect_generation_unit.svg")));
|
|
|
|
#ifndef LTS_DEBUG
|
|
m_ui->lblAssetId->hide();
|
|
m_ui->lbAssetId->hide();
|
|
m_ui->lbStructElementId->hide();
|
|
#endif
|
|
|
|
// Set default selection
|
|
m_ui->r0->setChecked(false);
|
|
|
|
m_stateTimer = new QTimer(this);
|
|
connect(m_stateTimer, &QTimer::timeout, this, &EgcuOperationalDialog::onUpdateState);
|
|
m_stateTimer->start(1000);
|
|
|
|
} // end of ctor()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
EgcuOperationalDialog::~EgcuOperationalDialog()
|
|
{
|
|
delete m_stateTimer;
|
|
delete m_ui;
|
|
} // end of dtor()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void EgcuOperationalDialog::retranslate()
|
|
{
|
|
translate(m_ui, tr("Effect Generation Unit"));
|
|
|
|
m_ui->lbStatus->setText(QStringLiteral("<font color='orange'>%1</font>").arg(tr("Connecting...")));
|
|
}
|
|
|
|
bool EgcuOperationalDialog::init(LtsSession::LtsSession *session)
|
|
{
|
|
if (!InfoDialogBase::init(session))
|
|
return false;
|
|
showBuildingInfos();
|
|
connect(m_ismDtc.get(), &InfrastructureManagement::Interface::Dtc::instrumentationAssetChanged,
|
|
this, &EgcuOperationalDialog::onInstrumentationAssetChanged);
|
|
|
|
m_ismDtc->enableMountingPointAsset();
|
|
|
|
auto cbMountPoint = [this](const InfrastructureManagement::MountingPointAsset & c)
|
|
{
|
|
onMountPointAssetChanged(c, DataModification::eAdded);
|
|
};
|
|
m_ismDtc->mountingPointAssets(cbMountPoint);
|
|
|
|
m_ismDtc->enableInstrumentationAsset();
|
|
|
|
auto cbInstrumentationAsset = [this](const InfrastructureManagement::InstrumentationAsset & c)
|
|
{
|
|
onInstrumentationAssetChanged(c, DataModification::eAdded);
|
|
};
|
|
m_ismDtc->instrumentationAssets(cbInstrumentationAsset);
|
|
|
|
return true;
|
|
} // end of init()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
void EgcuOperationalDialog::showBuildingInfos()
|
|
{
|
|
Buildings::BuildingState bldState = m_dtc->buildingState(m_mseId);
|
|
|
|
m_ui->lblAssetId->setText(QString::number(bldState.assetId()));
|
|
} // end of showBuildingInfos()
|
|
//-----------------------------------------------------------------------------
|
|
|
|
bool EgcuOperationalDialog::apply()
|
|
{
|
|
int relay = 0;
|
|
int state = 0;
|
|
int pulse = 0;
|
|
int repeat = 0;
|
|
int delay = 0;
|
|
if (m_ui->r0->isChecked())
|
|
relay = 0;
|
|
|
|
if (m_ui->r0->isChecked())
|
|
state = 1;
|
|
|
|
DEBUG_LOG(m_logger) << "Apply: relay = " << relay
|
|
<< " state=" << state
|
|
<< " pulse=" << pulse
|
|
<< " repeat=" << repeat
|
|
<< " delay=" << delay;
|
|
|
|
QUrl url;
|
|
QString request;
|
|
if (state == 3)
|
|
{
|
|
request = QStringLiteral("http://%1/relay.php?state=%3").arg(m_host).arg(3);
|
|
}
|
|
else
|
|
{
|
|
request = QStringLiteral("http://%1/relay.php?number=%2&state=%3&pulse=%4&repeat=%5&delay=%6")
|
|
.arg(m_host)
|
|
.arg(relay)
|
|
.arg(state)
|
|
.arg(pulse)
|
|
.arg(repeat)
|
|
.arg(delay);
|
|
DEBUG_LOG(m_logger) << "startRequest" << request;
|
|
}
|
|
url.setUrl(request);
|
|
url.setUserName(m_user);
|
|
url.setPassword(m_passwd);
|
|
DEBUG_LOG(m_logger) << "startRequest: " << url.toString();
|
|
|
|
QNetworkReply *reply = m_qnam.get(QNetworkRequest(url));
|
|
{
|
|
LtsWidgets::LtsWaitDialogContainer wait;
|
|
QEventLoop loop;
|
|
connect(reply, &QNetworkReply::finished,
|
|
&loop, &QEventLoop::quit);
|
|
loop.exec();
|
|
}
|
|
|
|
bool result = (reply->error() == QNetworkReply::NetworkError::NoError);
|
|
|
|
if (!result)
|
|
LtsWidgets::LtsMessageDialog::showMessage(this, tr("Cannot configure relays: %1").arg(reply->errorString()));
|
|
|
|
reply->deleteLater();
|
|
return result;
|
|
}
|
|
|
|
void EgcuOperationalDialog::startRequest(const QUrl &requestedUrl)
|
|
{
|
|
DEBUG_LOG(m_logger) << "startRequest:" << requestedUrl.toString();
|
|
m_httpRequestAborted = false;
|
|
|
|
auto reply = m_qnam.get(QNetworkRequest(requestedUrl));
|
|
connect(reply, &QNetworkReply::finished, this, &EgcuOperationalDialog::httpFinished);
|
|
}
|
|
|
|
|
|
void EgcuOperationalDialog::httpFinished()
|
|
{
|
|
auto reply = qobject_cast<QNetworkReply*>(sender());
|
|
if (!reply)
|
|
return;
|
|
|
|
int i = 0;
|
|
if (m_httpRequestAborted)
|
|
{
|
|
m_ui->groupBoxSelectRelay->setEnabled(false);
|
|
ERROR_LOG(m_logger) << "httpRequestAborted";
|
|
reply->deleteLater();
|
|
return;
|
|
}
|
|
|
|
if (reply->error())
|
|
{
|
|
m_ui->groupBoxSelectRelay->setEnabled(false);
|
|
m_ui->lbStatus->setText(QStringLiteral("<font color='red'>%1</font>").arg(tr("Cannot connect to egcu: %1").arg(reply->errorString())));
|
|
ERROR_LOG(m_logger) << "reply->error: " << reply->errorString();
|
|
reply->deleteLater();
|
|
return;
|
|
}
|
|
|
|
|
|
// Sucess
|
|
m_ui->groupBoxSelectRelay->setEnabled(true);
|
|
m_ui->lbStatus->setText(QStringLiteral("<font color='green'>%1</font>").arg(tr("Connected to egcu")));
|
|
|
|
//parse the reply JSON and display result in the UI
|
|
QJsonObject jsonObject = QJsonDocument::fromJson(reply->readAll()).object();
|
|
QJsonDocument doc(jsonObject);
|
|
DEBUG_LOG(m_logger) << doc.toJson(QJsonDocument::Indented);
|
|
const QJsonArray jsonArray = jsonObject[QStringLiteral("State")].toArray();
|
|
for (const QJsonValue &value : jsonArray)
|
|
{
|
|
QString valueString = value.toString();
|
|
if (valueString == QStringLiteral("on"))
|
|
valueString = QStringLiteral("<b>%1</b>").arg(tr("Actual State: on"));
|
|
if (valueString == QStringLiteral("off"))
|
|
valueString = QStringLiteral("<b>%1</b>").arg(tr("Actual State: off"));
|
|
if (valueString == QStringLiteral("undefined"))
|
|
valueString = QStringLiteral("<b>%1</b>").arg(tr("Actual State: undefined"));
|
|
|
|
switch (i)
|
|
{
|
|
case 0:
|
|
m_ui->lbStateR0->setText(valueString);
|
|
break;
|
|
case 1:
|
|
case 2:
|
|
case 3:
|
|
case 4:
|
|
case 5:
|
|
case 6:
|
|
case 7:
|
|
case 8:
|
|
case 9:
|
|
break;
|
|
}
|
|
i++;
|
|
}
|
|
reply->deleteLater();
|
|
}
|
|
|
|
void EgcuOperationalDialog::onUpdateState()
|
|
{
|
|
DEBUG_LOG(m_logger) << "onUpdateState";
|
|
if (m_assetId == 0)
|
|
return;
|
|
QUrl url;
|
|
QString request;
|
|
request = QStringLiteral("http://%1/relay.php?state=3").arg(m_host);
|
|
DEBUG_LOG(m_logger) << "startRequest:" << request;
|
|
url.setUrl(request);
|
|
url.setUserName(m_user);
|
|
url.setPassword(m_passwd);
|
|
startRequest(url);
|
|
}
|
|
|
|
void EgcuOperationalDialog::onInstrumentationAssetChanged(
|
|
const InfrastructureManagement::InstrumentationAsset &data, DataModification modType)
|
|
{
|
|
if (modType != DataModification::eAdded &&
|
|
modType != DataModification::eModified)
|
|
return;
|
|
DEBUG_LOG(m_logger) << "onInstrumentationAssetChanged id:" << data.id();
|
|
// Only EGCUs
|
|
if (data.mountingPointTypeId() == InfrastructureManagement::MountingPointType_def::EffectGenerationControlUnit
|
|
&& data.assetStateId() == InfrastructureManagement::InstrumentationAssetState_def::Mounted)
|
|
{
|
|
DEBUG_LOG(m_logger) << "Insert instrumentation asset id=" << data.id();
|
|
m_instrumentationAssets.insert(data.id(), data);
|
|
if (data.id() == m_assetId)
|
|
showEgcuInfos();
|
|
}
|
|
} // end of onInstrumentationAssetChanged()
|
|
|
|
void EgcuOperationalDialog::onMountPointAssetChanged(
|
|
const InfrastructureManagement::MountingPointAsset &data, DataModification modType)
|
|
{
|
|
if (modType != DataModification::eAdded &&
|
|
modType != DataModification::eModified)
|
|
return;
|
|
DEBUG_LOG(m_logger) << "onMountPointAssetChanged id:" << data.id();
|
|
if (m_mountPointId == data.structureElementId())
|
|
{
|
|
DEBUG_LOG(m_logger) << "FOUND m_assetId=" << m_assetId;
|
|
m_assetId = data.assetId();
|
|
if (m_instrumentationAssets.contains(m_assetId))
|
|
showEgcuInfos();
|
|
}
|
|
else
|
|
{
|
|
DEBUG_LOG(m_logger) << "NOT FOUND m_assetId=" << m_assetId;
|
|
return;
|
|
}
|
|
} // end of onMountPointAssetChanged()
|
|
|
|
void EgcuOperationalDialog::showEgcuInfos()
|
|
{
|
|
InfrastructureManagement::InstrumentationAsset dataInst = m_instrumentationAssets.value(m_assetId);
|
|
m_host = QString::fromStdString(dataInst.hostname());
|
|
m_egcuName = QString::fromStdString(dataInst.name());
|
|
|
|
const QByteArray properties = QByteArray::fromRawData(dataInst.properties().c_str(),
|
|
dataInst.properties().size());
|
|
QJsonDocument propertyJsonDoc = QJsonDocument::fromJson(properties);
|
|
QJsonObject propertyJsonObj = propertyJsonDoc.object();
|
|
|
|
m_user = propertyJsonObj[QStringLiteral("username")].toString();
|
|
m_passwd = propertyJsonObj[QStringLiteral("password")].toString();
|
|
|
|
DEBUG_LOG(m_logger) << "onMountPointAssetChanged m_host:" << m_host
|
|
<< " m_egcuName" << m_host
|
|
<< " m_user" << m_user
|
|
<< " m_passwd=" << m_passwd;
|
|
m_ui->lblAssetName->setText(m_egcuName);
|
|
m_ui->lblAssetId->setText(QString::number(m_assetId));
|
|
m_ui->lblHostName->setText(m_host);
|
|
}
|
|
|
|
} // namespace Buildings
|