Writer.cpp
Go to the documentation of this file.
1#include "Writer.h"
2
3#include <mutex>
4#include <optional>
5
6#include <IceUtil/Time.h>
7
8#include <SimoxUtility/algorithm/get_map_keys_values.h>
9
12
17#include <RobotAPI/libraries/armem_robot_state/aron/Robot.aron.generated.h>
18#include <RobotAPI/libraries/armem_robot_state/aron/RobotDescription.aron.generated.h>
21
23{
25 memoryNameSystem(memoryNameSystem)
26 {
27 }
28
29 void
31 {
32 ARMARX_DEBUG << "Writer: registerPropertyDefinitions";
33
34 const std::string prefix = propertyPrefix;
35
36 def->optional(properties.memoryName, prefix + "MemoryName");
37
38 def->optional(properties.coreAttachmentsSegmentName,
39 prefix + "CoreSegment",
40 "Name of the memory core segment to use for object attachments.");
41 def->optional(properties.providerName, prefix + "ProviderName");
42 }
43
44 void
46 {
47 // Wait for the memory to become available and add it as dependency.
48 ARMARX_IMPORTANT << "Writer: Waiting for memory '" << properties.memoryName << "' ...";
49 try
50 {
51 memoryWriter = memoryNameSystem.useWriter(properties.memoryName);
52 memoryReader = memoryNameSystem.useReader(properties.memoryName);
53 ARMARX_IMPORTANT << "Writer: Connected to memory '" << properties.memoryName << "'";
54 }
56 {
57 ARMARX_ERROR << e.what();
58 return;
59 }
60 }
61
62 std::optional<armem::MemoryID>
64 {
65 std::lock_guard g{memoryWriterMutex};
66
67 const auto result =
68 memoryWriter.addSegment(properties.coreAttachmentsSegmentName, properties.providerName);
69
70 if (not result.success)
71 {
72 ARMARX_ERROR << "Creating core segment failed. Reason: " << result.errorMessage;
73 return std::nullopt;
74 }
75
76 const auto& timestamp = attachment.timestamp;
77
78 const auto providerId = armem::MemoryID(result.segmentID);
79 const auto entityID =
80 providerId
81 .withEntityName(attachment.object.entityName) // TODO check if meaningful
82 .withTimestamp(timestamp);
83
85 update.entityID = entityID;
86
87 arondto::attachment::ObjectAttachment aronAttachment;
88 toAron(aronAttachment, attachment);
89
90 update.instancesData = {aronAttachment.toAron()};
91 update.referencedTime = timestamp;
92
93 ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
94 armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
95
96 ARMARX_DEBUG << updateResult;
97
98 if (not updateResult.success)
99 {
100 ARMARX_ERROR << updateResult.errorMessage;
101 return std::nullopt;
102 }
103
104 return updateResult.snapshotID;
105 }
106
107 std::optional<armem::MemoryID>
109 {
110 std::lock_guard g{memoryWriterMutex};
111
112 const auto result =
113 memoryWriter.addSegment(properties.coreAttachmentsSegmentName, properties.providerName);
114
115 if (not result.success)
116 {
117 ARMARX_ERROR << "Creating core segment failed. Reason: " << result.errorMessage;
118 return std::nullopt;
119 }
120
121 const auto& timestamp = attachment.timestamp;
122
123 const auto providerId = armem::MemoryID(result.segmentID);
124 const auto entityID =
125 providerId
126 .withEntityName(attachment.object.id.entityName) // TODO check if meaningful
127 .withTimestamp(timestamp);
128
129 armem::EntityUpdate update;
130 update.entityID = entityID;
131
132 arondto::attachment::ArticulatedObjectAttachment aronAttachment;
133 toAron(aronAttachment, attachment);
134
135 update.instancesData = {aronAttachment.toAron()};
136 update.referencedTime = timestamp;
137
138 ARMARX_DEBUG << "Committing " << update << " at time " << timestamp;
139 armem::EntityUpdateResult updateResult = memoryWriter.commit(update);
140
141 ARMARX_DEBUG << updateResult;
142
143 if (not updateResult.success)
144 {
145 ARMARX_ERROR << updateResult.errorMessage;
146 return std::nullopt;
147 }
148
149 return updateResult.snapshotID;
150 }
151
152
153} // namespace armarx::armem::attachment
std::string timestamp()
std::optional< armem::MemoryID > commit(const ObjectAttachment &attachment)
Definition Writer.cpp:63
Writer(armem::client::MemoryNameSystem &memoryNameSystem)
Definition Writer.cpp:24
void registerPropertyDefinitions(armarx::PropertyDefinitionsPtr &def)
Definition Writer.cpp:30
The memory name system (MNS) client.
Indicates that a query to the Memory Name System failed.
Definition mns.h:25
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_DEBUG
The logging level for output that is only interesting while debugging.
Definition Logging.h:184
void toAron(arondto::MemoryID &dto, const MemoryID &bo)
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Result of an EntityUpdate.
Definition Commit.h:75
An update of an entity for a specific point in time.
Definition Commit.h:26
ArticulatedObjectAttachment describes a fixed transformation between an agent and an articulated obje...
Definition types.h:122
ObjectAttachment describes a fixed transformation between an agent and an object.
Definition types.h:101