6#include <SimoxUtility/algorithm/string/string_tools.h>
14 using StatusMap = std::map<skills::SkillExecutionID, skills::SkillStatusUpdate>;
16 std::map<skills::ProviderID, std::map<skills::SkillID, skills::SkillDescription>>;
19 SkillManagerWrapper::fetchExecutionsFromMemory()
29 std::scoped_lock l(mutex_memory);
34 auto currentManagerStatuses =
35 memory->ice_invocationTimeout(10000)->getSkillExecutionStatuses();
38 for (
const auto& [k, v] : currentManagerStatuses)
44 statusMap[executionId] = statusUpdate;
48 catch (Ice::Exception
const& e)
51 <<
"Unhandled Ice exception encountered while updating executions. Exception was: "
59 ARMARX_WARNING <<
"Unknown exception encountered while updating executions.";
66 matches(std::string skillName, std::vector<std::string>& searches)
69 for (std::string& substring : searches)
71 size_t occurance = skillName.find(substring,
index);
72 if (occurance == std::string::npos)
84 SkillManagerWrapper::Snapshot
85 SkillManagerWrapper::filterUpdate(Snapshot update)
88 if (this->currentSkillSearch.empty())
96 filtered.statuses = update.statuses;
98 std::vector<std::string> substrings;
101 std::vector<std::string> rawSubstrings = simox::alg::split(currentSkillSearch);
102 for (
auto&
string : rawSubstrings)
104 substrings.push_back(simox::alg::to_lower(
string));
109 for (
auto& provider_and_descrMap :
update.skills)
111 using DescriptionMap = std::map<skills::SkillID, skills::SkillDescription>;
113 DescriptionMap& descriptionMap = provider_and_descrMap.second;
114 skills::ProviderID provider = provider_and_descrMap.first;
116 for (
auto& skill_and_description : descriptionMap)
118 skills::SkillID sid = skill_and_description.first;
119 skills::SkillDescription descr = skill_and_description.second;
121 if (
matches(simox::alg::to_lower(sid.skillName), substrings))
124 filtered.skills[provider][sid] = descr;
133 SkillManagerWrapper::fetchSkillsFromMemory()
142 std::scoped_lock l(mutex_memory);
146 auto managerSkills = memory->ice_invocationTimeout(5000)->getSkillDescriptions();
148 for (
const auto& [sid, desc] : managerSkills)
152 auto providerId = skillId.providerId.value_or(
153 skills::ProviderID{.providerName =
"UNKNOWN PROVIDER NAME"});
157 auto& providedSkillsMap = skills[providerId];
158 providedSkillsMap.insert({skillId, description});
163 catch (Ice::Exception
const& e)
166 <<
"Unhandled Ice exception encountered while updating skills. Exception was: "
174 ARMARX_WARNING <<
"Unknown exception encountered while updating skills.";
181 skills::manager::dti::SkillManagerInterfacePrx
const& updatedMemory)
183 std::scoped_lock l(mutex_memory);
184 this->memory = updatedMemory;
192 std::scoped_lock l(mutex_memory);
193 this->memory =
nullptr;
199 this->currentSkillSearch = search;
215 executions = this->fetchExecutionsFromMemory();
223 for (
auto& [executionId,
status] : executions)
226 if (!
status.hasBeenTerminated())
237 std::scoped_lock l(mutex_snapshot);
239 snapshot.skills = fetchSkillsFromMemory();
240 snapshot.statuses = fetchExecutionsFromMemory();
246 std::vector<Parameters>
249 if (this->skillParameterExecutionHistory.find(sid) ==
250 this->skillParameterExecutionHistory.end())
254 return this->skillParameterExecutionHistory[sid];
261 if (this->skillParameterExecutionHistory.find(sid) ==
262 this->skillParameterExecutionHistory.end())
265 this->skillParameterExecutionHistory.insert({sid, {params}});
270 this->skillParameterExecutionHistory[sid].emplace_back(params);
274 std::optional<Parameters>
278 if (parameterList.empty())
282 return parameterList.back();
285 const std::optional<ProviderID>
291 ARMARX_WARNING <<
"The memory snapshot was searched for any provider, when a provider "
296 for (
auto& [prov, skillMap] : map)
298 for (
auto& [skill, desc] : skillMap)
300 if (skill == skillId)
312 std::scoped_lock l(mutex_snapshot);
314 Snapshot filtered = filterUpdate(snapshot);
322 std::scoped_lock l(mutex_snapshot);
324 return snapshot.skills;
330 std::scoped_lock l(mutex_snapshot);
331 return snapshot.statuses;
336 const unsigned int max_retries)
344 unsigned int retries = max_retries;
352 std::scoped_lock l(mutex_memory);
353 this->memory->ice_invocationTimeout(5000)->abortSkillAsync(
356 catch (Ice::Exception
const& e)
359 ARMARX_ERROR <<
"Unhandeled Ice exception while aborting skill '"
367 ARMARX_ERROR <<
"Unhandled error while aborting skill '"
377 ARMARX_WARNING <<
"There where errors aborting skills. Retrying...";
381 ARMARX_ERROR <<
"Couldn't abort all skills after " << max_retries
382 <<
" tries. Giving up.";
400 if (!providerId.has_value())
403 <<
"' has been requested to be executed, but no provider was "
404 "given. Aborting...";
408 std::map<skills::SkillID, skills::SkillDescription> skillDescriptions;
409 if (this->UPDATE_ON_EXECUTION_REQUEST)
411 skillDescriptions = this->fetchSkillsFromMemory().at(providerId.value());
415 skillDescriptions = this->
getSkills().at(providerId.value());
418 if (skillDescriptions.find(skillId) == skillDescriptions.end())
421 <<
"' has been requested to be executed, but no skill description was "
422 "found. Aborting...";
426 char hostname[HOST_NAME_MAX];
427 gethostname(hostname, HOST_NAME_MAX);
431 .executorName =
"Skills.Manager GUI (hostname: " + std::string(hostname) +
")",
432 .parameters = params};
440 std::scoped_lock l(mutex_memory);
441 memory->ice_invocationTimeout(5000)->executeSkillAsync(req.toManagerIce());
443 catch (Ice::Exception
const& e)
Brief description of class memory.
bool isProviderSpecified() const
std::optional< ProviderID > providerId
bool isFullySpecified() const
static SkillID FromIce(const manager::dto::SkillID &)
void stopAllExecutions()
Stops all available (and running) executions.
void disconnectMemory()
Disconnects the interface from memory.
static const std::optional< skills::ProviderID > findFirstProvider(SkillMap const &map, SkillID const &skillId)
StatusMap getExecutions()
Returns the latest status snapshot.
void connectMemory(skills::manager::dti::SkillManagerInterfacePrx const &updatedMemory)
Updates the memory pointer.
void acceptSearchRequest(std::string const &search)
Applies the search word to the update filter.
SkillMap getSkillsFiltered()
Returns the latest skills snapshot.
SkillMap getSkills()
Returns the latest skills snapshot.
void stopExecution(skills::SkillExecutionID const &executionId, const unsigned int max_retries=0)
Attempts to stop an execution.
void startExecutionWithParams(skills::SkillID &skillId, aron::data::DictPtr const params)
Attempts to start an execution with given parameters.
void connectionUpdate(std::string const &message, std::string const &error)
void addParametersToHistory(const skills::SkillID &sid, const Parameters ¶ms)
std::optional< Parameters > getLatestParametersForSkill(const skills::SkillID &sid)
void updateAvailable(Snapshot update)
void updateFromMemory()
Requests this wrapper to overwrite its own state from memory.
std::vector< Parameters > getParameterHistoryForSkill(const skills::SkillID &sid)
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
#define ARMARX_INFO
The normal logging level.
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
std::shared_ptr< Dict > DictPtr
bool matches(std::string skillName, std::vector< std::string > &searches)
std::map< skills::SkillExecutionID, skills::SkillStatusUpdate > StatusMap
aron::data::DictPtr Parameters
std::map< skills::ProviderID, std::map< skills::SkillID, skills::SkillDescription > > SkillMap
static SkillDescription FromIce(const provider::dto::SkillDescription &i, const std::optional< ProviderID > &=std::nullopt)
static SkillExecutionID FromIce(const skills::manager::dto::SkillExecutionID &)
skills::manager::dto::SkillExecutionID toManagerIce() const
static SkillStatusUpdate FromIce(const provider::dto::SkillStatusUpdate &update, const std::optional< skills::ProviderID > &providerId=std::nullopt)