22 #include "remote_adapter.h"
26 #include <blackboard/remote.h>
27 #include <config/netconf.h>
28 #include <core/threading/mutex_locker.h>
29 #include <logging/console.h>
30 #include <navgraph/navgraph.h>
31 #include <navgraph/yaml_navgraph.h>
32 #include <netcomm/fawkes/client.h>
33 #include <utils/system/fam.h>
34 #include <utils/time/clock.h>
36 #include <AdapterConfiguration.hh>
37 #include <AdapterExecInterface.hh>
38 #include <AdapterFactory.hh>
41 #include <InterfaceManager.hh>
42 #include <boost/filesystem.hpp>
45 namespace fs = boost::filesystem;
56 : InterfaceAdapter(execInterface)
66 pugi::xml_node
const xml)
67 : InterfaceAdapter(execInterface, xml)
84 bool cfg_navgraph_allow_multi;
86 clock_ = Clock::instance();
87 logger_ = std::make_unique<fawkes::ConsoleLogger>();
89 cfg_host = get_xml_config_value(getXml(),
"host");
90 cfg_port = std::stoi(get_xml_config_value(getXml(),
"port"));
92 cfg_navgraph_filename_ = get_xml_config_value(getXml(),
"navgraph_filename");
93 cfg_navgraph_allow_multi = get_xml_config_value(getXml(),
"navgraph_allow_multi") ==
"true";
95 client_ = std::make_unique<fawkes::FawkesNetworkClient>(cfg_host.c_str(), cfg_port);
98 logger_->log_info(
"FawkesRemote",
"Connecting to Fawkes at %s:%i", cfg_host.c_str(), cfg_port);
101 warn(
"FawkesRemoteAdapter:initialize: failed to connect to Fawkes: " << e.
what_no_backtrace());
105 logger_->log_info(
"FawkesRemote",
"Mirroring configuration");
106 config_ = std::make_unique<fawkes::NetworkConfiguration>(client_.get());
107 config_->set_mirror_mode(
true);
109 logger_->log_info(
"FawkesRemote",
"Accessing blackboard");
110 blackboard_ = std::make_unique<fawkes::RemoteBlackBoard>(client_.get());
112 if (cfg_navgraph_filename_[0] !=
'/') {
113 cfg_navgraph_filename_ = std::string(CONFDIR) +
"/" + cfg_navgraph_filename_;
115 fs::path p(cfg_navgraph_filename_);
117 cfg_navgraph_filename_ = p.string();
118 logger_->log_info(
"FawkesRemote",
"Loading navgraph file %s", cfg_navgraph_filename_.c_str());
121 fs::create_directories(p.parent_path());
122 navgraph_fam_ = std::make_unique<fawkes::FileAlterationMonitor>();
123 navgraph_fam_->add_filter((std::string(
"^") + p.filename().string() +
"$").c_str());
124 navgraph_fam_->watch_dir(p.parent_path().string().c_str());
125 navgraph_fam_->add_listener(
this);
127 navgraph_fam_thread_ = std::thread([
this]() {
129 std::unique_lock<std::mutex> lock(this->navgraph_fam_mutex_);
130 if (!this->navgraph_fam_)
133 using namespace std::chrono_literals;
134 std::this_thread::sleep_for(1s);
138 m_execInterface.setProperty(
"::Fawkes::Config", config_.get());
139 m_execInterface.setProperty(
"::Fawkes::Clock", clock_);
140 m_execInterface.setProperty(
"::Fawkes::Logger", logger_.get());
141 m_execInterface.setProperty(
"::Fawkes::BlackBoard", blackboard_.get());
142 m_execInterface.setProperty(
"::Fawkes::NavGraph", &navgraph_);
157 warn(
"FawkesRemoteAdapter:fam_event: navgraph file deleted, clearing");
163 warn(
"FawkesRemoteAdapter:fam_event: NavGraph changed on disk, reloading");
173 **navgraph_ = **new_graph;
178 warn(
"FawkesRemoteAdapter:fam_event: loading new graph failed: " << e.
what_no_backtrace());
180 }
catch (std::runtime_error &e) {
181 warn(
"FawkesRemoteAdapter:fam_event: loading new graph failed: " << e.
what());
221 client_->disconnect();
223 std::unique_lock<std::mutex> lock(navgraph_fam_mutex_);
224 navgraph_fam_.reset();
225 navgraph_fam_thread_.join();
261 initFawkesRemoteAdapter()
Interface adapter to provide logging facilities.
virtual bool reset()
Reset adapter.
virtual bool shutdown()
Shut adapter down.
void executeCommand(PLEXIL::Command *cmd)
Perform given command.
virtual void fam_event(const char *filename, unsigned int mask)
Event has been raised.
virtual bool initialize()
Initialize adapter.
void invokeAbort(PLEXIL::Command *cmd)
Abort currently running execution.
virtual ~FawkesRemotePlexilAdapter()
Destructor.
virtual bool stop()
Stop adapter.
virtual bool start()
Start adapter.
FawkesRemotePlexilAdapter(PLEXIL::AdapterExecInterface &execInterface)
Constructor.
static void finalize()
Finalize.
Base class for exceptions in Fawkes.
virtual const char * what() const noexcept
Get primary string.
virtual const char * what_no_backtrace() const noexcept
Get primary string (does not implicitly print the back trace).
static const unsigned int FAM_MODIFY
File was modified.
static const unsigned int FAM_DELETE
Subfile was deleted.
static const unsigned int FAM_IGNORED
File was ignored.
void lock() const
Lock access to the encapsulated object.
void unlock() const
Unlock object mutex.
void clear()
Remove all nodes and edges from navgraph.
void set_notifications_enabled(bool enabled)
Enable or disable notifications.
void notify_of_change() noexcept
Notify all listeners of a change.
Fawkes library namespace.
NavGraph * load_yaml_navgraph(std::string filename, bool allow_multi_graph)
Load topological map graph stored in RCSoft format.