line_parser.cpp
Go to the documentation of this file.
1 
2 #include "line_parser.h"
3 
4 using namespace PJ;
5 
6 class MsgParserImpl : public MessageParser
7 {
8 public:
9  MsgParserImpl(const std::string& topic_name, const std::string& type_name,
10  const std::string&, PJ::PlotDataMapRef& data)
11  : MessageParser(topic_name, data), topic_name_(topic_name)
12  {
13  }
14 
15  bool parseMessage(const MessageRef msg, double& timestamp) override
16  {
17  auto ToChar = [](const auto* ptr) { return reinterpret_cast<const char*>(ptr); };
18 
19  const auto str = QString::fromLocal8Bit(ToChar(msg.data()), msg.size());
20 
21  std::string key;
22  std::string prefix;
23  // Obtain the key name from measurement name and tags
24  for (auto line : str.splitRef('\n', PJ::SkipEmptyParts))
25  {
26  auto parts = line.split(' ', PJ::SkipEmptyParts);
27  if (parts.size() != 2 && parts.size() != 3)
28  {
29  continue;
30  }
31  const auto tags = parts[0].split(',', PJ::SkipEmptyParts);
32  const auto fields = parts[1].split(',', PJ::SkipEmptyParts);
33  if (tags.size() < 1 || fields.size() < 1)
34  {
35  continue;
36  }
37  uint64_t timestamp = 0;
38  if (parts.size() == 3)
39  {
40  timestamp = parts[2].toULongLong();
41  }
42  else
43  {
44  using namespace std::chrono;
45  auto now = steady_clock::now();
46  timestamp = duration_cast<nanoseconds>(now.time_since_epoch()).count();
47  }
48  const double ts_sec = double(timestamp) * 1e-9;
49 
50  prefix = topic_name_;
51  for (auto tag : tags)
52  {
53  prefix += '/';
54  auto tag_str = tag.toLocal8Bit();
55  prefix.append(tag_str.data(), tag_str.size());
56  }
57  for (auto field : fields)
58  {
59  const auto field_parts = field.split('=');
60  const auto name = field_parts[0].toLocal8Bit();
61  auto value = field_parts[1].toLocal8Bit();
62 
63  key = prefix;
64  key += '/';
65  key.append(name.data(), name.size());
66 
67  if (value.startsWith('"') && value.endsWith('"'))
68  {
69  auto& data = _plot_data.getOrCreateStringSeries(key);
71  ts_sec, StringRef(value.data() + 1, value.size() - 2)));
72  }
73  else if (value == "t" || value == "T" || value == "true" || value == "True" ||
74  value == "TRUE")
75  {
76  auto& data = _plot_data.getOrCreateNumeric(key);
77  data.pushBack({ ts_sec, 1.0 });
78  }
79  else if (value == "f" || value == "F" || value == "false" || value == "False" ||
80  value == "FALSE")
81  {
82  auto& data = _plot_data.getOrCreateNumeric(key);
83  data.pushBack({ ts_sec, 0.0 });
84  }
85  else
86  {
87  bool ok = false;
88  // remove last character if there is an integer suffix
89  if (value.endsWith('i') || value.endsWith('u'))
90  {
91  value.chop(1);
92  }
93  double num = value.toDouble(&ok);
94  if (ok)
95  {
96  auto& data = _plot_data.getOrCreateNumeric(key);
97  data.pushBack({ ts_sec, num });
98  }
99  }
100  }
101  }
102  return true;
103  }
104 
105 private:
106  std::string topic_name_;
107 };
108 
109 MessageParserPtr ParserLine::createParser(const std::string& topic_name,
110  const std::string& type_name,
111  const std::string& schema,
113 {
114  return std::make_shared<MsgParserImpl>(topic_name, type_name, schema, data);
115 }
PJ::MessageParserPtr
std::shared_ptr< MessageParser > MessageParserPtr
Definition: messageparser_base.h:136
PJ::StringRef
Super simple, unmutable, string_view with small string optimization. If the string is 15 bytes or les...
Definition: string_ref_sso.h:21
sol::type_name
std::string type_name(lua_State *L, type t)
Definition: sol.hpp:8079
MsgParserImpl::MsgParserImpl
MsgParserImpl(const std::string &topic_name, const std::string &type_name, const std::string &, PJ::PlotDataMapRef &data)
Definition: line_parser.cpp:9
PJ::SkipEmptyParts
const auto SkipEmptyParts
Definition: plotdatabase.h:31
mqtt_test_proto.msg
msg
Definition: mqtt_test_proto.py:43
ok
ROSCPP_DECL bool ok()
MsgParserImpl::parseMessage
bool parseMessage(const MessageRef msg, double &timestamp) override
Definition: line_parser.cpp:15
PJ::MessageParser
The MessageParser is the base class used to parse a message with a specific encoding+schema.
Definition: messageparser_base.h:75
PJ::TimeseriesBase< StringRef >::Point
typename PlotDataBase< double, StringRef >::Point Point
Definition: timeseries.h:23
MsgParserImpl
Definition: datatamer_parser.cpp:8
field
static void field(LexState *ls, ConsControl *cc)
Definition: lparser.c:891
PJ::MessageRef
Definition: messageparser_base.h:28
ParserLine::createParser
PJ::MessageParserPtr createParser(const std::string &topic_name, const std::string &type_name, const std::string &schema, PJ::PlotDataMapRef &data) override
Definition: line_parser.cpp:109
PJ
Definition: dataloader_base.h:16
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
PJ::PlotDataMapRef
Definition: plotdata.h:34
line_parser.h
sol::detail::ptr
T * ptr(T &val)
Definition: sol.hpp:2106


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Nov 11 2024 03:23:44