13 #include <boost/asio.hpp> 14 #include <boost/property_tree/json_parser.hpp> 31 using boost::asio::ip::tcp;
34 boost::asio::io_service io_service;
37 tcp::resolver resolver(io_service);
39 tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
40 tcp::resolver::iterator end;
43 tcp::socket socket(io_service);
44 boost::system::error_code error = boost::asio::error::host_not_found;
47 while (error && endpoint_iterator != end)
50 socket.connect(*endpoint_iterator++, error);
53 throw boost::system::system_error(error);
56 boost::asio::streambuf request;
57 std::ostream request_stream(&request);
58 request_stream <<
"GET " << request_path <<
" HTTP/1.0\r\n\r\n";
60 boost::asio::write(socket, request);
65 boost::asio::streambuf response;
66 boost::asio::read_until(socket, response,
"\r\n");
69 std::istream response_stream(&response);
70 std::string http_version;
71 response_stream >> http_version;
72 unsigned int status_code;
73 response_stream >> status_code;
74 std::string status_message;
75 std::getline(response_stream, status_message);
76 if (!response_stream || http_version.substr(0, 5) !=
"HTTP/")
78 std::cout <<
"Invalid response\n";
83 boost::asio::read_until(socket, response,
"\r\n\r\n");
87 while (std::getline(response_stream, tmp) && tmp !=
"\r")
91 while (std::getline(response_stream, tmp))
95 while (boost::asio::read(socket, response, boost::asio::transfer_at_least(1), error))
97 response_stream.clear();
98 while (std::getline(response_stream, tmp))
102 if (error != boost::asio::error::eof)
103 throw boost::system::system_error(error);
106 for( std::size_t i=0; i<header.size(); i++ )
107 if( header[i] ==
'\r' )
110 for( std::size_t i=0; i<content.size(); i++ )
111 if( content[i] ==
'\r' )
116 catch (std::exception& e)
118 std::cerr <<
"Exception: " << e.what() << std::endl;
127 std::string request_str =
"/cmd/" + cmd +
"?";
128 for(
auto& kv : param_values )
129 request_str += kv.first +
"=" + kv.second +
"&";
130 if(request_str.back() ==
'&' )
131 request_str = request_str.substr(0,request_str.size()-1);
134 std::string
header, content;
140 std::stringstream ss(content);
141 boost::property_tree::json_parser::read_json(ss,
pt_);
143 catch (std::exception& e)
145 std::cerr <<
"ERROR: Exception: " << e.what() << std::endl;
159 std::map<std::string, std::string> param_values;
161 param_values[param] = value;
175 return boost::optional<std::string>();
176 return pt_.get_optional<std::string>(name);
183 std::map< std::string, std::string > key_values;
184 std::string namelist;
185 for(
const auto&
s: names )
186 namelist += (
s +
";");
187 namelist.substr(0,namelist.size()-1);
194 for(
const auto&
s: names )
196 auto ov =
pt_.get_optional<std::string>(
s);
200 key_values[
s] =
"--COULD NOT RETRIEVE VALUE--";
210 boost::optional<int> error_code =
pt_.get_optional<
int>(
"error_code");
211 boost::optional<std::string> error_text =
pt_.get_optional<std::string>(
"error_text");
212 if( !error_code || (*error_code) != 0 || !error_text || (*error_text) !=
"success" )
215 std::cerr <<
"ERROR: scanner replied: " << *error_text << std::endl;
226 return boost::optional<ProtocolInfo>();
229 boost::optional<std::string> protocol_name =
pt_.get_optional<std::string>(
"protocol_name");
230 boost::optional<int> version_major =
pt_.get_optional<
int>(
"version_major");
231 boost::optional<int> version_minor =
pt_.get_optional<
int>(
"version_minor");
232 auto ocommands =
pt_.get_child_optional(
"commands");
233 if( !protocol_name || !version_major || !version_minor || !ocommands )
234 return boost::optional<ProtocolInfo>();
238 pi.version_major = *version_major;
239 pi.version_minor = *version_minor;
242 boost::property_tree::ptree commands = *ocommands;
243 for(
auto i= commands.begin(); i!=commands.end(); i++ )
245 std::string cmd = i->second.get<std::string>(
"");
246 pi.commands.push_back(cmd);
256 std::vector< std::string > parameter_list;
258 return parameter_list;
261 auto oparameters =
pt_.get_child_optional(
"parameters");
263 return parameter_list;
266 boost::property_tree::ptree parameters = *oparameters;
267 for(
auto i= parameters.begin(); i!=parameters.end(); i++ )
269 std::string
param = i->second.get<std::string>(
"");
270 parameter_list.push_back(param);
273 return parameter_list;
281 std::map< std::string, std::string > params;
282 params[
"packet_type"] =
"C";
283 params[
"start_angle"] = std::to_string(start_angle);
287 return boost::optional<HandleInfo>();
290 boost::optional<int> port =
pt_.get_optional<
int>(
"port");
291 boost::optional<std::string> handle =
pt_.get_optional<std::string>(
"handle");
293 return boost::optional<HandleInfo>();
301 hi.packet_type =
'C';
302 hi.start_angle = start_angle;
303 hi.watchdog_enabled =
true;
304 hi.watchdog_timeout = 60000;
314 std::map< std::string, std::string > params;
315 params[
"packet_type"] =
"C";
316 params[
"start_angle"] = std::to_string(start_angle);
317 params[
"port"] = std::to_string(port);
318 params[
"address"] = hostname;
322 return boost::optional<HandleInfo>();
325 boost::optional<std::string> handle =
pt_.get_optional<std::string>(
"handle");
327 return boost::optional<HandleInfo>();
333 hi.hostname = hostname;
335 hi.packet_type =
'C';
336 hi.start_angle = start_angle;
337 hi.watchdog_enabled =
true;
338 hi.watchdog_timeout = 60000;
385 std::string namelist;
386 for(
const auto&
s: names )
387 namelist += (
s +
";");
388 namelist.substr(0,namelist.size()-1);
399 std::string local_ip;
402 using boost::asio::ip::udp;
403 boost::asio::io_service netService;
404 udp::resolver resolver(netService);
405 udp::resolver::query query(udp::v4(),
http_host_ ,
"");
406 udp::resolver::iterator endpoints = resolver.resolve(query);
407 udp::endpoint ep = *endpoints;
408 udp::socket socket(netService);
410 boost::asio::ip::address addr = socket.local_endpoint().address();
411 local_ip = addr.to_string();
413 catch (std::exception& e)
415 std::cerr <<
"Could not deal with socket-exception: " << e.what() << std::endl;
static const int HANDLE_TYPE_UDP
bool param(const std::string ¶m_name, T ¶m_val, const T &default_val)
bool setParameter(const std::string name, const std::string value)
int httpGet(const std::string request_path, std::string &header, std::string &content)
bool startScanOutput(const std::string &handle)
Initiate output of scan data.
bool sendHttpCommand(const std::string cmd, const std::map< std::string, std::string > param_values)
Information about the HTTP/JSON protocol.
bool resetParameters(const std::vector< std::string > &names)
boost::optional< HandleInfo > requestHandleTCP(int start_angle=-1800000)
HttpCommandInterface(const std::string &http_host, int http_port=80)
boost::optional< std::string > getParameter(const std::string name)
std_msgs::Header * header(M &m)
bool releaseHandle(const std::string &handle)
Release handle.
int http_status_code_
HTTP-Status code of last request.
std::map< std::string, std::string > getParameters(const std::vector< std::string > &names)
bool rebootDevice()
Reboot laserscanner.
std::string discoverLocalIP()
boost::optional< HandleInfo > requestHandleUDP(int port, std::string hostname=std::string(""), int start_angle=-1800000)
Encapsulates data about an etablished data connection.
std::string http_host_
Scanner IP.
boost::optional< ProtocolInfo > getProtocolInfo()
int http_port_
Port of HTTP-Interface.
std::string protocol_name
protocol name, defaults to "pfsdp"
bool stopScanOutput(const std::string &handle)
Terminate output of scan data.
static const int HANDLE_TYPE_TCP
boost::property_tree::ptree pt_
Returned JSON as property_tree.
std::vector< std::string > getParameterList()
bool feedWatchdog(const std::string &handle)
Feed the watchdog to keep the handle alive.