奥特QT DDS 插件库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

93 lines
2.2KB

  1. #include "AQTSampleMachinePlug.hpp"
  2. #include <QDebug>
  3. #include <fastdds/dds/domain/DomainParticipantFactory.hpp>
  4. #include "HelloWorld.hpp"
  5. #include "HelloWorldPubSubTypes.hpp"
  6. AQTSampleMachinePlug::AQTSampleMachinePlug(QObject *parent)
  7. : QObject(parent)
  8. , participant_(nullptr)
  9. , publisher_(nullptr)
  10. , topic_(nullptr)
  11. , writer_(nullptr)
  12. , message_index_(0)
  13. {
  14. type_ = TypeSupport(new HelloWorldPubSubType());
  15. }
  16. AQTSampleMachinePlug::~AQTSampleMachinePlug()
  17. {
  18. if (writer_ != nullptr) {
  19. publisher_->delete_datawriter(writer_);
  20. }
  21. if (topic_ != nullptr) {
  22. participant_->delete_topic(topic_);
  23. }
  24. if (publisher_ != nullptr) {
  25. participant_->delete_publisher(publisher_);
  26. }
  27. if (participant_ != nullptr) {
  28. DomainParticipantFactory::get_instance()->delete_participant(participant_);
  29. }
  30. }
  31. bool AQTSampleMachinePlug::init()
  32. {
  33. DomainParticipantQos participantQos;
  34. participantQos.name("Participant_publisher");
  35. participant_ = DomainParticipantFactory::get_instance()->create_participant(0, participantQos);
  36. if (participant_ == nullptr) {
  37. return false;
  38. }
  39. type_.register_type(participant_);
  40. publisher_ = participant_->create_publisher(PUBLISHER_QOS_DEFAULT);
  41. if (publisher_ == nullptr) {
  42. return false;
  43. }
  44. topic_ = participant_->create_topic(
  45. "HelloWorldTopic",
  46. "HelloWorld",
  47. TOPIC_QOS_DEFAULT);
  48. if (topic_ == nullptr) {
  49. return false;
  50. }
  51. writer_ = publisher_->create_datawriter(
  52. topic_,
  53. DATAWRITER_QOS_DEFAULT);
  54. if (writer_ == nullptr) {
  55. return false;
  56. }
  57. return true;
  58. }
  59. void AQTSampleMachinePlug::publishOnce()
  60. {
  61. HelloWorld st;
  62. st.index(message_index_);
  63. st.message("Hello World " + std::to_string(message_index_));
  64. writer_->write(&st);
  65. emit messagePublished(QString::fromStdString(st.message()), message_index_);
  66. message_index_++;
  67. }
  68. QString AQTSampleMachinePlug::echo(const QString& message)
  69. {
  70. return QString("Plugin Echo: %1").arg(message);
  71. }
  72. int AQTSampleMachinePlug::calculate(int a, int b)
  73. {
  74. return a + b;
  75. }