-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make elasticlib C++ and Python libraries more idiomatic (#140)
All: * Rename the Notification class to Alert to match the user-facing function * Remove the Elastic prefix from class names since the namespace (C++), outer class (Java), or package name (Python) already provides that namespacing C++: * Flatten the type structure * Put all classes and functions in an elastic namespace * Since the Alert class provides setters and getters, just make the members public to allow [aggregate initialization](https://en.cppreference.com/w/cpp/language/aggregate_initialization) * Remove redundant Javadoc tags from comments * Rewrite the Javadoc-like comments to follow Doxygen conventions * Make pixel counts use ints, since them being fractional makes no sense * Move the JSON serialization and NT publishing to a .cpp file * Use WPILib's bundled fmtlib instead of iostream for performance Python: * Flatten type structure * Remove decorator functions since optional keyword arguments provides the same functionality with the same reduction in verbosity as C++ * Rename the Notification class to Alert to match the user-facing function * Move JSON serialization into send_alert() I confirmed the C++ version built within https://github.com/wpilibsuite/allwpilib/tree/main/developerRobot. Co-authored-by: Gold87 <[email protected]>
- Loading branch information
Showing
4 changed files
with
159 additions
and
662 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// Copyright (c) 2023-2024 Gold87 and other Elastic contributors | ||
// This software can be modified and/or shared under the terms | ||
// defined by the Elastic license: | ||
// https://github.com/Gold872/elastic-dashboard/blob/main/LICENSE | ||
|
||
#include "elasticlib.h" | ||
|
||
#include <exception> | ||
|
||
#include <fmt/core.h> | ||
#include <networktables/NetworkTableInstance.h> | ||
#include <networktables/StringTopic.h> | ||
#include <wpi/json.h> | ||
|
||
namespace elastic { | ||
|
||
void SendNotification(const Notification& notification) { | ||
static nt::StringTopic topic = | ||
nt::NetworkTableInstance::GetDefault().GetStringTopic( | ||
"/Elastic/RobotNotifications"); | ||
static nt::StringPublisher publisher = | ||
topic.Publish({.sendAll = true, .keepDuplicates = true}); | ||
|
||
try { | ||
// Convert Notification to JSON string | ||
wpi::json jsonData; | ||
|
||
if (notification.level == NotificationLevel::INFO) { | ||
jsonData["level"] = "INFO"; | ||
} else if (notification.level == NotificationLevel::WARNING) { | ||
jsonData["level"] = "WARNING"; | ||
} else if (notification.level == NotificationLevel::ERROR) { | ||
jsonData["level"] = "ERROR"; | ||
} else { | ||
jsonData["level"] = "UNKNOWN"; | ||
} | ||
|
||
jsonData["title"] = notification.title; | ||
jsonData["description"] = notification.description; | ||
jsonData["displayTime"] = notification.displayTime.value(); | ||
jsonData["width"] = notification.width; | ||
jsonData["height"] = notification.height; | ||
|
||
// Publish the JSON string | ||
publisher.Set(jsonData.dump()); | ||
} catch (const std::exception& e) { | ||
fmt::println(stderr, "Error processing JSON: {}", e.what()); | ||
} catch (...) { | ||
fmt::println(stderr, "Unknown error occurred while processing JSON."); | ||
} | ||
} | ||
|
||
} // namespace elastic |
Oops, something went wrong.