

终止支持通知：2026 年 5 月 20 日， AWS 将终止对的支持。 AWS IoT Events 2026 年 5 月 20 日之后，您将无法再访问 AWS IoT Events 控制台或 AWS IoT Events 资源。有关更多信息，请参阅[AWS IoT Events 终止支持](https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-end-of-support.html)。

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 在 AWS IoT Events (AWS CLI) 中分析探测器模型
<a name="analyze-api-api"></a>

以编程方式分析 AWS IoT Events 探测器模型，可提供有关其结构、行为和性能的宝贵见解。这种基于 API 的方法允许自动分析、与现有工作流程集成，并能够跨多个探测器模型执行批量操作。通过利用 [StartDetectorModelAnalysis](https://docs.aws.amazon.com/iotevents/latest/apireference/API_StartDetectorModelAnalysis.html)API，您可以启动对模型的深入检查，帮助您识别潜在问题，优化逻辑流程，并确保您的物联网事件处理符合您的业务需求。

以下步骤使用 AWS CLI 来分析探测器模型。

**要使用分析探测器模型 AWS CLI**

1. 运行以下命令以启动分析。

   ```
   aws iotevents start-detector-model-analysis --cli-input-json file://file-name.json
   ```
**注意**  
*file-name*替换为包含探测器模型定义的文件名。  
**Example 探测器模型定义**  

   ```
   {
       "detectorModelDefinition": {
           "states": [
               {
                   "stateName": "TemperatureCheck",
                   "onInput": {
                       "events": [
                           {
                               "eventName": "Temperature Received",
                               "condition": "isNull($input.TemperatureInput.sensorData.temperature)==false",
                               "actions": [
                                   {
                                       "iotTopicPublish": {
                                           "mqttTopic": "IoTEvents/Output"
                                       }
                                   }
                               ]
                           }
                       ],
                       "transitionEvents": []
                   },
                   "onEnter": {
                       "events": [
                           {
                               "eventName": "Init",
                               "condition": "true",
                               "actions": [
                                   {
                                       "setVariable": {
                                           "variableName": "temperatureChecked",
                                           "value": "0"
                                       }
                                   }
                               ]
                           }
                       ]
                   },
                   "onExit": {
                       "events": []
                   }
               }
           ],
           "initialStateName": "TemperatureCheck"
       }
   }
   ```

   如果您使用 AWS CLI 来分析现有的探测器模型，请选择以下选项之一来检索探测器模型定义：
   + 如果要使用 AWS IoT Events 控制台，请执行以下操作：

     1. 在导航窗格中，选择**探测器模型**。

     1. 在**探测器模型**下，选择目标探测器模型。

     1. 从**操作**中选择**导出探测器模型**以下载探测器模型。此探测器模型以 JSON 格式保存。

     1. 打开探测器模型 JSON 文件。

     1. 您只需要 `detectorModelDefinition` 对象。移除以下内容：
        + 页面顶部的第一个大括号 (`{`)
        + `detectorModel` 线
        + `detectorModelConfiguration` 对象
        + 页面底部的最后一个大括号（`}`）

     1. 保存该文件。
   + 如果要使用 AWS CLI，请执行以下操作：

     1. 在终端中运行以下命令。

        ```
        aws iotevents describe-detector-model --detector-model-name detector-model-name
        ```

     1. *detector-model-name*替换为探测器型号的名称。

     1. 将`detectorModelDefinition`对象复制至文本编辑器。

     1. 在`detectorModelDefinition`外面添加大括号 (`{}`)。

     1. 以 JSON 格式保存此文件。  
**Example 响应示例**  

   ```
   {
       "analysisId": "c1133390-14e3-4204-9a66-31efd92a4fed"
   }
   ```

1. 从输出项中复制分析 ID。

1. 运行以下命令，以检索分析状态。

   ```
   aws iotevents describe-detector-model-analysis --analysis-id "analysis-id"
   ```
**注意**  
*analysis-id*替换为您复制的分析 ID。  
**Example 响应示例**  

   ```
   {
       "status": "COMPLETE"
   }
   ```

   状态可以是以下值之一：
   + `RUNNING`— AWS IoT Events 正在分析您的探测器模型。完成此过程可能最多需要一分钟。
   + `COMPLETE`— AWS IoT Events 已完成对探测器模型的分析。
   + `FAILED`— AWS IoT Events 无法分析您的探测器模型。请稍后重试。

1. 运行以下命令，以检索一个或多个探测器模型分析结果。
**注意**  
*analysis-id*替换为您复制的分析 ID。

   ```
   aws iotevents get-detector-model-analysis-results --analysis-id "analysis-id"
   ```  
**Example 响应示例**  

   ```
   {
       "analysisResults": [
           {
               "type": "data-type",
               "level": "INFO",
               "message": "Inferred data types [Integer] for $variable.temperatureChecked",
               "locations": []
           },
           {
               "type": "referenced-resource",
               "level": "ERROR",
               "message": "Detector Model Definition contains reference to Input 'TemperatureInput' that does not exist.",
               "locations": [
                   {
                       "path": "states[0].onInput.events[0]"
                   }
               ]
           }
       ]
   }
   ```

**注意**  
 AWS IoT Events 开始分析您的探测器模型后，您最多有 24 小时的时间来检索分析结果。