pfsdp_base.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
5 
6 PFSDPBase::PFSDPBase(std::shared_ptr<HandleInfo> info, std::shared_ptr<ScanConfig> config,
7  std::shared_ptr<ScanParameters> params)
8  : http_interface(new HTTPInterface(info->hostname, "cmd")), info_(info), config_(config), params_(params)
9 {
10 }
11 
12 const std::map<std::string, std::string> PFSDPBase::get_request(const std::string& command,
13  const std::vector<std::string>& json_keys,
14  const std::initializer_list<param_type>& query)
15 {
16  return get_request(command, json_keys, param_map_type(query.begin(), query.end()));
17 }
18 
19 const std::map<std::string, std::string> PFSDPBase::get_request(const std::string& command,
20  const std::vector<std::string>& json_keys,
21  const param_map_type& query)
22 {
23  const std::string err_code = "error_code";
24  const std::string err_text = "error_text";
25  const std::string err_http = "error_http";
26  std::vector<std::string> keys = { err_code, err_text };
27  keys.insert(keys.end(), json_keys.begin(), json_keys.end());
28  std::map<std::string, std::string> json_resp = http_interface->get(keys, command, query);
29 
30  if (!check_error(json_resp, err_code, err_text, err_http))
31  {
32  return std::map<std::string, std::string>();
33  }
34 
35  return json_resp;
36 }
37 
38 bool PFSDPBase::get_request_bool(const std::string& command, const std::vector<std::string>& json_keys,
39  const std::initializer_list<param_type>& query)
40 {
41  std::map<std::string, std::string> resp = get_request(command, json_keys, query);
42  if (resp.empty())
43  {
44  return false;
45  }
46  return true;
47 }
48 
49 bool PFSDPBase::is_connection_failure(const std::string& http_error)
50 {
51  std::string error_1 = "Failed to connect to ";
52  std::string error_2 = "No route to host";
53 
54  if (http_error.find(error_1) != std::string::npos && http_error.find(error_2) != std::string::npos)
55  {
56  return true;
57  }
58  return false;
59 }
60 
61 bool PFSDPBase::check_error(std::map<std::string, std::string>& mp, const std::string& err_code,
62  const std::string& err_text, const std::string& err_http)
63 {
64  const std::string http_error = mp[err_http];
65  const std::string code = mp[err_code];
66  const std::string text = mp[err_text];
67 
68  // remove error related key-value pairs
69  mp.erase(err_http);
70  mp.erase(err_code);
71  mp.erase(err_text);
72 
73  // check if HTTP has an error
74  if (http_error.compare(std::string("OK")))
75  {
76  std::cerr << "HTTP ERROR: " << http_error << std::endl;
77  if (is_connection_failure(http_error))
78  {
80  {
82  }
83  }
84  return false;
85  }
86 
87  // check if 'error_code' and 'error_text' does not exist in the response
88  // this happens in case of invalid command
89  if (!code.compare("--COULD NOT RETRIEVE VALUE--") || !text.compare("--COULD NOT RETRIEVE VALUE--"))
90  {
91  std::cout << "Invalid command or parameter requested." << std::endl;
92  return false;
93  }
94  // check for error messages in protocol response
95  if (code.compare("0") || text.compare("success"))
96  {
97  std::cout << "protocol error: " << code << " " << text << std::endl;
98  return false;
99  }
100  return true;
101 }
102 
103 void PFSDPBase::set_connection_failure_cb(std::function<void()> callback)
104 {
105  handle_connection_failure = callback;
106 }
107 
108 const std::vector<std::string> PFSDPBase::list_parameters()
109 {
110  auto resp = get_request("list_parameters", { "parameters" });
111  return parser_utils::split(resp["parameters"]);
112 }
113 
115 {
116  return get_request_bool("reboot_device");
117 }
118 
120 {
121  get_request("factory_reset");
122 }
123 
124 bool PFSDPBase::release_handle(const std::string& handle)
125 {
126  get_request("release_handle", { "" }, { KV("handle", handle) });
127  return true;
128 }
129 
131 {
132  ProtocolInfo opi;
133  auto resp = get_request("get_protocol_info", { "protocol_name", "version_major", "version_minor", "commands" });
134  if (resp.empty())
135  {
136  opi.isError = true;
137  return opi;
138  }
139 
140  opi.version_major = atoi(resp["version_major"].c_str());
141  opi.version_minor = atoi(resp["version_minor"].c_str());
142  opi.protocol_name = resp["protocol_name"];
143  opi.device_family = get_parameter_int("device_family");
144 
145  return opi;
146 }
147 
148 int64_t PFSDPBase::get_parameter_int(const std::string& param)
149 {
150  std::map<std::string, std::string> resp = get_parameter(param);
151  if (resp.empty())
152  {
153  return std::numeric_limits<std::int64_t>::quiet_NaN();
154  }
155  return parser_utils::to_long(resp[param]);
156 }
157 
158 float PFSDPBase::get_parameter_float(const std::string& param)
159 {
160  std::map<std::string, std::string> resp = get_parameter(param);
161  if (resp.empty())
162  {
163  return std::numeric_limits<float>::quiet_NaN();
164  }
165  return parser_utils::to_float(resp[param]);
166 }
167 
168 std::string PFSDPBase::get_parameter_str(const std::string& param)
169 {
170  std::map<std::string, std::string> resp = get_parameter(param);
171  if (resp.empty())
172  {
173  return std::string("");
174  }
175  return resp[param];
176 }
177 
178 void PFSDPBase::request_handle_tcp(const std::string& port, const std::string& packet_type)
179 {
180  param_map_type query;
181  if (!packet_type.empty())
182  {
183  query["packet_type"] = packet_type;
184  }
185  else
186  {
187  query["packet_type"] = config_->packet_type;
188  }
189  if (!port.empty())
190  {
191  query["port"] = port;
192  }
193  else if (info_->port.compare("0") != 0)
194  {
195  query["port"] = info_->port;
196  }
197  auto resp = get_request("request_handle_tcp", { "handle", "port" }, query);
198 
199  info_->handle = resp["handle"];
200  info_->port = resp["port"];
201 
202  // TODO: port and pkt_type should be updated in config_
203 }
204 
205 void PFSDPBase::request_handle_udp(const std::string& packet_type)
206 {
207  param_map_type query = { KV("address", info_->endpoint), KV("port", info_->port) };
208  if (!packet_type.empty())
209  {
210  query["packet_type"] = packet_type;
211  }
212  else
213  {
214  query["packet_type"] = config_->packet_type;
215  }
216  auto resp = get_request("request_handle_udp", { "handle", "port" }, query);
217  info_->handle = resp["handle"];
218 }
219 
220 void PFSDPBase::get_scanoutput_config(const std::string& handle)
221 {
222  auto resp =
223  get_request("get_scanoutput_config",
224  { "start_angle", "packet_type", "watchdogtimeout", "skip_scans", "watchdog", "max_num_points_scan" },
225  { KV("handle", handle) });
226  config_->packet_type = resp["packet_type"];
227  config_->start_angle = parser_utils::to_long(resp["start_angle"]);
228  config_->watchdogtimeout = parser_utils::to_long(resp["watchdogtimeout"]);
229  config_->watchdog = (resp["watchdog"] == "off") ? false : true;
230  config_->skip_scans = parser_utils::to_long(resp["skip_scans"]);
231  config_->max_num_points_scan = parser_utils::to_long(resp["max_num_points_scan"]);
232 }
233 
234 bool PFSDPBase::set_scanoutput_config(const std::string& handle, const ScanConfig& config)
235 {
236  param_map_type query = { KV("handle", handle),
237  KV("start_angle", config.start_angle),
238  KV("packet_type", config.packet_type),
239  KV("max_num_points_scan", config.max_num_points_scan),
240  KV("watchdogtimeout", config.watchdogtimeout),
241  KV("skip_scans", config.skip_scans),
242  KV("watchdog", config.watchdog ? "on" : "off") };
243  auto resp = get_request("set_scanoutput_config", { "" }, query);
244 
245  // update global config_
246  get_scanoutput_config(handle);
248  return true;
249 }
250 
252 {
253  param_map_type query = { KV("handle", info_->handle),
254  KV("start_angle", config_->start_angle),
255  KV("packet_type", config_->packet_type),
256  KV("max_num_points_scan", config_->max_num_points_scan),
257  KV("watchdogtimeout", config_->watchdogtimeout),
258  KV("skip_scans", config_->skip_scans),
259  KV("watchdog", config_->watchdog ? "on" : "off") };
260  auto resp = get_request("set_scanoutput_config", { "" }, query);
261 
262  // recalculate scan params
264  return true;
265 }
266 
268 {
269  get_request("start_scanoutput", { "" }, { { "handle", info_->handle } });
270  return true;
271 }
272 
273 bool PFSDPBase::stop_scanoutput(const std::string& handle)
274 {
275  return get_request_bool("stop_scanoutput", { "" }, { { "handle", handle } });
276 }
277 
278 std::string PFSDPBase::get_scanoutput_config(const std::string& param, const std::string& handle)
279 {
280  auto resp = get_request("get_scanoutput_config", { param }, { KV("handle", handle) });
281  return resp[param];
282 }
283 
284 bool PFSDPBase::feed_watchdog(const std::string& handle)
285 {
286  return get_request_bool("feed_watchdog", { "" }, { { "handle", handle } });
287 }
288 
290 {
291  return std::string("");
292 }
293 
294 std::string PFSDPBase::get_part()
295 {
296  return std::string("");
297 }
298 
300 {
301 }
302 
304 {
305 }
PFSDPBase::check_error
bool check_error(std::map< std::string, std::string > &mp, const std::string &err_code, const std::string &err_text, const std::string &err_http)
Definition: pfsdp_base.cpp:61
PFSDPBase::setup_param_server
virtual void setup_param_server()
Definition: pfsdp_base.cpp:303
PFSDPBase::get_parameter
std::map< std::string, std::string > get_parameter(const Ts &... ts)
Definition: pfsdp_base.h:83
PFSDPBase::request_handle_tcp
void request_handle_tcp(const std::string &port="", const std::string &packet_type="")
Definition: pfsdp_base.cpp:178
PFSDPBase::handle_connection_failure
std::function< void()> handle_connection_failure
Definition: pfsdp_base.h:37
parser_utils::to_float
float to_float(const std::string &s)
Definition: parser_utils.cpp:28
PFSDPBase::request_handle_udp
virtual void request_handle_udp(const std::string &packet_type="")
Definition: pfsdp_base.cpp:205
KV
Definition: kv.h:9
command
ROSLIB_DECL std::string command(const std::string &cmd)
PFSDPBase::config_
std::shared_ptr< ScanConfig > config_
Definition: pfsdp_base.h:57
PFSDPBase::get_protocol_info
ProtocolInfo get_protocol_info()
Definition: pfsdp_base.cpp:130
ProtocolInfo::device_family
uint16_t device_family
Definition: protocol_info.h:15
PFSDPBase::PFSDPBase
PFSDPBase(std::shared_ptr< HandleInfo > info, std::shared_ptr< ScanConfig > config, std::shared_ptr< ScanParameters > params)
Definition: pfsdp_base.cpp:6
PFSDPBase::update_scanoutput_config
bool update_scanoutput_config()
Definition: pfsdp_base.cpp:251
PFSDPBase::get_scan_parameters
virtual void get_scan_parameters()
Definition: pfsdp_base.cpp:299
PFSDPBase::get_parameter_float
float get_parameter_float(const std::string &param)
Definition: pfsdp_base.cpp:158
PFSDPBase::release_handle
bool release_handle(const std::string &handle)
Definition: pfsdp_base.cpp:124
PFSDPBase::list_parameters
const std::vector< std::string > list_parameters()
Definition: pfsdp_base.cpp:108
HTTPInterface
Definition: http_interface.h:24
PFSDPBase::is_connection_failure
bool is_connection_failure(const std::string &http_error)
Definition: pfsdp_base.cpp:49
PFSDPBase::info_
std::shared_ptr< HandleInfo > info_
Definition: pfsdp_base.h:56
ProtocolInfo::version_major
int version_major
Definition: protocol_info.h:10
PFSDPBase::feed_watchdog
bool feed_watchdog(const std::string &handle)
Definition: pfsdp_base.cpp:284
PFSDPBase::factory_reset
void factory_reset()
Definition: pfsdp_base.cpp:119
PFSDPBase::get_parameter_int
std::int64_t get_parameter_int(const std::string &param)
Definition: pfsdp_base.cpp:148
param_map_type
std::map< std::string, std::string > param_map_type
Definition: param_map_type.h:20
PFSDPBase::get_part
virtual std::string get_part()
Definition: pfsdp_base.cpp:294
ProtocolInfo
Definition: protocol_info.h:6
PFSDPBase::get_request_bool
bool get_request_bool(const std::string &command, const std::vector< std::string > &json_keys=std::vector< std::string >(), const std::initializer_list< param_type > &query=std::initializer_list< param_type >())
Definition: pfsdp_base.cpp:38
PFSDPBase::reboot_device
bool reboot_device()
Definition: pfsdp_base.cpp:114
PFSDPBase::get_scanoutput_config
virtual void get_scanoutput_config(const std::string &handle)
Definition: pfsdp_base.cpp:220
PFSDPBase::set_connection_failure_cb
void set_connection_failure_cb(std::function< void()> callback)
Definition: pfsdp_base.cpp:103
PFSDPBase::stop_scanoutput
bool stop_scanoutput(const std::string &handle)
Definition: pfsdp_base.cpp:273
ProtocolInfo::protocol_name
std::string protocol_name
Definition: protocol_info.h:9
PFSDPBase::http_interface
HTTPInterfacePtr http_interface
Definition: pfsdp_base.h:36
PFSDPBase::start_scanoutput
bool start_scanoutput()
Definition: pfsdp_base.cpp:267
ScanConfig
Definition: scan_config.h:5
param
T param(const std::string &param_name, const T &default_val)
pfsdp_base.h
ProtocolInfo::isError
bool isError
Definition: protocol_info.h:8
parser_utils::split
const std::vector< std::string > split(const std::string &str, const char delim=';')
Definition: parser_utils.cpp:6
PFSDPBase::get_request
const std::map< std::string, std::string > get_request(const std::string &command, const std::vector< std::string > &json_keys, const std::initializer_list< param_type > &query)
Definition: pfsdp_base.cpp:12
parser_utils.h
ProtocolInfo::version_minor
int version_minor
Definition: protocol_info.h:11
config
config
PFSDPBase::set_scanoutput_config
bool set_scanoutput_config(const std::string &handle, const ScanConfig &config)
Definition: pfsdp_base.cpp:234
parser_utils::to_long
std::int64_t to_long(const std::string &s)
Definition: parser_utils.cpp:13
PFSDPBase::get_parameter_str
std::string get_parameter_str(const std::string &param)
Definition: pfsdp_base.cpp:168
PFSDPBase::get_product
virtual std::string get_product()
Definition: pfsdp_base.cpp:289


pf_driver
Author(s): Harsh Deshpande
autogenerated on Sun Feb 4 2024 03:32:56