From db75914520c541572e59f93137f2bb69c4fba5c4 Mon Sep 17 00:00:00 2001
From: Fabian Paus <fabian.paus@kit.edu>
Date: Thu, 30 Dec 2021 16:52:21 +0100
Subject: [PATCH] ArViz: Storage saves interactions in internal buffer

---
 .../components/ArViz/ArVizStorage.cpp         | 63 +++++++++++--------
 .../RobotAPI/components/ArViz/ArVizStorage.h  |  8 ++-
 .../RobotAPI/components/ArViz/CMakeLists.txt  |  1 -
 3 files changed, 42 insertions(+), 30 deletions(-)

diff --git a/source/RobotAPI/components/ArViz/ArVizStorage.cpp b/source/RobotAPI/components/ArViz/ArVizStorage.cpp
index 3891d216e..629966489 100644
--- a/source/RobotAPI/components/ArViz/ArVizStorage.cpp
+++ b/source/RobotAPI/components/ArViz/ArVizStorage.cpp
@@ -22,18 +22,12 @@
 
 #include "ArVizStorage.h"
 
-#include <ArmarXCore/core/exceptions/local/ExpressionException.h>
-
-#include <ArmarXCore/core/util/OnScopeExit.h>
-#include <ArmarXCore/core/time/TimeUtil.h>
 #include <ArmarXCore/core/util/IceBlobToObject.h>
 #include <ArmarXCore/core/util/ObjectToIceBlob.h>
 #include <ArmarXCore/core/system/ArmarXDataPath.h>
 
 #include <SimoxUtility/json/json.hpp>
 
-#include <iomanip>
-
 
 namespace armarx
 {
@@ -137,7 +131,7 @@ namespace armarx
         std::unique_lock<std::mutex> lock(historyMutex);
 
         revision += 1;
-        IceUtil::Time now = TimeUtil::GetTime();
+        IceUtil::Time now = IceUtil::Time::now();
         long nowInMicroSeconds = now.toMicroSeconds();
 
         for (auto& update : updates)
@@ -208,9 +202,41 @@ namespace armarx
     viz::data::LayerUpdates ArVizStorage::pullUpdatesSinceAndSendInteractions(
             Ice::Long revision, viz::data::InteractionFeedbackSeq const& interactions, const Ice::Current& c)
     {
-        // TODO: Implement storing the interactions somewhere
+        viz::data::LayerUpdates result;
+
+        std::unique_lock<std::mutex> lock(historyMutex);
 
-        return pullUpdatesSince(revision, c);
+        for (auto& interaction : interactions)
+        {
+            for (auto& entry : interactionBuffer)
+            {
+                if (entry.component == interaction.component
+                        && entry.layer == interaction.layer
+                        && entry.element == interaction.element)
+                {
+                    // Should we check whether this interaction is compatible / should be overwritten?
+                    entry = interaction;
+                    goto for_end;
+                }
+            }
+
+            // Interaction did not exist, add it to the buffer
+            interactionBuffer.push_back(interaction);
+
+            for_end: ;
+        }
+
+        result.updates.reserve(currentState.size());
+        for (auto& layer : currentState)
+        {
+            if (layer.revision > revision)
+            {
+                result.updates.push_back(layer.update);
+            }
+        }
+        result.revision = this->revision;
+
+        return result;
     }
 
     void ArVizStorage::record()
@@ -348,17 +374,9 @@ void armarx::ArVizStorage::recordBatch(armarx::viz::data::RecordingBatch& batch)
         return;
     }
 
-    const auto startT = TimeUtil::GetTime();
-
     auto& first = batch.updates.front();
     auto& last = batch.updates.back();
 
-    ARMARX_ON_SCOPE_EXIT
-    {
-        ARMARX_INFO << "Storing batch " << first.revision << " - " << last.revision << " took "
-                    << (TimeUtil::GetTime() - startT).toSecondsDouble() << "s";
-    };
-
     batch.header.index = -1; // TODO: Should we save the index?
     batch.header.firstRevision = first.revision;
     batch.header.lastRevision = last.revision;
@@ -421,12 +439,11 @@ std::string armarx::ArVizStorage::startRecording(std::string const& newRecording
             return recordingMetaData.id;
         }
 
-        auto t = std::time(nullptr);
-        auto tm = *std::localtime(&t);
+        IceUtil::Time now = IceUtil::Time::now();
         std::ostringstream id;
         id << newRecordingPrefix
            << '_'
-           << std::put_time(&tm, "%Y-%m-%d_%H-%M-%S");
+           << now.toString("%Y-%m-%d_%H-%M-%S");
         std::string newRecordingID = id.str();
 
         recordingPath = historyPath / newRecordingID;
@@ -508,12 +525,6 @@ armarx::viz::data::RecordingSeq armarx::ArVizStorage::getAllRecordings(const Ice
 
 armarx::viz::data::RecordingBatch armarx::ArVizStorage::getRecordingBatch(std::string const& recordingID, Ice::Long batchIndex, const Ice::Current&)
 {
-    const auto startT = TimeUtil::GetTime();
-    ARMARX_ON_SCOPE_EXIT
-    {
-        ARMARX_INFO << "Loading " << recordingID << " " << batchIndex << " took "
-                    << (TimeUtil::GetTime() - startT).toSecondsDouble() << "s";
-    };
     viz::data::RecordingBatch result;
     result.header.index = -1;
 
diff --git a/source/RobotAPI/components/ArViz/ArVizStorage.h b/source/RobotAPI/components/ArViz/ArVizStorage.h
index 69299fb6e..a7ffb8709 100644
--- a/source/RobotAPI/components/ArViz/ArVizStorage.h
+++ b/source/RobotAPI/components/ArViz/ArVizStorage.h
@@ -22,8 +22,6 @@
 
 #pragma once
 
-#include "Coin/Visualizer.h"
-
 #include <RobotAPI/interface/ArViz.h>
 #include <ArmarXCore/core/Component.h>
 #include <ArmarXCore/core/services/tasks/RunningTask.h>
@@ -108,10 +106,14 @@ namespace armarx
         std::mutex historyMutex;
 
         std::vector<viz::data::TimestampedLayerUpdate> currentState;
-        // We ignore the history for now
         std::vector<viz::data::TimestampedLayerUpdate> history;
         long revision = 0;
 
+        // We store all interactions in here
+        // But we curate them, so that only the last interaction with an element is reported
+        // in case the component never retrieves the interactions
+        std::vector<viz::data::InteractionFeedback> interactionBuffer;
+
         std::mutex recordingMutex;
         std::filesystem::path recordingPath;
         std::vector<viz::data::TimestampedLayerUpdate> recordingInitialState;
diff --git a/source/RobotAPI/components/ArViz/CMakeLists.txt b/source/RobotAPI/components/ArViz/CMakeLists.txt
index 9a5a3adcb..e4022f09a 100644
--- a/source/RobotAPI/components/ArViz/CMakeLists.txt
+++ b/source/RobotAPI/components/ArViz/CMakeLists.txt
@@ -6,7 +6,6 @@ set(COMPONENT_LIBS
     RobotAPIInterfaces
     RobotAPIArmarXObjects
     RobotStatechartHelpers  # For RobotNameHelper, used by RobotHand
-    boost_iostreams #compression
 )
 
 set(SOURCES
-- 
GitLab