RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
cognitive_exercises.hpp
Go to the documentation of this file.
1 #ifndef RAPP_CLOUD_COGNITIVE_EXERCISES
2 #define RAPP_CLOUD_COGNITIVE_EXERCISES
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
14 {
15 public:
23  const std::string user,
24  const std::string test_type,
25  const std::string token,
26  std::function<void(std::vector<std::string>,
27  std::vector<std::string>,
28  std::vector<std::string>,
29  std::string,
30  std::string,
31  std::string)> callback
32  )
33  : asio_service_http(token), delegate_(callback)
34  {
35  boost::property_tree::ptree tree;
36  tree.put("test_type", test_type);
37  std::stringstream ss;
38  boost::property_tree::write_json(ss, tree, false);
39  post_ = ss.str();
40  header_ = "POST /hop/cognitive_test_selector HTTP/1.1\r\n"
41  + "Content-Type: application/x-www-form-urlencoded\r\n";
42  callback_ = std::bind(&cognitive_test_selector::handle_reply, this, std::placeholders::_1);
43  }
44 
45 private:
49  void handle_reply(std::string json)
50  {
51  std::stringstream ss(json);
52  std::vector<std::string> questions;
53  std::vector<std::string> possib_ans;
54  std::vector<std::string> correct_ans;
55  std::string test_instance;
56  std::string test_type;
57  std::string test_subtype;
58  try {
59  boost::property_tree::ptree tree;
60  boost::property_tree::read_json(ss, tree);
61  // NOTE: untested!
62  for (auto child : tree.get_child("questions")) {
63  questions.push_back(child.second.get_value<std::string>());
64  }
65  for (auto child : tree.get_child("possib_ans")) {
66  possib_ans.push_back(child.second.get_value<std::string>());
67  }
68  for (auto child : tree.get_child("correct_ans")) {
69  correct_ans.push_back(child.second.get_value<std::string>());
70  }
71  for (auto child : tree.get_child("test_instance")) {
72  test_instance = child.second.get_value<std::string>();
73  }
74  for (auto child : tree.get_child("test_type")) {
75  test_type = child.second.get_value<std::string>();
76  }
77  for (auto child : tree.get_child("test_subtype")) {
78  test_subtype = child.second.get_value<std::string>();
79  }
80  for (auto child : tree.get_child("error")) {
81  const std::string value = child.second.get_value<std::string>();
82  if (!value.empty()) {
83  std::cerr << "cognitive_test_selector JSON error: " << value << std::endl;
84  }
85  }
86  }
87  catch (boost::property_tree::json_parser::json_parser_error & je) {
88  std::cerr << "cognitive_test_selector::handle_reply Error parsing: " << je.filename()
89  << " on line: " << je.line() << std::endl;
90  std::cerr << je.message() << std::endl;
91  }
92  delegate_(questions,
93  possib_ans,
94  correct_ans,
95  test_instance
96  test_type,
97  test_subtype);
98  }
99 
101  std::function<void(std::vector<std::string>,
102  std::vector<std::string>,
103  std::vector<std::string>,
104  std::string,
105  std::string,
106  std::string)> delegate_;
107 };
108 
116 {
117 public:
126  const std::string test_instance,
127  const float score,
128  const std::string token,
129  std::function<void(std::string)> callback
130  )
131  : asio_service_http(token), delegate_(callback)
132  {
133  boost::property_tree::ptree tree;
134  tree.put("test_instance", test_instance);
135  tree.put("score", boost::lexical_cast<std::string>(score));
136  std::stringstream ss;
137  boost::property_tree::write_json(ss, tree, false);
138  post_ = ss.str();
139  header_ = "POST /hop/cognitive_record_performance HTTP/1.1\r\n"
140  + "Content-Type: application/x-www-form-urlencoded\r\n";
141  callback_ = std::bind(&cognitive_record_performance::handle_reply, this, std::placeholders::_1);
142  }
143 
144 private:
148  void handle_reply(std::string json)
149  {
150  std::stringstream ss(json);
151  std::string performance_entry;
152  try {
153  boost::property_tree::ptree tree;
154  boost::property_tree::read_json(ss, tree);
155  // NOTE: untested!
156  for (auto child : tree.get_child("performance_entry")) {
157  performance_entry = second.get_value<std::string>();
158  }
159  for (auto child : tree.get_child("error")) {
160  const std::string value = child.second.get_value<std::string>();
161  if (!value.empty()) {
162  std::cerr << "cognitive_record_performance JSON error: " << value << std::endl;
163  }
164  }
165  }
166  catch (boost::property_tree::json_parser::json_parser_error & je) {
167  std::cerr << "cognitive_record_performance::handle_reply Error parsing: " << je.filename()
168  << " on line: " << je.line() << std::endl;
169  std::cerr << je.message() << std::endl;
170  }
171  delegate_(performance_entry);
172  }
173 
175  std::function<void(std::string)> delegate_;
176 };
177 
185 {
186 public:
196  unsigned int from_time,
197  unsigned int to_time,
198  const std::string test_type,
199  const std::string token,
200  std::function<void(std::string)> callback
201  )
202  : asio_service_http(token), delegate_(callback)
203  {
204  boost::property_tree::ptree tree;
205  tree.put("from_time", boost::lexical_cast<std::string>(from_time));
206  tree.put("to_time", boost::lexical_cast<std::string>(to_time));
207  tree.put("test_type", test_type);
208  std::stringstream ss;
209  boost::property_tree::write_json(ss, tree, false);
210  post_ = ss.str();
211  header_ = "POST /hop/cognitive_get_history HTTP/1.1\r\n"
212  + "Content-Type: application/x-www-form-urlencoded\r\n";
213  callback_ = std::bind(&cognitive_get_history::handle_reply, this, std::placeholders::_1);
214  }
215 
216 private:
220  void handle_reply(std::string json)
221  {
222  delegate_(std::move(json));
223  }
224 
226  std::function<void(std::string)> delegate_;
227 };
228 
236 {
237 public:
246  unsigned int up_to_time,
247  const std::string test_type,
248  const std::string token,
249  std::function<void(std::vector<unsigned int>,
250  std::vector<float>)> callback
251  )
252  : asio_service_http(token), delegate_(callback)
253  {
254  boost::property_tree::ptree tree;
255  tree.put("up_to_time", boost::lexical_cast<std::string>(from_time));
256  tree.put("test_type", test_type);
257  std::stringstream ss;
258  boost::property_tree::write_json(ss, tree, false);
259  post_ = ss.str();
260  header_ = "POST /hop/cognitive_get_scores HTTP/1.1\r\n"
261  + "Content-Type: application/x-www-form-urlencoded\r\n";
262  callback_ = std::bind(&cognitive_get_scores::handle_reply, this, std::placeholders::_1);
263  }
264 
265 private:
269  void handle_reply(std::string json)
270  {
271  std::stringstream ss(json);
272  std::vector<unsigned int> test_classes;
273  std::vector<float> scores;
274  try {
275  boost::property_tree::ptree tree;
276  boost::property_tree::read_json(ss, tree);
277  // NOTE: untested!
278  for (auto child : tree.get_child("test_classes")) {
279  test_classes.push_back(second.get_value<unsigned int>());
280  }
281  for (auto child : tree.get_child("scores")) {
282  scores.push_back(second.get_value<float>());
283  }
284  for (auto child : tree.get_child("error")) {
285  const std::string value = child.second.get_value<std::string>();
286  if (!value.empty()) {
287  std::cerr << "cognitive_get_scores JSON error: " << value << std::endl;
288  }
289  }
290  }
291  catch (boost::property_tree::json_parser::json_parser_error & je) {
292  std::cerr << "cognitive_get_scores::handle_reply Error parsing: " << je.filename()
293  << " on line: " << je.line() << std::endl;
294  std::cerr << je.message() << std::endl;
295  }
296  delegate_(test_classes, scores);
297  }
298 
300  std::function<void(std::vector<unsigned int>,
301  std::vector<float>)> delegate_;
302 };
303 }
304 }
305 #endif
std::function< void(std::string)> delegate_
std::string header_
Header that will be used.
cognitive_get_scores(unsigned int up_to_time, const std::string test_type, const std::string token, std::function< void(std::vector< unsigned int >, std::vector< float >)> callback)
get cognitive test scores
cognitive_test_selector(const std::string user, const std::string test_type, const std::string token, std::function< void(std::vector< std::string >, std::vector< std::string >, std::vector< std::string >, std::string, std::string, std::string)> callback)
handler obtains a cognitive test from cloud.rapp
std::function< void(std::string)> delegate_
cognitive_record_performance(const std::string test_instance, const float score, const std::string token, std::function< void(std::string)> callback)
record performance for a cognitive test
void handle_reply(std::string json)
handle platform's JSON reply
std::string post_
Actual post Data.
void handle_reply(std::string json)
forward (don't parse) platform reply
std::function< void(std::vector< std::string >, std::vector< std::string >, std::vector< std::string >, std::string, std::string, std::string)> delegate_
chose a congitive game to play
cognitive_get_history(unsigned int from_time, unsigned int to_time, const std::string test_type, const std::string token, std::function< void(std::string)> callback)
get history of cognitive game
std::function< void(std::vector< unsigned int >, std::vector< float >)> delegate_
std::function< void(std::string)> callback_
Callback Handler - use with std::bind or boost variant.
base class for asynchronous http websockets used for connecting to cloud services ...
void handle_reply(std::string json)
handle platform's JSON reply
void handle_reply(std::string json)
handle platform's JSON reply