建立及測試新模擬裝置
遠端監視解決方案加速器可讓您定義自己的模擬裝置。 本文會示範如何定義新的模擬燈泡裝置,然後在本機進行測試。 解決方案加速器中包含冷卻器和卡車等模擬裝置。 不過,您可以定義自己的模擬裝置,以在部署實際裝置之前,先測試您的 IoT 解決方案。
注意
本文說明如何使用裝置模擬服務中裝載的模擬裝置。 如果您想要建立真實裝置,請參閱將裝置連線到遠端監視解決方案加速器。
本操作指南會示範如何自訂裝置模擬微服務。 此微服務會是遠端監視解決方案加速器的一部份。 為說明裝置模擬的功能,本操作指南會使用 Contoso IoT 應用程式中的兩個情節:
在第一個情節中,您要將新的遙測類型新增至 Contoso 的現有 Chiller 裝置類型。
在第二個情節中,Contoso 需要測試新的智慧型燈泡裝置。 若要執行測試,您可以建立具有下列特性的新模擬裝置:
屬性
名稱 | 值 |
---|---|
色彩 | 白色、紅色、藍色 |
亮度 | 0 到 100 |
估計的剩餘時間 | 10,000 個小時倒數計時 |
遙測
下表顯示燈泡 (lightbulb) 以資料流向雲端報告的資料:
名稱 | 值 |
---|---|
狀態 | 「開啟」、「關閉」 |
溫度 | 華氏度數 |
線上 | true、false |
注意
線上遙測值會強制用於所有模擬類型。
方法
下表會顯示新裝置所支援的動作:
名稱 |
---|
開啟 |
關閉 |
初始狀態
下表會顯示裝置的初始狀態:
名稱 | 值 |
---|---|
初始色彩 | 白色 |
初始亮度 | 75 |
初始剩餘時間 | 10,000 |
初始遙測狀態 | 「開啟」 |
初始遙測溫度 | 200 |
若要完成此操作指南中的步驟,您必須具備有效的 Azure 訂用帳戶。
如果您沒有 Azure 訂用帳戶,請在開始前建立免費帳戶。
使用 Azure Cloud Shell
Azure Cloud Shell 是裝載於 Azure 中的互動式殼層環境,可在瀏覽器中使用。 您可以使用 Bash 或 PowerShell 搭配 Cloud Shell,與 Azure 服務共同使用。 Azure Cloud Shell 已預先安裝一些命令,可讓您執行本文提到的程式碼,而不必在本機環境上安裝任何工具。
要啟動 Azure Cloud Shell:
選項 | 範例/連結 |
---|---|
選取程式碼區塊右上角的 [試試看]。 選取 [試用] 並不會自動將程式碼複製到 Cloud Shell 中。 | |
請前往 https://shell.azure.com,或選取 [啟動 Cloud Shell] 按鈕,在瀏覽器中開啟 Cloud Shell。 | |
選取 Azure 入口網站右上方功能表列上的 [Cloud Shell] 按鈕。 |
若要在 Azure Cloud Shell 中執行本文中的程式碼:
啟動 Cloud Shell。
選取程式碼區塊上的 [複製] 按鈕,複製程式碼。
在 Windows 和 Linux 上選取 Ctrl+Shift+V;或在 macOS 上選取 Cmd+Shift+V,將程式碼貼到 Cloud Shell 工作階段中。
選取 Enter 鍵執行程式碼。
先決條件
若要遵循本操作說明指南,您需要:
- Visual Studio Code。 您可以下載適用于 Mac、Linux 和 Windows 的Visual Studio Code。
- .NET Core。 您可以下載適用於 Mac、Linux 和 Windows 的 .NET Code。
- C# for Visual Studio Code
- Postman。 您可以下載 適用于 Mac、Windows 或 Linux 的 Postman。
- 部署至 Azure 訂用帳戶的 IoT 中樞。 您需要 IoT 中樞的連接字串才能完成本指南中的步驟。 您可以從 Azure 入口網站取得連接字串。
- Cosmos DB 資料庫,其必須使用 SQL API,並已針對強式一致性進行設定。 您需要 Cosmos DB 資料庫的連接字串才能完成本指南中的步驟。 您可以從 Azure 入口網站取得連接字串。
準備您的開發環境
請完成下列工作以準備開發環境:
- 下載裝置模擬微服務的來源。
- 下載儲存體配接器微服務的來源。
- 本機執行儲存體配接器微服務。
本文中的指示假設您使用的是 Windows。 如果您使用其他作業系統,則可能需要調整某些檔案路徑和命令以符合環境。
下載微服務
將 遠端監視微服務 從 GitHub 下載並解壓縮到本機電腦上的適當位置。 本文假設這個資料夾的名稱是 remote-monitoring-services-dotnet-master。
從 GitHub 將裝置模擬微服務下載並解壓縮到本機電腦上的適當位置。 本文假設這個資料夾的名稱是 device-simulation-dotnet-master。
執行儲存體配接器微服務
在 Visual Studio Code 中開啟 remote-monitoring-services-dotnet-master\storage-adapter 資料夾。 按一下任何 [還原] 按鈕,以修正任何無法解決的相依性。
開啟 storage-adapter/WebService/appsettings.ini 檔案,並將 Cosmos DB 連接字串指派給 documentDBConnectionString 變數。
若要在本機執行微服務,請按一下 [偵錯] > [開始偵錯]。
Visual Studio Code 中的 [終端機] 視窗會顯示執行中微服務的輸出,包括 Web 服務健康情況檢查的 URL:http://127.0.0.1:9022/v1/status。 當您瀏覽至此位址時,狀態應該是「正確: 運作良好」。
在完成後續步驟時,請讓儲存體配接器微服務繼續在 Visual Studio Code 的這個執行個體中執行。
修改 Chiller
在本節中,您會將新的內部溫度遙測類型新增至現有 Chiller 裝置類型:
在本機電腦上建立新的資料夾 C:\temp\devicemodels。
將下列檔案複製到所下載裝置模擬微服務複本中的新資料夾:
來源 Destination Services\data\devicemodels\chiller-01.json C:\temp\devicemodels\chiller-01.json Services\data\devicemodels\scripts\chiller-01-state.js C:\temp\devicemodels\scripts\chiller-01-state.js Services\data\devicemodels\scripts\Reboot-method.js C:\temp\devicemodels\scripts\Reboot-method.js Services\data\devicemodels\scripts\FirmwareUpdate-method.js C:\temp\devicemodels\scripts\FirmwareUpdate-method.js Services\data\devicemodels\scripts\EmergencyValveRelease-method.js C:\temp\devicemodels\scripts\EmergencyValveRelease-method.js Services\data\devicemodels\scripts\IncreasePressure-method.js C:\temp\devicemodels\scripts\IncreasePressure-method.js 開啟 C:\temp\devicemodels\chiller-01.json 檔案。
在 InitialState 區段中,新增下列兩個定義:
"internal_temperature": 65.0, "internal_temperature_unit": "F",
在遙測 陣列中,新增下列定義:
{ "Interval": "00:00:05", "MessageTemplate": "{\"internal_temperature\":${internal_temperature},\"internal_temperature_unit\":\"${internal_temperature_unit}\"}", "MessageSchema": { "Name": "chiller-internal-temperature;v1", "Format": "JSON", "Fields": { "temperature": "double", "temperature_unit": "text" } } },
儲存 C:\temp\devicemodels\chiller-01.json 檔案。
開啟 C:\temp\devicemodels\scripts\chiller-01-state.js 檔案。
將下列欄位新增至 state 變數:
internal_temperature: 65.0, internal_temperature_unit: "F",
更新 main 函式,如下所示:
function main(context, previousState, previousProperties) { // Restore the global state before generating the new telemetry, so that // the telemetry can apply changes using the previous function state. restoreSimulation(previousState, previousProperties); // 75F +/- 5%, Min 25F, Max 100F state.temperature = vary(75, 5, 25, 100); // 70% +/- 5%, Min 2%, Max 99% state.humidity = vary(70, 5, 2, 99); // 65F +/- 2%, Min 15F, Max 125F state.internal_temperature = vary(65, 2, 15, 125); log("Simulation state: " + state.simulation_state); if (state.simulation_state === "high_pressure") { // 250 psig +/- 25%, Min 50 psig, Max 300 psig state.pressure = vary(250, 25, 50, 300); } else { // 150 psig +/- 10%, Min 50 psig, Max 300 psig state.pressure = vary(150, 10, 50, 300); } updateState(state); return state; }
儲存 C:\temp\devicemodels\scripts\chiller-01-state.js 檔案。
建立燈泡
在本節中,您會定義新的燈泡裝置類型:
建立 C:\temp\devicemodels\lightbulb-01.json 檔案,並新增下列內容:
{ "SchemaVersion": "1.0.0", "Id": "lightbulb-01", "Version": "0.0.1", "Name": "Lightbulb", "Description": "Smart lightbulb device.", "Protocol": "MQTT", "Simulation": { "InitialState": { "online": true, "temperature": 200.0, "temperature_unit": "F", "status": "on" }, "Interval": "00:00:20", "Scripts": [ { "Type": "javascript", "Path": "lightbulb-01-state.js" } ] }, "Properties": { "Type": "Lightbulb", "Color": "White", "Brightness": 75, "EstimatedRemainingLife": 10000 }, "Tags": { "Location": "Building 2", "Floor": "2", "Campus": "Redmond" }, "Telemetry": [ { "Interval": "00:00:20", "MessageTemplate": "{\"temperature\":${temperature},\"temperature_unit\":\"${temperature_unit}\",\"status\":\"${status}\"}", "MessageSchema": { "Name": "lightbulb-status;v1", "Format": "JSON", "Fields": { "temperature": "double", "temperature_unit": "text", "status": "text" } } } ], "CloudToDeviceMethods": { "SwitchOn": { "Type": "javascript", "Path": "SwitchOn-method.js" }, "SwitchOff": { "Type": "javascript", "Path": "SwitchOff-method.js" } } }
將變更儲存到 C:\temp\devicemodels\lightbulb-01.json。
建立 C:\temp\devicemodels\scripts\lightbulb-01-state.js 檔案,並新增下列內容:
"use strict"; // Default state var state = { online: true, temperature: 200.0, temperature_unit: "F", status: "on" }; // Default device properties var properties = {}; /** * Restore the global state using data from the previous iteration. * * @param previousState device state from the previous iteration * @param previousProperties device properties from the previous iteration */ function restoreSimulation(previousState, previousProperties) { // If the previous state is null, force a default state if (previousState) { state = previousState; } else { log("Using default state"); } if (previousProperties) { properties = previousProperties; } else { log("Using default properties"); } } /** * Simple formula generating a random value around the average * in between min and max * * @returns random value with given parameters */ function vary(avg, percentage, min, max) { var value = avg * (1 + ((percentage / 100) * (2 * Math.random() - 1))); value = Math.max(value, min); value = Math.min(value, max); return value; } /** * Simple formula that sometimes flips the status of the lightbulb */ function flip(value) { if (Math.random() < 0.2) { return (value == "on") ? "off" : "on" } return value; } /** * Entry point function called by the simulation engine. * Returns updated simulation state. * Device property updates must call updateProperties() to persist. * * @param context The context contains current time, device model and id * @param previousState The device state since the last iteration * @param previousProperties The device properties since the last iteration */ function main(context, previousState, previousProperties) { // Restore the global device properties and the global state before // generating the new telemetry, so that the telemetry can apply changes // using the previous function state. restoreSimulation(previousState, previousProperties); state.temperature = vary(200, 5, 150, 250); // Make this flip every so often state.status = flip(state.status); updateState(state); return state; }
將變更儲存到 C:\temp\devicemodels\scripts\lightbulb-01-state.js。
建立 C:\temp\devicemodels\scripts\SwitchOn-method.js 檔案,並新增下列內容:
"use strict"; // Default state var state = { status: "on" }; /** * Entry point function called by the method. * * @param context The context contains current time, device model and id * @param previousState The device state since the last iteration * @param previousProperties The device properties since the last iteration */ function main(context, previousState) { log("Executing lightbulb Switch On method."); state.status = "on"; updateState(state); }
將變更儲存到 C:\temp\devicemodels\scripts\SwitchOn-method.js。
建立 C:\temp\devicemodels\scripts\SwitchOff-method.js 檔案,並新增下列內容:
"use strict"; // Default state var state = { status: "on" }; /** * Entry point function called by the method. * * @param context The context contains current time, device model and id * @param previousState The device state since the last iteration * @param previousProperties The device properties since the last iteration */ function main(context, previousState) { log("Executing lightbulb Switch Off method."); state.status = "off"; updateState(state); }
將變更儲存到 C:\temp\devicemodels\scripts\SwitchOff-method.js。
您現在已建立自訂的 Chiller 裝置類型版本,並已建立新的燈泡裝置類型。
測試裝置
在本節中,您會測試在先前章節中於本機建立的裝置類型。
執行裝置模擬微服務
在 Visual Studio Code 的新執行個體中,開啟您從 GitHub 下載的 device-simulation-dotnet-master 資料夾。 按一下任何 [還原] 按鈕,以修正任何無法解決的相依性。
開啟 WebService/appsettings.ini 檔案,並將 Cosmos DB 連接字串指派給 documentdb_connstring 變數,並修改設定,如下所示:
device_models_folder = C:\temp\devicemodels\
device_models_scripts_folder = C:\temp\devicemodels\scripts\
若要在本機執行微服務,請按一下 [偵錯] > [開始偵錯]。
Visual Studio Code 中的 [終端機] 視窗會顯示執行中微服務的輸出。
在完成後續步驟時,請讓裝置模擬微服務繼續在 Visual Studio Code 的這個執行個體中執行。
設定裝置事件監視器
在本節中,您會使用 Azure CLI 來設定事件監視器,以檢視從連線至 IoT 中樞的裝置所傳來的遙測資料。
下列指令碼假設 IoT 中樞的名稱是 device-simulation-test。
# Install the IoT extension if it's not already installed
az extension add --name azure-iot
# Monitor telemetry sent to your hub
az iot hub monitor-events --hub-name device-simulation-test
在測試模擬裝置時,請讓事件監視器繼續執行。
使用更新的 Chiller 裝置類型來建立模擬
在本節中,您會使用 Postman 工具來要求裝置模擬微服務,以使用更新的 Chiller 裝置類型來執行模擬。 Postman 這個工具可讓您傳送 REST 要求給 Web 服務。 您所需的 Postman 設定檔位於 device-simulation-dotnet 存放庫的本機複本。
若要設定 Postman:
在本機電腦上開啟 Postman。
按一下 [檔案] > [匯入]。 然後按一下 [選擇檔案]。
瀏覽至 device-simulation-dotnet-master/docs/postman 資料夾。 選取 [Azure IoT 裝置模擬解決方案accelerator.postman_collection ] 和 [Azure IoT 裝置模擬解決方案accelerator.postman_environment], 然後按一下 [ 開啟]。
將 Azure IoT Device Simulation solution accelerator 展開至您可以傳送的要求。
按一下 [無環境 ],然後選取 [Azure IoT 裝置模擬解決方案加速器]。
現在,您已在 Postman 工作區中載入集合和環境,以供用來與裝置模擬微服務互動。
若要設定和執行模擬:
在 Postman 集合中,選取 [建立已修改的 Chiller 模擬],然後按一下 [傳送]。 此要求會針對模擬的 Chiller 裝置類型建立四個執行個體。
Azure CLI 視窗中的事件監視器輸出會顯示模擬裝置的遙測資料,包括新的 internal_temperature 值。
若要停止模擬,請在 Postman 中選取 [停止模擬] 要求,然後按一下 [傳送]。
使用燈泡裝置類型來建立模擬
在本節中,您會使用 Postman 工具來要求裝置模擬微服務,以使用燈泡裝置類型來執行模擬。 Postman 這個工具可讓您傳送 REST 要求給 Web 服務。
若要設定和執行模擬:
在 Postman 集合中,選取 [建立燈泡模擬],然後按一下 [傳送]。 此要求會針對模擬的燈泡裝置類型建立兩個執行個體。
Azure CLI 視窗中的事件監視器輸出會顯示模擬燈泡的遙測資料。
若要停止模擬,請在 Postman 中選取 [停止模擬] 要求,然後按一下 [傳送]。
清除資源
您可以停止兩個在其 Visual Studio Code 執行個體中本機執行的微服務 ([偵錯] > [停止偵錯])。
如果您不再需要 IoT 中樞和 Cosmos DB 執行個體,請從 Azure 訂用帳戶中加以刪除,以避免產生任何不必要的費用。
後續步驟
本指南已示範如何建立自訂的模擬裝置類型,並藉由在本機執行裝置模擬微服務來進行測試。
接下來,建議您了解如何將自訂模擬裝置類型部署至遠端監視解決方案加速器。