22 #include "skiller-rest-api.h"
24 #include <core/threading/mutex_locker.h>
25 #include <interfaces/SkillerDebugInterface.h>
26 #include <interfaces/SkillerInterface.h>
27 #include <utils/time/wait.h>
28 #include <webview/rest_api_manager.h>
55 WebRequest::METHOD_GET,
"/skills", std::bind(&SkillerRestApi::cb_list_skills,
this));
56 rest_api_->add_handler<
Skill>(WebRequest::METHOD_GET,
58 std::bind(&SkillerRestApi::cb_get_skill,
60 std::placeholders::_1));
61 rest_api_->add_handler(WebRequest::METHOD_DELETE,
63 std::bind(&SkillerRestApi::cb_stop_skill,
this, std::placeholders::_1));
66 std::bind(&SkillerRestApi::cb_exec_skill,
68 std::placeholders::_1));
85 SkillerRestApi::set_and_wait_graph(
const char *graph)
87 if (strcmp(skdb_if_->
graph_fsm(), graph) != 0) {
97 }
while (strcmp(skdb_if_->
graph_fsm(), graph) != 0 && (now - &
start) <= 5.0);
100 if (strcmp(skdb_if_->
graph_fsm(), graph) != 0) {
102 "Did not receive '%s' in time from skiller",
108 SkillerRestApi::cb_list_skills()
115 "Behavior Engine plugin is not loaded");
118 std::string prev_fsm = skdb_if_->
graph_fsm();
120 set_and_wait_graph(
"LIST");
122 std::stringstream ss(skdb_if_->
graph());
123 std::string skill_name;
124 while (std::getline(ss, skill_name,
'\n')) {
142 std::string skill_name{params.
path_arg(
"id")};
144 if (skill_name ==
"active") {
145 skill_name =
"ACTIVE";
152 "Behavior Engine plugin is not loaded");
155 set_and_wait_graph(skill_name.c_str());
163 if (skill_name ==
"ACTIVE") {
169 switch (skiller_if_->
status()) {
170 case SkillerInterface::S_RUNNING: s.
set_status(
"RUNNING");
break;
171 case SkillerInterface::S_FINAL: s.
set_status(
"FINAL");
break;
172 case SkillerInterface::S_FAILED: s.
set_status(
"FAILED");
break;
181 SkillerRestApi::cb_exec_skill(
const SkillCall &call)
190 "Behavior Engine plugin is not loaded");
206 "Failed to execute skill: %s",
219 std::unique_ptr<fawkes::WebviewRestReply>
222 std::string skill_name{params.
path_arg(
"id")};
224 if (skill_name !=
"active") {
226 "Only the 'active' skill can be stopped");
232 "Behavior Engine plugin is not loaded");
244 "Failed to execute skill: %s",
248 return std::make_unique<WebviewRestReply>(WebReply::HTTP_OK,
"OK",
"text/plain");
SkillCall representation for JSON transfer.
std::optional< std::string > skill_string() const
Get skill_string value.
SkillInfo representation for JSON transfer.
void set_name(const std::string &name)
Set name value.
static std::string api_version()
Get version of implemented API.
void set_apiVersion(const std::string &apiVersion)
Set apiVersion value.
void set_kind(const std::string &kind)
Set kind value.
Skill representation for JSON transfer.
void set_graph(const std::string &graph)
Set graph value.
void set_error(const std::string &error)
Set error value.
void set_status(const std::string &status)
Set status value.
void set_exclusive_controller(const std::string &exclusive_controller)
Set exclusive_controller value.
void set_kind(const std::string &kind)
Set kind value.
void set_skill_string(const std::string &skill_string)
Set skill-string value.
void set_apiVersion(const std::string &apiVersion)
Set apiVersion value.
static std::string api_version()
Get version of implemented API.
void set_msg_id(const int64_t &msg_id)
Set msg_id value.
void set_name(const std::string &name)
Set name value.
virtual void init()
Initialize the thread.
virtual void finalize()
Finalize the thread.
~SkillerRestApi()
Destructor.
SkillerRestApi()
Constructor.
virtual void loop()
Code to execute in the thread.
Container to return array via REST.
void push_back(M &m)
Add item at the back of the container.
BlackBoard * blackboard
This is the BlackBoard instance you can use to interact with the BlackBoard.
virtual Interface * open_for_reading(const char *interface_type, const char *identifier, const char *owner=NULL)=0
Open interface for reading.
Clock * clock
By means of this member access to the clock is given.
Base class for exceptions in Fawkes.
virtual const char * what_no_backtrace() const noexcept
Get primary string (does not implicitly print the back trace).
unsigned int msgq_enqueue(Message *message, bool proxy=false)
Enqueue message at end of queue.
void read()
Read from BlackBoard into local copy.
bool has_writer() const
Check if there is a writer for the interface.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
Logger * logger
This is the Logger member used to access the logger.
unsigned int id() const
Get message ID.
void unref()
Decrement reference count and conditionally delete this instance.
void ref()
Increment reference count.
SetGraphMessage Fawkes BlackBoard Interface Message.
SkillerDebugInterface Fawkes BlackBoard Interface.
char * graph_fsm() const
Get graph_fsm value.
char * graph() const
Get graph value.
ExecSkillMessage Fawkes BlackBoard Interface Message.
StopExecMessage Fawkes BlackBoard Interface Message.
SkillerInterface Fawkes BlackBoard Interface.
char * error() const
Get error value.
SkillStatusEnum status() const
Get status value.
uint32_t msgid() const
Get msgid value.
char * skill_string() const
Get skill_string value.
char * exclusive_controller() const
Get exclusive_controller value.
Thread class encapsulation of pthreads.
const char * name() const
Get name of thread.
void start(bool wait=true)
Call this method to start the thread.
void wait_systime()
Wait until minimum loop time has been reached in real time.
A class for handling time.
WebviewRestApiManager * webview_rest_api_manager
Webview REST API manager.
void unregister_api(WebviewRestApi *api)
Remove a request processor.
void register_api(WebviewRestApi *api)
Add a REST API.
Webview REST API component.
REST processing exception.
REST parameters to pass to handlers.
std::string path_arg(const std::string &what)
Get a path argument.
Fawkes library namespace.