# 提升开发效率：MQTTX 1.9.3 推出强大的 IoT 场景数据模拟功能

> MQTTX 1.9.3 为命令行客户端带来了一项重要增强：开发者可以模拟 IoT 场景数据，满足特定的测试需求。

> Language: zh-CN · Published: 2023-05-29 · Category: 版本发布 · Version: 1.9.3 · Canonical: https://mqttx.app/zh/blog/mqttx-1-9-3-introduces-powerful-iot-scenario-data-simulation-feature · Translation: [English](https://mqttx.app/blog/mqttx-1-9-3-introduces-powerful-iot-scenario-data-simulation-feature.md)

> MQTTX 是一款强大的跨平台 [MQTT 5.0](https://www.emqx.com/zh/blog/introduction-to-mqtt-5) 桌面端和命令行客户端，让学习、探索和开发 [MQTT](https://www.emqx.com/zh/blog/the-easiest-guide-to-getting-started-with-mqtt) 变得简单而快速。它是一款全面的 MQTT 工具箱，为使用 MQTT 应用和服务的开发者和用户提供友好的界面和丰富功能。
>
> 官网：[https://mqttx.app/](https://mqttx.app/zh)
>
> GitHub：[https://github.com/emqx/MQTTX](https://github.com/emqx/MQTTX)

## 简介

5 月中旬，MQTTX 团队发布了全新的 1.9.3 版本。该版本正式将 MQTT X 更名为 MQTTX，以进一步统一品牌形象。同时，最新版本为命令行客户端带来了一项重要增强：开发者可以模拟 IoT 场景数据，满足特定的测试需求。此外，MQTTX 在 Docker 镜像优化方面也取得了显著进展，文件体积明显减小。

> 立即下载[最新版本 MQTTX](https://github.com/emqx/MQTTX/releases/tag/v1.9.3)！→

## MQTTX CLI 增强：基于场景的数据模拟

MQTTX 命令行客户端 [MQTTX CLI](https://mqttx.app/zh/cli) 新增了名为 `simulate` 的命令，专门用于模拟特定场景下的 MQTT 发布消息。该功能对于进行系统测试的开发者尤为重要，因为它允许开发者根据测试场景需求模拟数据。例如，开发者可以模拟各种设备发送的数据，从而显著提升开发和测试效率。

该功能的主要参数包括：

- `-sc, --scenario`：要模拟的内置场景名称
- `-f, --file`：本地自定义场景脚本的文件路径
- `-t, --topic`：消息主题，可选，支持 %u（用户名）、%c（客户端 ID）、%i（索引）、%sc（场景）等变量。默认主题格式为 `mqttx/simulate/%sc/%c`

必须指定 `--scenario` 或 `--file` 参数。如果同时设置，`--file` 参数优先。

### 内置场景

MQTTX CLI 提供了多个内置场景，可通过 `--scenario` 参数指定。例如，运行 `mqttx simulate --scenario tesla` 可以模拟特斯拉汽车的数据。您还可以使用 `mqttx ls --scenarios` 命令列出所有内置场景。该命令会输出一个表格，展示每个场景的名称和描述。如果要在 simulate 命令中使用其中某个场景，请在 `--scenario` 选项中指定场景名称：

```
mqttx simulate --scenario <SCENARIO_NAME>
```

内置场景包括：

- tesla：模拟特斯拉汽车的数据。
- IEM：模拟工业能耗监控数据。
- smart_home：模拟智能家居数据。
- weather：模拟气象站数据。

### 自定义场景

开发者可以编写自定义 IoT 数据脚本，以模拟特定场景。例如，以下脚本会生成随机的温度和湿度数据：

```
function generator (faker, options) {
  return {
    message: JSON.stringify({
      temp: faker.datatype.number({ min: 20, max: 80 }),  // Generate a random temperature between 20 and 80.
      hum: faker.datatype.number({ min: 40, max: 90 }),   // Generate a random humidity between 40 and 90.
    })
  }
}
module.exports = {
  name: 'myScenario',  // Name of the scenario
  generator,          // Generator function
}
```

更多示例和详细的编辑指南，请参考 MQTTX GitHub 仓库中的 [scripts-example](https://github.com/emqx/MQTTX/tree/main/scripts-example/IoT-data-scenarios) 文件夹。您还可以探索使用 [faker.js](https://fakerjs.dev/) 生成各种类型的随机数据。

如需了解更多信息，用户还可以运行 `help` 命令：

```
mqttx simulate --help
```

### 示例：智能家居场景

假设您想模拟 10 个智能家居设备向指定主题发布数据的场景，可以运行以下命令：

```
mqttx simulate -sc smart_home -c 10 -h broker.emqx.io -t testtopic/smart_home
```

该命令将启动 10 个模拟器（由 `-c 10` 参数指定），它们会向 `testtopic/smart_home` 主题（由 `-t testtopic/smart_home` 参数指定）发布数据，模拟智能家居设备的行为。数据由内置的 `smart_home` 场景生成（由 `-sc smart_home` 参数指定）。此外，这些模拟器会连接到服务器 `broker.emqx.io`（由 `-h broker.emqx.io` 参数指定）。订阅该主题后，您将看到类似以下的数据：

```json
{
  "home_id": "e5ee7759-464f-4df8-9b68-2afc906da39a",
  "owner_name": "Dustin Hessel",
  "address": "8850 Ona Circle",
  "rooms": [{
    "room_type": "living room",
    "temperature": 19,
    "humidity": 45,
    "lights_on": true,
    "window_open": false
  }, {
    "room_type": "bedroom",
    "temperature": 23,
    "humidity": 33,
    "lights_on": true,
    "window_open": false,
    "bed_occupancy": false
  }, {
    "room_type": "kitchen",
    "temperature": 23,
    "humidity": 50,
    "lights_on": false,
    "window_open": false,
    "fridge_temperature": 5,
    "oven_on": true
  }, {
    "room_type": "bathroom",
    "temperature": 23,
    "humidity": 46,
    "lights_on": true,
    "window_open": true,
    "water_tap_running": false,
    "bath_water_level": 68
  }],
  "timestamp": 1684810770255
}
```

这些数据展示了智能家居环境的状态，包括每个房间的温度、湿度、照明等信息。

## 部署优化：精简 Docker 镜像

在 MQTTX 1.9.3 中，我们显著优化了 MQTTX CLI 和 [MQTTX Web Client](https://mqttx.app/web-client/) 的 Docker 镜像。我们采用了多阶段构建技术，并切换到基于 Alpine Linux 的 Node.js 环境（node:16-alpine）。这些调整将 Docker 镜像体积减少了约 81.3%。具体而言，MQTTX CLI 的 Docker 镜像从原来的 1.07GB 减小到 200.52MB，MQTTX Web Client 的 Docker 镜像从原来的 886.75MB 缩小到 146.4MB。

这些优化大幅降低了用户的存储占用，加快了镜像下载速度，从而降低了使用成本。优化后的镜像在保持完整功能的同时，还提升了运行效率。此外，这些改进减轻了网络传输压力，使在 Docker 环境中私有部署 MQTTX Web Client 更加便捷高效。

![MQTTX CLI Docker](https://assets.emqx.com/images/edb85e5ba132bef570e11e1d96922da8.png?x-image-process=image/resize,w_1520/format,webp)

![MQTTX Web Client Docker](https://assets.emqx.com/images/d298063bd058efd0b0082bf1c3253d20.png?x-image-process=image/resize,w_1520/format,webp)

## 桌面客户端增强

### 自动重连

连接默认启用自动重连。如果 MQTTX 桌面客户端因网络问题或其他原因与 [MQTT 服务器](https://www.emqx.com/zh/blog/the-ultimate-guide-to-mqtt-broker-comparison)断开连接，客户端会自动尝试重新连接。

### 连接状态下禁止编辑

在连接状态下禁用编辑，以防止意外修改并确保数据一致性。

### 修复 Ubuntu 上的启动问题

我们解决了 MQTTX 在 Ubuntu 系统上无法启动的问题。现在，Ubuntu 用户可以正常使用我们的桌面客户端。

### Logo 更新

我们优化了应用 Logo，将 MQTT X 更改为 MQTTX，使其更加简洁、易于识别。

![MQTTX](https://assets.emqx.com/images/f849ef91f21ca2fc6386874edcdfc49b.png?x-image-process=image/resize,w_1520/format,webp)

## 未来规划

1. 将 IoT 场景数据模拟功能同步到桌面客户端。
2. 增强消息展示框中 JSON 等特定数据格式的高亮显示。
3. 支持 Protobuf 消息格式。
4. 支持 [Sparkplug B](https://www.emqx.com/en/solutions/mqtt-sparkplug)。
5. 支持配置忽略存储 QoS 0 消息，以减少存储空间占用。
6. MQTT Debug 功能。
7. 自动为接收到的消息绘制图表。
8. 支持 CoAP、[MQTT-SN](https://www.emqx.com/zh/blog/connecting-mqtt-sn-devices-using-emqx) 等协议扩展的插件功能。
9. 脚本测试自动化（Flow）。

MQTTX 始终致力于提供最佳用户体验，成为全球 MQTT 服务和应用开发者不可或缺的客户端工具。
