Dripline-Cpp  v2.10.11
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, const scarab::authentication& a_auth ) :
30  scarab::cancelable(),
31  hub( a_config, a_auth ),
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  auto t_cwrap = scarab::wrap_cancelable( *this );
51  scarab::signal_handler::add_cancelable( t_cwrap );
52 
53  try
54  {
55  run();
56  }
57  catch( std::exception& e )
58  {
59  LERROR( dlog, "Exception caught: " << e.what() );
60  LERROR( dlog, "Exiting service" );
61  f_return = dl_service_error().rc_value() / 100;
62  scarab::signal_handler::cancel_all( f_return );
63  }
64 
65  if( scarab::signal_handler::get_exited() )
66  {
67  f_return = scarab::signal_handler::get_return_code();
68  }
69 
70  return;
71  }
72 
74  {
75  f_oscillator.set_frequency( a_request->payload()["values"][0]().as_double() );
76  return a_request->reply( dl_success(), "Frequency set" );
77  }
78 
80  {
81  param_ptr_t t_reply_payload( new param_node() );
82  param_node& t_reply_node = t_reply_payload->as_node();
83  t_reply_node.add( "value", f_oscillator.get_frequency() );
84  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
85  }
86 
87 
89  {
90  f_oscillator.set_amplitude( a_request->payload()["values"][0]().as_double() );
91  return a_request->reply( dl_success(), "Amplitude set" );
92  }
93 
95  {
96  param_ptr_t t_reply_payload( new param_node() );
97  param_node& t_reply_node = t_reply_payload->as_node();
98  t_reply_node.add( "value", f_oscillator.get_amplitude() );
99  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
100  }
101 
102 /*
103  // 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
104  // windows: https://stackoverflow.com/questions/321849/strptime-equivalent-on-windows/321877#321877
105  // posix: https://stackoverflow.com/questions/21021388/how-to-parse-a-date-string-into-a-c11-stdchrono-time-point-or-similar
106  // the "windows" page has a boost-based cross-platform solution. current me recommends that since we already use the date_time library
107 
108  reply_ptr_t oscillator_service_hub::handle_set_start_time_request( const request_ptr_t a_request )
109  {
110 
111  }
112 
113  reply_ptr_t oscillator_service_hub::handle_get_start_time_request( const request_ptr_t a_request )
114  {
115  param_ptr_t t_reply_payload( new param_node() );
116  param_node& t_reply_node = t_reply_payload->as_node();
117  std::time_t t_time = std::chrono::steady_clock::to_time_t( f_oscillator.get_start_time() );
118  std::tm t_tm = *std::localtime(&t_time);
119  char t_time_arr[100];
120  if( std::strftime( t_time_arr, sizeof(t_time_arr), "%c", &t_tm))
121  {
122  t_reply_node.add( "value", t_time_arr );
123  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
124  }
125  return a_request->reply( dl_resource_error(), "Time decoding failed" );
126  }
127 */
128 
130  {
131  param_ptr_t t_reply_payload( new param_node() );
132  param_node& t_reply_node = t_reply_payload->as_node();
133  t_reply_node.add( "value", f_oscillator.in_phase().second );
134  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
135  }
136 
138  {
139  param_ptr_t t_reply_payload( new param_node() );
140  param_node& t_reply_node = t_reply_payload->as_node();
141  t_reply_node.add( "value", f_oscillator.quadrature().second );
142  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
143  }
144 
146  {
147  param_ptr_t t_reply_payload( new param_node() );
148  param_node& t_reply_node = t_reply_payload->as_node();
149  param_array t_iq_param;
150  t_iq_param.push_back( f_oscillator.iq().second.real() );
151  t_iq_param.push_back( f_oscillator.iq().second.imag() );
152  t_reply_node.add( "value", std::move(t_iq_param) );
153  return a_request->reply( dl_success(), "Get request succeeded", std::move(t_reply_payload) );
154  }
155 
156 
157 } /* namespace dripline */
Service class aimed at adding a Dripline API to an existing codebase.
Definition: hub.hh:71
void register_set_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a set request handler function.
Definition: hub.cc:58
void register_get_handler(const std::string &a_key, const handler_func_t &a_func)
Sets a get request handler function.
Definition: hub.cc:51
reply_ptr_t handle_get_quadrature_request(const request_ptr_t a_request)
reply_ptr_t handle_set_amplitude_request(const request_ptr_t a_request)
oscillator_service_hub(const scarab::param_node &a_config, const scarab::authentication &a_auth)
reply_ptr_t handle_get_frequency_request(const request_ptr_t a_request)
reply_ptr_t handle_get_amplitude_request(const request_ptr_t a_request)
reply_ptr_t handle_get_iq_request(const request_ptr_t a_request)
reply_ptr_t handle_get_in_phase_request(const request_ptr_t a_request)
reply_ptr_t handle_set_frequency_request(const request_ptr_t a_request)
virtual void run()
Definition: service.cc:142
std::shared_ptr< msg_reply > reply_ptr_t
Definition: dripline_fwd.hh:24
static ::scarab::logger_type< ::scarab::spd_initializer_async_stdout_color_mt > dlog("agent", __FILE_NAME__, __LINE__)
std::shared_ptr< msg_request > request_ptr_t
Definition: dripline_fwd.hh:23
Definition: agent.hh:18
static ::scarab::logger_type< ::scarab::spd_initializer_async_stdout_color_mt > dlog("oscillator_service_hub", __FILE_NAME__, __LINE__)
virtual unsigned rc_value() const