HLCC Documentation 2.2.0
Loading...
Searching...
No Matches
commandsImpl.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2020-2025 European Southern Observatory (ESO)
2//
3// SPDX-License-Identifier: LGPL-3.0-only
4
13#ifndef HLCC_TELIF_ELTPK_COMMANDSIMPL_HPP
14#define HLCC_TELIF_ELTPK_COMMANDSIMPL_HPP
15
16#include <rad/smAdapter.hpp>
17
18#include <eltpk/commands.rad.hpp>
19#include "eltpk/dataContext.hpp"
20#include "eltpk/logger.hpp"
21
22
23namespace hlcc::eltpk {
24
29class CommandsImpl : public eltpkif::AsyncPointingKernelCommands {
30public:
31 explicit CommandsImpl(rad::SMAdapter& sm, DataContext& data)
32 : m_sm{sm},
33 m_data{data} {
34
35 /* GCH
36 * I do the registration of the RejectionHandler
37 * in the definition of each command handler method
38 * by adding the line:
39 * m_sm.RegisterDefaultRequestRejectHandler<EventsStd::Exit>();
40 * This has the advantage of putting the registration one by one
41 * together with the code of each handler, making it easy to keep the
42 * code aligned and to handle special cases, for example with
43 * specific handlers for commands returning something different form
44 * a string or not setting an handler for commands that are always available,
45 * in any state and will never be rejected.
46 * The drawback is a small performance penalty, since it will try to
47 * insert the registration in the std::map every time.
48 * Nothing will happen, since std::map will not insert a duplicate and will
49 * simply ignore the additional insertion.
50 * If that is a problem, we might do the registration here in the constructor,
51 * but it will be error prone when keeping the code aligned when
52 * adding or removing commands
53 */
55 }
56
57 virtual ~CommandsImpl() {
59 }
60
61 elt::mal::future<std::string> SetTargetAltAzPos(const std::shared_ptr<ccsinsif::AltAz>& axes_position) override {
63
64 LOG4CPLUS_INFO(GetLogger(), "SetTargetAltAzPos command received with Alt:"
65 << axes_position->getAlt() << " Az:" << axes_position->getAz());
66
67 // WARN do not just copy shared_ptr, as the AltAz object is valid only within the scope of this method
68 // (CII MAL idiosyncrasy). Instead we must clone the object before creating the SM event that references it.
69 std::shared_ptr<PkCommands::SetTargetAltAzPos> ev = std::make_shared<PkCommands::SetTargetAltAzPos>(axes_position->clone());
70
71 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::SetTargetAltAzPos>();
72
73 m_sm.PostEvent(ev);
74
75 elt::mal::future<std::string> ret_rad = ev->GetPayload().GetReplyFuture();
76 elt::mal::future<std::string> ret = ret_rad.then([](elt::mal::future<std::string> fut_then) {
77 return ContinuationCheckRejection(std::move(fut_then), "SetTargetAltAzPos");
78 });
79
80 return ret;
81 }
82
83
87 elt::mal::future<std::string> SetTargetRaDec(const std::shared_ptr<::ccsinsif::PresetArgs>& preset_args) override {
89
90 std::shared_ptr<ccsinsif::PresetData> preset_data = preset_args->getPreset_data();
91 LOG4CPLUS_INFO(GetLogger(), "SetTargetRaDec command received with PresetData - Ra "
92 << preset_data->getRa() << ", Dec " << preset_data->getDec()
93 << ", Epoch " << preset_data->getEpoch() << " , Parallax " << preset_data->getParallax()
94 << ", Object_name " << preset_data->getObject_name()
95 << ", Proper_motion_ra " << preset_data->getProper_motion_ra()
96 << ", Proper_motion_dec " << preset_data->getProper_motion_dec()
97 << ", Radvel " << preset_data->getRadvel()
98 << ", Rshift " << preset_data->getRshift()
99 << ", System " << preset_data->getSystem()
100 << ", Velocity_offset_ra " << preset_data->getVelocity_offset_ra()
101 << ", Velocity_offset_dec " << preset_data->getVelocity_offset_dec() );
102
103 // WARN do not just copy shared_ptr, as the PresetArgs object is valid only within the scope of this method
104 // (CII MAL idiosyncrasy). Instead we must clone the object before creating the SM event that references it.
105 std::shared_ptr<PkCommands::SetTargetRaDec> ev = std::make_shared<PkCommands::SetTargetRaDec>(preset_args->clone());
106
107 // The rejection handler will notify the client not only when the SM is outside "On::Operational",
108 // but also when the condition/guard "ActionsPreset.IsPresetAllowed" prevents the execution of
109 // "ActionsPreset.Start" because of bad PTP sync state.
110 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::SetTargetRaDec>();
111
112 m_sm.PostEvent(ev);
113
114 elt::mal::future<std::string> ret_rad = ev->GetPayload().GetReplyFuture();
115 elt::mal::future<std::string> ret = ret_rad.then([](elt::mal::future<std::string> fut_then) {
116 return ContinuationCheckRejection(std::move(fut_then), "SetTargetRaDec");
117 });
118
119 return ret;
120 }
121
122
123 // TODO: Add PresetEphem, with PresetEphemArgs.
124
125
126 ::elt::mal::future<std::string> SkyOffset(const std::shared_ptr<::ccsinsif::OffsetSkyArgs>& offset_args) override {
128
129 LOG4CPLUS_INFO(GetLogger(), "SkyOffset command received with Ra "
130 << offset_args->getRa() << ", Dec " << offset_args->getDec()
131 << ", Field_stabilization " << offset_args->getField_stabilization()
132 << ", Guide_star_param " << offset_args->getGuide_star_param() );
133
134 // WARN do not copy shared_ptr, it is valid only within a scope of this method (CII idiosyncrasy).
135 auto ev = std::make_shared<PkCommands::SkyOffset>(offset_args->clone());
136
137 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::SkyOffset>();
138
139 m_sm.PostEvent(ev);
140
141 elt::mal::future<std::string> ret_rad = ev->GetPayload().GetReplyFuture();
142 elt::mal::future<std::string> ret = ret_rad.then([](elt::mal::future<std::string> fut_then) {
143 return ContinuationCheckRejection(std::move(fut_then), "SkyOffset");
144 });
145
146 return ret;
147 }
148
149
150 virtual elt::mal::future<std::string> SetObservingWavelength(double wavelength) override {
152 LOG4CPLUS_INFO(GetLogger(), "SetObservingWavelength command received with Wavelength:" << wavelength);
153
154 auto ev = std::make_shared<PkCommands::SetObservingWavelength>(wavelength);
155 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::SetObservingWavelength>();
156
157 m_sm.PostEvent(ev);
158
159 elt::mal::future<std::string> ret_rad = ev->GetPayload().GetReplyFuture();
160 elt::mal::future<std::string> ret = ret_rad.then([](elt::mal::future<std::string> fut_then) {
161 return ContinuationCheckRejection(std::move(fut_then), "SetObservingWavelength");
162 });
163
164 return ret;
165 }
166
167 virtual elt::mal::future<std::string> StopTracking() override {
168
170 LOG4CPLUS_INFO(GetLogger(), "Received StopTracking");
171 auto ev = std::make_shared<PkCommands::StopTracking>();
172 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::StopTracking>();
173 m_sm.PostEvent(ev);
174
175 return ev->GetPayload().GetReplyFuture();
176 }
177
178private:
179 rad::SMAdapter& m_sm;
180 DataContext& m_data;
181
201 static std::string ContinuationCheckRejection(elt::mal::future<std::string> future, std::string method_name) {
202 std::string reply = future.get(); // the future is (normally) already fulfilled when called from a then-continuation.
203 if (reply.find("Request rejected in state") != std::string::npos) { // TODO C++20 use std::string::starts_with.
204 LOG4CPLUS_INFO(GetLogger(), method_name << " command rejected by eltpk: " << reply);
205 }
206 return reply;
207 };
208};
209
210} // namespace hlcc::eltpk
211
212#endif // HLCC_TELIF_ELTPK_COMMANDSIMPL_HPP
DataContext class header file.
Default logger name.
Definition commandsImpl.hpp:29
elt::mal::future< std::string > SetTargetAltAzPos(const std::shared_ptr< ccsinsif::AltAz > &axes_position) override
Definition commandsImpl.hpp:61
virtual elt::mal::future< std::string > SetObservingWavelength(double wavelength) override
Definition commandsImpl.hpp:150
virtual ~CommandsImpl()
Definition commandsImpl.hpp:57
::elt::mal::future< std::string > SkyOffset(const std::shared_ptr<::ccsinsif::OffsetSkyArgs > &offset_args) override
Definition commandsImpl.hpp:126
virtual elt::mal::future< std::string > StopTracking() override
Definition commandsImpl.hpp:167
elt::mal::future< std::string > SetTargetRaDec(const std::shared_ptr<::ccsinsif::PresetArgs > &preset_args) override
Definition commandsImpl.hpp:87
CommandsImpl(rad::SMAdapter &sm, DataContext &data)
Definition commandsImpl.hpp:31
Definition dataContext.hpp:125
Definition actionMgr.cpp:31
elt::mal::future< T > future
Definition actionsCommands.cpp:103
log4cplus::Logger & GetLogger()
Definition logger.cpp:21
ccsinsdetifllnetio::PointingKernelPositions data
Definition pkp_llnetio_subscriber.cpp:33