parameters.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2018 The urg_stamped Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  * http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef SCIP2_RESPONSE_PARAMETERS_H
00018 #define SCIP2_RESPONSE_PARAMETERS_H
00019 
00020 #include <boost/asio.hpp>
00021 
00022 #include <string>
00023 #include <map>
00024 
00025 #include <scip2/response/abstract.h>
00026 
00027 namespace scip2
00028 {
00029 class ResponseParams : public Response
00030 {
00031 public:
00032   using Callback = boost::function<void(
00033       const boost::posix_time::ptime &,
00034       const std::string &,
00035       const std::string &,
00036       const std::map<std::string, std::string> &)>;
00037 
00038 protected:
00039   Callback cb_;
00040 
00041 public:
00042   virtual std::string getCommandCode() const = 0;
00043   void operator()(
00044       const boost::posix_time::ptime &time_read,
00045       const std::string &echo_back,
00046       const std::string &status,
00047       std::istream &stream)
00048   {
00049     std::map<std::string, std::string> params;
00050     if (status != "00")
00051     {
00052       if (cb_)
00053         cb_(time_read, echo_back, status, params);
00054       std::cout << echo_back << " errored with " << status << std::endl;
00055       return;
00056     }
00057     std::string line;
00058     while (std::getline(stream, line))
00059     {
00060       if (line.size() == 0)
00061         break;
00062       const auto delm = std::find(line.begin(), line.end(), ':');
00063       if (delm == line.end())
00064       {
00065         std::cout << "Parameter decode error" << std::endl;
00066         return;
00067       }
00068       const auto end = std::find(line.begin(), line.end(), ';');
00069       if (end == line.end())
00070       {
00071         std::cout << "Parameter decode error" << std::endl;
00072         return;
00073       }
00074       const uint8_t checksum = line.back();
00075       uint8_t sum = 0;
00076       for (auto it = line.begin(); it != end; ++it)
00077       {
00078         sum += *it;
00079       }
00080       if ((sum & 0x3F) + 0x30 != checksum)
00081       {
00082         std::cerr << "Checksum mismatch; parameters dropped" << std::endl;
00083         return;
00084       }
00085       const std::string key(line.begin(), delm);
00086       const std::string value(delm + 1, end);
00087       params[key] = value;
00088     }
00089     if (cb_)
00090       cb_(time_read, echo_back, status, params);
00091   }
00092   void registerCallback(Callback cb)
00093   {
00094     cb_ = cb;
00095   }
00096 };
00097 
00098 class ResponsePP : public ResponseParams
00099 {
00100 public:
00101   std::string getCommandCode() const
00102   {
00103     return std::string("PP");
00104   }
00105 };
00106 
00107 class ResponseVV : public ResponseParams
00108 {
00109 public:
00110   std::string getCommandCode() const
00111   {
00112     return std::string("VV");
00113   }
00114 };
00115 
00116 class ResponseII : public ResponseParams
00117 {
00118 public:
00119   std::string getCommandCode() const
00120   {
00121     return std::string("II");
00122   }
00123 };
00124 
00125 }  // namespace scip2
00126 
00127 #endif  // SCIP2_RESPONSE_PARAMETERS_H


urg_stamped
Author(s): Atsushi Watanabe
autogenerated on Thu Jun 6 2019 18:59:51