5分钟掌握Open MCT API:从安装到自定义插件开发完整指南

【免费下载链接】openmct A web based mission control framework. 【免费下载链接】openmct 项目地址: https://gitcode.com/GitHub_Trending/ope/openmct

Open MCT是一款强大的基于Web的任务控制框架,专为实时数据可视化和任务管理设计。本指南将帮助你快速掌握Open MCT API,从环境搭建到自定义插件开发,让你在短时间内具备扩展和定制Open MCT的能力。

🚀 快速安装与启动

一键安装步骤

首先,通过Git克隆仓库并安装依赖:

git clone https://gitcode.com/GitHub_Trending/ope/openmct
cd openmct
npm install

启动开发服务器

安装完成后,启动内置的开发服务器:

npm start

服务器启动后,访问 http://localhost:8080 即可看到Open MCT的主界面。

Open MCT启动界面

Open MCT启动界面,展示了其宇宙主题的视觉设计

🔑 核心API概览

Open MCT的核心功能通过模块化API提供,主要包括:

  • 对象API:管理领域对象的创建与交互
  • 插件系统:扩展Open MCT功能的主要方式
  • 遥测API:处理实时和历史数据
  • 时间API:控制时间范围和时钟同步

最小化应用示例

创建一个简单的HTML文件,引入Open MCT并启动应用:

<!DOCTYPE html>
<html>
<head>
  <title>Open MCT应用</title>
  <script src="dist/openmct.js"></script>
  <script>
    // 安装必要插件
    openmct.install(openmct.plugins.LocalStorage());
    openmct.install(openmct.plugins.MyItems());
    openmct.install(openmct.plugins.UTCTimeSystem());
    
    // 设置时间系统
    openmct.time.setTimeSystem('utc');
    
    // 启动应用
    openmct.start();
  </script>
</head>
<body></body>
</html>

⚙️ 插件开发入门

插件基础结构

Open MCT插件是一个返回安装函数的对象,基本结构如下:

export default function MyPlugin() {
  return function install(openmct) {
    // 插件逻辑
  };
}

创建你的第一个插件

下面是一个简单的"Hello World"插件示例:

openmct.install(function install(openmct) {
  // 添加自定义操作
  openmct.actions.addAction({
    name: 'Hello World',
    description: '显示欢迎消息',
    key: 'hello-world',
    invoke: function () {
      alert('Hello, Open MCT!');
    }
  });
});

将此代码保存为 src/plugins/helloWorld/plugin.js,然后在应用初始化时安装:

openmct.install(openmct.plugins.helloWorld());

📊 遥测数据集成

Open MCT的核心功能之一是实时遥测数据可视化。以下是集成遥测数据源的基本步骤:

1. 定义遥测对象

{
  "identifier": {
    "namespace": "example.telemetry",
    "key": "temperature"
  },
  "name": "温度传感器",
  "type": "telemetry",
  "telemetry": {
    "values": [
      {
        "key": "value",
        "name": "温度",
        "unit": "°C",
        "format": "float",
        "hints": { "range": 1 }
      },
      {
        "key": "timestamp",
        "name": "时间",
        "format": "utc",
        "hints": { "domain": 1 }
      }
    ]
  }
}

2. 创建遥测提供者

openmct.telemetry.addProvider({
  supportsRequest: function (domainObject) {
    return domainObject.type === 'telemetry';
  },
  request: function (domainObject, options) {
    // 返回遥测数据的Promise
    return fetch('/api/telemetry/' + domainObject.identifier.key)
      .then(response => response.json());
  }
});

遥测数据可视化

Open MCT的遥测数据绘图功能,展示了温度随时间变化的波形图

⏱️ 时间系统配置

时间管理是Open MCT的核心功能,通过时间API可以配置时间系统、时钟和时间范围:

配置时间系统

openmct.time.addTimeSystem({
  key: 'custom-time',
  name: '自定义时间',
  cssClass: 'icon-clock',
  timeFormat: 'utc',
  durationFormat: 'duration',
  isUTCBased: true
});

// 设置为活动时间系统
openmct.time.setTimeSystem('custom-time');

时间 conductor 配置

时间Conductor

Open MCT的时间Conductor组件,用于控制时间范围和模式

📝 最佳实践与资源

推荐学习路径

  1. 官方文档:详细API参考见 API.md
  2. 示例插件:参考 example/ 目录下的各类示例
  3. 核心插件:研究 src/plugins/ 了解内置功能实现

性能优化技巧

  • 使用可见性渲染:仅在视图可见时执行渲染逻辑
  • 合理使用遥测数据请求策略:latestminmax 策略减少数据传输
  • 避免在插件中阻塞主线程,使用Web Worker处理复杂计算

🎯 总结

通过本指南,你已经了解了Open MCT的基本安装、核心API和插件开发流程。Open MCT的强大之处在于其灵活的插件系统和丰富的数据可视化能力,无论是构建简单的数据监控面板还是复杂的任务控制系统,都能满足需求。

现在,你可以开始探索更多高级功能,如自定义视图、集成外部数据源或开发复杂的领域特定插件。查看 src/plugins/ 目录下的现有插件代码,获取更多灵感和最佳实践!

【免费下载链接】openmct A web based mission control framework. 【免费下载链接】openmct 项目地址: https://gitcode.com/GitHub_Trending/ope/openmct

Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