31 #include <ArmarXCore/interface/observers/VariantBase.h>
33 #include <ArmarXCore/interface/observers/ObserverInterface.h>
36 #include <unordered_map>
57 defineOptionalProperty<int>(
"MaxHistorySize", 5000,
"Maximum number of entries in the Observer history");
58 defineOptionalProperty<float>(
"MaxHistoryRecordFrequency", 50.f,
"The Observer history is written with this maximum frequency. Everything faster is being skipped.");
59 defineOptionalProperty<bool>(
"CreateUpdateFrequenciesChannel",
false,
"If true, an additional channel is created that shows the update frequency of every other channel in that observer.");
81 virtual public ObserverInterface,
91 std::string getObserverName()
const;
92 void getObserverName_async(
93 const AMD_ObserverInterface_getObserverNamePtr& amd,
94 const Ice::Current&)
const override;
106 CheckIdentifier installCheck(
const CheckConfiguration& configuration);
107 void installCheck_async(
108 const AMD_ObserverInterface_installCheckPtr& amd,
109 const CheckConfiguration& configuration,
110 const Ice::Current&)
override;
119 void removeCheck(
const CheckIdentifier&
id);
120 void removeCheck_async(
121 const AMD_ObserverInterface_removeCheckPtr& amd,
122 const CheckIdentifier&
id,
123 const Ice::Current&)
override;
133 TimedVariantBasePtr getDataField(
135 const Ice::Current&
c = Ice::emptyCurrent)
const;
136 void getDataField_async(
137 const AMD_ObserverInterface_getDataFieldPtr& amd,
139 const Ice::Current&
c)
const override;
142 TimedVariantBasePtr getDatafieldByName(
143 const std::string& channelName,
144 const std::string& datafieldName)
const;
145 void getDatafieldByName_async(
146 const AMD_ObserverInterface_getDatafieldByNamePtr& amd,
147 const std::string& channelName,
148 const std::string& datafieldName,
149 const Ice::Current&)
const override;
152 DatafieldRefBasePtr getDataFieldRef(
154 void getDataFieldRef_async(
155 const AMD_ObserverInterface_getDataFieldRefPtr& amd,
157 const Ice::Current&)
const override;
160 DatafieldRefBasePtr getDatafieldRefByName(
161 const std::string& channelName,
162 const std::string& datafieldName)
const;
163 void getDatafieldRefByName_async(
164 const AMD_ObserverInterface_getDatafieldRefByNamePtr& amd,
165 const std::string& channelName,
166 const std::string& datafieldName,
167 const Ice::Current&)
const override;
177 TimedVariantBaseList getDataFields(
179 const Ice::Current&
c);
180 void getDataFields_async(
181 const AMD_ObserverInterface_getDataFieldsPtr& amd,
183 const Ice::Current&
c)
override;
186 StringTimedVariantBaseMap getDatafieldsOfChannel(
187 const std::string& channelName)
const;
188 void getDatafieldsOfChannel_async(
189 const AMD_ObserverInterface_getDatafieldsOfChannelPtr& amd,
190 const std::string& channelName,
191 const Ice::Current&)
const override;
202 ChannelRegistryEntry getChannel(
203 const std::string& channelName)
const;
204 void getChannel_async(
205 const AMD_ObserverInterface_getChannelPtr& amd,
206 const std::string& channelName,
207 const Ice::Current&)
const override;
217 ChannelRegistry getAvailableChannels(
218 bool includeMetaChannels);
219 void getAvailableChannels_async(
220 const AMD_ObserverInterface_getAvailableChannelsPtr& amd,
221 bool includeMetaChannels,
222 const Ice::Current&)
override;
232 StringConditionCheckMap getAvailableChecks();
233 void getAvailableChecks_async(
234 const AMD_ObserverInterface_getAvailableChecksPtr& amd,
235 const Ice::Current&)
override;
240 const std::string& channelName)
const;
241 void existsChannel_async(
242 const AMD_ObserverInterface_existsChannelPtr& amd,
243 const std::string& channelName,
244 const Ice::Current&)
const override;
247 bool existsDataField(
248 const std::string& channelName,
249 const std::string& datafieldName)
const;
250 void existsDataField_async(
251 const AMD_ObserverInterface_existsDataFieldPtr& amd,
252 const std::string& channelName,
253 const std::string& datafieldName,
254 const Ice::Current&)
const override;
265 DatafieldRefBasePtr createFilteredDatafield(
266 const DatafieldFilterBasePtr& filter,
267 const DatafieldRefBasePtr& datafieldRef);
268 void createFilteredDatafield_async(
269 const AMD_ObserverInterface_createFilteredDatafieldPtr& amd,
270 const DatafieldFilterBasePtr& filter,
271 const DatafieldRefBasePtr& datafieldRef,
272 const Ice::Current&)
override;
275 DatafieldRefBasePtr createNamedFilteredDatafield(
276 const std::string& filterDatafieldName,
277 const DatafieldFilterBasePtr& filter,
278 const DatafieldRefBasePtr& datafieldRef);
279 void createNamedFilteredDatafield_async(
280 const AMD_ObserverInterface_createNamedFilteredDatafieldPtr& amd,
281 const std::string& filterDatafieldName,
282 const DatafieldFilterBasePtr& filter,
283 const DatafieldRefBasePtr& datafieldRef,
284 const Ice::Current&)
override;
292 void removeFilteredDatafield(
293 const DatafieldRefBasePtr& datafieldRef);
294 void removeFilteredDatafield_async(
295 const AMD_ObserverInterface_removeFilteredDatafieldPtr& amd,
296 const DatafieldRefBasePtr& datafieldRef,
297 const Ice::Current&)
override;
300 ChannelHistory getChannelHistory(
301 const std::string& channelName,
303 const Ice::Current&
c)
const;
304 void getChannelHistory_async(
305 const AMD_ObserverInterface_getChannelHistoryPtr& amd,
306 const std::string& channelName,
308 const Ice::Current&
c)
const override;
311 ChannelHistory getPartialChannelHistory(
312 const std::string& channelName,
316 const Ice::Current&
c)
const;
317 void getPartialChannelHistory_async(
318 const AMD_ObserverInterface_getPartialChannelHistoryPtr& amd,
319 const std::string& channelName,
323 const Ice::Current&
c)
const override;
326 TimedVariantBaseList getDatafieldHistory(
327 const std::string& channelName,
328 const std::string& datafieldName,
330 const Ice::Current&
c)
const;
331 void getDatafieldHistory_async(
332 const AMD_ObserverInterface_getDatafieldHistoryPtr& amd,
333 const std::string& channelName,
334 const std::string& datafieldName,
336 const Ice::Current&
c)
const override;
339 TimedVariantBaseList getPartialDatafieldHistory(
340 const std::string& channelName,
341 const std::string& datafieldName,
345 const Ice::Current&
c)
const;
346 void getPartialDatafieldHistory_async(
347 const AMD_ObserverInterface_getPartialDatafieldHistoryPtr& amd,
348 const std::string& channelName,
349 const std::string& datafieldName,
353 const Ice::Current&
c)
const override;
367 void offerChannel(std::string channelName, std::string description);
379 void offerDataFieldWithDefault(std::string channelName, std::string datafieldName,
const Variant& defaultValue, std::string description);
391 void offerDataField(std::string channelName, std::string datafieldName,
VariantTypeId type, std::string description);
392 bool offerOrUpdateDataField(std::string channelName, std::string datafieldName,
const Variant&
value,
const std::string& description);
394 void offerOrUpdateDataFieldsFlatCopy(
const std::string& channelName,
const StringVariantBaseMap& valueMap);
403 void offerConditionCheck(std::string checkName,
ConditionCheck* conditionCheck);
415 void setDataField(
const std::string& channelName,
const std::string& datafieldName,
const Variant&
value,
bool triggerFilterUpdate =
true);
416 void setDataFieldFlatCopy(
const std::string& channelName,
const std::string& datafieldName,
const VariantPtr&
value,
bool triggerFilterUpdate =
true);
417 void setDataFieldsFlatCopy(
const std::string& channelName,
const StringVariantBaseMap& datafieldValues,
bool triggerFilterUpdate =
true);
418 void setDataFieldsFlatCopy(
const std::string& channelName,
const std::unordered_map< ::std::string, ::armarx::VariantBasePtr>& datafieldValues,
bool triggerFilterUpdate =
true);
419 void updateDatafieldTimestamps(
const std::string& channelName,
const std::unordered_map<std::string, Ice::Long>& datafieldValues);
420 void updateDatafieldTimestamps(
const std::string& channelName,
Ice::Long timestamp);
421 void maybeOfferChannelAndSetDataFieldsFlatCopy(
422 const std::string& channelName,
423 const std::string& description,
425 bool triggerFilterUpdate =
true);
434 void updateChannel(
const std::string& channelName,
const std::set<std::string>& updatedDatafields = std::set<std::string>());
441 void removeChannel(std::string channelName);
445 std::set<std::string> updateDatafieldFilter(
const std::string& channelName,
const std::string& datafieldName,
const VariantBasePtr&
value);
447 void scheduleDatafieldFilterUpdate(
const std::string& channelName,
const std::string& datafieldName,
const VariantBasePtr&
value);
454 virtual void onInitObserver() = 0;
459 virtual void onConnectObserver() = 0;
466 void metaUpdateTask();
472 void setDataFieldFlatCopy(
const DataFieldRegistry::iterator& dataFieldIter,
const VariantPtr&
value);
478 void evaluateCheck(
const ConditionCheckPtr& check,
const ChannelRegistryEntry& channel)
const;
481 std::string getDefaultName()
const override
488 void onInitComponent()
override;
489 void postOnConnectComponent()
override;
490 void onConnectComponent()
override;
491 void preOnDisconnectComponent()
override;
492 void onExitComponent()
override;
494 void updateRefreshRateChannel(
const std::string& channelName);
496 void channelUpdateFunction();
497 void doChannelUpdate(
const std::string& channelName,
const std::set<std::string>& updatedDatafields);
499 void updateFilters();
501 void addToChannelHistory(
const std::pair<IceUtil::Time, ChannelRegistryEntry>& historyEntry,
const std::string& channelName);
504 void addWorkerJob(
const std::string& name, std::function<
void(
void)>&& f)
const;
505 void addWorkerJob(
const std::string& name, std::function<
void(
void)>&& f);
512 catch (std::exception& e)
515 amd->ice_exception(e);
521 amd->ice_exception(std::runtime_error{std::move(
s)});
525 template<
class...Params>
526 void internalAddWorkerJob(
const std::string& name,
530 const Params& ...ps)
const
533 addWorkerJob(name, [
this, derivedPtr, amd, fnc, params = std::make_tuple(ps...)]
535 callAndPassExceptionToAMD(amd, [&]
537 auto lambda = [&](auto&& ...ps2)
539 return (derivedPtr->*fnc)(std::move(ps2)...);
541 using return_t = decltype(std::apply(lambda, params));
542 if constexpr(std::is_same_v<void, return_t>)
545 std::apply(lambda, params);
550 amd->ice_response(std::apply(lambda, params));
557 template<
class AMDPtr,
class FReturn,
class FOwner,
class...FParams,
class...Params>
560 FReturn(FOwner::*fnc)(FParams...)
const,
561 const Params& ...ps)
const
563 static_assert(std::is_base_of_v<Observer, FOwner>);
564 internalAddWorkerJob(name,
dynamic_cast<const FOwner*
>(
this), amd, fnc, ps...);
566 template<
class AMDPtr,
class FReturn,
class FOwner,
class...FParams,
class...Params>
569 FReturn(FOwner::*fnc)(FParams...),
572 static_assert(std::is_base_of_v<Observer, FOwner>);
573 internalAddWorkerJob(name,
dynamic_cast<FOwner*
>(
this), amd, fnc, ps...);
576 virtual void preWorkerJobs();
577 virtual void postWorkerJobs();
578 using ClockT = std::chrono::high_resolution_clock;
589 std::unique_ptr<Impl>
impl;