25 #include "PlatformFactory.hpp"
29 #include <ccut/Exception.hpp>
30 #include <logger/Logger.hpp>
32 #include "Exception.hpp"
33 #include "MotionLibConfig.hpp"
34 #include "platform/PlatformBase.hpp"
35 #include "specialized/GrblPlatform/GrblPlatform.hpp"
36 #include "specialized/MGrblPlatform/MGrblPlatform.hpp"
37 #include "specialized/MockPlatform/MockPlatform.hpp"
39 #ifdef PLATFORM_SAMBUCA
40 #include "specialized/MFEPlatform/MFEPlatform.hpp"
43 using namespace logger;
44 namespace yml = ccut::yml;
49 static const std::string s_loggerCat{
"smc:platform"};
50 static std::map<std::string,
51 std::function<PlatformBase::Shared(yml::NodeRef node)>>
52 s_registry{{
"MockPlatform", MockPlatform::create},
53 {
"GrblPlatform", GrblPlatform::create},
54 {
"MGrblPlatform", MGrblPlatform::create},
55 #ifdef PLATFORM_SAMBUCA
56 {
"MFEPlatform", MFEPlatform::create}
60 PlatformFactory::~PlatformFactory()
62 for (PlatformBase::Shared p : m_platforms)
66 std::size_t PlatformFactory::buildPlatforms(
const std::string &platformDesc)
71 yml::Tree tree = parser.parse_in_arena(
"<yml>",
72 yml::to_csubstr(platformDesc));
73 yml::NodeRef platformsNode = yml::get(tree.rootref(),
"platforms");
74 return buildPlatforms(platformsNode, &parser);
76 catch (std::exception &ex)
78 error(s_loggerCat) <<
"failed to parse platform: " << ex.what();
83 std::size_t PlatformFactory::buildPlatforms(
const yml::NodeRef &platformsNode,
84 const yml::Parser *parser)
86 std::size_t newPlatformCount = 0;
89 if (!platformsNode.valid() || !platformsNode.is_seq())
90 throw Exception(ErrorCode::InvalidArguments,
91 "platforms description must be an array");
93 for (yml::NodeRef node : platformsNode)
96 yml::get(node,
"type") >> yml::default_to(type, std::string());
100 throw yml::make_error(*parser, node,
"type not found");
102 throw ccut::Exception(ccut::ErrorCode::InvalidArguments,
106 decltype(s_registry)::const_iterator it = s_registry.find(type);
107 if (it == s_registry.end())
108 error(s_loggerCat) <<
"unknown platform: " << type;
113 PlatformBase::Shared platform{(it->second)(node)};
115 const std::lock_guard<std::mutex> lock{m_platformsMutex};
116 m_platforms.emplace_back(platform);
120 catch (std::exception &ex)
123 <<
"failed to create platform: " << ex.what();
128 catch (std::exception &ex)
130 error(s_loggerCat) <<
"failed to parse platform: " << ex.what();
132 info(s_loggerCat) << newPlatformCount <<
" platforms created";
133 return newPlatformCount;
136 std::list<PlatformBase::Shared> PlatformFactory::getPlatforms()
138 const std::lock_guard<std::mutex> lock{m_platformsMutex};
139 std::list<PlatformBase::Shared> newList{m_platforms};
Exception thrown by MotionController in case of issues with command.
main motion-lib namespace