Amazon IoT TwinMaker cookie 出厂示例时间序列连接器 - Amazon IoT TwinMaker
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

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

Amazon IoT TwinMaker cookie 出厂示例时间序列连接器

cookie 工厂 Lambda 函数的完整代码可在上找到。 GitHub尽管在将连接器连接至组件类型之后,您仍然可以更新实施,但我们强烈建议您在集成 Amazon IoT TwinMaker之前验证 Lambda 连接器。您可以在 Lambda 控制台测试 Lambda 函数,也可以在 Amazon CDK本地测试。有关测试 Lambda 函数的更多信息,请参阅测试 Lambda 函数和本地测试应用程序。 Amazon CDK

cookie 出厂组件类型示例

组件类型定义了跨组件共享的通用属性。对于 cookie 工厂示例,相同类型的物理组件共享相同的测量值,因此我们可以在组件类型中定义测量架构。例如,以下示例中定义了混合器类型。

{ "componentTypeId": "com.example.cookiefactory.mixer" "propertyDefinitions": { "RPM": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true }, "Temperature": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true } } }

例如,物理组件可能在 Timestream 数据库中包含测量值,在 SQL 数据库中包含维护记录,或者在警报系统中包含警报数据。创建多个组件,并将它们与实体关联,将不同的数据来源连接至实体并填充实体组件图。在这种情况下,每个组件都需要一个telemetryId属性来标识相应数据源中该组件的唯一密钥。指定该telemetryId属性有两个好处:可以在数据连接器中将该属性用作筛选条件,仅查询给定组件的值;如果您在数据平面 API 响应中包含该telemetryId属性值,则客户端会获取 ID,并在必要时可以执行反向查找。

如果将作为外部 ID 添加到组件类型,则它会在TimeStream表格中标识该组件。TelemetryId

{ "componentTypeId": "com.example.cookiefactory.mixer" "propertyDefinitions": { "telemetryId": { "dataType": { "type": "STRING" }, "isTimeSeries": false, "isRequiredInEntity": true, "isExternalId": true, "isStoredExternally": false }, "RPM": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true }, "Temperature": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true } } }

同样,如以下 JSON 示例所示,我们的组件类型为 WaterTank

{ "componentTypeId": "com.example.cookiefactory.watertank", "propertyDefinitions": { "flowRate1": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true }, "flowrate2": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true }, "tankVolume1": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true }, "tankVolume2": { "dataType": { "type": "DOUBLE" }, "isTimeSeries": true, "isRequiredInEntity": false, "isExternalId": false, "isStoredExternally": true }, "telemetryId": { "dataType": { "type": "STRING" }, "isTimeSeries": false, "isRequiredInEntity": true, "isExternalId": true, "isStoredExternally": false } } }

如果在实体范围内查询属性值,TelemetryType 是可选的组件类型属性 有关示例,请参阅Amazon IoT TwinMaker 示例 GitHub 存储库中定义的组件类型。同一个表中还嵌入了警报类型,因此可定义 TelemetryType,且您可将 TelemetryIdTelemetryType 等常见属性提取至父级组件类型,以供其他子类型共享。

Lambda 示例

Lambda 连接器需要访问数据来源,并根据输入生成查询语句并将其转发至数据来源。以下 JSON 示例显示了发送至 Lambda 的示例请求。

{ 'workspaceId': 'CookieFactory', 'selectedProperties': ['Temperature'], 'startDateTime': 1648796400, 'startTime': '2022-04-01T07:00:00.000Z', 'endDateTime': 1650610799, 'endTime': '2022-04-22T06:59:59.000Z', 'properties': { 'telemetryId': { 'definition': { 'dataType': { 'type': 'STRING' }, 'isTimeSeries': False, 'isRequiredInEntity': True, 'isExternalId': True, 'isStoredExternally': False, 'isImported': False, 'isFinal': False, 'isInherited': True, }, 'value': { 'stringValue': 'Mixer_22_680b5b8e-1afe-4a77-87ab-834fbe5ba01e' } } 'Temperature': { 'definition': { 'dataType': { 'type': 'DOUBLE' }, 'isTimeSeries': True, 'isRequiredInEntity': False, 'isExternalId': False, 'isStoredExternally': True, 'isImported': False, 'isFinal': False, 'isInherited': False } } 'RPM': { 'definition': { 'dataType': { 'type': 'DOUBLE' }, 'isTimeSeries': True, 'isRequiredInEntity': False, 'isExternalId': False, 'isStoredExternally': True, 'isImported': False, 'isFinal':False, 'isInherited': False } }, 'entityId': 'Mixer_22_d133c9d0-472c-48bb-8f14-54f3890bc0fe', 'componentName': 'MixerComponent', 'maxResults': 100, 'orderByTime': 'ASCENDING' }

Lambda 函数的目标是查询给定实体的历史测量数据。 Amazon IoT TwinMaker 提供了组件属性映射,您应该为组件 ID 指定一个实例化的值。例如,要处理组件类型级别的查询(这在警报用例中很常见)并返回工作区中所有组件的警报状态,则属性图具有组件类型属性定义。

对于最直接的情况,就像在前面的请求中一样,我们希望在给定时间窗口内按时间升序为给定组件提供一系列温度样本。查询语句可概括如下:

... SELECT measure_name, time, measure_value::double FROM {database_name}.{table_name} WHERE time < from_iso8601_timestamp('{request.start_time}') AND time >= from_iso8601_timestamp('{request.end_time}') AND TelemetryId = '{telemetry_id}' AND measure_name = '{selected_property}' ORDER BY time {request.orderByTime} ...