Program Listing for File DdsCommand.hpp

Return to documentation for file (sinspekto/DdsCommand.hpp)

#pragma once

#include <cinttypes>
#include "sinspekto/QtToDds.hpp"
#include <QTimer>

class DdsCommandSubscriber : public QObject
{
  Q_OBJECT
  Q_PROPERTY(fkin::CommandType command READ command NOTIFY commandChanged)
  Q_PROPERTY(QString commandName READ commandName NOTIFY commandNameChanged)

public:
  explicit DdsCommandSubscriber(QObject *parent = nullptr);
  virtual ~DdsCommandSubscriber();

  fkin::CommandType command() const;
  QString commandName() const;
  Q_INVOKABLE void init(
      QtToDds* dds,
      const QString& topic,
      const QString& recipient,
      const QString& replyTopic=QString());

signals:
  void commandChanged(fkin::CommandType command);
  void commandNameChanged(QString commandName);
  void eventHeard();

public slots:
  void updateCommand();
  void sendReply();

private:
  std::unique_ptr<sinspekto::Reader<fkin::Command>> m_reader;
  std::unique_ptr<sinspekto::Writer<fkin::CommandResponse>> m_writer;
  QString m_recipient;
};

class DdsCommandPublisher : public QObject
{
  Q_OBJECT
  Q_PROPERTY(fkin::CommandType command READ command WRITE setCommand NOTIFY commandChanged)
  Q_PROPERTY(QString commandName READ commandName NOTIFY commandNameChanged)
  Q_PROPERTY(QString responseMessage READ responseMessage NOTIFY responseMessageChanged)
  Q_PROPERTY(bool responseStatus READ responseStatus NOTIFY responseStatusChanged)
public:
  explicit DdsCommandPublisher(QObject *parent = nullptr);
  virtual ~DdsCommandPublisher();
  fkin::CommandType command() const;
  QString commandName() const;
  QString responseMessage() const;
  bool responseStatus() const;
  Q_INVOKABLE void init(
      QtToDds* dds,
      const QString& topic,
      const QString& recipient,
      const QString& responseTopic=QString(),
      int responseTimeout_ms=1000);

signals:
  void commandChanged(fkin::CommandType command);
  void commandNameChanged(const QString& commandName);
  void responseMessageChanged(const QString& responseMessage);
  void responseStatusChanged(bool responseStatus);
  void gotResponse();
  void noResponse();
  void confirmedResponseSeqNr(int sequenceNr);

public slots:
  void setCommand(fkin::CommandType command);
  void updateResponse();
  void setResponseMessage(const QString& responseMessage);
  void setResponseStatus(bool status);
  void handleNoResponse();

private:
  std::unique_ptr<sinspekto::Writer<fkin::Command>> m_writer;
  std::unique_ptr<sinspekto::Reader<fkin::CommandResponse>> m_reader;
  QString m_recipient;
  QString m_responseMessage;
  bool m_responseStatus;
  std::int32_t m_awaitingSeqNr;
  QTimer m_timer;
};