22 #include "config_adapter.h"
26 #include <AdapterConfiguration.hh>
27 #include <AdapterExecInterface.hh>
28 #include <AdapterFactory.hh>
30 #include <InterfaceManager.hh>
31 #include <StateCacheEntry.hh>
45 : InterfaceAdapter(execInterface)
55 pugi::xml_node
const xml)
56 : InterfaceAdapter(execInterface, xml)
71 logger_ =
reinterpret_cast<fawkes::Logger *
>(m_execInterface.getProperty(
"::Fawkes::Logger"));
75 namespace p = std::placeholders;
77 {
"config_get_int_or_default",
78 std::bind(&ConfigurationPlexilAdapter::config_get_value_or_default,
81 PLEXIL::INTEGER_TYPE)},
82 {
"config_get_real_or_default",
84 &ConfigurationPlexilAdapter::config_get_value_or_default,
this, p::_1, PLEXIL::REAL_TYPE)},
85 {
"config_get_bool_or_default",
86 std::bind(&ConfigurationPlexilAdapter::config_get_value_or_default,
89 PLEXIL::BOOLEAN_TYPE)},
90 {
"config_get_string_or_default",
92 &ConfigurationPlexilAdapter::config_get_value_or_default,
this, p::_1, PLEXIL::STRING_TYPE)},
94 std::bind(&ConfigurationPlexilAdapter::config_get_value,
this, p::_1, PLEXIL::INTEGER_TYPE)},
96 std::bind(&ConfigurationPlexilAdapter::config_get_value,
this, p::_1, PLEXIL::REAL_TYPE)},
98 std::bind(&ConfigurationPlexilAdapter::config_get_value,
this, p::_1, PLEXIL::BOOLEAN_TYPE)},
100 std::bind(&ConfigurationPlexilAdapter::config_get_value,
this, p::_1, PLEXIL::STRING_TYPE)},
101 {
"config_exists", std::bind(&ConfigurationPlexilAdapter::config_exists,
this, p::_1)},
104 for (
const auto &c : commands_) {
105 PLEXIL::g_configuration->registerCommandInterface(c.first,
this);
153 std::string
const &name = cmd->getName();
155 auto c = commands_.find(name);
156 if (c != commands_.end()) {
159 warn(
"ConfigCommAdapter:executeCommand: called for unknown"
162 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_FAILED);
163 m_execInterface.notifyOfExternalEvent();
173 m_execInterface.handleCommandAbortAck(cmd,
false);
174 m_execInterface.notifyOfExternalEvent();
178 ConfigurationPlexilAdapter::config_get_value_or_default(PLEXIL::Command * cmd,
179 PLEXIL::ValueType value_type)
181 std::vector<PLEXIL::Value>
const &args = cmd->getArgValues();
182 if (!verify_args(args,
183 "ConfigCommAdapter:config_get_value_or_default",
184 {{
"path", PLEXIL::STRING_TYPE}, {
"default", value_type}})) {
185 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_FAILED);
186 m_execInterface.notifyOfExternalEvent();
191 args[0].getValue(path);
194 switch (value_type) {
195 case PLEXIL::STRING_TYPE: {
196 std::string default_value;
197 args[1].getValue(default_value);
199 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(v));
202 case PLEXIL::REAL_TYPE: {
203 double default_value;
204 args[1].getValue(default_value);
206 double v = default_value;
212 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(v));
215 case PLEXIL::BOOLEAN_TYPE: {
217 args[1].getValue(default_value);
219 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(v));
222 case PLEXIL::INTEGER_TYPE: {
224 args[1].getValue(default_value);
225 if (config_->
is_uint(path.c_str())) {
226 unsigned int uv = config_->
get_uint(path.c_str());
227 if (uv > std::numeric_limits<int>::max()) {
228 warn(
"ConfigCommAdapter:config_get_value_or_default:"
229 <<
" Unsigned integer too large to store in int (" << uv <<
")");
230 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
231 m_execInterface.notifyOfExternalEvent();
236 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(v));
241 warn(
"ConfigCommAdapter:config_get_value_or_default:"
242 <<
" Unsupported Plexil type " << PLEXIL::valueTypeName(value_type));
243 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
244 m_execInterface.notifyOfExternalEvent();
248 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
249 m_execInterface.notifyOfExternalEvent();
252 warn(
"ConfigCommAdapter:config_get_value_or_default:"
253 <<
" Failed to get value " << path <<
" as " << PLEXIL::valueTypeName(value_type) <<
": "
255 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
256 m_execInterface.notifyOfExternalEvent();
262 ConfigurationPlexilAdapter::config_get_value(PLEXIL::Command *cmd, PLEXIL::ValueType value_type)
264 std::vector<PLEXIL::Value>
const &args = cmd->getArgValues();
265 if (!verify_args(args,
"ConfigCommAdapter:config_get_value", {{
"path", PLEXIL::STRING_TYPE}})) {
266 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_FAILED);
267 m_execInterface.notifyOfExternalEvent();
272 args[0].getValue(path);
275 switch (value_type) {
276 case PLEXIL::STRING_TYPE: {
277 std::string v = config_->
get_string(path.c_str());
278 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(v));
281 case PLEXIL::REAL_TYPE: {
282 float v = config_->
get_float(path.c_str());
283 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value((
double)v));
286 case PLEXIL::BOOLEAN_TYPE: {
287 bool v = config_->
get_bool(path.c_str());
288 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(v));
291 case PLEXIL::INTEGER_TYPE: {
292 if (config_->
is_uint(path.c_str())) {
293 unsigned int uv = config_->
get_uint(path.c_str());
294 if (uv > std::numeric_limits<int>::max()) {
295 warn(
"ConfigCommAdapter:config_get_value:"
296 <<
" Unsigned integer too large to store in int (" << uv <<
")");
297 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
298 m_execInterface.notifyOfExternalEvent();
302 int v = config_->
get_int(path.c_str());
303 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(v));
308 warn(
"ConfigCommAdapter:config_get_value:"
309 <<
" Unsupported Plexil type " << PLEXIL::valueTypeName(value_type));
310 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_FAILED);
311 m_execInterface.notifyOfExternalEvent();
315 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
316 m_execInterface.notifyOfExternalEvent();
319 warn(
"ConfigCommAdapter:config_get_value:"
320 <<
" Failed to get value " << path <<
" as " << PLEXIL::valueTypeName(value_type) <<
": "
322 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_FAILED);
323 m_execInterface.notifyOfExternalEvent();
329 ConfigurationPlexilAdapter::config_exists(PLEXIL::Command *cmd)
331 std::vector<PLEXIL::Value>
const &args = cmd->getArgValues();
332 if (!verify_args(args,
"ConfigCommAdapter:config_exists", {{
"path", PLEXIL::STRING_TYPE}})) {
333 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_FAILED);
334 m_execInterface.notifyOfExternalEvent();
339 args[0].getValue(path);
342 m_execInterface.handleCommandReturn(cmd, PLEXIL::Value(config_->
exists(path.c_str())));
343 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
344 m_execInterface.notifyOfExternalEvent();
346 warn(
"ConfigCommAdapter:config_exists:"
348 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
349 m_execInterface.notifyOfExternalEvent();
356 initFawkesConfigurationAdapter()