HLCC Documentation 3.0.1
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 */
54 RAD_TRACE(GetLogger());
55 }
56
57 virtual ~CommandsImpl() {
58 RAD_TRACE(GetLogger());
59 }
60
61 elt::mal::future<std::string> SetTargetAltAzPos(const std::shared_ptr<ccscommonif::AltAz>& axes_position) override {
62 RAD_TRACE(GetLogger());
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 {
88 RAD_TRACE(GetLogger());
89
90 std::shared_ptr<ccsinsif::PresetData> preset_data = preset_args->getPreset_data();
91
92 LOG4CPLUS_INFO_FMT(GetLogger(), "SetTargetRaDec command received with PresetData - Ra %1.15f, "
93 "Dec %1.15f, Epoch %s, Parallax %1.15f, Object_name %s, "
94 "Proper_motion_ra %1.15f, Proper_motion_dec %1.15f, Radvel %1.15f, "
95 "Rshift %1.15f, Velocity_offset_ra %1.15f, "
96 "Velocity_offset_dec %1.15f, Wavelength %1.15f, Time_ephem_utc %1.15f",
97 preset_data->getTarget_data()->getRa(), preset_data->getTarget_data()->getDec(), preset_data->getTarget_data()->getEpoch().c_str(),
98 preset_data->getTarget_data()->getParallax(), preset_data->getTarget_data()->getObject_name().c_str(),
99 preset_data->getTarget_data()->getProper_motion_ra(), preset_data->getTarget_data()->getProper_motion_dec(),
100 preset_data->getTarget_data()->getRadvel(), preset_data->getTarget_data()->getRshift(),
101 preset_data->getTarget_data()->getVelocity_offset_ra(), preset_data->getTarget_data()->getVelocity_offset_dec(),
102 preset_data->getTarget_data()->getWavelength(), preset_data->getTarget_data()->getTime_ephem_utc());
103
104 // WARN do not just copy shared_ptr, as the PresetArgs object is valid only within the scope of this method
105 // (CII MAL idiosyncrasy). Instead we must clone the object before creating the SM event that references it.
106 std::shared_ptr<PkCommands::SetTargetRaDec> ev = std::make_shared<PkCommands::SetTargetRaDec>(preset_args->clone());
107
108 // The rejection handler will notify the client not only when the SM is outside "On::Operational",
109 // but also when the condition/guard "ActionsPreset.IsPresetAllowed" prevents the execution of
110 // "ActionsPreset.Start" because of bad PTP sync state.
111 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::SetTargetRaDec>();
112
113 m_sm.PostEvent(ev);
114
115 elt::mal::future<std::string> ret_rad = ev->GetPayload().GetReplyFuture();
116 elt::mal::future<std::string> ret = ret_rad.then([](elt::mal::future<std::string> fut_then) {
117 return ContinuationCheckRejection(std::move(fut_then), "SetTargetRaDec");
118 });
119
120 return ret;
121 }
122
123 ::elt::mal::future<std::string> UpdateTarget(const std::shared_ptr<::ccsinsif::PresetArgs>& preset_args) override {
124 RAD_TRACE(GetLogger());
125
126 std::shared_ptr<ccsinsif::PresetData> preset_data = preset_args->getPreset_data();
127
128 LOG4CPLUS_INFO_FMT(GetLogger(), "UpdateTarget command received with PresetData - Ra %1.15f, "
129 "Dec %1.15f, Epoch %s, Parallax %1.15f, Object_name %s, "
130 "Proper_motion_ra %1.15f, Proper_motion_dec %1.15f, Radvel %1.15f, "
131 "Rshift %1.15f, Velocity_offset_ra %1.15f, "
132 "Velocity_offset_dec %1.15f, Wavelength %1.15f, Time_ephem_utc %1.15f",
133 preset_data->getTarget_data()->getRa(), preset_data->getTarget_data()->getDec(), preset_data->getTarget_data()->getEpoch().c_str(),
134 preset_data->getTarget_data()->getParallax(), preset_data->getTarget_data()->getObject_name().c_str(),
135 preset_data->getTarget_data()->getProper_motion_ra(), preset_data->getTarget_data()->getProper_motion_dec(),
136 preset_data->getTarget_data()->getRadvel(), preset_data->getTarget_data()->getRshift(),
137 preset_data->getTarget_data()->getVelocity_offset_ra(), preset_data->getTarget_data()->getVelocity_offset_dec(),
138 preset_data->getTarget_data()->getWavelength(), preset_data->getTarget_data()->getTime_ephem_utc());
139
140 // WARN do not copy shared_ptr, it is valid only within a scope of this method (CII idiosyncrasy).
141 auto ev = std::make_shared<PkCommands::UpdateTarget>(preset_args->clone());
142
143 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::UpdateTarget>();
144
145 m_sm.PostEvent(ev);
146
147 elt::mal::future<std::string> ret_rad = ev->GetPayload().GetReplyFuture();
148 elt::mal::future<std::string> ret = ret_rad.then([](elt::mal::future<std::string> fut_then) {
149 return ContinuationCheckRejection(std::move(fut_then), "UpdateTarget");
150 });
151
152 return ret;
153 }
154
155 [[deprecated("SetObservingWavelength is Deprecated - Use UpdateTarget")]]
156 virtual elt::mal::future<std::string> SetObservingWavelength(double wavelength) override {
157 RAD_TRACE(GetLogger());
158 LOG4CPLUS_INFO(GetLogger(), "SetObservingWavelength command received with Wavelength:" << wavelength);
159
160 auto ev = std::make_shared<PkCommands::SetObservingWavelength>(wavelength);
161 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::SetObservingWavelength>();
162
163 m_sm.PostEvent(ev);
164
165 elt::mal::future<std::string> ret_rad = ev->GetPayload().GetReplyFuture();
166 elt::mal::future<std::string> ret = ret_rad.then([](elt::mal::future<std::string> fut_then) {
167 return ContinuationCheckRejection(std::move(fut_then), "SetObservingWavelength");
168 });
169
170 return ret;
171 }
172
173 virtual elt::mal::future<std::string> StopTracking() override {
174
175 RAD_TRACE(GetLogger());
176 LOG4CPLUS_INFO(GetLogger(), "Received StopTracking");
177 auto ev = std::make_shared<PkCommands::StopTracking>();
178 m_sm.RegisterDefaultRequestRejectHandler<PkCommands::StopTracking>();
179 m_sm.PostEvent(ev);
180
181 return ev->GetPayload().GetReplyFuture();
182 }
183
184private:
185 rad::SMAdapter& m_sm;
186 DataContext& m_data;
187
207 static std::string ContinuationCheckRejection(elt::mal::future<std::string> future, std::string method_name) {
208 std::string reply = future.get(); // the future is (normally) already fulfilled when called from a then-continuation.
209 if (reply.find("Request rejected in state") != std::string::npos) { // TODO C++20 use std::string::starts_with.
210 LOG4CPLUS_INFO(GetLogger(), method_name << " command rejected by eltpk: " << reply);
211 }
212 return reply;
213 };
214};
215
216} // namespace hlcc::eltpk
217
218#endif // HLCC_TELIF_ELTPK_COMMANDSIMPL_HPP
DataContext class header file.
Default logger name.
::elt::mal::future< std::string > UpdateTarget(const std::shared_ptr<::ccsinsif::PresetArgs > &preset_args) override
Definition commandsImpl.hpp:123
virtual elt::mal::future< std::string > SetObservingWavelength(double wavelength) override
Definition commandsImpl.hpp:156
virtual ~CommandsImpl()
Definition commandsImpl.hpp:57
virtual elt::mal::future< std::string > StopTracking() override
Definition commandsImpl.hpp:173
elt::mal::future< std::string > SetTargetRaDec(const std::shared_ptr<::ccsinsif::PresetArgs > &preset_args) override
Definition commandsImpl.hpp:87
elt::mal::future< std::string > SetTargetAltAzPos(const std::shared_ptr< ccscommonif::AltAz > &axes_position) override
Definition commandsImpl.hpp:61
CommandsImpl(rad::SMAdapter &sm, DataContext &data)
Definition commandsImpl.hpp:31
Definition dataContext.hpp:108
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:29