Dripline-Cpp  v2.4.2
Dripline Implementation in C++
oscillator_service_hub.cc
Go to the documentation of this file.
1 /*
2  * oscillator_service_hub.cc
3  *
4  * Created on: May 16, 2019
5  * Author: N.S. Oblath
6  */
7 
8 #define DRIPLINE_EXAMPLES_API_EXPORTS
9 
11 
12 #include "logger.hh"
13 #include "signal_handler.hh"
14 
15 //#include <ctime>
16 #include <functional>
17 
18 using scarab::param_array;
19 using scarab::param_node;
20 using scarab::param_ptr_t;
21 
22 using std::placeholders::_1;
23 
24 LOGGER( dlog, "oscillator_service_hub" );
25 
26 namespace dripline
27 {
28 
29  oscillator_service_hub::oscillator_service_hub( const scarab::param_node& a_config ) :
30  scarab::cancelable(),
31  hub( a_config, "osc_svc_hub" ),
32  f_oscillator(),
33  f_return( RETURN_SUCCESS )
34  {
35  register_set_handler( "frequency", std::bind( &oscillator_service_hub::handle_set_frequency_request, this, _1 ) );
36  register_get_handler( "frequency", std::bind( &oscillator_service_hub::handle_get_frequency_request, this, _1 ) );
37  register_set_handler( "amplitude", std::bind( &oscillator_service_hub::handle_set_amplitude_request, this, _1 ) );
38  register_get_handler( "amplitude", std::bind( &oscillator_service_hub::handle_get_amplitude_request, this, _1 ) );
40  register_get_handler( "quadrature", std::bind( &oscillator_service_hub::handle_get_quadrature_request, this, _1 ) );
42  }
43 
45  {
46  }
47 
49  {
50  scarab::signal_handler t_sig_hand;
51  t_sig_hand.add_cancelable( this );
52 
53  try
54  {
55  if( ! start() ) throw dripline_error() << "Unable to start service";
56 
57  if( ! listen() ) throw dripline_error() << "Unable to start listening";
58 
59  if( ! stop() ) throw dripline_error() << "Unable to stop service";
60  }
61  catch( std::exception& e )
62  {
63  LERROR( dlog, "Exception caught: " << e.what() );
64  f_return = dl_service_error().rc_value() / 100;
65  }
66 
67  if( scarab::signal_handler::get_exited() )
68  {
69  f_return = scarab::signal_handler::get_return_code();
70  }
71 
72  return;
73  }
74 
76  {
77  f_oscillator.set_frequency( a_request->payload()["values"][0]().as_double() );
78  return a_request->reply( dl_success(), "Frequency set" );
79  }
80 
82  {
83  param_ptr_t t_reply_payload( new param_node() );
84  param_node& t_reply_node = t_reply_payload->as_node();
85  t_reply_node.add( "value", f_oscillator.get_frequency() );
86  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
87  }
88 
89 
91  {
92  f_oscillator.set_amplitude( a_request->payload()["values"][0]().as_double() );
93  return a_request->reply( dl_success(), "Amplitude set" );
94  }
95 
97  {
98  param_ptr_t t_reply_payload( new param_node() );
99  param_node& t_reply_node = t_reply_payload->as_node();
100  t_reply_node.add( "value", f_oscillator.get_amplitude() );
101  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
102  }
103 
104 /*
105  // this could be implemented in a cross-platform way, but it requires more work than I have time for right now, so I'm going to leave it out
106  // windows: https://stackoverflow.com/questions/321849/strptime-equivalent-on-windows/321877#321877
107  // posix: https://stackoverflow.com/questions/21021388/how-to-parse-a-date-string-into-a-c11-stdchrono-time-point-or-similar
108  // the "windows" page has a boost-based cross-platform solution. current me recommends that since we already use the date_time library
109 
110  reply_ptr_t oscillator_service_hub::handle_set_start_time_request( const request_ptr_t a_request )
111  {
112 
113  }
114 
115  reply_ptr_t oscillator_service_hub::handle_get_start_time_request( const request_ptr_t a_request )
116  {
117  param_ptr_t t_reply_payload( new param_node() );
118  param_node& t_reply_node = t_reply_payload->as_node();
119  std::time_t t_time = std::chrono::steady_clock::to_time_t( f_oscillator.get_start_time() );
120  std::tm t_tm = *std::localtime(&t_time);
121  char t_time_arr[100];
122  if( std::strftime( t_time_arr, sizeof(t_time_arr), "%c", &t_tm))
123  {
124  t_reply_node.add( "value", t_time_arr );
125  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
126  }
127  return a_request->reply( dl_resource_error(), "Time decoding failed" );
128  }
129 */
130 
132  {
133  param_ptr_t t_reply_payload( new param_node() );
134  param_node& t_reply_node = t_reply_payload->as_node();
135  t_reply_node.add( "value", f_oscillator.in_phase().second );
136  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
137  }
138 
140  {
141  param_ptr_t t_reply_payload( new param_node() );
142  param_node& t_reply_node = t_reply_payload->as_node();
143  t_reply_node.add( "value", f_oscillator.quadrature().second );
144  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
145  }
146 
148  {
149  param_ptr_t t_reply_payload( new param_node() );
150  param_node& t_reply_node = t_reply_payload->as_node();
151  param_array t_iq_param;
152  t_iq_param.push_back( f_oscillator.iq().second.real() );
153  t_iq_param.push_back( f_oscillator.iq().second.imag() );
154  t_reply_node.add( "value", std::move(t_iq_param) );
155  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
156  }
157 
158 
159 } /* namespace dripline */
reply_ptr_t handle_get_amplitude_request(const request_ptr_t a_request)
reply_ptr_t handle_get_quadrature_request(const request_ptr_t a_request)
virtual unsigned rc_value() const
std::shared_ptr< msg_request > request_ptr_t
Definition: dripline_fwd.hh:23
void register_get_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a get request handler function.
Definition: hub.cc:43
Dripline-specific errors.
Definition: core.hh:17
Service class aimed at adding a Dripline API to an existing codebase.
Definition: hub.hh:70
reply_ptr_t handle_set_amplitude_request(const request_ptr_t a_request)
reply_ptr_t handle_get_frequency_request(const request_ptr_t a_request)
reply_ptr_t handle_get_iq_request(const request_ptr_t a_request)
reply_ptr_t handle_set_frequency_request(const request_ptr_t a_request)
static scarab::logger dlog("agent")
void register_set_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a set request handler function.
Definition: hub.cc:50
std::shared_ptr< msg_reply > reply_ptr_t
Definition: dripline_fwd.hh:24
reply_ptr_t handle_get_in_phase_request(const request_ptr_t a_request)
static scarab::logger dlog("oscillator_service_hub")
oscillator_service_hub(const scarab::param_node &a_config=scarab::param_node())