ImageRecordingManager.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package VisionX::ArmarXObjects::RecordingManager
17  * @author Christian R. G. Dreher <c.dreher@kit.edu>
18  * @date 2020
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
25 
26 
27 // Simox
28 #include <SimoxUtility/json.h>
29 
30 // ArmarX
32 
33 
35 {
36 
37 }
38 
39 
40 void
42 {
43  if (not m_configs_json.empty())
44  {
45  std::map<std::string, imrec::Config> configs = nlohmann::json::parse(m_configs_json);
46  m_configs = configs;
47  }
48 }
49 
50 
51 void
53 {
54 
55 }
56 
57 
58 void
60 {
61 
62 }
63 
64 
65 void
67 {
68 
69 }
70 
71 
72 void
74  const std::map<std::string, imrec::Config>& configs,
75  const Ice::Current&)
76 {
77  std::unique_lock l{m_configs_mx};
78 
79  if (not configs.empty())
80  {
81  m_configs = configs;
82  }
83  else
84  {
85  m_configs.reset();
86  }
87 }
88 
89 
90 void
92  const std::string& configs_json,
93  const Ice::Current& c)
94 {
95  const std::map<std::string, imrec::Config>& configs = nlohmann::json::parse(configs_json);
96  configureRecordings(configs, c);
97 }
98 
99 
100 std::map<std::string, visionx::imrec::Config>
102 {
103  std::shared_lock l{m_configs_mx};
104 
105  if (not m_configs.has_value())
106  {
108  }
109 
110  return m_configs.value();
111 }
112 
113 
114 bool
116 {
117  std::shared_lock l{m_configs_mx};
118  return m_configs.has_value();
119 }
120 
121 
122 std::map<std::string, visionx::imrecman::StartStopStatus>
124 {
125  ARMARX_TRACE;
126  ARMARX_DEBUG << "Starting recordings...";
127 
128  std::shared_lock l{m_configs_mx};
129 
130  std::map<std::string, visionx::imrecman::StartStopStatus> statuses;
131 
132  if (not m_configs.has_value())
133  {
135  }
136 
137  for (auto [proxy_name, config] : m_configs.value())
138  {
140  try
141  {
142  ARMARX_DEBUG << "Calling startRecording on image provider '" << proxy_name << "'.";
143  const bool call_status =
144  getProxy<RecordingImageProviderInterfacePrx>(proxy_name)->startImageRecording(config);
145  ARMARX_DEBUG << "Got response from image provider.";
147  }
148  catch (...)
149  {
150  ARMARX_DEBUG << "Image provider '" << proxy_name << "' offline.";
152  }
153  statuses[proxy_name] = status;
154  }
155 
156  ARMARX_DEBUG << "Done delegating call.";
157 
158  return statuses;
159 }
160 
161 
162 std::map<std::string, visionx::imrec::Status>
164 {
165  ARMARX_TRACE;
166  ARMARX_DEBUG << "Fetching statuses...";
167 
168  std::shared_lock l{m_configs_mx};
169 
170  std::map<std::string, visionx::imrec::Status> statuses;
171 
172  if (not m_configs.has_value())
173  {
175  }
176 
177  for (auto& [proxy_name, config] : m_configs.value())
178  {
179  imrec::Status status;
180  try
181  {
182  ARMARX_DEBUG << "Calling getRecordingStatus on image provider '" << proxy_name << "'.";
183  status = getProxy<RecordingImageProviderInterfacePrx>(proxy_name)->getImageRecordingStatus();
184  ARMARX_DEBUG << "Got response from image provider.";
185  }
186  catch (...)
187  {
188  ARMARX_DEBUG << "Image provider '" << proxy_name << "' offline.";
190  status.framesBuffered = -1;
191  status.framesWritten = -1;
192  }
193  statuses[proxy_name] = status;
194  }
195 
196  ARMARX_DEBUG << "Done delegating call.";
197 
198  return statuses;
199 }
200 
201 
202 std::map<std::string, visionx::imrecman::StartStopStatus>
204 {
205  ARMARX_TRACE;
206  ARMARX_DEBUG << "Stopping recordings...";
207 
208  std::shared_lock l{m_configs_mx};
209 
210  std::map<std::string, visionx::imrecman::StartStopStatus> statuses;
211 
212  if (not m_configs.has_value())
213  {
215  }
216 
217  std::map<std::string, Ice::AsyncResultPtr> promises;
218 
219  for (auto& [proxy_name, config] : m_configs.value())
220  {
221  try
222  {
223  ARMARX_DEBUG << "Calling stopRecording on image provider '" << proxy_name << "'.";
224  promises[proxy_name] = getProxy<RecordingImageProviderInterfacePrx>(proxy_name)->begin_stopImageRecording();
225  }
226  catch (...)
227  {
228  promises[proxy_name] = nullptr;
229  }
230  }
231 
232  for (auto& [proxy_name, promise] : promises)
233  {
235  try
236  {
237  if (promise)
238  {
239  const bool call_status = getProxy<RecordingImageProviderInterfacePrx>(proxy_name)->end_stopImageRecording(promise);
240  ARMARX_DEBUG << "Got response from image provider.";
242  }
243  else
244  {
245  ARMARX_DEBUG << "Image provider '" << proxy_name << "' offline.";
247  }
248  }
249  catch (...)
250  {
251  ARMARX_DEBUG << "Image provider '" << proxy_name << "' offline.";
253  }
254  statuses[proxy_name] = status;
255  }
256 
257  ARMARX_DEBUG << "Done delegating stopRecording call.";
258 
259  return statuses;
260 }
261 
262 
263 std::string
265 {
266  return "ImageRecordingManager";
267 }
268 
269 
270 std::string
272 {
273  return GetDefaultName();
274 }
275 
276 
279 {
281  new armarx::ComponentPropertyDefinitions{getConfigIdentifier()};
282 
283  defs->optional(m_configs_json, "config_json", "JSON to initialize default config from.");
284 
285  return defs;
286 }
visionx::ImageRecordingManagerInterface::isConfigured
bool isConfigured()
visionx::ImageRecordingManagerInterface::stopRecordings
imrecman::StartStopStatusesMap stopRecordings()
visionx::imrecman::Component::onExitComponent
void onExitComponent() override
Hook for subclass.
Definition: ImageRecordingManager.cpp:66
visionx::imrecman::Component::configureRecordingsFromJson
void configureRecordingsFromJson(const std::string &, const Ice::Current &) override
Definition: ImageRecordingManager.cpp:91
visionx::imrecman::Component::createPropertyDefinitions
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
Definition: ImageRecordingManager.cpp:278
visionx::imrecman::Component::onDisconnectComponent
void onDisconnectComponent() override
Hook for subclass.
Definition: ImageRecordingManager.cpp:59
c
constexpr T c
Definition: UnscentedKalmanFilterTest.cpp:43
visionx::imrecman::Component::onConnectComponent
void onConnectComponent() override
Pure virtual hook for the subclass.
Definition: ImageRecordingManager.cpp:52
visionx::imrecman::ok
@ ok
Definition: ImageRecordingManagerInterface.ice:46
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::status
status
Definition: FiniteStateMachine.h:259
visionx::imrecman::already_recording
@ already_recording
Definition: ImageRecordingManagerInterface.ice:48
visionx::imrecman::Component::~Component
~Component() override
Definition: ImageRecordingManager.cpp:34
ARMARX_DEBUG
#define ARMARX_DEBUG
Definition: Logging.h:177
visionx::ImageRecordingManagerInterface::startRecordings
imrecman::StartStopStatusesMap startRecordings()
ImageRecordingManager.h
visionx::imrecman::NotConfiguredException
Definition: ImageRecordingManagerInterface.ice:38
visionx::imrecman::Component::getDefaultName
std::string getDefaultName() const override
Retrieve default name of component.
Definition: ImageRecordingManager.cpp:271
visionx::ImageRecordingManagerInterface::getConfiguration
imrec::ConfigMap getConfiguration()
armarx::ComponentPropertyDefinitions
Default component property definition container.
Definition: Component.h:70
IceUtil::Handle< class PropertyDefinitionContainer >
visionx::imrecman::offline
@ offline
Definition: ImageRecordingManagerInterface.ice:49
json_conversions.h
visionx::imrecman::Component::configureRecordings
void configureRecordings(const std::map< std::string, imrec::Config > &, const Ice::Current &) override
Definition: ImageRecordingManager.cpp:73
visionx::imrecman::Component::GetDefaultName
static std::string GetDefaultName()
Definition: ImageRecordingManager.cpp:264
visionx::imrecman::StartStopStatus
StartStopStatus
Definition: ImageRecordingManagerInterface.ice:44
visionx::imrecman::Component::onInitComponent
void onInitComponent() override
Pure virtual hook for the subclass.
Definition: ImageRecordingManager.cpp:41
visionx::ImageRecordingManagerInterface::getRecordingStatuses
imrec::StatusesMap getRecordingStatuses()