diff --git a/src/command.cpp b/src/command.cpp index 6d8571308682de62f806deb6d1245d6b35555ac1..e400e0ef95be0eebaa6eff52d5f22115af056a2b 100644 --- a/src/command.cpp +++ b/src/command.cpp @@ -77,6 +77,8 @@ Command Command::fromString(const QString& source) return parseStoreToken(rootobj); case Action::updateExperimentState: return parseUpdateExperimentState(rootobj); + case Action::setTargetState: + return parseSetTargetState(rootobj); case Action::sendHistogramUpdate: case Action::moveTargetCloser: case Action::moveTargetFarther: @@ -91,7 +93,6 @@ Command Command::fromString(const QString& source) // Not yet implemented: case Action::queryServerInformation: case Action::updateHistogram: - case Action::setTargetState: case Action::fillHistogram: return Command(); default: @@ -207,6 +208,26 @@ Protocol::Command Protocol::Command::parseUpdateExperimentState(const QJsonObjec ); } +Protocol::Command Protocol::Command::parseSetTargetState(const QJsonObject& input) +{ + if (!input.keys().contains(QStringLiteral("newstate")) || !input.value(QStringLiteral("newstate")).isString()) { + logError(QStringLiteral("setTargetState command does not contain newstate.")); + return Command(); + } + if (input.value(QStringLiteral("newstate")).toString() != QStringLiteral("open") + && input.value(QStringLiteral("newstate")).toString() != QStringLiteral("closed") + && input.value(QStringLiteral("newstate")).toString() != QStringLiteral("goldfoil")) { + logError(QStringLiteral("newstate parameter of setTargetState command is invalid.")); + return Command(); + } + + return Command( + Action::setTargetState, + Arguments{std::make_pair(QStringLiteral("newstate"), input.value(QStringLiteral("newstate")).toString())}, + input.value(QStringLiteral("token")).toString() + ); +} + QString Command::toString() const { if (action == Action::invalid) diff --git a/src/command.h b/src/command.h index d87819ca333563819df5b1a8a1a2e6afa5143d95..d6e9586d703d998d15ba6eaad457e9e5ca008529 100644 --- a/src/command.h +++ b/src/command.h @@ -73,6 +73,7 @@ private: static Command parseStoreToken(const QJsonObject& input); static Command parseError(const QJsonObject& input); static Command parseUpdateExperimentState(const QJsonObject& input); + static Command parseSetTargetState(const QJsonObject& input); static QString getActionString(const Action action); };