From fde44896fd1e9abc3dc9cad62da9b136de2beebe Mon Sep 17 00:00:00 2001
From: Tobias Triffterer <tobias@ep1.ruhr-uni-bochum.de>
Date: Wed, 11 Aug 2021 02:51:16 +0200
Subject: [PATCH] Process Histogram Updates from Server

This parses a command from the server with the contents of all the bins
to synchronize the client with the server.
---
 src/clientgui.cpp | 27 +++++++++++++++++++++++++++
 src/clientgui.h   |  1 +
 2 files changed, 28 insertions(+)

diff --git a/src/clientgui.cpp b/src/clientgui.cpp
index 63218e0..0c7b4d2 100644
--- a/src/clientgui.cpp
+++ b/src/clientgui.cpp
@@ -66,6 +66,7 @@ ClientGui::ClientGui(const QString& name, const QString& token, std::unique_ptr<
     connect(_ui.cmdStopAdc, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::stopAdc));
     connect(_ui.cmdSaveData, &QPushButton::clicked, this, &ClientGui::saveRootFile);
     connect(_ui.cmdClearHisto, &QPushButton::clicked, this, &ClientGui::clearHistogram);
+    connect(_ui.cmdUpdateHisto, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::sendHistogramUpdate));
     connect(_ui.cmdAdcThresholdUp, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::adcThresholdUp));
     connect(_ui.cmdAdcThresholdDown, &QPushButton::clicked, std::bind(&ClientGui::sendSimpleActiontoServer, this, Protocol::Action::adcThresholdDown));
 
@@ -119,6 +120,7 @@ void ClientGui::handleMessageFromServer(const QString& message)
             displayExperimentState();
             break;
         case Protocol::Action::updateHistogram:
+            updateHistogram(command.arguments[QStringLiteral("bincontents")]);
             break;
         case Protocol::Action::fillHistogram:
             fillEventsIntoHistogram(command.arguments[QStringLiteral("adcchannels")]);
@@ -259,6 +261,31 @@ void ClientGui::fillEventsIntoHistogram(const QString& adcchannels)
     _rootcanvas.getCanvas()->Update();
 }
 
+void Fp311Online::ClientGui::updateHistogram(const QString& bincontents)
+{
+    if (_state.adcState == ExperimentState::AdcState::Running)
+        return;
+
+    const QStringList binlist = bincontents.split(QChar(';'), QString::SkipEmptyParts, Qt::CaseInsensitive);
+    if (binlist.size() != ExperimentState::NumberOfAdcChannels)
+    {
+        logError("The number of bins in the “bincontents” argument of the ”updateHistogram” command does not match ExperimentState::NumberOfAdcChannels.");
+        return;
+    }
+    for(int i = 1; i <= ExperimentState::NumberOfAdcChannels; i++)
+    {
+        bool ok = false;
+        double content = binlist[i-1].toDouble(&ok);
+        if (ok)
+            _histo->SetBinContent(i, content);
+        else
+            logWarning(QStringLiteral("Cannot convert bin content string ") + binlist[i] + QStringLiteral(" to double."));
+    }
+    _histo->ResetStats();
+    _rootcanvas.getCanvas()->Modified();
+    _rootcanvas.getCanvas()->Update();
+}
+
 void ClientGui::clearHistogram(const bool fromServer)
 {
     for (int i = 0; i < ExperimentState::NumberOfAdcChannels - 1; i++)
diff --git a/src/clientgui.h b/src/clientgui.h
index 334a3f7..77ad320 100644
--- a/src/clientgui.h
+++ b/src/clientgui.h
@@ -88,6 +88,7 @@ private:
 
     void displayExperimentState();
     void fillEventsIntoHistogram(const QString& adcchannels);
+    void updateHistogram(const QString& bincontents);
     void clearHistogram(const bool fromServer = false);
 
     void saveRootFile();
-- 
GitLab