/** *@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 // DDS #include #include #include #include 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(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(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 allRelayIds = m_relayInfoWidgets.keys(); for (int i=0; isetEffectOverride(static_cast(value)); } for (auto relayId : qAsConst(allRelayIds)) m_relayInfoWidgets[relayId]->setEffectOverride(InfrastructureManagement::EffectOverride_def::Automatic); } } // namespace Buildings