定義咖啡機裝置範本

已完成

Azure IoT Central 中的裝置範本是藍圖,可定義連線至應用程式之裝置類型的特性與行為。 例如,裝置範本會定義裝置傳送的遙測,讓 IoT Central 可以建立使用正確單位與資料類型的視覺效果。

裝置範本包含下列區段:

  • 裝置模型。 咖啡機模擬器與您的應用程式交換的資料在裝置模型中指定。
    • 根元件。 根元件的介面描述裝置模型特定的功能。
    • 元件。 除了用於描述裝置功能的根元件之外,裝置模型也可能會包含元件。 在此案例中,您只使用根元件定義「連線的咖啡機」模型。
    • 繼承介面。 裝置模型包含一或多個介面,可擴充根元件的功能。 在此案例中,您只定義一個介面。
  • 檢視表。 此部分的裝置範本可讓您定義視覺效果,以檢視來自裝置的資料,以及用於管理及控制咖啡機的表單。

建立裝置範本後,您即可連線模擬裝置。 模擬裝置會產生遙測,因此您可以在連線至實際裝置前,先測試應用程式的行為。

這裡,您將了解在已啟用 IoT 的咖啡機範例中,如何在裝置模型中指定遙測、屬性和命令的定義,以及管理和控制裝置所需的雲端屬性和檢視。

遙測

遙測是從您裝置串流的資料。 您可以將多個遙測類型新增到裝置範本,使其符合裝置的功能。 咖啡機會傳送來自其感應器的空氣溼度和水溫值。 其也會傳送目前是否正在沖泡以及何時偵測到杯子的狀態資訊。

屬性

使用屬性以在裝置與應用程式之間共用設定資料。

在此案例中,您可以使用「可寫入」屬性來設定最佳水溫,並將其傳送到咖啡機。 當可寫入屬性在 IoT Central 中更新時,屬性即會在 UI 中標記為暫止,直到裝置承認其已回應更新為止。

您可以使用「唯讀」屬性來讓裝置將值傳送至應用程式。 唯讀屬性只能由裝置進行變更。 在此案例中,您會在裝置範本中定義一個唯讀裝置屬性,稱為 [Device Warranty Expired]。 [Device Warranty Expired] 欄位會維持在空白狀態,直到咖啡機連線至 IoT Central 為止。 一旦連線,咖啡機就會將瑕疵擔保狀態傳送到應用程式。

雲端屬性是與裝置相關聯的裝置中繼資料。 使用雲端屬性以在 IoT Central 應用程式中記錄裝置的相關資訊。 在此案例中,您會使用雲端屬性來記錄咖啡機的理想水溫範圍。 雲端屬性會儲存在 IoT Central 應用程式中,且不會與裝置同步。

命令

使用命令從您的應用程式遠端管理您的裝置。 您可以從雲端直接在裝置上執行命令來控制裝置。 在此案例中,您會在咖啡機上執行命令,以將其設為維護模式或開始沖泡。

檢視

您可以自訂 IoT Central 所顯示的 UI,其用於管理及監視與裝置範本建立關聯的裝置。 在此案例中,您會建立檢視來繪製來自裝置的遙測值,並建立表單來管理裝置屬性。

連線的咖啡機模型

裝置模型是裝置範本的一部分,定義裝置支援的遙測、屬性和命令。 您可以將裝置功能模型儲存在 JSON 檔案中,並將其匯入 IoT Central。 下列 JSON 是連線咖啡機的裝置功能模型。

