parameters.h
Go to the documentation of this file.
1 /*
2  * Copyright 2018 The urg_stamped Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SCIP2_RESPONSE_PARAMETERS_H
18 #define SCIP2_RESPONSE_PARAMETERS_H
19 
20 #include <boost/asio.hpp>
21 
22 #include <string>
23 #include <map>
24 
26 #include <scip2/logger.h>
27 #include <scip2/param.h>
28 
29 namespace scip2
30 {
31 class ResponseParams : public Response
32 {
33 public:
34  using Callback = boost::function<void(
35  const boost::posix_time::ptime&,
36  const std::string&,
37  const std::string&,
38  const std::map<std::string, std::string>&)>;
39 
40 protected:
42 
43 public:
44  virtual std::string getCommandCode() const = 0;
45  void operator()(
46  const boost::posix_time::ptime& time_read,
47  const std::string& echo_back,
48  const std::string& status,
49  std::istream& stream)
50  {
51  std::map<std::string, std::string> params;
52  if (status != "00")
53  {
54  if (cb_)
55  cb_(time_read, echo_back, status, params);
56  return;
57  }
58  std::string line;
59  while (std::getline(stream, line))
60  {
61  if (line.size() == 0)
62  break;
63 
64  const ParsedParam p = parseParamLine(line);
65  if (!p.parsed)
66  {
67  logger::error() << "Parameter decode error: " << p.error << std::endl;
68  return;
69  }
70  if (!p.checksum_matched)
71  {
72  logger::error() << "Checksum mismatch; parameters dropped: " << p.error << std::endl;
73  return;
74  }
75  params[p.key] = p.value;
76  }
77  if (cb_)
78  cb_(time_read, echo_back, status, params);
79  }
81  {
82  cb_ = cb;
83  }
84 };
85 
86 class ResponsePP : public ResponseParams
87 {
88 public:
89  std::string getCommandCode() const
90  {
91  return std::string("PP");
92  }
93 };
94 
95 class ResponseVV : public ResponseParams
96 {
97 public:
98  std::string getCommandCode() const
99  {
100  return std::string("VV");
101  }
102 };
103 
105 {
106 public:
107  std::string getCommandCode() const
108  {
109  return std::string("II");
110  }
111 };
112 
113 } // namespace scip2
114 
115 #endif // SCIP2_RESPONSE_PARAMETERS_H
bool checksum_matched
Definition: param.h:28
std::string getCommandCode() const
Definition: parameters.h:89
void registerCallback(Callback cb)
Definition: parameters.h:80
virtual std::string getCommandCode() const =0
boost::function< void(const boost::posix_time::ptime &, const std::string &, const std::string &, const std::map< std::string, std::string > &)> Callback
Definition: parameters.h:38
std::string key
Definition: param.h:29
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream)
Definition: parameters.h:45
std::string getCommandCode() const
Definition: parameters.h:107
static ParsedParam parseParamLine(const std::string &line)
Definition: param.h:40
std::string getCommandCode() const
Definition: parameters.h:98
std::string error
Definition: param.h:31
std::string value
Definition: param.h:30
std::ostream & error()
Definition: logger.cpp:105


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Mon Jul 3 2023 02:11:32