25 #include "rtxiConfig.h" 30 const std::string& plugin_name)
32 std::optional<Widgets::FactoryMethods> fact_methods;
54 : event_manager(ev_manager)
57 this->m_plugin_loader = std::make_unique<DLL::Loader>();
58 this->m_driver_loader = std::make_unique<DLL::Loader>();
59 const QDir bin_dir = QCoreApplication::applicationDirPath();
60 const std::string nidaq_driver_name =
"librtxinidaqdriver.so";
61 if (bin_dir.exists(QString::fromStdString(nidaq_driver_name))) {
62 this->registerDriver(bin_dir.path().toStdString() + std::string(
"/")
69 for (
const auto& plugin_list : this->rtxi_widgets_registry) {
70 for (
const auto& plugin : plugin_list.second) {
71 this->event_manager->unregisterHandler(plugin.get());
74 const std::vector<DAQ::Device*> devices = getAllDevices();
75 std::vector<Event::Object> unregister_device_events(
77 for (
size_t i = 0; i < devices.size(); ++i) {
78 unregister_device_events[i].setParam(
79 "device", std::any(
static_cast<RT::Device*
>(devices[i])));
81 this->event_manager->postEvent(unregister_device_events);
83 void (*unloadDriversFunc)() =
nullptr;
84 for (
auto& driver : m_driver_registry) {
85 unloadDriversFunc = this->m_driver_loader->dlsym<void (*)()>(
86 driver.first.c_str(),
"deleteRTXIDAQDriver");
89 this->event_manager->unregisterHandler(
this);
94 std::vector<std::unique_ptr<Widgets::Plugin>>::iterator start_iter;
95 std::vector<std::unique_ptr<Widgets::Plugin>>::iterator end_iter;
96 bool registered =
false;
97 for (
auto& widgets_list : rtxi_widgets_registry) {
98 start_iter = widgets_list.second.begin();
99 end_iter = widgets_list.second.end();
100 registered = std::any_of(
103 [plugin](
const std::unique_ptr<Widgets::Plugin>& temp_plugin)
104 {
return plugin == temp_plugin.get(); });
113 const std::string& driver)
115 auto iter = std::find_if(m_driver_registry.begin(),
116 m_driver_registry.end(),
117 [&](
const driver_registry_entry& entry)
118 { return entry.second->getDriverName() == driver; });
119 if (iter == m_driver_registry.end()) {
122 return iter->second->getDevices();
127 std::vector<DAQ::Device*> devices;
128 std::vector<DAQ::Device*> temp_driver_devices;
129 for (
auto& entry : this->m_driver_registry) {
130 temp_driver_devices = entry.second->getDevices();
132 devices.end(), temp_driver_devices.begin(), temp_driver_devices.end());
137 template<
class... Ts>
140 using Ts::operator()...;
142 template<
class... Ts>
146 QSettings settings(settings_prefix + profile_name, QSettings::IniFormat);
147 settings.beginGroup(
"widgets");
149 int widget_count = 0;
150 for (
const auto& entry : this->rtxi_widgets_registry) {
151 widget_name = QString::fromStdString(entry.first);
152 settings.beginGroup(widget_name);
153 for (
const auto& plugin : entry.second) {
154 settings.beginGroup(QString::number(widget_count));
155 for (
const auto& param_info : plugin->getComponentParametersInfo()) {
157 QString::fromStdString(param_info.name),
158 std::visit(
overload {[](const int64_t& val) -> QString
159 { return QString::number(val); },
160 [](
const double& val) -> QString
161 { return QString::number(val); },
162 [](
const uint64_t& val) -> QString
163 { return QString::number(val); },
164 [](
const std::string& val) -> QString
165 { return QString::fromStdString(val); },
167 return QString::number(
168 static_cast<int8_t>(val));
182 QSettings settings(profile_name, QSettings::IniFormat);
185 Widgets::Plugin* Workspace::Manager::loadCorePlugin(
const std::string& library)
188 std::optional<Widgets::FactoryMethods> fact_methods =
190 if (!fact_methods.has_value()) {
193 std::unique_ptr<Widgets::Plugin> plugin;
194 this->registerFactories(library, *fact_methods);
195 plugin = this->rtxi_factories_registry[library].createPlugin(event_manager);
196 plugin_ptr = this->registerWidget(std::move(plugin));
203 const std::string& library_loc = library;
206 if (this->rtxi_factories_registry.find(library_loc)
207 != this->rtxi_factories_registry.end())
209 std::unique_ptr<Widgets::Plugin> plugin =
210 this->rtxi_factories_registry[library_loc].createPlugin(
211 this->event_manager);
212 plugin_ptr = this->registerWidget(std::move(plugin));
214 this->rtxi_factories_registry[library_loc].createComponent(plugin_ptr));
219 plugin_ptr = this->loadCorePlugin(library_loc);
220 if (plugin_ptr !=
nullptr) {
224 if (this->m_plugin_loader->load(library_loc.c_str()) != 0) {
225 ERROR_MSG(
"Plugin::load : failed to load {}", library_loc.c_str());
229 auto gen_fact_methods =
231 library_loc.c_str(),
"getFactories");
233 if (gen_fact_methods ==
nullptr) {
234 ERROR_MSG(
"Plugin::load : failed to retrieve getFactories symbol");
237 this->m_plugin_loader->unload(library_loc.c_str());
242 this->rtxi_factories_registry[library] = *fact_methods;
243 std::unique_ptr<Widgets::Plugin> plugin =
245 plugin->setLibrary(library);
246 if (plugin ==
nullptr) {
247 ERROR_MSG(
"Plugin::load : failed to create plugin from library {} ",
249 this->m_plugin_loader->unload(library_loc.c_str());
252 std::unique_ptr<Widgets::Component> component =
254 plugin->attachComponent(std::move(component));
263 plugin_ptr = this->registerWidget(std::move(plugin));
270 const std::string library = plugin->
getName();
271 this->unregisterWidget(plugin);
272 if (this->rtxi_widgets_registry[library].empty()) {
273 this->m_plugin_loader->unload(library.c_str());
274 this->unregisterFactories(library);
278 void Workspace::Manager::registerDriver(
const std::string& driver_location)
280 auto iter = std::find_if(m_driver_registry.begin(),
281 m_driver_registry.end(),
282 [&](
const driver_registry_entry& entry)
283 { return entry.first == driver_location; });
284 if (iter != this->m_driver_registry.end()) {
287 if (this->m_driver_loader->load(driver_location.c_str()) < 0) {
290 auto getDriver = this->m_driver_loader->dlsym<
DAQ::Driver* (*)()>(
291 driver_location.c_str(),
"getRTXIDAQDriver");
292 if (getDriver ==
nullptr) {
294 "Workspace::Manager::registerDriver : Unable to load dynamic library " 300 if (driver ==
nullptr) {
302 "Workspace::Manager::registerDriver : Unable to load driver from " 307 this->m_driver_registry.emplace_back(driver_location, driver);
308 std::vector<Event::Object> plug_device_events;
311 plug_device_events.back().setParam(
312 "device", std::any(
static_cast<RT::Device*
>(device)));
314 this->event_manager->postEvent(plug_device_events);
317 void Workspace::Manager::unregisterDriver(
const std::string& driver_location)
319 auto iter = std::find_if(m_driver_registry.begin(),
320 m_driver_registry.end(),
321 [&](
const driver_registry_entry& entry)
322 { return entry.first == driver_location; });
324 if (iter == this->m_driver_registry.end()) {
327 std::vector<Event::Object> unplug_device_events;
328 for (
auto* device : iter->second->getDevices()) {
330 unplug_device_events.back().setParam(
331 "device", std::any(
static_cast<RT::Device*
>(device)));
333 this->event_manager->postEvent(unplug_device_events);
334 this->m_driver_registry.erase(iter);
335 this->m_driver_loader->unload(driver_location.c_str());
339 std::unique_ptr<Widgets::Plugin> widget)
341 const std::unique_lock<std::mutex> lk(this->m_widgets_mut);
342 const std::string mod_name = widget->getName();
343 this->rtxi_widgets_registry[mod_name].push_back(std::move(widget));
344 return this->rtxi_widgets_registry[mod_name].back().get();
349 if (plugin ==
nullptr) {
352 const std::unique_lock<std::mutex> lk(this->m_widgets_mut);
353 const std::string plugin_name = plugin->
getName();
354 auto start_iter = this->rtxi_widgets_registry[plugin_name].begin();
355 auto end_iter = this->rtxi_widgets_registry[plugin_name].end();
357 std::find_if(start_iter,
359 [plugin](
const std::unique_ptr<Widgets::Plugin>& widget)
360 {
return plugin == widget.get(); });
361 if (loc == end_iter) {
365 this->rtxi_widgets_registry[plugin_name].erase(loc);
368 void Workspace::Manager::registerFactories(
const std::string& widget_name,
371 this->rtxi_factories_registry[widget_name] = fact;
374 void Workspace::Manager::unregisterFactories(
const std::string& widget_name)
376 if (this->rtxi_factories_registry.find(widget_name)
377 != this->rtxi_factories_registry.end())
379 this->rtxi_factories_registry.erase(widget_name);
385 std::string plugin_name;
390 std::any_cast<Widgets::Plugin*>(event->
getParam(
"pluginPointer"));
391 this->unloadPlugin(plugin_ptr);
394 plugin_name = std::any_cast<std::string>(event->
getParam(
"pluginName"));
395 plugin_ptr = this->loadPlugin(plugin_name);
396 if (plugin_ptr !=
nullptr) {
397 event->setParam(
"status", std::any(std::string(
"success")));
400 std::any(this->rtxi_factories_registry[plugin_name].createPanel));
401 event->setParam(
"pluginPointer", std::any(plugin_ptr));
403 event->setParam(
"status", std::any(std::string(
"failure")));
407 event->setParam(
"devices", std::any(this->getAllDevices()));
virtual std::vector< DAQ::Device * > getDevices()=0
void registerHandler(Handler *handler)
Event::Type getType() const
std::any getParam(const std::string ¶m_name) const
void saveSettings(const QString &profile_name)
void loadSettings(const QString &profile_name)
void receiveEvent(Event::Object *event) override
bool isRegistered(const Widgets::Plugin *plugin)
Widgets::Plugin * loadPlugin(const std::string &library)
std::vector< DAQ::Device * > getAllDevices()
Manager(const Manager &)=delete
std::vector< DAQ::Device * > getDevices(const std::string &driver)
void unloadPlugin(Widgets::Plugin *plugin)
void ERROR_MSG(const std::string &errmsg, Args... args)
constexpr std::string_view MODULE_NAME
Widgets::FactoryMethods getFactories()
constexpr std::string_view MODULE_NAME
Widgets::FactoryMethods getFactories()
constexpr std::string_view MODULE_NAME
Widgets::FactoryMethods getFactories()
Widgets::FactoryMethods getFactories()
constexpr std::string_view MODULE_NAME
constexpr std::string_view MODULE_NAME
Widgets::FactoryMethods getFactories()
Widgets::FactoryMethods getFactories()
constexpr std::string_view MODULE_NAME
std::optional< Widgets::FactoryMethods > get_core_plugin_factory(const std::string &plugin_name)
overload(Ts...) -> overload< Ts... >