クラウド側から IoT Edge 上のモジュールの状態を監視する方法をご案内します。
ただし、Azure IoT Edge は現在プレビューであるため、一般公開時には変更となる可能性がございます点にご留意ください。
1. Azure ポータルから状態を確認
=======================
以下の手順でAzure ポータルよりご確認いただくことができます。
(1) Azure ポータルのIoT Hub の左側のペインから [IoT Edge (preview)] をクリックし、右側のペインで IoT Edge デバイスの デバイス ID をクリックします。
(2) [デバイスの詳細] 画面の下部の Deployed Modules タブで$edgeAgent と$edgeHub などの各モジュールのRUNTIME STATUS がrunning となっていることが確認できます。
2. プログラムから確認する手順
=======================
プログラムから監視する場合は、HTTP REST API を利用することでモジュール ツインから状態を取得可能です。
-
- Azure IoT REST –APIs – Module Twin
< https://github.com/jonbgallant/azure-iot-rest#module-twin >
以下にREST API を呼び出す時に設定すべき詳細なパラメーターが公開されています。
api-version が 2017-11-08-preview であることにご注意ください。
< https://github.com/jonbgallant/azure-iot-rest/blob/master/data-plane/devices/modules/module-twin.py >
ご参考までに、上記を元にC# で作成した REST API の呼び出しサンプルを以下に記載します。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Net;
using System.Net.Http;
using System.Security.Cryptography;
using System.Net.Http.Headers;
namespace IoTREST
{
public class IoTRESTClient
{
private string IoTHubName;
private string httpResult;
private string Key;
private string policyName;
public IoTRESTClient(string _IoTHubName, string _Key, string _policyName)
{
IoTHubName = _IoTHubName;
httpResult = "";
Key = _Key;
policyName = _policyName;
}
public string generateSasToken(string resourceUri, string key, string policyName, int expiryInSeconds =
{
TimeSpan fromEpochStart = DateTime.UtcNow - new DateTime(1970, 1, 1);
string expiry = Convert.ToString((int)fromEpochStart.TotalSeconds + expiryInSeconds);
string stringToSign = WebUtility.UrlEncode(resourceUri).ToLower() + "n" + expiry;
HMACSHA256 hmac = new HMACSHA256(Convert.FromBase64String(key));
string signature = Convert.ToBase64String(hmac.ComputeHash(Encoding.UTF8.GetBytes(stringToSign)));
string token = String.Format(CultureInfo.InvariantCulture, "SharedAccessSignature sr={0}&sig={1}&se={2}", WebUtility.UrlEncode(resourceUri).ToLower(), WebUtility.UrlEncode(signature), expiry);
if (!String.IsNullOrEmpty(policyName))
{
token += "&skn=" + policyName;
}
return token;
}
public async void GetModuleTwin(string deviceId, string moduleId)
{
HttpClient client = new HttpClient();
string uri = "https://" + IoTHubName + ".azure-devices.net/twins/" + deviceId + "/modules/"+ moduleId + "?api-version=2017-11-08-preview";
Console.WriteLine("uri:" + uri);
HttpResponseMessage response;
string sasToken = generateSasToken(IoTHubName + ".azure-devices.net", Key, policyName);
Console.WriteLine("sasToken:" + sasToken);
client.DefaultRequestHeaders.Add("Authorization", sasToken);
try
{
response = await client.GetAsync(uri);
httpResult = await response.Content.ReadAsStringAsync();
Console.WriteLine(response.StatusCode.ToString() + "rn" + httpResult);
}
catch (Exception ex)
{
Console.WriteLine();
Console.WriteLine("Error in sample: {0}", ex.Message);
}
}
class Program
{
static void Main(string[] args)
{
IoTRESTClient client = new IoTRESTClient("{IoT Hub Name}", "{Shared access keys}", "{Policy Name, e.g. iothubowner}");
client.GetModuleTwin("test-edge-device1", "$edgeAgent");
Console.ReadKey();
Console.WriteLine("Press any key to exit...");
}
}
}
} |
このサンプルを使って確認した手順を記載しますので、ご参考になりましたら幸いです。
-
- Visual Studio 2017 のC # のコンソールアプリケーションのプロジェクトを作成します。
-
- 添付のソースを貼り、以下を変更します。
76 行目: {IoT Hub Name} はIoT Hub の名前、{Shared access keys} はiothubowner のプライマリ キー(接続文字列ではありません)、{Policy Name, e.g. iothubowner} は iothubowner
77 行目: test-edge-device1 はIoT Edge のデバイスID、$edgeAgent はそのまま使用すればedgeAgent の監視、$edgeHub に変更すればedgeHub の監視になります。
-
- ビルドします。
-
- コマンドプロンプトから exe を実行します。
3. デプロイの監視手順
=======================
「Edge エージェントと Edge ハブのモジュール ツインのプロパティ」のドキュメントに関連する、デプロイの監視手順については、以下のドキュメントをご参照ください。
-
- 大規模なIoT Edge モジュールの展開と監視- プレビュー
< https://docs.microsoft.com/ja-jp/azure/iot-edge/how-to-deploy-monitor >
「デプロイの監視」の項目をご参照ください。
上記の内容がお役に立てば幸いです。
Azure IoT 開発サポートチーム 津田