Azure IoT Edge + .NET Core(Ubuntu 14.04)

.NET Coreサンプルをビルド、実行してみたいと思います。

軸となる手順は.NET Core Sampleです。

OSのインストール

下記と同じ設定の、AzureのA0 Basicを使います。

matsujirushi.hatenablog.jp

Azure IoT Edgeのインストール

こちらのSet up a Linux development environmentのとおり、必要なパッケージを インストールして、Azure IoT Edgeのgitをクローンします。

matsujirushi@edge2:~$ sudo apt-get update
matsujirushi@edge2:~$ sudo apt-get install curl build-essential libcurl4-openssl-dev git cmake pkg-config libssl-dev uuid-dev valgrind libglib2.0-dev libtool autoconf
matsujirushi@edge2:~$ git clone https://github.com/Azure/iot-edge.git

.NET Coreのインストール

Install .NET Core SDKUbuntu 14.04のとおり、.NET Coreをインストールします。

matsujirushi@edge2:~$ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-trusty-prod trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
matsujirushi@edge2:~$ sudo apt-get update
matsujirushi@edge2:~$ sudo apt-get install dotnet-sdk-2.0.0

参考にしたInstall .NET Core SDKには"sudo apt-get install dotnet-sdk-2.0.0"と書かれているので、最新の.NET Core v2.0をインストールする手順ですが、.NET Core Sampleには、"Our current version of the binding was tested and loads modules written in .NET Core v1.1.1.“と、.NET Core v1.1.1でテストしたとあります、、、

まぁ、このままv2.0で進めてみます。

.NET Coreの動作確認

とくに手順には記載ありませんが、.NET Coreが動くか確認しておきましょう。

matsujirushi@edge2:~$ dotnet new console -o consoleapp
matsujirushi@edge2:~$ cd consoleapp
matsujirushi@edge2:~/consoleapp$ dotnet run
Hello World!
matsujirushi@edge2:~/consoleapp$ cd ~

サンプルをビルド、実行

ビルド

Building the sampleのとおり、ビルドします。

matsujirushi@edge2:~$ cd iot-edge
matsujirushi@edge2:~/iot-edge$ tools/build.sh --enable-dotnet-core-binding

ビルド時に自動的に実行されるテストでエラーが発生。

Starting test execution, please wait...
Testhost process exited with error: It was not possible to find any compatible framework version
The specified framework 'Microsoft.NETCore.App', version '1.1.1' was not found.
  - Check application dependencies and target a framework version installed at:
      /
  - Alternatively, install the framework version '1.1.1'.

v1.1.1じゃないとダメですか。そうですか。

.NET Core v1.1.1をインストール

v1.1.1の入れ方が分からず、あれこれネット徘徊してようやく見つけました。

github.com

Ubuntu 14.04はこちらです。

matsujirushi@edge2:~$ sudo sh -c 'echo "deb [arch=amd64] https://apt-mo.trafficmanager.net/repos/dotnet-release/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'
matsujirushi@edge2:~$ sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys 417A0893
matsujirushi@edge2:~$ sudo apt-get update
matsujirushi@edge2:~$ sudo apt-get install dotnet-dev-1.0.1

ビルド(2回目)

ハイ、気を取り直して再びビルド。

matsujirushi@edge2:~$ cd iot-edge
matsujirushi@edge2:~/iot-edge$ tools/build.sh --enable-dotnet-core-binding

CMakeのバージョンが古いと言われてしまいました。

CMake Error at CMakeLists.txt:320 (cmake_minimum_required):
  CMake 3.2.2 or higher is required.  You are running version 2.8.12.2

最新CMakeをビルド&インストール

CMakeサイトを見ると、現在の最新版は3.9.2でした。

3.9.2のソースをダウンロードして、ビルド、インストールします。

matsujirushi@edge2:~$ wget https://cmake.org/files/v3.9/cmake-3.9.2.tar.gz
matsujirushi@edge2:~$ tar xvf cmake-3.9.2.tar.gz
matsujirushi@edge2:~$ cd cmake-3.9.2
matsujirushi@edge2:~/cmake-3.9.2$ ./bootstrap && make && sudo make install

ビルド(3回目)

はぁ、気を取り直して再びビルド。

matsujirushi@edge2:~$ cd iot-edge
matsujirushi@edge2:~/iot-edge$ tools/build.sh --enable-dotnet-core-binding

オッケー、エラー無し!

実行

Running the sampleに記載がありますが、この文章ではちょっと分からない、、、

ファイルを漁ったところ、これで実行できることが分かりました。

matsujirushi@edge2:~$ cd ~/iot-edge/build/samples/dotnet_core_module_sample
matsujirushi@edge2:~/iot-edge/build/samples/dotnet_core_module_sample$ ./dotnet_core_module_sample ../../../samples/dotnet_core_module_sample/src/dotnet_core_module_sample_lin.json

実行すると、このように表示されます。

gateway successfully created from JSON
gateway shall run until ENTER is pressed
9/9/17 1:45:02 PM>?Printer?module?received?message:?SensorData: 1360631426
        Property[0]>?Key=source?:?Value=sensor
9/9/17 1:45:06 PM>?Printer?module?received?message:?SensorData: 276754225
        Property[0]>?Key=source?:?Value=sensor
9/9/17 1:45:11 PM>?Printer?module?received?message:?SensorData: 1900584355
        Property[0]>?Key=source?:?Value=sensor
9/9/17 1:45:16 PM>?Printer?module?received?message:?SensorData: 1806553877
        Property[0]>?Key=source?:?Value=sensor

まとめ

  • .NET Coreは、v1.1.1をインストールする。
  • CMakeは、3.2.2以上をインストールする。

さあ、次は自作モジュールか。その前に、VisualStudioからデプロイ、リモートデバッグできる環境が使えると良いのですが、それって可能なんだろうか?