MotionLib  1.0.0
SamBuCa motion library
MotionController.cpp
1 /*
2  * Copyright (C) 2021 CERN
3  *
4  * This software is provided 'as-is', without any express or implied
5  * warranty. In no event will the authors be held liable for any damages
6  * arising from the use of this software.
7  *
8  * Permission is granted to anyone to use this software for any purpose,
9  * including commercial applications, and to alter it and redistribute it
10  * freely, subject to the following restrictions:
11  *
12  * 1. The origin of this software must not be misrepresented; you must not
13  * claim that you wrote the original software. If you use this software
14  * in a product, an acknowledgment in the product documentation would be
15  * appreciated but is not required.
16  * 2. Altered source versions must be plainly marked as such, and must not be
17  * misrepresented as being the original software.
18  * 3. This notice may not be removed or altered from any source distribution.
19  *
20  * Created on: 2021-10-08
21  * Author: Michal Mysior <mmysior> <michal.mysior@cern.ch>
22  *
23  */
24 
25 #include "MotionController.hpp"
26 
27 #include <ccut/utils.hpp>
28 #include <ccut/yml.hpp>
29 #include <logger/Logger.hpp>
30 
31 #include "Exception.hpp"
32 #include "MotionLibConfig.hpp"
33 #include "device/Axis.hpp"
34 #include "device/DeviceStore.hpp"
35 #include "local-config.h"
36 #include "platform/NotificationWorker.hpp"
37 #include "platform/PlatformFactory.hpp"
38 
39 using namespace logger;
40 namespace yml = ccut::yml;
41 namespace smc {
42 
43 static const std::string s_loggerCat{"smc:controller"};
44 
45 using namespace internal;
46 
47 MotionController::MotionController() :
48  m_platformFactory{std::make_shared<PlatformFactory>()},
49  m_deviceStore{std::make_shared<DeviceStore>()},
50  m_notificationWorker{std::make_shared<NotificationWorker>(m_deviceStore)}
51 {
52  ccut::ccutVersion(true, true);
53  logger::info(s_loggerCat) << "motion-lib: " << MOTIONLIB_VERSION << "-"
54  << MOTIONLIB_GIT_SHORT << MOTIONLIB_GIT_DIRTY;
55  m_notificationWorker->start();
56 }
57 
58 MotionController::~MotionController()
59 {
60  m_notificationWorker->stop();
61  m_deviceStore.reset();
62  m_platformFactory.reset();
63 }
64 
65 size_t MotionController::load(const std::string &config)
66 {
67  try
68  {
69  yml::Parser parser;
70  yml::Tree tree = parser.parse_in_arena("<yml>", yml::to_csubstr(config));
71  yml::NodeRef node = yml::get(tree.rootref(), "platforms");
72  if (node.valid())
73  m_platformFactory->buildPlatforms(node, &parser);
74 
75  node = yml::get(tree.rootref(), "alias");
76  if (node.valid())
77  m_deviceStore->loadAlias(node, &parser);
78  }
79  catch (std::exception &ex)
80  {
81  error(s_loggerCat) << "failed to parse config: " << ex.what();
82  }
83 
84  std::size_t newDeviceCount = 0;
85  for (const PlatformBase::Shared &platform :
86  m_platformFactory->getPlatforms())
87  {
88  platform->setNotificationWorker(m_notificationWorker);
89  newDeviceCount += m_deviceStore->probeDevices(platform);
90  }
91 
92  return newDeviceCount;
93 }
94 
95 DeviceBase::Shared MotionController::getDevice(const std::string &address)
96 {
97  return m_deviceStore->getDevice(address);
98 }
99 
101 {
102  return m_deviceStore->getDeviceCount();
103 }
104 
105 std::set<std::string> MotionController::getDeviceList() const
106 {
107  return m_deviceStore->getDeviceList();
108 }
109 
110 std::set<std::string> MotionController::getDeviceList(DeviceType type) const
111 {
112  return m_deviceStore->getDeviceList(type);
113 }
114 
115 std::set<std::string> MotionController::getAliasList() const
116 {
117  return m_deviceStore->getAliasList();
118 }
119 
120 DeviceId MotionController::getAlias(const std::string &alias) const
121 {
122  return m_deviceStore->getAlias(alias);
123 }
124 
125 bool MotionController::addAlias(const std::string &alias,
126  const DeviceId &deviceId,
127  bool override)
128 {
129  return m_deviceStore->addAlias(alias, deviceId, override);
130 }
131 
132 void MotionController::removeAlias(const std::string &alias)
133 {
134  m_deviceStore->removeAlias(alias);
135 }
136 
137 } /* namespace smc */
DeviceId getAlias(const std::string &alias) const
Get the given alias.
std::size_t load(const std::string &config)
Construct platforms and devices based on given configuration string.
std::set< DeviceId > getDeviceList() const
retrieve complete list of devices
std::size_t getDeviceCount() const
Get the number of registered devices.
bool addAlias(const std::string &alias, const DeviceId &deviceId, bool override=true)
add an alias
void removeAlias(const std::string &alias)
remove an alias
std::set< std::string > getAliasList() const
Get current Alias list.
DeviceBase::Shared getDevice(const DeviceId &address)
get a generic device according to its address
DeviceType
List of possible device types supported.
Definition: DeviceBase.hpp:52
main motion-lib namespace
Definition: Client.cpp:30