MotionLib  1.0.0
SamBuCa motion library
AsyncPositionController.hpp
1 /*
2 ** Copyright (C) 2023 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: 2023-06-21T14:39:33
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #ifndef ASYNCPOSITIONCONTROLLER_HPP__
25 #define ASYNCPOSITIONCONTROLLER_HPP__
26 
27 #include <algorithm>
28 #include <cmath>
29 #include <memory>
30 
31 #include <ccut/async.hpp>
32 #include <ccut/utils.hpp>
33 #include <logger/Logger.hpp>
34 #include <oatpp/core/macro/component.hpp>
35 
36 #include "Exception.hpp"
37 #include "FutureCoroutine.hpp"
38 #include "OatExt.hpp"
39 #include "SmcController.hpp"
40 #include "device/DeviceBase.hpp"
41 #include "device/DeviceStore.hpp"
42 #include "device/PositionSensor.hpp"
43 #include "platform/PlatformFactory.hpp"
44 #include "util/serialize.hpp"
45 
46 #include OATPP_CODEGEN_BEGIN(ApiController)
47 
48 // clang-format off
50 {
51 public:
52  explicit AsyncPositionController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>,
53  objectMapper)) :
54  SmcController(objectMapper)
55  {
56  addTag(*this, "position");
57  }
58 
59  void addParams(const std::shared_ptr<oatpp::web::server::api::Endpoint::Info>& info)
60  {
61  info->pathParams.add<String>("id")
62  .addExample("Resolver for axis X", String("mfe://1/resolver/X"))
63  .addExample("Resolver for axis Y", String("mfe://1/resolver/Y"))
64  .addExample("Resolver for axis Z", String("mfe://1/resolver/Z"));
65 
66  {
67  auto &param = info->queryParams.add<String>("unit")
68  .addExample("steps (default)", String("steps"))
69  .addExample("radians", String("rad"));
70 
71  param.required = false;
72  param.description = "request unit";
73  }
74  }
75 
76  ENDPOINT_INFO(getPosition)
77  {
78  info->summary = "get current position";
79  info->addResponse<Float64>(Status::CODE_200, "application/json")
80  .addExample("example", Float64(1234));
81  addParams(info);
82  }
83  ENDPOINT_ASYNC("GET", "/position/{id}/value", getPosition)
84  {
85  public:
86  ENDPOINT_ASYNC_INIT(getPosition)
87  Action act() override;
88  Action onResult(const smc::units::value_t &value);
89  };
90 
91  ENDPOINT_INFO(putCurrentPosition)
92  {
93  info->summary = "Set current position";
94  info->addResponse(Status::CODE_200, "application/json");
95  info->addConsumes<Float64>("application/json")
96  .addExample("absolute position", Float64(123));
97  addParams(info);
98  }
99  ENDPOINT_ASYNC("PUT", "/position/{id}/settings/value", putCurrentPosition)
100  {
101  public:
102  ENDPOINT_ASYNC_INIT(putCurrentPosition)
103  Action act() override;
104  Action onSetCurrentPosition(const Float64 &position);
105  };
106 
107 };
108 
109 //clang-format on
110 
111 #include OATPP_CODEGEN_END(ApiController)
112 
113 
114 #endif