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 
27 namespace scip2
28 {
29 class ResponseParams : public Response
30 {
31 public:
32  using Callback = boost::function<void(
33  const boost::posix_time::ptime &,
34  const std::string &,
35  const std::string &,
36  const std::map<std::string, std::string> &)>;
37 
38 protected:
40 
41 public:
42  virtual std::string getCommandCode() const = 0;
43  void operator()(
44  const boost::posix_time::ptime &time_read,
45  const std::string &echo_back,
46  const std::string &status,
47  std::istream &stream)
48  {
49  std::map<std::string, std::string> params;
50  if (status != "00")
51  {
52  if (cb_)
53  cb_(time_read, echo_back, status, params);
54  std::cout << echo_back << " errored with " << status << std::endl;
55  return;
56  }
57  std::string line;
58  while (std::getline(stream, line))
59  {
60  if (line.size() == 0)
61  break;
62  const auto delm = std::find(line.begin(), line.end(), ':');
63  if (delm == line.end())
64  {
65  std::cout << "Parameter decode error" << std::endl;
66  return;
67  }
68  const auto end = std::find(line.begin(), line.end(), ';');
69  if (end == line.end())
70  {
71  std::cout << "Parameter decode error" << std::endl;
72  return;
73  }
74  const uint8_t checksum = line.back();
75  uint8_t sum = 0;
76  for (auto it = line.begin(); it != end; ++it)
77  {
78  sum += *it;
79  }
80  if ((sum & 0x3F) + 0x30 != checksum)
81  {
82  std::cerr << "Checksum mismatch; parameters dropped" << std::endl;
83  return;
84  }
85  const std::string key(line.begin(), delm);
86  const std::string value(delm + 1, end);
87  params[key] = value;
88  }
89  if (cb_)
90  cb_(time_read, echo_back, status, params);
91  }
93  {
94  cb_ = cb;
95  }
96 };
97 
98 class ResponsePP : public ResponseParams
99 {
100 public:
101  std::string getCommandCode() const
102  {
103  return std::string("PP");
104  }
105 };
106 
108 {
109 public:
110  std::string getCommandCode() const
111  {
112  return std::string("VV");
113  }
114 };
115 
117 {
118 public:
119  std::string getCommandCode() const
120  {
121  return std::string("II");
122  }
123 };
124 
125 } // namespace scip2
126 
127 #endif // SCIP2_RESPONSE_PARAMETERS_H
void registerCallback(Callback cb)
Definition: parameters.h:92
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:36
void operator()(const boost::posix_time::ptime &time_read, const std::string &echo_back, const std::string &status, std::istream &stream)
Definition: parameters.h:43
std::string getCommandCode() const
Definition: parameters.h:110
std::string getCommandCode() const
Definition: parameters.h:119
std::string getCommandCode() const
Definition: parameters.h:101


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 19:55:58