2 #include "ui_datastream_mqtt.h" 17 static QString uuid = QString::number(qrand());
18 ui->lineEditClientID->setText(tr(
"Plotjuggler-") + uuid);
22 while(
ui->layoutOptions->count() > 0)
24 auto item =
ui->layoutOptions->takeAt(0);
25 item->widget()->setParent(
nullptr);
30 Ui::DataStreamMQTT*
ui;
57 return QString::number(rc);
66 qDebug() <<
"MQTT Connection lost. Reconnecting...";
75 _this->
_error_msg = QString(
"Failed to start connect, return code %1").arg(
Code(rc));
85 auto it = _this->
_parsers.find(topicName);
89 it = _this->
_parsers.insert( {topicName, parser} ).first;
91 auto& parser = it->second;
96 using namespace std::chrono;
97 auto ts = high_resolution_clock::now().time_since_epoch();
98 double timestamp = 1e-6* double( duration_cast<microseconds>(ts).
count() );
100 parser->parseMessage(msg, timestamp);
102 }
catch (std::exception& ) {
137 _this->
_error_msg = QString(
"Subscription Failure. Code %1").arg(
Code(response->
code));
169 _this->
_error_msg = QString(
"Failed to start subscribe, return code %1").arg(
Code(rc));
176 _protocol_issue(false),
188 QMessageBox::warning(
nullptr,tr(
"DataStream MQTT"),
189 tr(
"Exception while parsing the message. Probably the format was not recognized (%1 used)").
arg( this->
_protocol),
206 QMessageBox::warning(
nullptr,tr(
"Websocket Server"), tr(
"No available MessageParsers"), QMessageBox::Ok);
215 dialog->
ui->comboBoxProtocol->addItem( it.first );
217 if(
auto widget = it.second->optionsWidget() )
219 widget->setVisible(
false);
220 dialog->
ui->layoutOptions->addWidget( widget );
224 std::shared_ptr<MessageParserCreator> parser_creator;
226 connect(dialog->
ui->comboBoxProtocol, qOverload<const QString &>( &QComboBox::currentIndexChanged),
this,
227 [&](QString protocol)
229 if( parser_creator ){
230 QWidget* prev_widget = parser_creator->optionsWidget();
231 prev_widget->setVisible(false);
235 if(
auto widget = parser_creator->optionsWidget() ){
236 widget->setVisible(
true);
242 QString address = settings.value(
"DataStreamMQTT::address").toString();
243 _protocol = settings.value(
"DataStreamMQTT::protocol",
"JSON").toString();
244 _topic_filter = settings.value(
"DataStreamMQTT::filter").toString();
245 _qos = settings.value(
"DataStreamMQTT::qos", 0).toInt();
246 QString
username = settings.value(
"DataStreamMQTT::username",
"").toString();
247 QString
password = settings.value(
"DataStreamMQTT::password",
"").toString();
249 dialog->ui->lineEditAddress->setText( address );
250 dialog->ui->comboBoxProtocol->setCurrentText(
_protocol);
252 dialog->ui->comboBoxQoS->setCurrentIndex(
_qos);
253 dialog->ui->lineEditUsername->setText(
username);
254 dialog->ui->lineEditPassword->setText(
password);
256 if( dialog->exec() == QDialog::Rejected )
261 address = dialog->ui->lineEditAddress->text();
262 _protocol = dialog->ui->comboBoxProtocol->currentText();
264 _qos = dialog->ui->comboBoxQoS->currentIndex();
265 QString cliend_id = dialog->ui->lineEditClientID->text();
266 username = dialog->ui->lineEditUsername->text();
267 password = dialog->ui->lineEditPassword->text();
269 dialog->deleteLater();
272 settings.setValue(
"DataStreamMQTT::address", address);
274 settings.setValue(
"DataStreamMQTT::protocol",
_protocol);
275 settings.setValue(
"DataStreamMQTT::qos",
_qos);
276 settings.setValue(
"DataStreamMQTT::username",
username);
277 settings.setValue(
"DataStreamMQTT::password",
password);
286 address.toStdString().c_str(),
287 cliend_id.toStdString().c_str(),
293 QMessageBox::warning(
nullptr,tr(
"DataStream MQTT"),
294 tr(
"Failed create client MQTT. Error code %1").
arg(
Code(
rc)),
302 QMessageBox::warning(
nullptr,tr(
"DataStream MQTT"),
303 tr(
"Failed to set callbacks. Error code %1").
arg(
Code(
rc)),
321 QMessageBox::warning(
nullptr,tr(
"DataStream MQTT"),
322 tr(
"Failed to start connection. Error code %1").
arg(
Code(
rc)),
330 std::this_thread::sleep_for(std::chrono::milliseconds(10));
335 QMessageBox::warning(
nullptr,tr(
"DataStream MQTT"),
336 tr(
"Failed to start connection. Message: %1").
arg(
_error_msg),
361 QMessageBox::warning(
nullptr,tr(
"DataStream MQTT"),
362 tr(
"Failed to disconned cleanly. Error code %1").
arg(rc),
371 std::this_thread::sleep_for(std::chrono::milliseconds(10));
MQTTAsync_onFailure * onFailure
virtual ~DataStreamMQTT() override
#define MQTTASYNC_NULL_PARAMETER
#define MQTTASYNC_SSL_NOT_SUPPORTED
#define MQTTAsync_responseOptions_initializer
#define MQTTASYNC_FAILURE
int MQTTAsync_disconnect(MQTTAsync handle, const MQTTAsync_disconnectOptions *options)
int MQTTAsync_setCallbacks(MQTTAsync handle, void *context, MQTTAsync_connectionLost *cl, MQTTAsync_messageArrived *ma, MQTTAsync_deliveryComplete *dc)
#define MQTTASYNC_BAD_UTF8_STRING
virtual void shutdown() override
std::unordered_map< std::string, PJ::MessageParserPtr > _parsers
void MQTTAsync_free(void *memory)
void MQTTAsync_freeMessage(MQTTAsync_message **message)
#define MQTTASYNC_DISCONNECTED
void onConnect(void *context, MQTTAsync_successData *response)
int MQTTAsync_connect(MQTTAsync handle, const MQTTAsync_connectOptions *options)
MQTTAsync_connectOptions conn_opts
int MQTTAsync_subscribe(MQTTAsync handle, const char *topic, int qos, MQTTAsync_responseOptions *response)
MessageParserFactory * availableParsers()
#define MQTTASYNC_OPERATION_INCOMPLETE
virtual bool isRunning() const override
#define MQTTASYNC_BAD_MQTT_OPTION
#define MQTTASYNC_BAD_QOS
int MQTTAsync_create(MQTTAsync *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
QTimer _protocol_issue_timer
#define MQTTAsync_disconnectOptions_initializer
void onDisconnectFailure(void *context, MQTTAsync_failureData *response)
MQTTAsync_onFailure * onFailure
MQTTAsync_onFailure * onFailure
#define MQTTASYNC_NO_MORE_MSGIDS
void onSubscribeFailure(void *context, MQTTAsync_failureData *response)
#define MQTTASYNC_MAX_BUFFERED_MESSAGES
#define MQTTAsync_connectOptions_initializer
MQTTAsync_onSuccess * onSuccess
#define MQTTASYNC_BAD_STRUCTURE
void ConnectionLost(void *context, char *cause)
#define MQTTASYNC_WRONG_MQTT_VERSION
virtual bool start(QStringList *) override
PlotDataMapRef & dataMap()
detail::named_arg< Char, T > arg(const Char *name, const T &arg)
void onConnectFailure(void *context, MQTTAsync_failureData *response)
void MQTTAsync_destroy(MQTTAsync *handle)
#define MQTTASYNC_SUCCESS
#define MQTTCLIENT_PERSISTENCE_NONE
int MessageArrived(void *context, char *topicName, int topicLen, MQTTAsync_message *message)
#define MQTTASYNC_PERSISTENCE_ERROR
#define MQTTASYNC_BAD_PROTOCOL
void onSubscribe(void *context, MQTTAsync_successData *response)
#define MQTTASYNC_TOPICNAME_TRUNCATED
MQTTAsync_onSuccess * onSuccess
#define MQTTASYNC_MAX_MESSAGES_INFLIGHT
#define MQTTASYNC_0_LEN_WILL_TOPIC
void onDisconnect(void *context, MQTTAsync_successData *response)
MQTTAsync_onSuccess * onSuccess