SkillProxy.cpp
Go to the documentation of this file.
1 #include "SkillProxy.h"
2 
3 #include <chrono>
4 #include <thread>
5 
6 namespace armarx
7 {
8  namespace skills
9  {
10  SkillProxy::SkillProxy(const manager::dti::SkillManagerInterfacePrx& manager,
11  const SkillID& skillId) :
12  manager(manager)
13  {
15  IceUtil::Optional<manager::dto::SkillDescription> description =
16  manager->getSkillDescription(skillId.toManagerIce());
17  if (description)
18  {
19  skillDescription = SkillDescription::FromIce(description.value());
21  }
22  else
23  {
24  std::stringstream reason;
25  reason << "No skill with ID " << skillId << " found";
26  throw error::SkillNotFoundException(__PRETTY_FUNCTION__, reason.str());
27  }
28  }
29 
30  SkillProxy::SkillProxy(const manager::dti::SkillManagerInterfacePrx& manager,
31  const SkillDescription& skillDesc) :
32  manager(manager), skillDescription(skillDesc)
33  {
36  }
37 
40  {
41  return skillDescription;
42  }
43 
44  SkillID
46  {
48  }
49 
51  SkillProxy::executeSkill(const std::string& executorName,
52  const aron::data::DictPtr& params) const
53  {
55  skills::manager::dto::SkillExecutionRequest req;
56  req.executorName = executorName;
57  req.parameters = aron::data::Dict::ToAronDictDTO(params);
58  req.skillId = skillDescription.skillId.toManagerIce();
59 
60  auto terminatingUpdateIce = manager->executeSkill(req);
61  return TerminatedSkillStatusUpdate::FromIce(terminatingUpdateIce);
62  }
63 
65  SkillProxy::executeSkillAsync(const std::string& executorName,
66  const aron::data::DictPtr& params) const
67  {
69  skills::manager::dto::SkillExecutionRequest req;
70  req.executorName = executorName;
71  req.parameters = aron::data::Dict::ToAronDictDTO(params);
72  req.skillId = skillDescription.skillId.toManagerIce();
73 
74  auto execIdIce = manager->executeSkillAsync(req);
75  return SkillExecutionID::FromIce(execIdIce);
76  }
77 
78  std::optional<TerminatedSkillStatusUpdate>
79  SkillProxy::join(const SkillExecutionID& executionId) const
80  {
82  auto s = this->manager->getSkillExecutionStatus(executionId.toManagerIce());
83 
84  while (s)
85  {
86  auto statusUpdate = skills::SkillStatusUpdate::FromIce(*s);
87 
88  if (statusUpdate.hasBeenTerminated())
89  {
90  break;
91  }
92 
93  std::this_thread::sleep_for(std::chrono::milliseconds(50));
94  s = this->manager->getSkillExecutionStatus(executionId.toManagerIce());
95  }
96 
97  if (!s)
98  {
99  // Either the manager already removed the result (then it is unknown) or the execution id does not exist.
100  return std::nullopt;
101  }
102 
104  }
105 
106  bool
108  {
110  auto r = manager->abortSkill(id.toManagerIce());
111  return r.success;
112  }
113 
114  bool
116  {
118  auto r = manager->abortSkillAsync(id.toManagerIce());
119  return r.success;
120  }
121 
124  {
126  }
127  } // namespace skills
128 } // namespace armarx
armarx::skills::SkillProxy::getRootProfileParameters
aron::data::DictPtr getRootProfileParameters() const
get the default parameters of the skill. TODO: Skill profiles in memory!
Definition: SkillProxy.cpp:123
armarx::skills::SkillProxy::executeSkill
TerminatedSkillStatusUpdate executeSkill(const std::string &executorName, const aron::data::DictPtr &params=nullptr) const
execute a skill and block until skill terminates
Definition: SkillProxy.cpp:51
armarx::skills::SkillExecutionID
Definition: SkillExecutionID.h:19
armarx::skills::TerminatedSkillStatusUpdate::FromIce
static TerminatedSkillStatusUpdate FromIce(const provider::dto::SkillStatusUpdate &update, const std::optional< skills::ProviderID > &providerId=std::nullopt)
Definition: SkillStatusUpdate.cpp:341
skills
This file is part of ArmarX.
armarx::skills::SkillStatusUpdate::FromIce
static SkillStatusUpdate FromIce(const provider::dto::SkillStatusUpdate &update, const std::optional< skills::ProviderID > &providerId=std::nullopt)
Definition: SkillStatusUpdate.cpp:367
armarx::skills::SkillDescription
Definition: SkillDescription.h:18
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::skills::error::SkillNotFoundException
Indicates that a skill was not found, e.g., by the skill manager.
Definition: Exception.h:54
armarx::skills::SkillProxy::manager
manager::dti::SkillManagerInterfacePrx manager
Definition: SkillProxy.h:59
armarx::skills::SkillID::toManagerIce
manager::dto::SkillID toManagerIce() const
Definition: SkillID.cpp:51
ARMARX_CHECK
#define ARMARX_CHECK(expression)
Shortcut for ARMARX_CHECK_EXPRESSION.
Definition: ExpressionException.h:82
armarx::skills::SkillExecutionID::toManagerIce
skills::manager::dto::SkillExecutionID toManagerIce() const
Definition: SkillExecutionID.cpp:9
armarx::skills::SkillProxy::skillDescription
SkillDescription skillDescription
Definition: SkillProxy.h:60
armarx::skills::SkillDescription::skillId
SkillID skillId
Definition: SkillDescription.h:20
armarx::skills::SkillProxy::SkillProxy
SkillProxy()=delete
We remove the default constructor as every skill proxy requires a manager.
armarx::aron::data::Dict::ToAronDictDTO
static data::dto::DictPtr ToAronDictDTO(const PointerType &navigator)
Definition: Dict.cpp:139
armarx::skills::SkillDescription::rootProfileDefaults
aron::data::DictPtr rootProfileDefaults
Definition: SkillDescription.h:22
SkillProxy.h
armarx::skills::TerminatedSkillStatusUpdate
Definition: SkillStatusUpdate.h:79
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::skills::SkillProxy::getSkillDescription
SkillDescription getSkillDescription() const
get the skill description
Definition: SkillProxy.cpp:39
armarx::skills::SkillProxy::executeSkillAsync
SkillExecutionID executeSkillAsync(const std::string &executorName, const aron::data::DictPtr &params=nullptr) const
execute a skill. Do not block during execution
Definition: SkillProxy.cpp:65
armarx::skills::SkillExecutionID::FromIce
static SkillExecutionID FromIce(const skills::manager::dto::SkillExecutionID &)
Definition: SkillExecutionID.cpp:29
armarx::skills::SkillProxy::join
std::optional< TerminatedSkillStatusUpdate > join(const SkillExecutionID &executionId) const
poll execution status and block until its null or terminated
Definition: SkillProxy.cpp:79
armarx::skills::SkillProxy::abortSkill
bool abortSkill(const SkillExecutionID &executionId) const
ask skill to abort ASAP. Blocks until skill stopped
Definition: SkillProxy.cpp:107
armarx::skills::SkillDescription::FromIce
static SkillDescription FromIce(const provider::dto::SkillDescription &i, const std::optional< ProviderID > &=std::nullopt)
Definition: SkillDescription.cpp:39
armarx::skills::SkillProxy::abortSkillAsync
bool abortSkillAsync(const SkillExecutionID &executionId) const
ask skill to abort ASAP
Definition: SkillProxy.cpp:115
armarx::skills::SkillID
Definition: SkillID.h:17
armarx::skills::SkillID::isFullySpecified
bool isFullySpecified() const
Definition: SkillID.h:29
armarx::ctrlutil::s
double s(double t, double s0, double v0, double a0, double j)
Definition: CtrlUtil.h:33
armarx::skills::SkillProxy::getSkillId
SkillID getSkillId() const
get the skill id from the skill description
Definition: SkillProxy.cpp:45
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28