Writer.cpp
Go to the documentation of this file.
1 #include "Writer.h"
2 
3 #include <memory>
4 #include <mutex>
5 #include <string>
6 #include <type_traits>
7 
9 
14 
15 #include <armarx/navigation/core/aron/Events.aron.generated.h>
19 
21 {
22 
23  std::string
25  {
26  return "mem.nav.events.";
27  }
28 
31  {
32  return Properties{
35  .providerName = "" // clientId
36  };
37  }
38 
39  bool
40  Writer::store(const armem::Commit& commit)
41  {
42  std::lock_guard g{writeMutex};
43  armem::CommitResult updateResult = memoryWriter().commit(commit);
44 
45  ARMARX_DEBUG << updateResult;
46 
47  if (not updateResult.allSuccess())
48  {
49  ARMARX_WARNING << "Failed to commit to memory: " << updateResult.allErrorMessages();
50  }
51  return updateResult.allSuccess();
52  }
53 
54  bool
55  Writer::store(const armem::EntityUpdate& commit)
56  {
57  std::lock_guard g{writeMutex};
58  const armem::EntityUpdateResult updateResult = memoryWriter().commit(commit);
59 
60  ARMARX_DEBUG << updateResult;
61 
62  if (not updateResult.success)
63  {
64  ARMARX_WARNING << "Failed to commit to memory: " << updateResult.errorMessage;
65  }
66  return updateResult.success;
67  }
68 
69  bool
70  Writer::store(const core::GlobalTrajectoryUpdatedEvent& event, const std::string& clientID)
71  {
72  return storeImpl<core::arondto::GlobalTrajectoryUpdatedEvent>(
74  }
75 
76  bool
77  Writer::store(const core::LocalTrajectoryUpdatedEvent& event, const std::string& clientID)
78  {
79  return storeImpl<core::arondto::LocalTrajectoryUpdatedEvent>(
81  }
82 
83  bool
84  Writer::store(const core::GoalReachedEvent& event, const std::string& clientID)
85  {
86  return storeImpl<core::arondto::GoalReachedEvent>(
87  event, core::event_names::GoalReached, clientID);
88  }
89 
90  bool
91  Writer::store(const core::WaypointReachedEvent& event, const std::string& clientID)
92  {
93  return storeImpl<core::arondto::WaypointReachedEvent>(
94  event, core::event_names::WaypointReached, clientID);
95  }
96 
97  bool
98  Writer::store(const core::InternalErrorEvent& event, const std::string& clientID)
99  {
100  return storeImpl<core::arondto::InternalErrorEvent>(
101  event, core::event_names::InternalError, clientID);
102  }
103 
104  bool
105  Writer::store(const core::GlobalPlanningFailedEvent& event, const std::string& clientID)
106  {
107  return storeImpl<core::arondto::GlobalPlanningFailedEvent>(
108  event, core::event_names::GlobalPlanningFailed, clientID);
109  }
110 
111  bool
112  Writer::store(const core::LocalPlanningFailedEvent& event, const std::string& clientID)
113  {
114  return storeImpl<core::arondto::LocalPlanningFailedEvent>(
115  event, core::event_names::LocalPlanningFailed, clientID);
116  }
117 
118  bool
119  Writer::store(const core::MovementStartedEvent& event, const std::string& clientID)
120  {
121  return storeImpl<core::arondto::MovementStartedEvent>(
122  event, core::event_names::MovementStarted, clientID);
123  }
124 
125  bool
126  Writer::store(const core::UserAbortTriggeredEvent& event, const std::string& clientID)
127  {
128  return storeImpl<core::arondto::UserAbortTriggeredEvent>(
129  event, core::event_names::UserAbortTriggered, clientID);
130  }
131 
132  bool
133  Writer::store(const core::SafetyThrottlingTriggeredEvent& event, const std::string& clientID)
134  {
135  return storeImpl<core::arondto::SafetyThrottlingTriggeredEvent>(
137  }
138 
139  bool
140  Writer::store(const core::SafetyStopTriggeredEvent& event, const std::string& clientID)
141  {
142  return storeImpl<core::arondto::SafetyStopTriggeredEvent>(
143  event, core::event_names::SafetyStopTriggered, clientID);
144  }
145 
146  template <typename AronEventT, typename EventT>
147  bool
148  Writer::storeImpl(const EventT& event,
149  const std::string& eventName,
150  const std::string& clientID)
151  {
152  static_assert(std::is_base_of<core::Event, EventT>::value, "Only events supported.");
153 
154  ARMARX_VERBOSE << "Storing event `" << eventName << "` in memory.";
155 
156  const auto& timestamp = event.timestamp;
157 
159  update.entityID = armem::MemoryID()
161  .withCoreSegmentName(properties().coreSegmentName)
162  .withProviderSegmentName(clientID)
163  .withEntityName(eventName)
165  update.referencedTime = timestamp;
166 
167  const auto dto = core::toAron<AronEventT>(event);
168 
170  element->setElementCopy("event", std::make_shared<aron::data::String>(eventName));
171  element->setElementCopy("data", dto.toAron());
172 
173  update.instancesData = {element};
174 
175  return store(update);
176  }
177 } // namespace armarx::navigation::memory::client::events
ARMARX_VERBOSE
#define ARMARX_VERBOSE
Definition: Logging.h:187
armarx::navigation::core::event_names::MovementStarted
const std::string MovementStarted
Definition: events.h:20
Writer.h
armarx::armem::MemoryID::withMemoryName
MemoryID withMemoryName(const std::string &name) const
Definition: MemoryID.cpp:401
armarx::armem::Commit
A bundle of updates to be sent to the memory.
Definition: Commit.h:89
armarx::armem::EntityUpdateResult
Result of an EntityUpdate.
Definition: Commit.h:74
MemoryID.h
armarx::navigation::core::InternalErrorEvent
Event describing the occurance of an internal unhandled error.
Definition: events.h:104
armarx::navigation::core::SafetyThrottlingTriggeredEvent
Event desciribing that a significant safety throttling factor was reached.
Definition: events.h:71
armarx::armem::client::util::SimpleWriterBase::Properties
Definition: SimpleWriterBase.h:47
armarx::navigation::core::event_names::UserAbortTriggered
const std::string UserAbortTriggered
Definition: events.h:25
armarx::navigation::memory::constants::NavigationMemoryName
const std::string NavigationMemoryName
Definition: constants.h:29
armarx::navigation::core::event_names::WaypointReached
const std::string WaypointReached
Definition: events.h:22
armarx::navigation::core::event_names::GlobalTrajectoryUpdated
const std::string GlobalTrajectoryUpdated
Definition: events.h:16
armarx::navigation::memory::client::events::Writer::store
bool store(const core::GoalReachedEvent &event, const std::string &clientID)
Definition: Writer.cpp:84
Dict.h
events.h
armarx::navigation::core::event_names::SafetyStopTriggered
const std::string SafetyStopTriggered
Definition: events.h:24
armarx::navigation::core::event_names::SafetyThrottlingTriggered
const std::string SafetyThrottlingTriggered
Definition: events.h:23
armarx::armem::MemoryID::withProviderSegmentName
MemoryID withProviderSegmentName(const std::string &name) const
Definition: MemoryID.cpp:417
cxxopts::value
std::shared_ptr< Value > value()
Definition: cxxopts.hpp:855
armarx::navigation::core::event_names::GlobalPlanningFailed
const std::string GlobalPlanningFailed
Definition: events.h:18
armarx::armem::client::util::SimpleWriterBase::memoryWriter
armem::client::Writer & memoryWriter()
Definition: SimpleWriterBase.cpp:52
armarx::armem::MemoryID::withCoreSegmentName
MemoryID withCoreSegmentName(const std::string &name) const
Definition: MemoryID.cpp:409
armarx::navigation::core::SafetyStopTriggeredEvent
Event describing that for security reasons, the robot was stopped completely.
Definition: events.h:87
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:184
armarx::armem::client::util::SimpleWriterBase::Properties::memoryName
std::string memoryName
Definition: SimpleWriterBase.h:49
armarx::armem::EntityUpdate
An update of an entity for a specific point in time.
Definition: Commit.h:25
armarx::armem::CommitResult
Result of a Commit.
Definition: Commit.h:110
armarx::navigation::core::UserAbortTriggeredEvent
Event describing that the user aborted the current execution.
Definition: events.h:96
armarx::armem::client::Writer::commit
CommitResult commit(const Commit &commit) const
Writes a Commit to the memory.
Definition: Writer.cpp:59
timestamp
std::string timestamp()
Definition: CartographerAdapter.cpp:85
armarx::navigation::core::LocalTrajectoryUpdatedEvent
Event describing that the local trajectory was updated.
Definition: events.h:121
armarx::navigation::core::GlobalPlanningFailedEvent
Definition: events.h:34
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
armarx::navigation::core::GoalReachedEvent
Event describing that the targeted goal was successfully reached.
Definition: events.h:52
armarx::navigation::memory::constants::EventsCoreSegmentName
const std::string EventsCoreSegmentName
Definition: constants.h:33
armarx::navigation::core::event_names::GoalReached
const std::string GoalReached
Definition: events.h:21
armarx::navigation::core::event_names::InternalError
const std::string InternalError
Definition: events.h:26
armarx::navigation::memory::client::events::Writer::defaultProperties
Properties defaultProperties() const override
Definition: Writer.cpp:30
Commit.h
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::navigation::memory::client::events::Writer::propertyPrefix
std::string propertyPrefix() const override
Definition: Writer.cpp:24
armarx::armem::MemoryID::withEntityName
MemoryID withEntityName(const std::string &name) const
Definition: MemoryID.cpp:425
aron_conversions.h
armarx::armem::laser_scans::constants::memoryName
const std::string memoryName
Definition: constants.h:28
armarx::navigation::core::GlobalTrajectoryUpdatedEvent
Event describing that the global trajectory was updated.
Definition: events.h:113
armarx::armem::client::util::SimpleWriterBase::properties
const Properties & properties() const
Definition: SimpleWriterBase.cpp:58
armarx::armem::MemoryID::withTimestamp
MemoryID withTimestamp(Time time) const
Definition: MemoryID.cpp:433
constants.h
armarx::navigation::core::WaypointReachedEvent
Event describing that a user-defined waypoint was successfully reached.
Definition: events.h:60
armarx::navigation::memory::client::events
This file is part of ArmarX.
Definition: Writer.cpp:20
Logging.h
ARMARX_WARNING
#define ARMARX_WARNING
Definition: Logging.h:193
String.h
armarx::navigation::core::LocalPlanningFailedEvent
Definition: events.h:39
armarx::navigation::core::event_names::LocalPlanningFailed
const std::string LocalPlanningFailed
Definition: events.h:19
armarx::navigation::core::MovementStartedEvent
Definition: events.h:44
armarx::aron::data::Dict
Definition: Dict.h:44
armarx::navigation::core::event_names::LocalTrajectoryUpdated
const std::string LocalTrajectoryUpdated
Definition: events.h:17
armarx::human::MemoryID
const armem::MemoryID MemoryID
Definition: memory_ids.cpp:28