SkillManagerWrapper.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <mutex>
4 #include <vector>
5 
6 #include <QObject>
7 
13 #include <RobotAPI/interface/skills/SkillManagerInterface.h>
14 
15 namespace armarx::skills::gui
16 {
17  using StatusMap = std::map<skills::SkillExecutionID, skills::SkillStatusUpdate>;
18  using SkillMap =
19  std::map<skills::ProviderID, std::map<skills::SkillID, skills::SkillDescription>>;
20 
22  using SkillParameterMap =
23  std::map<skills::SkillID, std::vector<Parameters>>;
24 
25  class SkillManagerWrapper : public QObject
26  {
27  Q_OBJECT
28  public:
29  SkillManagerWrapper(skills::manager::dti::SkillManagerInterfacePrx& _memory,
30  QObject* parent = nullptr) :
31  QObject(parent), memory(_memory)
32  {
33  }
34 
35  // We can instantiate the proxy without memory, if it's not connected yet.
36  SkillManagerWrapper(QObject* parent = nullptr) : QObject(parent)
37  {
38  }
39 
40  /**
41  * @brief Updates the memory pointer. This should be called whenever the GUI connects/reconnects.
42  */
43  void connectMemory(skills::manager::dti::SkillManagerInterfacePrx const& updatedMemory);
44 
45  // Basic functions to work with. These will produce Ice calls!
46  /**
47  * @brief Attempts to start an execution with given parameters. If an Ice exception is encountered, a message will be logged and the method will abort.
48  * @param skillId The skill to execute.
49  * @param params The parameters to execute with.
50  */
52 
53  /**
54  * @brief Attempts to stop an execution. If this doesn't succeed, it will retry.
55  * @param executionId The ExecutionID to abort.
56  * @param max_retries The amount of times to retry, if aborting fails.
57  */
58  void stopExecution(skills::SkillExecutionID const& executionId,
59  const unsigned int max_retries = 0);
60 
61  static const std::optional<skills::ProviderID> findFirstProvider(SkillMap const& map,
62  SkillID const& skillId);
63 
64  /**
65  * @brief Returns the latest skills snapshot.
66  * @return The map representing all currently known skills.
67  */
69 
70  /**
71  * @brief Returns the latest skills snapshot.
72  * @return The map representing all currently known skills.
73  */
75 
76  /**
77  * @brief Returns the latest status snapshot.
78  * @return The map containing status updates for all execution ids.
79  */
81 
82  struct Snapshot
83  {
86 
87  // default constructor (construct empty)
88  Snapshot() : statuses({}), skills({})
89  {
90  }
91  };
92 
93  signals:
94  void disableAutoUpdate();
95  void connectionUpdate(std::string const& message, std::string const& error);
96  void updateAvailable(Snapshot update);
97  void searchAccepted();
98 
99  public slots:
100  /**
101  * @brief Disconnects the interface from memory.
102  */
103  void disconnectMemory();
104 
105  /**
106  * @brief Applies the search word to the update filter.
107  * @param search The search word.
108  */
109  void acceptSearchRequest(std::string const& search);
110 
111  /**
112  * @brief Stops all available (and running) executions.
113  */
114  void stopAllExecutions();
115 
116  /**
117  * @brief Requests this wrapper to overwrite its own state from memory.
118  */
119  void updateFromMemory();
120 
121  // == Skill Parameters State ==
122 
123  std::vector<Parameters> getParameterHistoryForSkill(const skills::SkillID& sid);
124  void addParametersToHistory(const skills::SkillID& sid, const Parameters& params);
125  std::optional<Parameters> getLatestParametersForSkill(const skills::SkillID& sid);
126 
127  private:
128  mutable std::mutex mutex_memory;
129  mutable std::mutex mutex_snapshot;
130 
131  armarx::skills::manager::dti::SkillManagerInterfacePrx memory;
132  std::string currentSkillSearch = "";
133 
134  // Store a list of all execution parameters for each skill
135  SkillParameterMap skillParameterExecutionHistory;
136  SkillParameterMap lostParameterHistory;
137 
138  Snapshot snapshot;
139 
140  /**
141  * @brief Fetches and returns the latest skills update from memory.
142  * @return The map representing all skills in memory. Empty, if error occurred.
143  */
144  SkillMap fetchSkillsFromMemory();
145 
146  /**
147  * @brief Fetches and returns the latest status update from memory.
148  * @return The map containing status updates for all execution ids. Empty, if error occurred.
149  */
150  StatusMap fetchExecutionsFromMemory();
151 
152  /**
153  * @brief Returns the given snapshot, matching the search.
154  * @param update The snapshot to modify.
155  */
156  Snapshot filterUpdate(Snapshot update);
157 
158  // config
159  // TODO: perhaps use proper config file for the gui
160 
161  // should we do an ice call to use the latest information when requesting an execution?
162  static constexpr bool UPDATE_ON_EXECUTION_REQUEST = true;
163 
164  // ---
165  };
166 } // namespace armarx::skills::gui
armarx::skills::SkillExecutionID
Definition: SkillExecutionID.h:15
ProviderID.h
armarx::skills::gui::SkillParameterMap
std::map< skills::SkillID, std::vector< Parameters > > SkillParameterMap
Definition: SkillManagerWrapper.h:23
armarx::skills::gui::SkillManagerWrapper::startExecutionWithParams
void startExecutionWithParams(skills::SkillID &skillId, aron::data::DictPtr const params)
Attempts to start an execution with given parameters.
Definition: SkillManagerWrapper.cpp:390
armarx::skills::gui::SkillManagerWrapper::updateAvailable
void updateAvailable(Snapshot update)
SkillStatusUpdate.h
armarx::skills::gui::SkillManagerWrapper::updateFromMemory
void updateFromMemory()
Requests this wrapper to overwrite its own state from memory.
Definition: SkillManagerWrapper.cpp:235
SkillID.h
armarx::skills::gui::StatusMap
std::map< skills::SkillExecutionID, skills::SkillStatusUpdate > StatusMap
Definition: SkillExecutionTreeWidget.h:12
armarx::skills::gui::SkillManagerWrapper::disconnectMemory
void disconnectMemory()
Disconnects the interface from memory.
Definition: SkillManagerWrapper.cpp:188
armarx::skills::gui::SkillManagerWrapper::connectionUpdate
void connectionUpdate(std::string const &message, std::string const &error)
armarx::skills::gui::SkillManagerWrapper::getLatestParametersForSkill
std::optional< Parameters > getLatestParametersForSkill(const skills::SkillID &sid)
Definition: SkillManagerWrapper.cpp:275
message
message(STATUS "Boost-Library-Dir: " "${Boost_LIBRARY_DIRS}") message(STATUS "Boost-LIBRARIES
Definition: CMakeLists.txt:8
SkillExecutionID.h
armarx::skills::gui::SkillManagerWrapper::searchAccepted
void searchAccepted()
armarx::skills::gui::Parameters
aron::data::DictPtr Parameters
Definition: SkillManagerWrapper.h:21
armarx::memory
Brief description of class memory.
Definition: memory.h:38
armarx::skills::gui::SkillManagerWrapper::findFirstProvider
static const std::optional< skills::ProviderID > findFirstProvider(SkillMap const &map, SkillID const &skillId)
Definition: SkillManagerWrapper.cpp:286
armarx::skills::gui::SkillManagerWrapper::getExecutions
StatusMap getExecutions()
Returns the latest status snapshot.
Definition: SkillManagerWrapper.cpp:328
armarx::skills::gui::SkillManagerWrapper::SkillManagerWrapper
SkillManagerWrapper(skills::manager::dti::SkillManagerInterfacePrx &_memory, QObject *parent=nullptr)
Definition: SkillManagerWrapper.h:29
armarx::skills::gui::SkillManagerWrapper::addParametersToHistory
void addParametersToHistory(const skills::SkillID &sid, const Parameters &params)
Definition: SkillManagerWrapper.cpp:258
armarx::skills::gui::SkillManagerWrapper::Snapshot
Definition: SkillManagerWrapper.h:82
armarx::skills::gui::SkillManagerWrapper::Snapshot::Snapshot
Snapshot()
Definition: SkillManagerWrapper.h:88
armarx::skills::gui::SkillManagerWrapper::Snapshot::skills
SkillMap skills
Definition: SkillManagerWrapper.h:85
armarx::skills::gui::SkillMap
std::map< skills::ProviderID, std::map< skills::SkillID, skills::SkillDescription > > SkillMap
Definition: SkillManagerWrapper.cpp:16
armarx::skills::gui::SkillManagerWrapper::getSkillsFiltered
SkillMap getSkillsFiltered()
Returns the latest skills snapshot.
Definition: SkillManagerWrapper.cpp:310
armarx::skills::gui
Definition: PeriodicUpdateWidget.cpp:11
armarx::armem::server::ltm::util::mongodb::detail::update
bool update(mongocxx::collection &coll, const nlohmann::json &query, const nlohmann::json &update)
Definition: mongodb.cpp:68
armarx::aron::data::DictPtr
std::shared_ptr< Dict > DictPtr
Definition: Dict.h:41
armarx::skills::gui::SkillManagerWrapper::Snapshot::statuses
StatusMap statuses
Definition: SkillManagerWrapper.h:84
armarx::skills::gui::SkillManagerWrapper::disableAutoUpdate
void disableAutoUpdate()
armarx::skills::gui::SkillManagerWrapper::stopAllExecutions
void stopAllExecutions()
Stops all available (and running) executions.
Definition: SkillManagerWrapper.cpp:204
armarx::skills::gui::SkillManagerWrapper::getParameterHistoryForSkill
std::vector< Parameters > getParameterHistoryForSkill(const skills::SkillID &sid)
Definition: SkillManagerWrapper.cpp:247
armarx::skills::SkillID
Definition: SkillID.h:14
armarx::skills::gui::SkillManagerWrapper::getSkills
SkillMap getSkills()
Returns the latest skills snapshot.
Definition: SkillManagerWrapper.cpp:320
armarx::skills::gui::SkillManagerWrapper::acceptSearchRequest
void acceptSearchRequest(std::string const &search)
Applies the search word to the update filter.
Definition: SkillManagerWrapper.cpp:197
SkillDescription.h
armarx::skills::gui::SkillManagerWrapper::connectMemory
void connectMemory(skills::manager::dti::SkillManagerInterfacePrx const &updatedMemory)
Updates the memory pointer.
Definition: SkillManagerWrapper.cpp:180
armarx::skills::gui::SkillManagerWrapper
Definition: SkillManagerWrapper.h:25
armarx::skills::gui::SkillManagerWrapper::stopExecution
void stopExecution(skills::SkillExecutionID const &executionId, const unsigned int max_retries=0)
Attempts to stop an execution.
Definition: SkillManagerWrapper.cpp:335
armarx::skills::gui::SkillManagerWrapper::SkillManagerWrapper
SkillManagerWrapper(QObject *parent=nullptr)
Definition: SkillManagerWrapper.h:36