{
    "@id": "dtmi:com:example:ConnectedCoffeeMaker;1",
    "@type": "Interface",
    "contents": [
        {
            "@type": ["Telemetry", "NumberValue", "Temperature"],
            "displayName": {
                "en": "Water Temperature"
            },
            "name": "WaterTemperature",
            "schema": "double",
            "unit": "degreeCelsius",
            "decimalPlaces": 1,
            "maxValue": 100,
            "minValue": 86
        },
        {
            "@type": ["Telemetry", "NumberValue", "Humidity"],
            "displayName": {
                "en": "Air Humidity"
            },
            "name": "AirHumidity",
            "schema": "integer",
            "unit": "percent",
            "maxValue": 100,
            "minValue": 20
        },
        {
            "@type": ["Telemetry", "State"],
            "displayName": {
                "en": "Brewing"
            },
            "name": "Brewing",
            "schema": {
                "@type": "Enum",
                "enumValues": [
                    {
                        "displayName": {
                            "en": "Brewing"
                        },
                        "enumValue": "brewing",
                        "name": "Brewing"
                    },
                    {
                        "displayName": {
                            "en": "Not Brewing"
                        },
                        "enumValue": "notbrewing",
                        "name": "NotBrewing"
                    }
                ],
                "valueSchema": "string"
            }
        },
        {
            "@type": ["Telemetry", "State"],
            "displayName": {
                "en": "Cup Detected"
            },
            "name": "CupDetected",
            "schema": {
                "@type": "Enum",
                "enumValues": [
                    {
                        "displayName": {
                            "en": "Detected"
                        },
                        "enumValue": "detected",
                        "name": "Detected"
                    },
                    {
                        "displayName": {
                            "en": "Not Detected"
                        },
                        "enumValue": "notdetected",
                        "name": "NotDetected"
                    }
                ],
                "valueSchema": "string"
            }
        },
        {
            "@type": ["Property", "NumberValue", "Initialized", "Temperature"],
            "displayName": {
                "en": "Optimal Temperature"
            },
            "name": "OptimalTemperature",
            "schema": "double",
            "unit": "degreeCelsius",
            "writable": true,
            "decimalPlaces": 1,
            "initialValue": 98,
            "maxValue": 100,
            "minValue": 86
        },
        {
            "@type": ["Property", "BooleanValue"],
            "displayName": {
                "en": "Device Warranty Expired"
            },
            "name": "DeviceWarrantyExpired",
            "schema": "boolean",
            "writable": false
        },
        {
            "@type": ["Property", "Cloud", "NumberValue", "Temperature"],
            "displayName": {
                "en": "Coffee Maker Min Temperature"
            },
            "name": "CoffeeMakerMinTemperature",
            "schema": "double",
            "unit": "degreeCelsius",
            "decimalPlaces": 1,
            "maxValue": 92,
            "minValue": 88
        },
        {
            "@type": ["Property", "Cloud", "NumberValue", "Temperature"],
            "displayName": {
                "en": "Coffee Maker Max Temperature"
            },
            "name": "CoffeeMakerMaxTemperature",
            "schema": "double",
            "unit": "degreeCelsius",
            "decimalPlaces": 1,
            "maxValue": 99,
            "minValue": 96
        },
        {
            "@type": "Command",
            "commandType": "synchronous",
            "displayName": {
                "en": "Set Maintenance Mode"
            },
            "name": "SetMaintenanceMode"
        },
        {
            "@type": "Command",
            "commandType": "synchronous",
            "displayName": {
                "en": "Start Brewing"
            },
            "name": "StartBrewing"
        }
    ],
    "displayName": {
        "en": "Connected Coffee Maker"
    },
    "@context": [
        "dtmi:iotcentral:context;2",
        "dtmi:dtdl:context;2"
    ]
}
  • [@id] 欄位是模型的數位對應項模型識別碼 (DTMI),格式為 dtmi:<domain>:<unique-model-identifier>;<model-version-number>
  • @type 識別要描述的資訊類型。 例如,"@type": "Interface" 用來表示數位對應項定義語言 (DTDL) 實體的型別。 在此案例中,實體是 Interface
  • [contents] 欄位是屬性定義的陣列。 每個屬性必須提供一個 @type,以識別其描述的介面資訊類型。 在此案例中,TelemetryPropertyCommand 物件描述連線的咖啡機功能。
  • [displayName] 欄位是物件,提供人類可讀的不同語言介面或實體名稱。 在此案例中,介面 displayName 為英文的 Connected Coffee Maker
  • [@context] 欄位是用來指定其中定義介面或實體的 DTDL 內容。 在此案例中,欄位參考 iotcentraldtdl 的內容。
  • [name] 欄位是 TelemetryPropertyCommand 的唯一識別碼。
  • 每個遙測欄位代表度量或事件,通常用來描述裝置感應器讀數。 例如 [Water Temperature] 的 schemadouble 以及 unitdegreeCelsius。 [decimalPlaces]、[maxValue] 和 [minValue] 欄位指定更多有關遙測資料的詳細資料。
  • 每個屬性欄位代表一個實體的狀態。 例如,[Device Warranty Expired] 屬於布林值型別,連線的裝置可以設定其狀態為 true 或 false。
  • [writable] 欄位指定是否可以從 IoT Central 應用程式設定該屬性。 在此案例中,可以從應用程式 UI 設定 [Optimal Temperature],但無法設定 [Device Warranty Expired]。
  • 如果屬性在型別清單中有 Cloud,則該屬性為雲端屬性。 在此案例中,[Coffee Maker Min Temperature] 和 [Coffee Maker Max Temperature] 是雲端屬性。
  • 每個命令欄位描述一個裝置可以執行的動作。 例如,[Start Brewing] 是雲端可以傳送至裝置以開始沖泡咖啡的命令。
  • [commandType] 欄位指定命令是同步 (等候回應) 還是非同步 (不等候回應)。

檢定您的知識

1.

連線的咖啡機模型中定義多少個遙測功能?

2.

是非題:最佳溫度是可寫入屬性。

3.

是非題:連線的咖啡機可以使用 StartBrewing 命令從遠端啟動沖泡。