MotionLib  1.0.0
SamBuCa motion library
GrblPlatformDevice.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-16T17:17:13
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #include "GrblPlatformDevice.hpp"
25 
26 #include <ccut/async.hpp>
27 
28 #include "GrblPlatform.hpp"
29 #include "util/grbl/GrblParser.hpp"
30 
31 using namespace logger;
32 using namespace smc;
33 using namespace smc::internal;
34 
36 
37 GrblPlatformDevice::GrblPlatformDevice(
38  const std::string &uid,
39  const std::shared_ptr<GrblPlatform> &grbl) :
40  PlatformDevice{uid},
41  m_grbl{grbl}
42 {}
43 
44 std::future<std::set<std::string>> GrblPlatformDevice::listConfig() const
45 {
46  try
47  {
48  std::shared_ptr<GrblPlatform> grbl(m_grbl.lock());
49  if (!grbl)
50  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
51 
52  return grbl->run<std::set<std::string>>([](ImmediateCmd &cmd,
53  GrblPlatform::Context &ctx) {
54  cmd.prom->get<std::set<std::string>>().set_value(ctx.listConfig());
55  });
56  }
57  catch (const Exception &ex)
58  {
59  return ccut::make_future_error<std::set<std::string>>(ex);
60  }
61 }
62 
63 std::future<std::string> GrblPlatformDevice::getConfig(
64  const std::string &name) const
65 {
66  try
67  {
68  std::shared_ptr<GrblPlatform> grbl(m_grbl.lock());
69  if (!grbl)
70  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
71 
72  return grbl->run<std::string>(
73  [name](ImmediateCmd &cmd, GrblPlatform::Context &ctx) {
74  std::string ret;
75  GrblPlatform::LocalSettingMap::const_iterator itls =
76  ctx.localSetting.find(name);
77  if (itls != ctx.localSetting.end())
78  ret = itls->second.get(ctx);
79  else
80  ret = ctx._sendGetSetting(ctx.findSetting(name).id);
81  cmd.prom->get<std::string>().set_value(ret);
82  });
83  }
84  catch (const Exception &ex)
85  {
86  return ccut::make_future_error<std::string>(ex);
87  }
88 }
89 
90 std::future<void> GrblPlatformDevice::setConfig(const std::string &name,
91  const std::string &value) const
92 {
93  try
94  {
95  std::shared_ptr<GrblPlatform> grbl(m_grbl.lock());
96  if (!grbl)
97  throw Exception(ErrorCode::Canceled, "platform stopped (deleted)");
98 
99  return grbl->run<void>(
100  [name, value](ImmediateCmd &cmd, GrblPlatform::Context &ctx) {
101  ctx.setSetting(name, value);
102  cmd.prom->get<void>().set_value();
103  });
104  }
105  catch (const Exception &ex)
106  {
107  return ccut::make_future_error(ex);
108  }
109 }
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
main motion-lib namespace
Definition: Client.cpp:30