182 lines
6.2 KiB
C++
182 lines
6.2 KiB
C++
|
|
#ifndef IPLUGIN_H
|
|||
|
|
#define IPLUGIN_H
|
|||
|
|
|
|||
|
|
#include <QObject>
|
|||
|
|
#include <QWidget>
|
|||
|
|
#include <QString>
|
|||
|
|
#include <QIcon>
|
|||
|
|
#include <QVariant>
|
|||
|
|
#include <QJsonObject>
|
|||
|
|
#include "mavlink_types.h"
|
|||
|
|
|
|||
|
|
class PluginContext;
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// VehicleState — 载具状态快照(插件通过此结构获取飞控数据)
|
|||
|
|
// ============================================================
|
|||
|
|
struct VehicleState
|
|||
|
|
{
|
|||
|
|
int sysid = 0;
|
|||
|
|
int compid = 0;
|
|||
|
|
double roll = 0;
|
|||
|
|
double pitch = 0;
|
|||
|
|
double yaw = 0;
|
|||
|
|
double altitude = 0;
|
|||
|
|
double groundspeed = 0;
|
|||
|
|
double airspeed = 0;
|
|||
|
|
double latitude = 0;
|
|||
|
|
double longitude = 0;
|
|||
|
|
int satellites = 0;
|
|||
|
|
int fixType = 0;
|
|||
|
|
double batteryRemaining = 0;
|
|||
|
|
QString mode;
|
|||
|
|
QString armStatus;
|
|||
|
|
bool isOnline = false;
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// PluginCommand — 插件向上层发送的指令(主界面或飞控)
|
|||
|
|
// ============================================================
|
|||
|
|
struct PluginCommand
|
|||
|
|
{
|
|||
|
|
enum Type {
|
|||
|
|
SendMavLink, // 发送 MAVLink 指令到飞控
|
|||
|
|
ShowMessage, // 在主界面显示消息
|
|||
|
|
RequestVehicleInfo, // 请求载具信息
|
|||
|
|
CustomAction // 自定义动作
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
Type type = ShowMessage;
|
|||
|
|
QString sourcePlugin; // 来源插件名
|
|||
|
|
QString target; // 目标(飞控ID/UI元素ID)
|
|||
|
|
QString action; // 动作名
|
|||
|
|
QVariantMap params; // 参数
|
|||
|
|
uint16_t mavCmd = 0; // MAVLink 指令号(type==SendMavLink 时)
|
|||
|
|
float mavParams[7] = {}; // MAVLink 指令参数
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// IPlugin — 工具箱插件接口 v2.0
|
|||
|
|
//
|
|||
|
|
// 插件通过此接口与地面站交互,分为三个层面:
|
|||
|
|
// 1. 生命周期:创建、激活、停用、销毁
|
|||
|
|
// 2. 数据输入:MAVLink 消息、载具状态、用户指令
|
|||
|
|
// 3. 数据输出:发送指令、消息通知、状态变更
|
|||
|
|
//
|
|||
|
|
// 继承方式:
|
|||
|
|
// class MyPlugin : public QObject, public IPlugin
|
|||
|
|
// {
|
|||
|
|
// Q_OBJECT
|
|||
|
|
// Q_INTERFACES(IPlugin)
|
|||
|
|
// Q_PLUGIN_METADATA(IID IPlugin_iid)
|
|||
|
|
// };
|
|||
|
|
// ============================================================
|
|||
|
|
class IPlugin
|
|||
|
|
{
|
|||
|
|
public:
|
|||
|
|
virtual ~IPlugin() = default;
|
|||
|
|
|
|||
|
|
// ==========================================================
|
|||
|
|
// 1. 元信息
|
|||
|
|
// ==========================================================
|
|||
|
|
virtual QString name() const = 0; // 唯一标识,如 "MAVLinkInspector"
|
|||
|
|
virtual QString title() const = 0; // 显示标题,如 "MAVLink 检查器"
|
|||
|
|
virtual QString version() const = 0; // 版本号 "1.0.0"
|
|||
|
|
virtual QString description() const { return QString(); }
|
|||
|
|
virtual QString author() const { return QString(); }
|
|||
|
|
virtual QIcon icon() const { return QIcon(); }
|
|||
|
|
|
|||
|
|
// ==========================================================
|
|||
|
|
// 2. 窗口 / UI
|
|||
|
|
// ==========================================================
|
|||
|
|
// 创建插件的 UI 面板(可能被调用多次,每次返回同一实例或新实例)
|
|||
|
|
virtual QWidget *createWidget(QWidget *parent) = 0;
|
|||
|
|
|
|||
|
|
// 插件偏好的停靠位置(工具箱可以据此排布)
|
|||
|
|
enum DockArea { Left, Right, Top, Bottom, Center, Floating };
|
|||
|
|
virtual DockArea preferredDockArea() const { return Center; }
|
|||
|
|
|
|||
|
|
// ==========================================================
|
|||
|
|
// 3. 生命周期
|
|||
|
|
// ==========================================================
|
|||
|
|
// 旧版 initialize(无上下文),保持向后兼容
|
|||
|
|
virtual bool initialize() { return true; }
|
|||
|
|
|
|||
|
|
// 新版 initialize(接收 PluginContext,VS Code 风格)
|
|||
|
|
// ExtensionHost 优先调用此版本
|
|||
|
|
virtual bool initialize(PluginContext *) { return initialize(); }
|
|||
|
|
|
|||
|
|
virtual void onActivated() {} // 切换到本插件时
|
|||
|
|
virtual void onDeactivated() {} // 切换到其他插件时
|
|||
|
|
virtual void onClose() {} // 关闭/卸载时
|
|||
|
|
|
|||
|
|
// ==========================================================
|
|||
|
|
// 4. 数据输入(主界面 → 插件)
|
|||
|
|
// ==========================================================
|
|||
|
|
|
|||
|
|
// 收到 MAVLink 原始消息
|
|||
|
|
virtual void onMavLinkMessage(const mavlink_message_t & /*msg*/) {}
|
|||
|
|
|
|||
|
|
// 载具状态更新(周期性推送)
|
|||
|
|
virtual void onVehicleStateChanged(int vehicleId, const VehicleState & /*state*/) {}
|
|||
|
|
|
|||
|
|
// 载具增删
|
|||
|
|
virtual void onVehicleAdded(int vehicleId) {}
|
|||
|
|
virtual void onVehicleRemoved(int vehicleId) {}
|
|||
|
|
|
|||
|
|
// 用户选中载具切换
|
|||
|
|
virtual void onVehicleSelected(int vehicleId) {}
|
|||
|
|
|
|||
|
|
// 通讯状态变化
|
|||
|
|
enum CommStatus { Connected, Lost, Recovering };
|
|||
|
|
virtual void onCommStatusChanged(CommStatus /*status*/) {}
|
|||
|
|
|
|||
|
|
// 主界面下发的命令(如:设置面板要求插件刷新参数)
|
|||
|
|
virtual void onCommand(const QString &command, const QVariantMap ¶ms) {}
|
|||
|
|
|
|||
|
|
// ==========================================================
|
|||
|
|
// 5. 数据输出(插件 → 主界面)—— 通过信号实现
|
|||
|
|
//
|
|||
|
|
// 插件类应声明以下信号(在 QObject 子类中):
|
|||
|
|
//
|
|||
|
|
// void sendCommand(const PluginCommand &cmd);
|
|||
|
|
// 插件向上层发送指令
|
|||
|
|
//
|
|||
|
|
// void showStatus(const QString &text, int timeout = 0);
|
|||
|
|
// 在状态栏显示消息
|
|||
|
|
//
|
|||
|
|
// void dataChanged(const QString &key, const QVariant &value);
|
|||
|
|
// 通知主界面数据变更(如自定义参数修改)
|
|||
|
|
//
|
|||
|
|
// void requestData(const QString &what, const QVariantMap ¶ms);
|
|||
|
|
// 请求主界面提供数据
|
|||
|
|
// ==========================================================
|
|||
|
|
|
|||
|
|
// 辅助:通过 QObject 子类安全地发出指令
|
|||
|
|
// 插件实现类应在 QObject 子类中调用此辅助方法
|
|||
|
|
template<typename PluginClass>
|
|||
|
|
static void emitCommand(PluginClass *plugin, const PluginCommand &cmd)
|
|||
|
|
{
|
|||
|
|
if (auto *obj = dynamic_cast<QObject*>(plugin))
|
|||
|
|
QMetaObject::invokeMethod(obj, "sendCommand", Qt::AutoConnection,
|
|||
|
|
Q_ARG(PluginCommand, cmd));
|
|||
|
|
}
|
|||
|
|
};
|
|||
|
|
|
|||
|
|
// Qt 插件接口标识
|
|||
|
|
#define IPlugin_iid "com.gcs.IPlugin/2.0"
|
|||
|
|
Q_DECLARE_INTERFACE(IPlugin, IPlugin_iid)
|
|||
|
|
|
|||
|
|
// ============================================================
|
|||
|
|
// PluginCommand 的 Qt 元类型注册辅助
|
|||
|
|
// ============================================================
|
|||
|
|
inline void registerPluginTypes()
|
|||
|
|
{
|
|||
|
|
qRegisterMetaType<PluginCommand>("PluginCommand");
|
|||
|
|
qRegisterMetaType<PluginCommand::Type>("PluginCommand::Type");
|
|||
|
|
qRegisterMetaType<VehicleState>("VehicleState");
|
|||
|
|
qRegisterMetaType<IPlugin::CommStatus>("IPlugin::CommStatus");
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
#endif // IPLUGIN_H
|