MotionLib  1.0.0
SamBuCa motion library
MGrblPlatformDevice.cpp
1 /*
2 ** Copyright (C) 2025 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: 2025-04-17T14:14:57
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "MGrblPlatformDevice.hpp"
25 
26 #include <ccut/async.hpp>
27 #include <ccut/utils.hpp>
28 
29 #include "MGrblPlatform.hpp"
30 
31 using namespace logger;
32 using namespace smc;
33 using namespace smc::internal;
35 
36 const std::set<std::string> MGrblPlatformDevice::s_ctrlSettings{
37  "layout", "grblCount", "trigArm"};
38 
39 MGrblPlatformDevice::MGrblPlatformDevice(
40  const std::string &uid,
41  const std::shared_ptr<MGrblPlatform> &mgrbl,
42  const std::shared_ptr<GrblPlatform> &grbl) :
43  GrblPlatformDevice(uid, grbl),
44  m_mgrbl{mgrbl}
45 {}
46 
47 std::future<std::set<std::string>> MGrblPlatformDevice::listConfig() const
48 {
49  try
50  {
51  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
52  if (!mgrbl)
53  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
54 
55  return mgrbl->run<std::set<std::string>>(
56  [mgrbl](ImmediateCmd &cmd, MGrblPlatform::Context &ctx) {
57  std::set<std::string> ret;
58  mgrbl->runOnInstance(
59  ctx,
60  [&ret](GrblPlatform::Shared &, GrblPlatform::Context &ctx) {
61  ret = ctx.listConfig();
62  },
63  true);
64  ret.insert(s_ctrlSettings.begin(), s_ctrlSettings.end());
65  cmd.prom->get<std::set<std::string>>().set_value(ret);
66  });
67  }
68  catch (const Exception &ex)
69  {
70  return ccut::make_future_error<std::set<std::string>>(ex);
71  }
72 }
73 
74 std::future<std::string> MGrblPlatformDevice::getConfig(
75  const std::string &name) const
76 {
77  try
78  {
79  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
80  if (!mgrbl)
81  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
82 
83  if (s_ctrlSettings.count(name))
84  {
85  return mgrbl->run<std::string>(
86  [mgrbl, name](ImmediateCmd &cmd, MGrblPlatform::Context &ctx) {
87  std::string ret;
88  const std::string settingCmd = mgrbl::ControlSymbol +
89  ("$" + name);
90  grbl::ErrorCode rc = ctx.device->send(
91  settingCmd,
92  [&settingCmd, &ret](const std::string &line) {
93  if (ccut::startsWith(line, settingCmd))
94  {
95  ret = line.substr(settingCmd.size() + 1);
96  return true;
97  }
98  return false;
99  });
100  ctx.checkReply(rc);
101  cmd.prom->get<std::string>().set_value(ret);
102  });
103  }
104  else
105  {
106  return GrblPlatformDevice::getConfig(name);
107  }
108  }
109  catch (const Exception &ex)
110  {
111  return ccut::make_future_error<std::string>(ex);
112  }
113 }
114 
115 std::future<void> MGrblPlatformDevice::setConfig(const std::string &name,
116  const std::string &value) const
117 {
118  try
119  {
120  std::shared_ptr<MGrblPlatform> mgrbl(m_mgrbl.lock());
121  if (!mgrbl)
122  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
123 
124  if (s_ctrlSettings.count(name))
125  {
126  return mgrbl->run<void>(
127  [mgrbl, name, value](ImmediateCmd &cmd,
128  MGrblPlatform::Context &ctx) {
129  grbl::ErrorCode rc = ctx.device->send(
130  mgrbl::ControlSymbol + ("$" + name) + "=" + value);
131  ctx.checkReply(rc);
132  mgrbl->loadLayout(ctx);
133  cmd.prom->get<void>().set_value();
134  });
135  }
136  else
137  {
138  return GrblPlatformDevice::setConfig(name, value);
139  }
140  }
141  catch (const Exception &ex)
142  {
143  return ccut::make_future_error<void>(ex);
144  }
145 }
Exception thrown by MotionController in case of issues with command.
Definition: Exception.hpp:61
virtual std::future< void > setConfig(const std::string &name, const std::string &value) const override
set device configuration value
virtual std::future< std::string > getConfig(const std::string &name) const override
get device configuration value
std::future< void > setConfig(const std::string &name, const std::string &value) const override
set device configuration value
std::future< std::string > getConfig(const std::string &name) const override
get device configuration value
main motion-lib namespace
Definition: Client.cpp:30
std::set< std::string > listConfig()
list config values