Skip to content

Commit

Permalink
Parse trainDetails (includes events)
Browse files Browse the repository at this point in the history
  • Loading branch information
j7eich committed May 2, 2020
1 parent a8d160b commit e9b270a
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 0 deletions.
20 changes: 20 additions & 0 deletions QtStsPlugin/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,26 @@ namespace QtSts {
void heatReceived(int heat);
void stitzReceived(int allgemein, int region);
void trainListReceived(const QList<QPair<int, QString>>& trainList);
void trainDetailsReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void incomingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void outgoingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void arrivingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void departingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void dataFromSts(const QByteArray& data);
void dataToSts(const QByteArray& data);

Expand Down
58 changes: 58 additions & 0 deletions QtStsPlugin/PluginCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@ along with this program. If not, see <https://www.gnu.org/licenses/>.
#include <QTime>

namespace QtSts {
static const QString stsABFAHRT("abfahrt");
static const QString stsANKUNFT("ankunft");
static const QString stsANLAGENINFO(QStringLiteral("anlageninfo"));
static const QString stsAUSFAHRT("ausfahrt");
static const QString stsEINFAHRT("einfahrt");
static const QString stsEREIGNIS(QStringLiteral("ereignis"));
static const QString stsHITZE(QStringLiteral("hitze"));
static const QString stsNAME(QStringLiteral("name"));
static const QString stsSENDER(QStringLiteral("sender"));
Expand Down Expand Up @@ -169,6 +174,10 @@ void QtSts::PluginCore::handleStartElement()
{
parseTrain(attributes);
}
else if (nameIs(stsZUGDETAILS))
{
parseTrainDetails(attributes);
}
else if (nameIs(stsZUGLISTE))
{
m_trainList.clear();
Expand Down Expand Up @@ -346,3 +355,52 @@ void QtSts::PluginCore::parseTrain(const QXmlStreamAttributes& attributes)
Q_ASSERT(m_trainList.contains(train) == false);
m_trainList.append(train);
}

void QtSts::PluginCore::parseTrainDetails(const QXmlStreamAttributes& attributes)
{
const QStringRef rVisible = attributes.value(QStringLiteral("sichtbar"));
const QStringRef rOnTrack = attributes.value(QStringLiteral("amgleis"));
const QStringRef rEvent = attributes.value(stsEREIGNIS);

auto toBool = [](const QStringRef& ref) ->bool {
return (ref.compare(QStringLiteral("true"), Qt::CaseInsensitive) == 0);
};

const bool bOnTrack = toBool(rOnTrack);
const bool bVisible = toBool(rVisible);
const int zid = attributes.value(stsZID).toInt();
const QString name = attributes.value(stsNAME).toString();
const int delay = attributes.value(QStringLiteral("verspaetung")).toInt();
const QString from = attributes.value(QStringLiteral("von")).toString();
const QString to = attributes.value(QStringLiteral("nach")).toString();
const QString track = attributes.value(QStringLiteral("gleis")).toString();
const QString plannedTrack = attributes.value(QStringLiteral("plangleis")).toString();

if (rEvent.isNull())
{
Q_EMIT trainDetailsReceived(zid, name, from, to, track, plannedTrack, delay, bOnTrack, bVisible);
}
else
{
auto isEvent = [&rEvent](const QString& eventName) ->bool {
return (rEvent.compare(eventName) == 0);
};

if (isEvent(stsEINFAHRT))
{
Q_EMIT incomingTrainReceived(zid, name, from, to, track, plannedTrack, delay, bOnTrack, bVisible);
}
else if (isEvent(stsANKUNFT))
{
Q_EMIT arrivingTrainReceived(zid, name, from, to, track, plannedTrack, delay, bOnTrack, bVisible);
}
else if (isEvent(stsABFAHRT))
{
Q_EMIT departingTrainReceived(zid, name, from, to, track, plannedTrack, delay, bOnTrack, bVisible);
}
else if (isEvent(stsAUSFAHRT))
{
Q_EMIT outgoingTrainReceived(zid, name, from, to, track, plannedTrack, delay, bOnTrack, bVisible);
}
}
}
21 changes: 21 additions & 0 deletions QtStsPlugin/PluginCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,26 @@ namespace QtSts {
void heatReceived(int heat);
void stitzReceived(int allgemein, int region);
void trainListReceived(const QList<QPair<int, QString>>& trainList);
void trainDetailsReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void incomingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void outgoingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void arrivingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void departingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);

private:
void handleStartElement();
Expand All @@ -86,6 +106,7 @@ namespace QtSts {
void parseHeat(const QXmlStreamAttributes& attributes);
void parseStitz(const QXmlStreamAttributes& attributes);
void parseTrain(const QXmlStreamAttributes& attributes);
void parseTrainDetails(const QXmlStreamAttributes& attributes);

std::unique_ptr<QXmlStreamReader> m_xmlReader;
QString m_pluginName;
Expand Down
64 changes: 64 additions & 0 deletions QtStsPluginTestGui/QtStsPluginTestGui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ void QtStsPluginTestGui::on_actionInstantiate_triggered()
Q_ASSERT(connection);
connection = QObject::connect(m_plugin, SIGNAL(trainListReceived(const QList<QPair<int, QString>>&)), this, SLOT(on_trainListReceived(const QList<QPair<int, QString>>&)));
Q_ASSERT(connection);
connection = QObject::connect(m_plugin, SIGNAL(trainDetailsReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)),
this, SLOT(on_trainDetailsReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)));
Q_ASSERT(connection);
connection = QObject::connect(m_plugin, SIGNAL(incomingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)),
this, SLOT(on_incomingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)));
Q_ASSERT(connection);
connection = QObject::connect(m_plugin, SIGNAL(outgoingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)),
this, SLOT(on_outgoingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)));
Q_ASSERT(connection);
connection = QObject::connect(m_plugin, SIGNAL(arrivingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)),
this, SLOT(on_arrivingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)));
Q_ASSERT(connection);
connection = QObject::connect(m_plugin, SIGNAL(departingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)),
this, SLOT(on_departingTrainReceived(int, const QString&, const QString&, const QString&, const QString&, const QString&, int, bool, bool)));
Q_ASSERT(connection);

