MotionLib  1.0.0
SamBuCa motion library
AsyncAxisPositionMonitorController.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-07-18T11:29:48
21 ** Author: Sylvain Fargier <sylvain.fargier@cern.ch>
22 */
23 
24 #ifndef ASYNCAXISPOSITIONMONITORCONTROLLER_HPP__
25 #define ASYNCAXISPOSITIONMONITORCONTROLLER_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/AxisPositionMonitor.hpp"
41 #include "device/DeviceBase.hpp"
42 #include "device/DeviceStore.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 AsyncAxisPositionMonitorController(OATPP_COMPONENT(std::shared_ptr<ObjectMapper>,
53  objectMapper)) :
54  SmcController(objectMapper)
55  {
56  addTag(*this, "monitor");
57  }
58 
59  void addParams(const std::shared_ptr<oatpp::web::server::api::Endpoint::Info>& info, bool units = false)
60  {
61  info->pathParams.add<String>("id")
62  .addExample("Resolver monitor for axis X", String("mfe://1/resolver/X/monitor"))
63  .addExample("Resolver monitor for axis Y", String("mfe://1/resolver/Y/monitor"))
64  .addExample("Resolver monitor for axis Z", String("mfe://1/resolver/Z/monitor"));
65 
66  if (units)
67  {
68  auto &param = info->queryParams.add<String>("unit")
69  .addExample("steps (default)", String("steps"))
70  .addExample("radians", String("rad"));
71 
72  param.required = false;
73  param.description = "request unit";
74  }
75  }
76 
77  ENDPOINT_INFO(getDifference)
78  {
79  info->summary = "get position difference between axis and positionSensor";
80  info->addResponse<Float64>(Status::CODE_200, "application/json")
81  .addExample("example", Float64(1234));
82  addParams(info, true);
83  }
84  ENDPOINT_ASYNC("GET", "/monitor/{id}/diff", getDifference)
85  {
86  public:
87  ENDPOINT_ASYNC_INIT(getDifference)
88  Action act() override;
89  Action onResult(const smc::units::value_t &value);
90  };
91 
92  ENDPOINT_INFO(getMaxDifference)
93  {
94  info->summary = "get max position difference measured since monitor was enabled";
95  info->addResponse(Status::CODE_200, "application/json")
96  .addExample("example", Float64(123));
97  addParams(info, true);
98  }
99  ENDPOINT_ASYNC("GET", "/monitor/{id}/diff/max", getMaxDifference)
100  {
101  public:
102  ENDPOINT_ASYNC_INIT(getMaxDifference)
103  Action act() override;
104  Action onResult(const smc::units::value_t &value);
105  };
106 
107  ENDPOINT_INFO(getState)
108  {
109  info->summary = "get current monitor state";
110  info->addResponse(Status::CODE_200, "application/json")
111  .addExample("example", String("enabled"))
112  .addExample("example2", String("triggered"));
113  addParams(info);
114  }
115  ENDPOINT_ASYNC("GET", "/monitor/{id}/state", getState)
116  {
117  public:
118  ENDPOINT_ASYNC_INIT(getState)
119  Action act() override;
120  Action onResult(const smc::AxisPositionMonitor::State &value);
121  };
122 
123  ENDPOINT_INFO(getEnabled)
124  {
125  info->summary = "check if monitor is enabled";
126  info->addResponse(Status::CODE_200, "application/json")
127  .addExample("example", Boolean(true));
128  addParams(info);
129  }
130  ENDPOINT_ASYNC("GET", "/monitor/{id}/enabled", getEnabled)
131  {
132  public:
133  ENDPOINT_ASYNC_INIT(getEnabled)
134  Action act() override;
135  Action onResult(const smc::AxisPositionMonitor::State &value);
136  };
137 
138  ENDPOINT_INFO(putEnabled)
139  {
140  info->summary = "set current monitor state";
141  info->addResponse(Status::CODE_200, "application/json");
142  info->addConsumes<Float64>("application/json")
143  .addExample("example", Boolean(true));
144  addParams(info);
145  }
146  ENDPOINT_ASYNC("PUT", "/monitor/{id}/enabled", putEnabled)
147  {
148  public:
149  ENDPOINT_ASYNC_INIT(putEnabled)
150  Action act() override;
151  Action onBody(const Boolean &value);
152  };
153 };
154 
155 //clang-format on
156 
157 #include OATPP_CODEGEN_END(ApiController)
158 
159 #endif