AWS IoT TwinMaker餅乾工廠示例時間序列連接器 - AWS IoT TwinMaker

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

AWS IoT TwinMaker餅乾工廠示例時間序列連接器

Cookie 工廠 Lambda 函數的完整程式碼可在上取得 GitHub。雖然您仍然可以在連接器連結至元件類型之後更新實作,但我們強烈建議您在與之整合之前先驗證 Lambda 連接器 AWS IoT TwinMaker。您可以在 Lambda 主控台或本機中測試 Lambda 函數 AWS CDK。如需有關測試 Lambda 函數的詳細資訊,請參閱測試 Lambda 函數本機測試 AWS CDK 應用程式

餅乾工廠組件類型示例

在組件類型中,我們定義跨組件共享的共同屬性。對於 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 } } }

同樣地,我們也有的元件類型WaterTank,如下面的 JSON 範例所示。

{ "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是查詢實體範圍中的屬性值,則是組件類型中的選用屬性。如需範例,請參閱AWS IoT TwinMaker 範例 GitHub 存放庫中已定義的元件類型。同一個資料表中也內嵌了警示類型,因此TelemetryType已定義,您可以擷取常用屬性 (如TelemetryId和) TelemetryType 至父元件類型,以供其他子類型共用。

範例 Lambda

Lambda 連接器需要存取資料來源,並根據輸入產生查詢陳述式,並將其轉寄至資料來源。傳送至 Lambda 的範例要求會顯示在下列 JSON 範例中。

{ '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 函數的目標是查詢特定實體的歷史測量資料。 AWS 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} ...