Files
qt5-legatus-legacy/Lts/Plugins/Buildings/Gui/BuildingInfo/egcu_info_dialog.cpp
T
2026-08-13 18:33:10 +02:00

382 lines
13 KiB
C++
Executable File

/**
*@RDE_HEADER@
* GUI Plugin for Building - Info
* $Id$
* $HeadURL$
* $Author$
* Created by hjl, SLSE, 2017-04-05
*/
#include "egcu_info_dialog.h"
#include "ui_egcu_info_dialog.h"
#include "relay_info.h"
// Lts
#include <LtsSession/lts_session.h>
// DDS
#include <AssetManagement/asm_data_plugin_interface.h>
#include <InfrastructureManagement/ism_dtc.h>
#include <QJsonDocument>
#include <QJsonArray>
namespace Buildings
{
EgcuInfoDialog::EgcuInfoDialog(int assetId, const QString &menuGroup, QWidget *parent)
: InfoDialogBase(assetId, menuGroup, QStringLiteral("EgcuInfoDialog"), LtsWidgets::ButtonCombination::CloseOnly, parent)
, m_ui(new Ui::EgcuInfoDialog)
, m_requestHelper(new LtsUtils::RequestHelper(this))
, m_assetId(assetId)
{
DEBUG_LOG(m_logger) << "EgcuInfoDialog::EgcuInfoDialog";
m_ui->setupUi(this);
m_ui->lblIcon->setPixmap(QPixmap(QStringLiteral(":/icons/svg/bgv_effect_generation_unit.svg")));
setOnlineHelpFilename(QStringLiteral("Info_Dialog-Effect_Generation_Control_Unit"));
#ifndef LTS_DEBUG
m_ui->lblAssetId->hide();
m_ui->lbxAssetId->hide();
m_ui->lblAssetName->hide();
m_ui->lbxAssetName->hide();
#endif
} // end of ctor()
//-----------------------------------------------------------------------------
EgcuInfoDialog::~EgcuInfoDialog()
{
//delete m_stateTimer;
delete m_ui;
DEBUG_LOG(m_logger) << "EgcuInfoDialog::~EgcuInfoDialog";
} // end of dtor()
//-----------------------------------------------------------------------------
void EgcuInfoDialog::retranslate()
{
translate(m_ui, tr("Segment: %1").arg(m_ui->lblSegment->text().isEmpty() ? m_ui->lblAssetName->text() : m_ui->lblSegment->text()));
}
bool EgcuInfoDialog::init(LtsSession::LtsSession *session)
{
DEBUG_LOG(m_logger) << "EgcuInfoDialog::init";
if (!InfoDialogBase::init(session))
return false;
// Get ASM DTC for RelayInfos
if (!m_session->getDtc<AssetManagement::DataInterface>(m_asmDtc))
return false;
m_asmDtc->enableRelayInfo();
m_relayLayout = new QVBoxLayout(m_ui->widgetRelais);
m_relayLayout->setMargin(0);
m_ui->widgetRelais->setLayout(m_relayLayout);
auto cbRelayInfos = [this](const AssetManagement::RelayInfo & c)
{
onRelayInfoChanged(c, DataModification::eAdded);
};
m_asmDtc->waitForHistoricalDataRelayInfo();
m_asmDtc->relayInfos(cbRelayInfos);
connect(m_asmDtc.get(), &AssetManagement::Interface::Dtc::relayInfoChanged,
this, &EgcuInfoDialog::onRelayInfoChanged);
// FTC for sending Relay changes:
if (!session->getFtc<InfrastructureManagement::DataInterface>(m_ftc))
return false;
// m_ftc->enableMainStructureElementModify();
m_ftc->enableStructureElementUpdateEffectOverride();
// Which EGCU is on this MountingPoint?
m_ismDtc->enableMountingPointAsset();
auto cbMountPoint = [this](const InfrastructureManagement::MountingPointAsset & c)
{
onMountPointAssetChanged(c, DataModification::eAdded);
};
// used by onMountPointAssetChanged
m_ismDtc->enableInstrumentationAsset();
m_ismDtc->mountingPointAssets(cbMountPoint);
connect(m_ismDtc.get(), &InfrastructureManagement::Interface::Dtc::instrumentationAssetChanged,
this, &EgcuInfoDialog::onInstrumentationAssetChanged);
connect(m_ismDtc.get(), &InfrastructureManagement::Interface::Dtc::mainStructureElementChanged,
this, &EgcuInfoDialog::onMainStructureElementChanged);
connect(m_ismDtc.get(), &InfrastructureManagement::Interface::Dtc::mountingPointAssetChanged,
this, &EgcuInfoDialog::onMountPointAssetChanged);
////////////////////
/// Relay State ///
////////////////////
m_dtc->enableRelayState();
m_dtc->waitForHistoricalDataRelayState(10000);
m_dtc->relayStates([this](const Buildings::RelayState & sample)
{
onRelayStateChanged(sample, DataModification::eAdded);
});
connect(m_dtc.get(), &Buildings::Interface::Dtc::relayStateChanged,
this, &EgcuInfoDialog::onRelayStateChanged);
return true;
}
QString EgcuInfoDialog::buildingDisplayName() const
{
return m_ui->lblBuildingName->text();
}
QString EgcuInfoDialog::structureElementName(int seId)
{
InfrastructureManagement::StructureElement se;
m_ismDtc->getStructureElementById(seId, se);
return QString::fromStdString(se.displayName());
}
void EgcuInfoDialog::showBuildingInfos()
{
// Nothing to do...
}
void EgcuInfoDialog::onInstrumentationAssetChanged(
const InfrastructureManagement::InstrumentationAsset &data, DataModification modType)
{
if (modType != DataModification::eAdded &&
modType != DataModification::eModified)
return;
DEBUG_LOG(m_logger) << "onInstrumentationAssetChanged id:" << data.id();
// Todo: Check, wether this impacts functionality!
// if (data.mountingPointTypeId() == InfrastructureManagement::MountingPointType_def::EffectGenerationControlUnit
// && data.assetStateId() == InfrastructureManagement::InstrumentationAssetState_def::Mounted)
if (data.id() != m_instrumentationAsset.id())
return;
DEBUG_LOG(m_logger) << "Insert instrumentation asset id=" << data.id();
m_instrumentationAsset = data;
showEgcuInfos();
}
void EgcuInfoDialog::onMainStructureElementChanged(const InfrastructureManagement::MainStructureElement &data, DataModification modType)
{
if (modType == DataModification::eRemoved)
return;
if (data.id() != m_structureElement.mainStructureElementId())
return;
for (const auto &se : data.elements())
{
if (se.id() != m_structureElement.id())
continue;
m_structureElement = se;
showEgcuInfos(se);
}
}
void EgcuInfoDialog::onEffectOverrideChanged(int relayId, InfrastructureManagement::EffectOverride override)
{
DEBUG_LOG(m_logger) << "onEffectOverrideChanged"
<< " m_structureElement.id=" << m_structureElement.id()
<< " relayId=" << relayId
<< " override=" << override;
// auto copy = m_structureElement;
// udpatePropertiesForRelayOverride(relayId, override, copy);
// InfrastructureManagement::StructureElementModifyRequest request;
// request.mainStructureElementId(copy.mainStructureElementId());
// request.elements().resize(1);
// request.elements().at(0) = copy;
// m_requestHelper->startSingleRequest([request, this]()
// {
// return m_ftc->structureElementModifyRequest(request, this);
// });
InfrastructureManagement::StructureElementUpdateEffectOverrideRequest request;
request.structureElementId(m_structureElement.id());
request.effectId(relayId);
request.effectOverride(override);
m_requestHelper->startSingleRequest([request, this]()
{
return m_ftc->structureElementUpdateEffectOverrideRequest(request, this);
});
}
void EgcuInfoDialog::onRelayInfoChanged(const AssetManagement::RelayInfo &data, DataModification modType)
{
DEBUG_LOG(m_logger) << "onRelayInfosChanged id:" << data.id();
if (modType == DataModification::eRemoved)
{
if (m_relayInfoWidgets.contains(data.id()))
{
m_relayInfoWidgets[data.id()]->deleteLater();
m_relayInfoWidgets.remove(data.id());
}
return;
}
if (m_relayInfoWidgets.contains(data.id()))
{
m_relayInfoWidgets[data.id()]->setName(QString::fromStdString(data.name()));
DEBUG_LOG(m_logger) << "contains Name=" << QString::fromStdString(data.name());
}
else
{
RelayInfoWidget *w = new RelayInfoWidget(data.id(), this);
m_relayInfoWidgets.insert(data.id(), w);
w->setName(QString::fromStdString(data.name()));
DEBUG_LOG(m_logger) << "Name=" << QString::fromStdString(data.name());
connect(w, &RelayInfoWidget::effectOverrideChanged, this, &EgcuInfoDialog::onEffectOverrideChanged);
w->setEffectOverrideEnabled(InfrastructureManagement::EffectOverride::PermanentOn, readAllowance(InfrastructureManagement::EffectOverride::PermanentOn, data.property()));
w->setEffectOverrideEnabled(InfrastructureManagement::EffectOverride::PermanentOff, readAllowance(InfrastructureManagement::EffectOverride::PermanentOff, data.property()));
w->setLedVisible(readShowStatus(data.property()));
m_relayLayout->addWidget(w);
}
DEBUG_LOG(m_logger) << "data.name=" << data.name();
} // end of onRelayInfosChanged()
bool EgcuInfoDialog::readAllowance(InfrastructureManagement::EffectOverride eo, const std::string &properties)
{
QJsonParseError parserError;
QByteArray content = QByteArray::fromStdString(properties);
const QJsonDocument doc(QJsonDocument::fromJson(content, &parserError));
if (parserError.error != QJsonParseError::NoError)
return true;
QJsonObject docObj = QJsonObject(doc.object());
if (eo == InfrastructureManagement::EffectOverride_def::PermanentOn)
return docObj.value(QStringLiteral("allowPermanentOn")).toBool(true);
else if (eo == InfrastructureManagement::EffectOverride_def::PermanentOff)
return docObj.value(QStringLiteral("allowPermanentOff")).toBool(true);
else
return true;
}
bool EgcuInfoDialog::readShowStatus(const std::string &properties)
{
QJsonParseError parserError;
QByteArray content = QByteArray::fromStdString(properties);
const QJsonDocument doc(QJsonDocument::fromJson(content, &parserError));
if (parserError.error != QJsonParseError::NoError)
return true;
QJsonObject docObj = QJsonObject(doc.object());
return docObj.value(QStringLiteral("showStatus")).toBool(true);
}
void EgcuInfoDialog::onRelayStateChanged(const Buildings::RelayState &data, DataModification modType)
{
DEBUG_LOG(m_logger) << "onRelayStateChanged id:" << data.id();
if (modType != DataModification::eAdded &&
modType != DataModification::eModified)
return;
if (data.id() != m_instrumentationAsset.id())
return;
for (const Buildings::RelayStateItem &relayState : data.relayStates())
{
if (!m_relayInfoWidgets.contains(relayState.id()))
continue;
m_relayInfoWidgets[relayState.id()]->setState(relayState.state(),
data.stateTime(),
data.offlineTime());
}
} // end of onRelayInfosChanged()
void EgcuInfoDialog::onMountPointAssetChanged(const InfrastructureManagement::MountingPointAsset &data, DataModification modType)
{
if (m_assetId != data.assetId())
return;
if (modType != DataModification::eAdded &&
modType != DataModification::eModified)
{
deleteLater();
return;
}
m_instrumentationAsset = m_ismDtc->instrumentationAsset(data.assetId());
m_ismDtc->getStructureElementById(data.structureElementId(), m_structureElement);
m_ui->lblSegment->setText(QString::fromStdString(data.name()));
showEgcuInfos(m_structureElement);
// To change title:
retranslate();
}
// end of onMountPointAssetChanged()
void EgcuInfoDialog::showEgcuInfos(const InfrastructureManagement::StructureElement &se)
{
DEBUG_LOG(m_logger) << "EgcuInfoDialog::showEgcuInfos";
m_ui->lblAssetName->setText(QString::fromStdString(m_instrumentationAsset.name()));
m_ui->lblAssetId->setText(QString::number(m_instrumentationAsset.id()));
m_ui->lblHostName->setText(QString::fromStdString(m_instrumentationAsset.hostname()));
// SET UI Infos
if (se.id() == 0)
return;
m_ui->lblRoom->hide();
m_ui->lbxRoom->hide();
m_ui->lblFloor->hide();
m_ui->lbxFloor->hide();
switch (se.path().size()) {
case 4:
m_ui->lblRoom->setText(structureElementName(se.path()[2]));
m_ui->lblRoom->show();
m_ui->lbxRoom->show();
Q_FALLTHROUGH();
case 3:
m_ui->lblFloor->setText(structureElementName(se.path()[1]));
m_ui->lblFloor->show();
m_ui->lbxFloor->show();
Q_FALLTHROUGH();
case 2:
{
m_ui->lblBuildingName->setText(structureElementName(se.path()[0]));
break;
}
default:
break;
}
// Set the correct Button down:
QJsonParseError parserError;
QByteArray content = QByteArray::fromStdString(se.properties());
const QJsonDocument doc(QJsonDocument::fromJson(content, &parserError));
if (parserError.error != QJsonParseError::NoError)
return;
QJsonObject docObj = QJsonObject(doc.object());
QJsonObject overridesObj = docObj.value(QStringLiteral("Overrides")).toObject();
QJsonArray arrRelays = overridesObj.value(QStringLiteral("relays")).toArray();
QList<int> allRelayIds = m_relayInfoWidgets.keys();
for (int i=0; i<arrRelays.size(); ++i)
{
QJsonObject objState = arrRelays[i].toObject();
QStringList relayIds = objState.keys();
QString relayIdString = relayIds.at(0);
int relayId = relayIdString.toInt();
allRelayIds.removeOne(relayId);
int value = objState.value(relayIdString).toInt();
if (m_relayInfoWidgets.contains(relayId))
m_relayInfoWidgets[relayId]->setEffectOverride(static_cast<InfrastructureManagement::EffectOverride::inner_enum>(value));
}
for (auto relayId : qAsConst(allRelayIds))
m_relayInfoWidgets[relayId]->setEffectOverride(InfrastructureManagement::EffectOverride_def::Automatic);
}
} // namespace Buildings