diff --git a/src/experimentstate.cpp b/src/experimentstate.cpp index a7d9b4161f8812776a83898404913cc247fc9e27..3616d01191e8d71aab2a34d72043323db445ec26 100644 --- a/src/experimentstate.cpp +++ b/src/experimentstate.cpp @@ -24,6 +24,8 @@ * **/ +#include <stdexcept> + #include "experimentstate.h" using namespace Fp311Online; @@ -64,3 +66,68 @@ Protocol::Command ExperimentState::getStateCommand() const QString() // Token will be added by class that requested the Command object ); } + +void ExperimentState::updateFromCommand(const Protocol::Command& command) +{ + if (command.action != Protocol::Action::updateExperimentState) + { + logError(QStringLiteral("Command instance passed to ExperimentState::updateFromCommand is not of type updateExperimentState.")); + return; + } + + if (command.arguments[QStringLiteral("adcstate")] == QStringLiteral("running")) + adcState = AdcState::Running; + else if (command.arguments[QStringLiteral("adcstate")] == QStringLiteral("stopped")) + adcState = AdcState::Stopped; + else + logError(QStringLiteral("Invalid value for argument \"adcstate\" of updateExperimentState command.")); + + if (command.arguments[QStringLiteral("beamhole")] == QStringLiteral("closed")) + beamHoleState = BeamHoleState::Closed; + else if (command.arguments[QStringLiteral("beamhole")] == QStringLiteral("open")) + beamHoleState = BeamHoleState::Open; + else if (command.arguments[QStringLiteral("beamhole")] == QStringLiteral("goldfoil")) + beamHoleState = BeamHoleState::GoldFoil; + else + logError(QStringLiteral("Invalid value for argument \"beamhole\" of updateExperimentState command.")); + + try { + bool ok = false; + pressure = command.arguments[QStringLiteral("pressurehPa")].toDouble(&ok); + if (!ok) + logError(QStringLiteral("Error converting the \"pressure\" string in the updateExperimentState command into a numeral.")); + } + catch(PressureHectoPascal::BoundaryExceeded& e) { + logError(QStringLiteral("\"pressure\" value in the updateExperimentState command out of range: ") + QString::fromUtf8(e.what())); + } + + try { + bool ok = false; + targetPosition = command.arguments[QStringLiteral("targetposition")].toDouble(&ok); + if (!ok) + logError(QStringLiteral("Error converting the \"targetposition\" string in the updateExperimentState command into a numeral.")); + } + catch(TargetPositionMilliMeter::BoundaryExceeded& e) { + logError(QStringLiteral("\"targetposition\" value in the updateExperimentState command out of range: ") + QString::fromUtf8(e.what())); + } + + try { + bool ok = false; + adcThreshold = static_cast<AdcConversion::valueType>(command.arguments[QStringLiteral("adcthreshold")].toUInt(&ok)); + if (!ok) + logError(QStringLiteral("Error converting the \"adcthreshold\" string in the updateExperimentState command into a numeral.")); + } + catch(AdcConversion::BoundaryExceeded& e) { + logError(QStringLiteral("\"adcthreshold\" value in the updateExperimentState command out of range: ") + QString::fromUtf8(e.what())); + } + + try { + bool ok = false; + vacuumValve = static_cast<VacuumValveOpening::valueType>(command.arguments[QStringLiteral("vacuumvalve")].toUInt(&ok)); + if (!ok) + logError(QStringLiteral("Error converting the \"vacuumvalve\" string in the updateExperimentState command into a numeral.")); + } + catch(VacuumValveOpening::BoundaryExceeded& e) { + logError(QStringLiteral("\"vacuumvalve\" value in the updateExperimentState command out of range: ") + QString::fromUtf8(e.what())); + } +} diff --git a/src/experimentstate.h b/src/experimentstate.h index a4066556caa8588a3252d70f55445273152f8369..77dbd1b49d07428f552d3f8075aa8f55d1ec15d6 100644 --- a/src/experimentstate.h +++ b/src/experimentstate.h @@ -71,6 +71,7 @@ public: static PressureTorr convertHectoPascalToTorr(const PressureHectoPascal& pressure); Protocol::Command getStateCommand() const; + void updateFromCommand(const Protocol::Command& command); ExperimentState() = default; ExperimentState(const uint64_t experimentId);