From 8896b92f8288908b6f7ae41bb3edda14733be3b8 Mon Sep 17 00:00:00 2001 From: Emma Broman Date: Thu, 28 Apr 2022 12:40:18 +0200 Subject: [PATCH] Only send skybrowser topic message if data changed Maybe a little ugly to check the jsonString, but it does the job for now. Later we should probably make a nicer implementation --- .../server/include/topics/skybrowsertopic.h | 2 + modules/server/src/topics/skybrowsertopic.cpp | 52 +++++-------------- .../skybrowser/include/targetbrowserpair.h | 6 ++- modules/skybrowser/src/targetbrowserpair.cpp | 38 +++++++++++++- 4 files changed, 58 insertions(+), 40 deletions(-) diff --git a/modules/server/include/topics/skybrowsertopic.h b/modules/server/include/topics/skybrowsertopic.h index 7aafc03b33..d2522b3618 100644 --- a/modules/server/include/topics/skybrowsertopic.h +++ b/modules/server/include/topics/skybrowsertopic.h @@ -46,6 +46,8 @@ private: int _targetDataCallbackHandle = UnsetOnChangeHandle; bool _isDone = false; std::chrono::system_clock::time_point _lastUpdateTime; + std::string _lastUpdateJsonString; + std::chrono::milliseconds _skyBrowserUpdateTime = std::chrono::milliseconds(100); }; diff --git a/modules/server/src/topics/skybrowsertopic.cpp b/modules/server/src/topics/skybrowsertopic.cpp index 9229ce9b41..09ecbf11d0 100644 --- a/modules/server/src/topics/skybrowsertopic.cpp +++ b/modules/server/src/topics/skybrowsertopic.cpp @@ -107,49 +107,25 @@ void SkyBrowserTopic::sendBrowserData() { ghoul::Dictionary targets; for (const std::unique_ptr& pair : pairs) { std::string id = pair->browserId(); - // Convert deque to vector so ghoul can read it - std::vector selectedImagesVector; - const std::deque selectedImages = pair->selectedImages(); - std::for_each( - selectedImages.begin(), - selectedImages.end(), - [&](int i) { - selectedImagesVector.push_back(i); - } - ); - - glm::dvec2 spherical = pair->targetDirectionEquatorial(); - glm::dvec3 cartesian = skybrowser::sphericalToCartesian(spherical); - - ghoul::Dictionary target; - // Set ("Key", value) - target.setValue("id", id); - target.setValue("name", pair->browserGuiName()); - target.setValue("fov", static_cast(pair->verticalFov())); - target.setValue("ra", spherical.x); - target.setValue("dec", spherical.y); - target.setValue("roll", pair->targetRoll()); - target.setValue("color", pair->borderColor()); - target.setValue("cartesianDirection", cartesian); - target.setValue("ratio", static_cast(pair->browserRatio())); - target.setValue("isFacingCamera", pair->isFacingCamera()); - target.setValue("isUsingRae", pair->isUsingRadiusAzimuthElevation()); - target.setValue("selectedImages", selectedImagesVector); - - std::vector> copies = pair->renderCopies(); - ghoul::Dictionary copiesData; - for (size_t i = 0; i < copies.size(); i++) { - copiesData.setValue(copies[i].first, copies[i].second); - } - // Set table for the current target - target.setValue("renderCopies", copiesData); + ghoul::Dictionary target = pair->dataAsDictionary(); targets.setValue(id, target); } data.setValue("browsers", targets); } + std::string jsonString = ghoul::formatJson(data); - json jsonData = json::parse(jsonString.begin(), jsonString.end()); - _connection->sendJson(wrappedPayload(jsonData)); + + // Only send message if data actually changed + if (jsonString != _lastUpdateJsonString) { + json jsonData = json::parse(jsonString.begin(), jsonString.end()); + _connection->sendJson(wrappedPayload(jsonData)); + } + + // @TODO (2022-04-28, emmbr) The message is stills ent every time the camera moves, + // because this changes the "roll" parameter of the browser. This is the update that + // occurs most often. Maybe it could be separated into it's own topic? + + _lastUpdateJsonString = jsonString; } } // namespace openspace diff --git a/modules/skybrowser/include/targetbrowserpair.h b/modules/skybrowser/include/targetbrowserpair.h index 93a79fd52b..e43b7ae2dd 100644 --- a/modules/skybrowser/include/targetbrowserpair.h +++ b/modules/skybrowser/include/targetbrowserpair.h @@ -29,6 +29,8 @@ #include #include +namespace ghoul { class Dictionary; } + namespace openspace { struct ImageData; @@ -64,7 +66,7 @@ public: // Target void centerTargetOnScreen(); - double targetRoll(); + double targetRoll() const; bool isFacingCamera() const; bool isUsingRadiusAzimuthElevation() const; @@ -94,6 +96,8 @@ public: ScreenSpaceSkyBrowser* browser() const; const std::deque& selectedImages() const; + ghoul::Dictionary dataAsDictionary() const; + // WorldWide Telescope image handling void setImageOrder(int i, int order); void selectImage(const ImageData& image, int i); diff --git a/modules/skybrowser/src/targetbrowserpair.cpp b/modules/skybrowser/src/targetbrowserpair.cpp index b16e27ba07..96fcd58435 100644 --- a/modules/skybrowser/src/targetbrowserpair.cpp +++ b/modules/skybrowser/src/targetbrowserpair.cpp @@ -191,6 +191,42 @@ const std::deque& TargetBrowserPair::selectedImages() const { return _browser->getSelectedImages(); } +ghoul::Dictionary TargetBrowserPair::dataAsDictionary() const { + // Convert deque to vector so ghoul can read it + std::vector selectedImagesVector; + const std::deque selectedImagesDeque = selectedImages(); + for (int i : selectedImagesDeque) { + selectedImagesVector.push_back(i); + } + + glm::dvec2 spherical = targetDirectionEquatorial(); + glm::dvec3 cartesian = skybrowser::sphericalToCartesian(spherical); + + ghoul::Dictionary res; + res.setValue("id", browserId()); + res.setValue("name", browserGuiName()); + res.setValue("fov", static_cast(verticalFov())); + res.setValue("ra", spherical.x); + res.setValue("dec", spherical.y); + res.setValue("roll", targetRoll()); + res.setValue("color", borderColor()); + res.setValue("cartesianDirection", cartesian); + res.setValue("ratio", static_cast(browserRatio())); + res.setValue("isFacingCamera", isFacingCamera()); + res.setValue("isUsingRae", isUsingRadiusAzimuthElevation()); + res.setValue("selectedImages", selectedImagesVector); + + std::vector> copies = renderCopies(); + ghoul::Dictionary copiesData; + for (size_t i = 0; i < copies.size(); i++) { + copiesData.setValue(copies[i].first, copies[i].second); + } + // Set table for the current target + res.setValue("renderCopies", copiesData); + + return res; +} + void TargetBrowserPair::selectImage(const ImageData& image, int i) { // Load image into browser _browser->selectImage(image.imageUrl, i); @@ -327,7 +363,7 @@ void TargetBrowserPair::centerTargetOnScreen() { startAnimation(viewDirection, currentFov); } -double TargetBrowserPair::targetRoll() { +double TargetBrowserPair::targetRoll() const { // To remove the lag effect when moving the camera while having a locked // target, send the locked coordinates to wwt glm::dvec3 normal = glm::normalize(