Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

クラウド側から IoT Edge 上のモジュールの状態を監視する方法

クラウド側から 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

 

以下にREST API を呼び出す時に設定すべき詳細なパラメーターが公開されています。

api-version 2017-11-08-preview であることにご注意ください。

 >

 

ご参考までに、上記を元に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 =
3600)

        {

            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...");

            }

       }

    }

}

 

このサンプルを使って確認した手順を記載しますので、ご参考になりましたら幸いです。

    1. Visual Studio 2017 C # のコンソールアプリケーションのプロジェクトを作成します。

    1. 添付のソースを貼り、以下を変更します。

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 の監視になります。

    1. ビルドします。

    1. コマンドプロンプトから exe を実行します。

 

 

3. デプロイの監視手順

=======================

Edge エージェントと Edge ハブのモジュール ツインのプロパティ」のドキュメントに関連する、デプロイの監視手順については、以下のドキュメントをご参照ください。

 

    • 大規模なIoT Edge モジュールの展開と監視- プレビュー

 

「デプロイの監視」の項目をご参照ください。

 

 

 

上記の内容がお役に立てば幸いです。

 

Azure IoT 開発サポートチーム 津田

 

Share the post

クラウド側から IoT Edge 上のモジュールの状態を監視する方法

×

Subscribe to Msdn Blogs | Get The Latest Information, Insights, Announcements, And News From Microsoft Experts And Developers In The Msdn Blogs.

Get updates delivered right to your inbox!

Thank you for your subscription

×