RAPP Platform API
 All Classes Namespaces Files Functions Variables Typedefs
hazard_detection.hpp
Go to the documentation of this file.
1 #ifndef RAPP_CLOUD_HAZARD_DETECTION
2 #define RAPP_CLOUD_HAZARD_DETECTION
3 #include "includes.ihh"
4 namespace rapp {
5 namespace cloud {
14 {
15 public:
23  const std::shared_ptr<rapp::object::picture> image,
24  std::function<void(double door_angle)> callback,
25  std::string token
26  )
27  : asio_service_http(token), delegate__(callback)
28  {
29  assert(image);
30  std::string boundary = random_boundary();
31  std::string fname = random_boundary()+"."+image->type();
32  boost::property_tree::ptree tree;
33  tree.put("file", fname);
34  std::stringstream ss;
35  boost::property_tree::write_json(ss, tree, false);
36  post_ = "--" + boundary + "\r\n"
37  + "Content-Disposition: form-data; name=\"json\"\r\n\r\n"
38  + ss.str() + "\r\n";
39  post_ += "--"+boundary+"\r\n"
40  + "Content-Disposition: form-data; name=\"file_uri\"; filename=\""+fname+"\"\r\n"
41  + "Content-Type: image/"+image->type()+"\r\n"
42  + "Content-Transfer-Encoding: binary\r\n\r\n";
43  // Append binary data
44  auto imagebytes = image->bytearray();
45  post_.insert(post_.end(), imagebytes.begin(), imagebytes.end());
46  post_ += "\r\n";
47  post_ += "--"+boundary+"--";
48  header_ = "POST /hop/hazard_detection_door_check HTTP/1.1\r\n";
49  header_ += "Content-Type: multipart/form-data; boundary="+boundary+"\r\n\r\n";
50  callback_ = std::bind(&hazard_detection_door_check::handle_reply, this, std::placeholders::_1);
51  }
52 private:
56  void handle_reply(std::string json)
57  {
58  std::stringstream ss(json);
59  double door_angle;
60  try {
61  boost::property_tree::ptree tree;
62  boost::property_tree::read_json(ss, tree);
63  // get door angle
64  for (auto child : tree.get_child("door_angle")) {
65  door_angle = child.second.get_value<double>();
66  }
67  // Check for Errors returned by the platform
68  for (auto child : tree.get_child("error")) {
69  const std::string value = child.second.get_value<std::string>();
70  if (!value.empty()) {
71  std::cerr << "hazard_detection_door_check JSON error: " << value << std::endl;
72  }
73  }
74  }
75  catch(boost::property_tree::json_parser::json_parser_error & je) {
76  std::cerr << "hazard_detection_door_check::handle_reply Error parsing: "
77  << je.filename() << " on line: " << je.line() << std::endl;
78  std::cerr << je.message() << std::endl;
79  }
80  delegate__(door_angle);
81  }
82 
84  std::function<void(double)> delegate__;
85 };
86 
95 {
96 public:
104  const std::shared_ptr<rapp::object::picture> image,
105  std::function<void(double light_level)> callback,
106  std::string token
107  )
108  : asio_service_http(token), delegate__(callback)
109  {
110  assert(image);
111  std::string boundary = random_boundary();
112  std::string fname = random_boundary()+"."+image->type();
113  boost::property_tree::ptree tree;
114  tree.put("file", fname);
115  std::stringstream ss;
116  boost::property_tree::write_json(ss, tree, false);
117  post_ = "--" + boundary + "\r\n"
118  + "Content-Disposition: form-data; name=\"json\"\r\n\r\n"
119  + ss.str() + "\r\n";
120  post_ += "--"+boundary+"\r\n"
121  + "Content-Disposition: form-data; name=\"file_uri\"; filename=\""+fname+"\"\r\n"
122  + "Content-Type: image/"+image->type()+"\r\n"
123  + "Content-Transfer-Encoding: binary\r\n\r\n";
124  // Append binary data
125  auto imagebytes = image->bytearray();
126  post_.insert(post_.end(), imagebytes.begin(), imagebytes.end());
127  post_ += "\r\n";
128  post_ += "--"+boundary+"--";
129  header_ = "POST /hop/hazard_detection_light_check HTTP/1.1\r\n";
130  header_ += "Content-Type: multipart/form-data; boundary="+boundary+"\r\n\r\n";
131  callback_ = std::bind(&hazard_detection_light_check::handle_reply, this, std::placeholders::_1);
132  }
133 
134 private:
138  void handle_reply(std::string json)
139  {
140  std::stringstream ss(json);
141  double light_level;
142  try {
143  boost::property_tree::ptree tree;
144  boost::property_tree::read_json(ss, tree);
145  // get door angle
146  for (auto child : tree.get_child("light_level")) {
147  light_level = child.second.get_value<double>();
148  }
149  // Check for Errors returned by the platform
150  for (auto child : tree.get_child("error")) {
151  const std::string value = child.second.get_value<std::string>();
152  if (!value.empty()) {
153  std::cerr << "hazard_detection_light_check JSON error: " << value << std::endl;
154  }
155  }
156  }
157  catch(boost::property_tree::json_parser::json_parser_error & je) {
158  std::cerr << "hazard_detection_light_check::handle_reply Error parsing: "
159  << je.filename() << " on line: " << je.line() << std::endl;
160  std::cerr << je.message() << std::endl;
161  }
162  delegate__(light_level);
163  }
164 
166  std::function<void(double)> delegate__;
167 };
168 }
169 }
170 #endif
std::string header_
Header that will be used.
std::string random_boundary() const
Create a random boundary for the multipart/form in HTTP.
void handle_reply(std::string json)
handle the rapp-platform JSON reply
std::string post_
Actual post Data.
hazard_detection_door_check(const std::shared_ptr< rapp::object::picture > image, std::function< void(double door_angle)> callback, std::string token)
Constructor.
void handle_reply(std::string json)
handle the rapp-platform JSON reply
std::function< void(std::string)> callback_
Callback Handler - use with std::bind or boost variant.
std::function< void(double)> delegate__
The callback called upon completion of receiving the detected faces.
hazard_detection_light_check(const std::shared_ptr< rapp::object::picture > image, std::function< void(double light_level)> callback, std::string token)
Constructor.
base class for asynchronous http websockets used for connecting to cloud services ...
std::function< void(double)> delegate__
The callback called upon completion of receiving the detected faces.