Files
qt5-legatus-legacy/Lts/Proxies/Buildings/BuildingControlUnit/bcu_xml_reader.cpp
T
2026-08-13 18:33:10 +02:00

208 lines
6.3 KiB
C++
Executable File

/**
* @RDE_HEADER@
* Read bsm xml config file
* $Id$
* $HeadURL$
* $Author$
* Created by Gerlinde Rudolphus, SLSA, 2013-03-05
*/
#include "bcu_xml_reader.h"
#include "logger.h"
#include <QDomElement>
#include <QDomDocument>
#include <QFile>
namespace BuildingControlUnit
{
static NGL::INGLLogger &logger()
{
return BuildingControlUnit::getLogger("BcuXmlReader");
}
QDomElement readFile(const QString &fn, const QString &rootTag)
{
QFile f(fn);
if (!f.open(QIODevice::ReadOnly))
{
WARNING_LOG(logger()) << "Could not open file '" << fn << "' for reading: " << f.errorString();
return QDomElement();
}
const QByteArray data = f.readAll();
if (data.isEmpty())
{
WARNING_LOG(logger()) << "file '" << fn << "' is empty!";
return QDomElement();
}
QString errMsg;
int errLine = 0;
int errColumn = 0;
QDomDocument doc;
if (!doc.setContent(data, &errMsg, &errLine, &errColumn))
{
WARNING_LOG(logger()) << "Could not parse xml file '" << fn << "' at " << errLine << ":" << errColumn << ": " << errMsg;
return QDomElement();
}
QDomElement domRoot = doc.documentElement();
if (domRoot.tagName() != rootTag)
{
WARNING_LOG(logger()) << "Xml file '" << fn << "' is not a valid " << rootTag << " xml file!";
return QDomElement();
}
return domRoot;
}
bool hasTagText(const QDomElement &element,
const QString &tagName)
{
QDomElement child = element.firstChildElement(tagName);
if (child.isNull() || child.text().isEmpty())
{
return false;
}
return true;
}
QString getTagText(const QDomElement &element,
const QString &tagName,
bool bOptional)
{
QDomElement child = element.firstChildElement(tagName);
if (child.isNull() || child.text().isEmpty())
{
if (bOptional)
return QString();
throw XmlReaderException(QLatin1String("Element ") + element.tagName() +
QLatin1String(" has no tag ") + tagName);
}
return child.text();
}
QString getAttribute(const QDomElement &element,
const QString &attributeName,
bool bOptional)
{
if (!element.hasAttribute(attributeName))
{
if (bOptional)
return QString();
throw XmlReaderException(QLatin1String("Element ") + element.tagName() +
QLatin1String(" has no attribute ") + attributeName);
}
return element.attribute(attributeName);
}
static LtsUtils::QPoint3D getCoord3D(const QDomElement &domCoord)
{
LtsUtils::QPoint3D ret;
ret.rx() = getAttribute(domCoord, QStringLiteral("x")).toDouble();
ret.ry() = getAttribute(domCoord, QStringLiteral("y")).toDouble();
ret.rz() = getAttribute(domCoord, QStringLiteral("z")).toDouble();
return ret;
}
static Bsm parseBsm(const QDomElement &domBsm)
{
Bsm ret;
ret.id = getAttribute(domBsm, QStringLiteral("id")).toInt();
ret.hostname = getAttribute(domBsm, QStringLiteral("hostname"));
ret.port = getAttribute(domBsm, QStringLiteral("port")).toUInt();
ret.position = getCoord3D(domBsm.firstChildElement(QStringLiteral("Position")));
ret.direction = getCoord3D(domBsm.firstChildElement(QStringLiteral("Direction")));
DEBUG_LOG(logger()) << "Bsm: id = " << ret.id
<< ", position = " << ret.position.x() << "/" << ret.position.y() << "/" << ret.position.z()
<< ", direction = " << ret.direction.x() << "/" << ret.direction.y() << "/" << ret.direction.z();
return ret;
}
static Egcu parseEgcu(const QDomElement &domEgcu)
{
Egcu ret;
ret.hostname = getAttribute(domEgcu, QStringLiteral("hostname"));
// rooms
QDomElement domRooms = domEgcu.firstChildElement(QStringLiteral("rooms"));
QDomElement domRoomId = domRooms.firstChildElement(QStringLiteral("room"));
for( ; !domRoomId.isNull(); domRoomId = domRoomId.nextSiblingElement(domRoomId.tagName()))
ret.roomIds += getAttribute(domRoomId, QStringLiteral("id")).toInt();
return ret;
}
bool parseBuildingConfigXml(const QString &fn, BuildingBsmConfig &bsmCfg, BuildingEgcuConfig &egcuConfig)
{
bsmCfg = BuildingBsmConfig();
egcuConfig = BuildingEgcuConfig();
DEBUG_LOG(logger()) << "****************************";
DEBUG_LOG(logger()) << "*** Building Config: ***";
DEBUG_LOG(logger()) << "****************************";
const QDomElement domRoot = readFile(fn, QStringLiteral("BuildingConfiguration"));
if (domRoot.isNull())
return false;
try
{
const QString buildingName = getTagText(domRoot, QStringLiteral("BuildingName"));
const int modelId = getTagText(domRoot, QStringLiteral("ModelId")).toInt();
DEBUG_LOG(logger()) << "buildingName= " << buildingName << " modelId= " << modelId;
DEBUG_LOG(logger()) << "domRoot.firstChildElement(QStringLiteral(\"BSM\"))";
QDomElement domBsm = domRoot.firstChildElement(QStringLiteral("BSM"));
if (domBsm.isNull())
DEBUG_LOG(logger()) << "Keine BSM's";
for ( ; !domBsm.isNull(); domBsm = domBsm.nextSiblingElement(domBsm.tagName()))
{
try
{
DEBUG_LOG(logger()) << "parseBsm";
const Bsm b = parseBsm(domBsm);
bsmCfg.bsmMap.insert(b.id, b);
}
catch (const XmlReaderException &ex)
{
QString errorString = QLatin1String("Error reading bsm config: ") + QLatin1String(ex.what());
ERROR_LOG(logger()) << errorString;
return false;
}
}
DEBUG_LOG(logger()) << "domRoot.firstChildElement(QStringLiteral(\"EGCU\"))";
QDomElement domEgcu = domRoot.firstChildElement(QStringLiteral("EGCU"));
if (domEgcu.isNull())
DEBUG_LOG(logger()) << "Keine EGCU's";
for ( ; !domEgcu.isNull(); domEgcu = domEgcu.nextSiblingElement(domEgcu.tagName()))
{
try
{
DEBUG_LOG(logger()) << "parseEgcu";
const Egcu e = parseEgcu(domEgcu);
egcuConfig.egcus.push_back(e);
}
catch (const XmlReaderException &ex)
{
QString errorString = QLatin1String("Error reading egcu config: ") + QLatin1String(ex.what());
ERROR_LOG(logger()) << errorString;
return false;
}
}
}
catch (const XmlReaderException &ex)
{
QString errorString = QLatin1String("Error reading BuildingConfiguration: ") + QLatin1String(ex.what());
ERROR_LOG(logger()) << errorString;
return false;
}
return true;
}
} // BuildingControlUnit