From 6ff16d56db60f32ad625596b0ae94c681867f9da Mon Sep 17 00:00:00 2001
From: Tobias Triffterer <tobias@ep1.ruhr-uni-bochum.de>
Date: Wed, 21 Apr 2021 20:47:30 +0200
Subject: [PATCH] Add Method to Generate updateExperimentState Command

The ExperimentState class can now put its properties into the update
command. Only the token needs to be added later by the class that
requested the command object.
---
 src/experimentstate.cpp | 30 +++++++++++++++++++++++++++++-
 src/experimentstate.h   |  3 +++
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/src/experimentstate.cpp b/src/experimentstate.cpp
index c107795..a7d9b41 100644
--- a/src/experimentstate.cpp
+++ b/src/experimentstate.cpp
@@ -34,5 +34,33 @@ ExperimentState::ExperimentState(const uint64_t experimentId)
 
 ExperimentState::PressureTorr ExperimentState::convertHectoPascalToTorr(const PressureHectoPascal& pressure)
 {
-    return pressure*76000.0/101325.0;
+    return pressure * 76000.0 / 101325.0;
+}
+
+Protocol::Command ExperimentState::getStateCommand() const
+{
+    const QString adcstate = (adcState == AdcState::Running) ? QStringLiteral("running") : QStringLiteral("stopped");
+    const QString beamhole = [this]() -> QString {
+        switch (beamHoleState) {
+            case BeamHoleState::Closed:
+            default:
+                return QStringLiteral("closed");
+            case BeamHoleState::Open:
+                return QStringLiteral("open");
+            case BeamHoleState::GoldFoil:
+                return QStringLiteral("goldfoil"); }
+        }();
+
+    return Protocol::Command(
+               Protocol::Action::updateExperimentState,
+               Protocol::Command::Arguments {
+                    std::make_pair(QStringLiteral("adcstate"), adcstate),
+                    std::make_pair(QStringLiteral("beamhole"), beamhole),
+                    std::make_pair(QStringLiteral("pressurehPa"), QString::number(pressure)),
+                    std::make_pair(QStringLiteral("targetposition"), QString::number(targetPosition)),
+                    std::make_pair(QStringLiteral("adcthreshold"), QString::number(adcThreshold)),
+                    std::make_pair(QStringLiteral("vacuumvalve"), QString::number(vacuumValve))
+               },
+               QString() // Token will be added by class that requested the Command object
+           );
 }
diff --git a/src/experimentstate.h b/src/experimentstate.h
index c72f266..a4dfa79 100644
--- a/src/experimentstate.h
+++ b/src/experimentstate.h
@@ -30,6 +30,7 @@
 #include <cinttypes>
 
 #include "boundednumeral.h"
+#include "command.h"
 #include "fp311online_export.h"
 
 namespace Fp311Online
@@ -68,6 +69,8 @@ public:
 
     static PressureTorr convertHectoPascalToTorr(const PressureHectoPascal& pressure);
 
+    Protocol::Command getStateCommand() const;
+
     ExperimentState() = default;
     ExperimentState(const uint64_t experimentId);
     ~ExperimentState() = default;
-- 
GitLab