nlohmann_parsers.h
Go to the documentation of this file.
1 /*
2  * This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5  */
6 
7 #ifndef NLOHMANN_PARSERS_H
8 #define NLOHMANN_PARSERS_H
9 
10 #include "nlohmann/json.hpp"
12 #include <QDebug>
13 #include <QSettings>
14 
15 using namespace PJ;
16 
18 {
19 public:
20  NlohmannParser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
21  const std::string& stamp_fieldname)
22  : MessageParser(topic_name, data)
23  , _use_message_stamp(use_msg_stamp)
24  , _stamp_fieldname(stamp_fieldname)
25  {
26  }
27 
28 protected:
29  bool parseMessageImpl(double& timestamp);
30 
33  std::string _stamp_fieldname;
34 };
35 
37 {
38 public:
39  JSON_Parser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
40  const std::string& stamp_fieldname)
41  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
42  {
43  }
44 
45  bool parseMessage(const MessageRef msg, double& timestamp) override;
46 };
47 
49 {
50 public:
51  CBOR_Parser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
52  const std::string& stamp_fieldname)
53  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
54  {
55  }
56 
57  bool parseMessage(const MessageRef msg, double& timestamp) override;
58 };
59 
61 {
62 public:
63  BSON_Parser(const std::string& topic_name, PlotDataMapRef& data, bool use_msg_stamp,
64  const std::string& stamp_fieldname)
65  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
66  {
67  }
68 
69  bool parseMessage(const MessageRef msg, double& timestamp) override;
70 };
71 
73 {
74 public:
75  MessagePack_Parser(const std::string& topic_name, PlotDataMapRef& data,
76  bool use_msg_stamp, const std::string& stamp_fieldname)
77  : NlohmannParser(topic_name, data, use_msg_stamp, stamp_fieldname)
78  {
79  }
80 
81  bool parseMessage(const MessageRef msg, double& timestamp) override;
82 };
83 
84 //------------------------------------------
85 
86 #include <QGroupBox>
87 #include <QVBoxLayout>
88 #include <QLineEdit>
89 
90 class QCheckBoxClose : public QGroupBox
91 {
92 public:
93  QLineEdit* lineedit;
94  QGroupBox* groupbox;
95  QCheckBoxClose(QString text) : QGroupBox(text)
96  {
97  QGroupBox::setCheckable(true);
98  QGroupBox::setChecked(false);
99  lineedit = new QLineEdit(this);
100  QVBoxLayout* vbox = new QVBoxLayout;
101  vbox->addSpacing(20);
102  vbox->addWidget(lineedit);
103  QGroupBox::setLayout(vbox);
104  }
105  ~QCheckBoxClose() override
106  {
107  qDebug() << "Destroying QCheckBoxClose";
108  }
109 };
110 
112 {
113 public:
114  NlohmannParserCreator(const char* encoding)
115  {
116  _encoding = encoding;
117  _checkbox_use_timestamp = new QCheckBoxClose("use field as timestamp if available");
118  loadSettings();
119  }
120 
121  template <typename ParserT>
122  MessageParserPtr createParserImpl(const std::string& topic_name, PlotDataMapRef& data)
123  {
124  saveSettings();
125 
126  std::string timestamp_name = _checkbox_use_timestamp->lineedit->text().toStdString();
127  return std::make_shared<ParserT>(
128  topic_name, data, _checkbox_use_timestamp->isChecked(), timestamp_name);
129  }
130 
131  virtual QWidget* optionsWidget()
132  {
133  loadSettings();
134 
135  return _checkbox_use_timestamp;
136  }
137 
138  const char* encoding() const override
139  {
140  return _encoding;
141  }
142 
143  virtual void loadSettings()
144  {
145  QSettings settings;
146  QString prefix = QString("NlohmannParser.") + QString(encoding());
147  bool checked = settings.value(prefix + ".timestampEnabled", false).toBool();
148  QString field = settings.value(prefix + ".timestampFieldName", "").toString();
149 
150  _checkbox_use_timestamp->setChecked(checked);
151  _checkbox_use_timestamp->lineedit->setText(field);
152  }
153 
154  virtual void saveSettings()
155  {
156  QSettings settings;
157  QString prefix = QString("NlohmannParser.") + QString(encoding());
158  settings.setValue(prefix + ".timestampEnabled", _checkbox_use_timestamp->isChecked());
159  settings.setValue(prefix + ".timestampFieldName",
160  _checkbox_use_timestamp->lineedit->text());
161  }
162 
163 protected:
165  const char* _encoding;
166 };
167 
169 {
170 public:
172  {
173  }
174 
175  MessageParserPtr createParser(const std::string& topic_name,
176  const std::string& /*type_name*/,
177  const std::string& /*schema*/,
178  PlotDataMapRef& data) override
179  {
180  return createParserImpl<JSON_Parser>(topic_name, data);
181  }
182  const char* name() const override
183  {
184  return "JSON_ParserFactory";
185  }
186 };
187 
189 {
190 public:
192  {
193  }
194 
195  MessageParserPtr createParser(const std::string& topic_name, const std::string&,
196  const std::string&, PlotDataMapRef& data) override
197  {
198  return createParserImpl<CBOR_Parser>(topic_name, data);
199  }
200  const char* name() const override
201  {
202  return "CBOR_ParserFactory";
203  }
204 };
205 
207 {
208 public:
210  {
211  }
212 
213  MessageParserPtr createParser(const std::string& topic_name, const std::string&,
214  const std::string&, PlotDataMapRef& data) override
215  {
216  return createParserImpl<BSON_Parser>(topic_name, data);
217  }
218  const char* name() const override
219  {
220  return "BSON_ParserFactory";
221  }
222 };
223 
225 {
226 public:
228  {
229  }
230 
231  MessageParserPtr createParser(const std::string& topic_name, const std::string&,
232  const std::string&, PlotDataMapRef& data) override
233  {
234  return createParserImpl<MessagePack_Parser>(topic_name, data);
235  }
236  const char* name() const override
237  {
238  return "MessagePack_ParserFactory";
239  }
240 };
241 
242 #endif // NLOHMANN_PARSERS_H
PJ::MessageParserPtr
std::shared_ptr< MessageParser > MessageParserPtr
Definition: messageparser_base.h:136
NlohmannParserCreator::saveSettings
virtual void saveSettings()
Definition: nlohmann_parsers.h:154
MessagePack_ParserFactory::name
const char * name() const override
Name of the plugin type, NOT the particular instance.
Definition: nlohmann_parsers.h:236
NlohmannParserCreator::_encoding
const char * _encoding
Definition: nlohmann_parsers.h:165
CBOR_ParserFactory
Definition: nlohmann_parsers.h:188
NlohmannParser::_json
nlohmann::json _json
Definition: nlohmann_parsers.h:31
BSON_ParserFactory::createParser
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
Definition: nlohmann_parsers.h:213
BSON_Parser::BSON_Parser
BSON_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
Definition: nlohmann_parsers.h:63
MessagePack_ParserFactory::MessagePack_ParserFactory
MessagePack_ParserFactory()
Definition: nlohmann_parsers.h:227
MessagePack_Parser
Definition: nlohmann_parsers.h:72
QCheckBoxClose::groupbox
QGroupBox * groupbox
Definition: nlohmann_parsers.h:94
BSON_ParserFactory
Definition: nlohmann_parsers.h:206
NlohmannParserCreator::createParserImpl
MessageParserPtr createParserImpl(const std::string &topic_name, PlotDataMapRef &data)
Definition: nlohmann_parsers.h:122
QCheckBoxClose
Definition: nlohmann_parsers.h:90
mqtt_test_proto.msg
msg
Definition: mqtt_test_proto.py:43
NlohmannParserCreator::_checkbox_use_timestamp
QCheckBoxClose * _checkbox_use_timestamp
Definition: nlohmann_parsers.h:164
JSON_ParserFactory::createParser
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
Definition: nlohmann_parsers.h:175
BSON_Parser
Definition: nlohmann_parsers.h:60
NlohmannParserCreator::loadSettings
virtual void loadSettings()
Definition: nlohmann_parsers.h:143
QCheckBoxClose::~QCheckBoxClose
~QCheckBoxClose() override
Definition: nlohmann_parsers.h:105
JSON_ParserFactory::name
const char * name() const override
Name of the plugin type, NOT the particular instance.
Definition: nlohmann_parsers.h:182
NlohmannParserCreator::optionsWidget
virtual QWidget * optionsWidget()
optionsWidget pointer to a persistent widget used to set the plugin options .
Definition: nlohmann_parsers.h:131
JSON_ParserFactory
Definition: nlohmann_parsers.h:168
PJ::MessageParser
The MessageParser is the base class used to parse a message with a specific encoding+schema.
Definition: messageparser_base.h:75
QCheckBoxClose::QCheckBoxClose
QCheckBoxClose(QString text)
Definition: nlohmann_parsers.h:95
MessagePack_Parser::MessagePack_Parser
MessagePack_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
Definition: nlohmann_parsers.h:75
JSON_Parser::JSON_Parser
JSON_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
Definition: nlohmann_parsers.h:39
NlohmannParser::NlohmannParser
NlohmannParser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
Definition: nlohmann_parsers.h:20
PJ::ParserFactoryPlugin
Definition: messageparser_base.h:139
MessagePack_ParserFactory
Definition: nlohmann_parsers.h:224
NlohmannParser
Definition: nlohmann_parsers.h:17
CBOR_ParserFactory::CBOR_ParserFactory
CBOR_ParserFactory()
Definition: nlohmann_parsers.h:191
BSON_ParserFactory::name
const char * name() const override
Name of the plugin type, NOT the particular instance.
Definition: nlohmann_parsers.h:218
json.hpp
NlohmannParser::_use_message_stamp
bool _use_message_stamp
Definition: nlohmann_parsers.h:32
MessagePack_ParserFactory::createParser
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
Definition: nlohmann_parsers.h:231
nlohmann::basic_json
a class to store JSON values
Definition: json.hpp:3326
JSON_ParserFactory::JSON_ParserFactory
JSON_ParserFactory()
Definition: nlohmann_parsers.h:171
CBOR_ParserFactory::name
const char * name() const override
Name of the plugin type, NOT the particular instance.
Definition: nlohmann_parsers.h:200
CBOR_Parser::CBOR_Parser
CBOR_Parser(const std::string &topic_name, PlotDataMapRef &data, bool use_msg_stamp, const std::string &stamp_fieldname)
Definition: nlohmann_parsers.h:51
QCheckBoxClose::lineedit
QLineEdit * lineedit
Definition: nlohmann_parsers.h:93
field
static void field(LexState *ls, ConsControl *cc)
Definition: lparser.c:891
PJ::MessageRef
Definition: messageparser_base.h:28
JSON_Parser
Definition: nlohmann_parsers.h:36
NlohmannParserCreator
Definition: nlohmann_parsers.h:111
BSON_ParserFactory::BSON_ParserFactory
BSON_ParserFactory()
Definition: nlohmann_parsers.h:209
PJ
Definition: dataloader_base.h:16
CBOR_ParserFactory::createParser
MessageParserPtr createParser(const std::string &topic_name, const std::string &, const std::string &, PlotDataMapRef &data) override
Definition: nlohmann_parsers.h:195
mqtt_test.data
dictionary data
Definition: mqtt_test.py:22
PJ::PlotDataMapRef
Definition: plotdata.h:34
CBOR_Parser
Definition: nlohmann_parsers.h:48
NlohmannParserCreator::NlohmannParserCreator
NlohmannParserCreator(const char *encoding)
Definition: nlohmann_parsers.h:114
NlohmannParser::_stamp_fieldname
std::string _stamp_fieldname
Definition: nlohmann_parsers.h:33
messageparser_base.h
NlohmannParserCreator::encoding
const char * encoding() const override
Definition: nlohmann_parsers.h:138


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