SkillDashboard.cpp
Go to the documentation of this file.
1
2#include "SkillDashboard.h"
3
4#include <fstream>
5#include <iostream>
6#include <sstream>
7#include <string>
8
9#include <nlohmann/json.hpp>
10
11namespace armarx
12{
13
16 {
18 new ::armarx::ComponentPropertyDefinitions(getConfigIdentifier());
19 def->optional(properties.shortcutPath,
20 "ShortcutPath",
21 "Add path of predefined dashboards. Syntax: Package/folderName/fileName");
22 return def;
23 }
24
25 bool
26 SkillDashboard::loadShortcuts(const std::string& packagepath)
27 {
28 this->registeredShortcuts = std::vector<SkillShortcut>();
29 std::stringstream ss(packagepath);
30 std::string item;
31 std::vector<std::string> result;
32 while (std::getline(ss, item, '/'))
33 {
34 result.push_back(item);
35 }
36 if (result.size() < 3)
37 {
38 ARMARX_ERROR << "Invalid property structure!";
39 return false;
40 }
41 this->packageName = result[0];
42
43 std::string absoluteFilepath;
44 std::string filename;
45 armarx::CMakePackageFinder finder(result[0]);
46 if (finder.packageFound())
47 {
48 ARMARX_INFO << "Found package.";
49
50 std::string packageDataDir = finder.getDataDir();
51
52 // add the data directory to the search list of ArmarXDataPath
53 ArmarXDataPath::addDataPaths(packageDataDir);
54 //std::filesystem::create_directory(packageDataDir + "/" + folder);
55
56 std::string relativeFilename;
57 for (size_t i = 1; i < result.size() - 1; i++)
58 {
59 if (!relativeFilename.empty())
60 {
61 relativeFilename += "/";
62 }
63 relativeFilename += result[i];
64 }
65
66 this->pathToDashboard = relativeFilename;
67 this->dahsboardName = result[result.size() - 1];
68
69 if (ArmarXDataPath::getAbsolutePath(relativeFilename, absoluteFilepath))
70 {
71 ARMARX_INFO << "Located file at:" << absoluteFilepath;
72 filename = result[result.size() - 1] + ".json";
73 }
74
75
76 std::ifstream file(absoluteFilepath + "/" + filename);
77 nlohmann::json jsonData;
78
79 if (!file.is_open())
80 {
81 ARMARX_WARNING << "Could not open file";
82 }
83
84 file >> jsonData;
85
86 for (const auto& item : jsonData["shortcuts"])
87 {
88 SkillShortcut shortcut;
89 shortcut.skillArgs = item["skill_args"].dump(2);
90 shortcut.skillId = item["skill_id"];
91 shortcut.shortcutName = item["skill_shortcut_name"];
92
93 this->registeredShortcuts.push_back(shortcut);
94 }
95 return true;
96 }
97 else
98 {
99 return false;
100 }
101 }
102
103 void
105 {
106
107 this->properties.shortcutPath = getProperty<std::string>("ShortcutPath").getValue();
108
109 if (not properties.shortcutPath.empty())
110 {
111 loadShortcuts(properties.shortcutPath);
112 }
113 }
114
115 void
119
120 SkillShortcutList
121 SkillDashboard::getShortcuts(const ::Ice::Current&)
122 {
123 return this->registeredShortcuts;
124 }
125
126 SkillShortcut
127 SkillDashboard::getShortcut(const std::string& name, const ::Ice::Current&)
128 {
129 for (const SkillShortcut& shortcut : this->registeredShortcuts)
130 {
131 if (shortcut.shortcutName == name)
132 {
133 return shortcut;
134 }
135 }
136
137 return SkillShortcut();
138 }
139
140 void
141 SkillDashboard::addNewShortcut(const SkillShortcut& newShortcut, const ::Ice::Current&)
142 {
143 if (newShortcut.shortcutName != "")
144 {
145 if (getShortcut(newShortcut.shortcutName).shortcutName == "")
146 {
147
148 this->registeredShortcuts.push_back(newShortcut);
149 ARMARX_INFO << "Added shortcut " << newShortcut.shortcutName;
150 }
151 else
152 {
153 for (SkillShortcut& shortcut : this->registeredShortcuts)
154 {
155 if (shortcut.shortcutName == newShortcut.shortcutName)
156 {
157 shortcut.skillId = newShortcut.skillId;
158 shortcut.skillArgs = newShortcut.skillArgs;
159 }
160 }
161 ARMARX_INFO << "changed shortcut";
162 }
163 }
164 }
165
166 void
167 SkillDashboard::deleteShortcut(const std::string& name, const ::Ice::Current&)
168 {
169 for (auto it = this->registeredShortcuts.begin(); it != this->registeredShortcuts.end();
170 ++it)
171 {
172 if (it->shortcutName == name)
173 {
174 this->registeredShortcuts.erase(it);
175 break;
176 }
177 }
178 }
179
180 void
181 SkillDashboard::saveShortcutOrder(const std::vector<std::string>& order, const ::Ice::Current&)
182 {
183 ARMARX_IMPORTANT << "Save shortcut order";
184 this->shortcutOrder = order;
185 }
186
187 std::vector<std::string>
188 SkillDashboard::getPathStructure(const ::Ice::Current&)
189 {
190 return std::vector<std::string>{
191 this->packageName, this->pathToDashboard, this->dahsboardName};
192 }
193
194 bool
195 SkillDashboard::importShortcuts(const std::string& package,
196 const std::string& folder,
197 const std::string& name,
198 const ::Ice::Current&)
199 {
200 std::string path = package + "/" + folder + "/" + name;
201 return loadShortcuts(path);
202 }
203
204 void
205 SkillDashboard::exportShortcuts(const std::string& package,
206 const std::string& folder,
207 const std::string& name,
208 const ::Ice::Current&)
209 {
210
211 nlohmann::json j;
212
213 std::unordered_map<std::string, SkillShortcut> lookup;
214 lookup.reserve(registeredShortcuts.size());
215 for (const auto& s : registeredShortcuts)
216 {
217 lookup[s.shortcutName] = s;
218 }
219
220
221 for (const auto& name : this->shortcutOrder)
222 {
223
224 auto it = lookup.find(name);
225 if (it == lookup.end())
226 {
227 ARMARX_IMPORTANT << "Shortcut does not exist.";
228 continue;
229 }
230 const auto& shortcut = it->second;
231
232 nlohmann::json skillArgs_json = nlohmann::json::parse(shortcut.skillArgs);
233
234 j["shortcuts"].push_back({{"skill_shortcut_name", shortcut.shortcutName},
235 {"skill_id", shortcut.skillId},
236 {"skill_args", skillArgs_json}});
237 }
238
239 std::string absoluteFilename;
240 armarx::CMakePackageFinder finder(package);
241 if (finder.packageFound())
242 {
243
244 std::string packageDataDir = finder.getDataDir();
245
246 // add the data directory to the search list of ArmarXDataPath
247 ArmarXDataPath::addDataPaths(packageDataDir);
248 std::filesystem::create_directories(packageDataDir + "/" + folder);
249
250 std::string relativeFilename(folder);
251
252 if (ArmarXDataPath::getAbsolutePath(relativeFilename, absoluteFilename))
253 {
254 ARMARX_INFO << "Located file at:" << absoluteFilename;
255 std::string filename = name + ".json";
256 std::ofstream outFile(absoluteFilename + "/" + filename);
257 outFile << std::setw(4) << j << std::endl;
258 outFile.close();
259 ARMARX_INFO << "Insert " << package << "/" << folder << "/" << name
260 << " into the component property.";
261 }
262 }
263 }
264
265 void
269
270 void
274
275
276} // namespace armarx
static bool getAbsolutePath(const std::string &relativeFilename, std::string &storeAbsoluteFilename, const std::vector< std::string > &additionalSearchPaths={}, bool verbose=true)
static void addDataPaths(const std::string &dataPathList)
The CMakePackageFinder class provides an interface to the CMake Package finder capabilities.
bool packageFound() const
Returns whether or not this package was found with cmake.
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Definition Component.cpp:90
Property< PropertyType > getProperty(const std::string &name)
void onInitComponent() override
bool importShortcuts(const std::string &package, const std::string &folder, const std::string &name, const ::Ice::Current &=::Ice::Current()) override
void deleteShortcut(const std::string &name, const ::Ice::Current &=::Ice::Current()) override
void addNewShortcut(const SkillShortcut &newShortcut, const ::Ice::Current &=::Ice::Current()) override
void onDisconnectComponent() override
SkillShortcut getShortcut(const std::string &name, const ::Ice::Current &=::Ice::Current()) override
SkillShortcutList getShortcuts(const ::Ice::Current &=::Ice::Current()) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void saveShortcutOrder(const std::vector< std::string > &order, const ::Ice::Current &=::Ice::Current()) override
void exportShortcuts(const std::string &package, const std::string &folder, const std::string &name, const ::Ice::Current &=::Ice::Current()) override
void onConnectComponent() override
std::vector< std::string > getPathStructure(const ::Ice::Current &=::Ice::Current()) override
void onExitComponent() override
#define ARMARX_INFO
The normal logging level.
Definition Logging.h:181
#define ARMARX_IMPORTANT
The logging level for always important information, but expected behaviour (in contrast to ARMARX_WAR...
Definition Logging.h:190
#define ARMARX_ERROR
The logging level for unexpected behaviour, that must be fixed.
Definition Logging.h:196
#define ARMARX_WARNING
The logging level for unexpected behaviour, but not a serious problem.
Definition Logging.h:193
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.