奥特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.

175 line
5.6KB

  1. #pragma once
  2. #include <QObject>
  3. #include <QtPlugin>
  4. #include "interfaces/plugin_interface.hpp"
  5. #include <fastdds/dds/domain/DomainParticipant.hpp>
  6. #include <fastdds/dds/publisher/Publisher.hpp>
  7. #include <fastdds/dds/publisher/DataWriter.hpp>
  8. #include <fastdds/dds/subscriber/Subscriber.hpp>
  9. #include <fastdds/dds/subscriber/DataReader.hpp>
  10. #include <fastdds/dds/topic/Topic.hpp>
  11. #include "DDSSampleMachine.hpp"
  12. #include "DDSSampleMachinePubSubTypes.hpp"
  13. #include "PLCController.hpp"
  14. #include "ConfigDialog.hpp"
  15. using namespace eprosima::fastdds::dds;
  16. using namespace SampleModule;
  17. // 导出宏定义
  18. #ifdef AQTSAMPLEMACHINEPLUG_LIBRARY
  19. #define AQTSAMPLEMACHINEPLUG_EXPORT Q_DECL_EXPORT
  20. #else
  21. #define AQTSAMPLEMACHINEPLUG_EXPORT Q_DECL_IMPORT
  22. #endif
  23. /**
  24. * @brief 采样机DDS插件类
  25. *
  26. * 实现采样机的DDS通信功能,包括:
  27. * - 采样机控制命令的发送
  28. * - 采样机状态的接收
  29. * - 数据点信息的管理
  30. */
  31. class AQTSAMPLEMACHINEPLUG_EXPORT AQTSampleMachinePlug : public QObject, public AuseftDDSPluginInterface, public DataReaderListener
  32. {
  33. Q_OBJECT
  34. Q_PLUGIN_METADATA(IID AuseftDDSPluginInterface_iid FILE "AQTSampleMachinePlug.json")
  35. Q_INTERFACES(AuseftDDSPluginInterface)
  36. public:
  37. /**
  38. * @brief 构造函数
  39. * @param parent 父对象指针
  40. */
  41. explicit AQTSampleMachinePlug(QObject *parent = nullptr);
  42. /**
  43. * @brief 析构函数
  44. * 负责清理DDS资源
  45. */
  46. ~AQTSampleMachinePlug();
  47. // 实现插件接口
  48. /**
  49. * @brief 初始化插件
  50. * @param domainId DDS域ID
  51. * @param domainName DDS域名称
  52. * @return 初始化是否成功
  53. */
  54. Q_INVOKABLE bool init(uint32_t domainId, const QString& domainName) override;
  55. /**
  56. * @brief 获取所有数据点信息
  57. * @return 数据点信息列表
  58. */
  59. Q_INVOKABLE QList<DataPointInfo> getDataPoints() override;
  60. /**
  61. * @brief 发布数据到DDS网络
  62. * @param dataList 要发布的数据列表
  63. * @return 发布是否成功
  64. */
  65. Q_INVOKABLE bool publishData(const QList<DataItem>& dataList) override;
  66. /**
  67. * @brief 显示配置对话框
  68. * @return 是否成功显示对话框
  69. */
  70. Q_INVOKABLE bool showConfig() override;
  71. /**
  72. * @brief 显示配置对话框
  73. * @return 是否成功显示对话框
  74. */
  75. Q_INVOKABLE bool showConfigDialog() override;
  76. /**
  77. * @brief 配置插件
  78. * @return 是否成功显示配置界面
  79. */
  80. Q_INVOKABLE bool config() override;
  81. // 实现 DataReaderListener 接口
  82. void on_data_available(DataReader* reader) override;
  83. private slots:
  84. // 处理配置对话框的配置完成信号
  85. void onDialogConfigurationChanged(const QString& config);
  86. private:
  87. // DDS 实体
  88. DomainParticipant* participant_; ///< DDS参与者
  89. Publisher* publisher_; ///< DDS发布者
  90. Subscriber* subscriber_; ///< DDS订阅者
  91. // 命令相关(改为订阅)
  92. Topic* commandTopic_; ///< 命令主题
  93. DataReader* commandReader_; ///< 命令读取器(改为读取器)
  94. TypeSupport commandType_; ///< 命令类型支持
  95. // 配置相关(改为订阅)
  96. Topic* configTopic_; ///< 配置主题
  97. DataReader* configReader_; ///< 配置读取器(改为读取器)
  98. TypeSupport configType_; ///< 配置类型支持
  99. // 采样点相关(改为订阅)
  100. Topic* pointsTopic_; ///< 采样点主题
  101. DataReader* pointsReader_; ///< 采样点读取器
  102. TypeSupport pointsType_; ///< 采样点类型支持
  103. // 机器信息相关(保持发布)
  104. Topic* machineinfoTopic_; ///< 机器信息主题
  105. DataWriter* machineinfoWriter_; ///< 机器信息写入器
  106. TypeSupport machineinfoType_; ///< 机器信息类型支持
  107. // PLC 相关
  108. PLCController plc_;
  109. bool plcConnected_;
  110. // PLC 数据区定义
  111. struct PLCDataAreas {
  112. static const int DB_CONFIG = 1; // 配置数据块号
  113. static const int DB_COMMAND = 2; // 命令数据块号
  114. static const int DB_STATUS = 3; // 状态数据块号
  115. static const int DB_POINTS = 4; // 点位数据块号
  116. };
  117. // PLC 通信方法
  118. bool writeToPLC(const SampleConfig& config);
  119. bool writeToPLC(const SampleCommand& command);
  120. bool writeToPLC(const SamplePoints& points);
  121. bool readFromPLC(SampleMachineInfo& info);
  122. // 初始化辅助函数
  123. /**
  124. * @brief 初始化DDS实体
  125. * @return 初始化是否成功
  126. */
  127. bool initDDSEntities();
  128. /**
  129. * @brief 清理DDS实体
  130. */
  131. void cleanupDDSEntities();
  132. // 数据转换辅助函数
  133. // DataItem 列表转换为具体类型
  134. SampleConfig dataItemsToConfig(const QList<DataItem>& items);
  135. SampleCommand dataItemsToCommand(const QList<DataItem>& items);
  136. SamplePoints dataItemsToPoints(const QList<DataItem>& items);
  137. SampleMachineInfo dataItemsToMachineInfo(const QList<DataItem>& items);
  138. // 具体类型转换为 DataItem 列表
  139. QList<DataItem> configToDataItems(const SampleConfig& config);
  140. QList<DataItem> commandToDataItems(const SampleCommand& command);
  141. QList<DataItem> pointsToDataItems(const SamplePoints& points);
  142. QList<DataItem> machineInfoToDataItems(const SampleMachineInfo& info);
  143. // 应用新的配置
  144. bool applyConfiguration(const QString& config);
  145. ConfigDialog* configDialog_;
  146. };