ui->actionDestroy->setEnabled(true);
ui->actionSetConnection->setEnabled(true);
ui->actionConnect->setEnabled(true);
Expand Down Expand Up @@ -210,6 +226,54 @@ void QtStsPluginTestGui::on_trainListReceived(const QList<QPair<int, QString>>&
ui->logText->append(log);
}

namespace {
void addLogText(QString& log, int trainId, const QString& name, const QString& from, const QString& to, const QString& track, const QString& plannedTrack, int delay, bool onTrack, bool visible)
{
log.append(QStringLiteral("\n\ttrainId=%1").arg(trainId));
log.append(QStringLiteral("\n\tname=\"%1\"").arg(name));
log.append(QStringLiteral("\n\tfrom=\"%1\"\n\tto=\"%2\"").arg(from).arg(to));
log.append(QStringLiteral("\n\ttrack=\"%1\"\n\tplannedTrack=\"%2\"").arg(track).arg(plannedTrack));
log.append(QStringLiteral("\n\tdelay=%1").arg(delay));
log.append(QStringLiteral("\n\tonTrack=%1\n\tvisible=%2").arg(onTrack).arg(visible));
}
}

void QtStsPluginTestGui::on_trainDetailsReceived(int trainId, const QString& name, const QString& from, const QString& to, const QString& track, const QString& plannedTrack, int delay, bool onTrack, bool visible)
{
QString log(tr("trainDetailsReceived:"));
addLogText(log, trainId, name, from, to, track, plannedTrack, delay, onTrack, visible);
ui->logText->append(log);

}

void QtStsPluginTestGui::on_incomingTrainReceived(int trainId, const QString& name, const QString& from, const QString& to, const QString& track, const QString& plannedTrack, int delay, bool onTrack, bool visible)
{
QString log(tr("incomingTrainReceived:"));
addLogText(log, trainId, name, from, to, track, plannedTrack, delay, onTrack, visible);
ui->logText->append(log);
}

void QtStsPluginTestGui::on_outgoingTrainReceived(int trainId, const QString& name, const QString& from, const QString& to, const QString& track, const QString& plannedTrack, int delay, bool onTrack, bool visible)
{
QString log(tr("outgoingTrainReceived:"));
addLogText(log, trainId, name, from, to, track, plannedTrack, delay, onTrack, visible);
ui->logText->append(log);
}

void QtStsPluginTestGui::on_arrivingTrainReceived(int trainId, const QString& name, const QString& from, const QString& to, const QString& track, const QString& plannedTrack, int delay, bool onTrack, bool visible)
{
QString log(tr("arrivingTrainReceived:"));
addLogText(log, trainId, name, from, to, track, plannedTrack, delay, onTrack, visible);
ui->logText->append(log);
}

void QtStsPluginTestGui::on_departingTrainReceived(int trainId, const QString& name, const QString& from, const QString& to, const QString& track, const QString& plannedTrack, int delay, bool onTrack, bool visible)
{
QString log(tr("departingTrainReceived:"));
addLogText(log, trainId, name, from, to, track, plannedTrack, delay, onTrack, visible);
ui->logText->append(log);
}

void QtStsPluginTestGui::addToCommunicationLog(const QByteArray& data, bool outgoing, bool scrollToEnd)
{
QString sHtml(QStringLiteral("<p style=\"color:"));
Expand Down
20 changes: 20 additions & 0 deletions QtStsPluginTestGui/QtStsPluginTestGui.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ public Q_SLOTS:
void on_heatReceived(int heat);
void on_stitzReceived(int allgemein, int region);
void on_trainListReceived(const QList<QPair<int, QString>>& trainList);
void on_trainDetailsReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void on_incomingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void on_outgoingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void on_arrivingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);
void on_departingTrainReceived(int trainId, const QString& name,
const QString& from, const QString& to,
const QString& track, const QString& plannedTrack,
int delay, bool onTrack, bool visible);

private:
void addToCommunicationLog(const QByteArray& data, bool outgoing, bool scrollToEnd);
Expand Down

0 comments on commit e9b270a

Please sign in to comment.