Skip to content
This repository has been archived by the owner on Oct 12, 2023. It is now read-only.

Updated project structure and assemblies for GA #629

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.0-runtime
WORKDIR /app
COPY --from=build-env /app/out ./

RUN useradd -ms /bin/bash moduleuser
USER moduleuser

ENTRYPOINT ["dotnet", "azureiotedge-simulated-temperature-sensor.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
FROM microsoft/dotnet:2.0-runtime-stretch AS base

RUN apt-get update && \
apt-get install -y --no-install-recommends unzip procps && \
rm -rf /var/lib/apt/lists/*

RUN useradd -ms /bin/bash moduleuser
USER moduleuser
RUN curl -sSL https://aka.ms/getvsdbgsh | bash /dev/stdin -v latest -l ~/vsdbg

FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Debug -o out

FROM base
WORKDIR /app
COPY --from=build-env /app/out ./

ENTRYPOINT ["dotnet", "azureiotedge-simulated-temperature-sensor.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.0-runtime-stretch-arm32v7
WORKDIR /app
COPY --from=build-env /app/out ./

RUN useradd -ms /bin/bash moduleuser
USER moduleuser

ENTRYPOINT ["dotnet", "azureiotedge-simulated-temperature-sensor.dll"]
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
FROM microsoft/dotnet:2.0-sdk AS build-env
WORKDIR /app

COPY *.csproj ./
RUN dotnet restore

COPY . ./
RUN dotnet publish -c Release -o out

FROM microsoft/dotnet:2.0-runtime
WORKDIR /app
COPY --from=build-env /app/out ./
ENTRYPOINT ["dotnet", "azureiotedge-simulated-temperature-sensor.dll"]
59 changes: 11 additions & 48 deletions v2/samples/azureiotedge-simulated-temperature-sensor/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;
using System.Net;
using AzureIotEdgeSimulatedTemperatureSensor.Simulation;

// disabling async warning as the SendSimulationData is an async method
// but we don't wait for it
Expand All @@ -33,13 +34,7 @@ class Program

static async Task Main(string[] args)
{
// The Edge runtime gives us the connection string we need -- it is injected as an environment variable
var connectionString = Environment.GetEnvironmentVariable("EdgeHubConnectionString");

// Cert verification is not yet fully functional when using Windows OS for the container
var bypassCertVerification = RuntimeInformation.IsOSPlatform(OSPlatform.Windows);
if (!bypassCertVerification) InstallCert();
await Init(connectionString, bypassCertVerification);
await Init();

// Wait until the app unloads or is cancelled
var cts = new CancellationTokenSource();
Expand All @@ -59,52 +54,20 @@ public static Task WhenCancelled(CancellationToken cancellationToken)
}

/// <summary>
/// Add certificate in local cert store for use by client for secure connection to IoT Edge runtime
/// </summary>
static void InstallCert()
{
var certPath = Environment.GetEnvironmentVariable("EdgeModuleCACertificateFile");
if (string.IsNullOrWhiteSpace(certPath))
{
// We cannot proceed further without a proper cert file
Console.WriteLine($"Missing path to certificate collection file: {certPath}");
throw new InvalidOperationException("Missing path to certificate file.");
}
else if (!File.Exists(certPath))
{
// We cannot proceed further without a proper cert file
Console.WriteLine($"Missing path to certificate collection file: {certPath}");
throw new InvalidOperationException("Missing certificate file.");
}
var store = new X509Store(StoreName.Root, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadWrite);
store.Add(new X509Certificate2(X509Certificate2.CreateFromCertFile(certPath)));
Console.WriteLine("Added Cert: " + certPath);
store.Close();
}


/// <summary>
/// Initializes the DeviceClient and sets up the callback to receive
/// Initializes the ModuleClient and sets up the callback to receive
/// messages containing temperature information
/// </summary>
static async Task Init(string connectionString, bool bypassCertVerification = false)
static async Task Init()
{
Console.WriteLine("Connection String {0}", connectionString);

var mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);
// During dev you might want to bypass the cert verification. It is highly recommended to verify certs systematically in production
if (bypassCertVerification)
{
mqttSetting.RemoteCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
}
MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);
ITransportSettings[] settings = { mqttSetting };

// Open a connection to the Edge runtime
var ioTHubModuleClient = DeviceClient.CreateFromConnectionString(connectionString, settings);
await ioTHubModuleClient.OpenAsync();
var ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
ioTHubModuleClient.OpenAsync();
Console.WriteLine("IoT Hub module client initialized.");

// initialize module twin properties
var moduleTwin = await ioTHubModuleClient.GetTwinAsync();
var moduleTwinCollection = moduleTwin.Properties.Desired;
desiredPropertiesData = new DesiredPropertiesData(moduleTwinCollection);
Expand All @@ -118,11 +81,11 @@ static async Task Init(string connectionString, bool bypassCertVerification = fa
// we don't pass ioTHubModuleClient as we're not sending any messages out to the message bus
await ioTHubModuleClient.SetInputMessageHandlerAsync("control", ControlMessageHandler, null);

// as this runs in a loop we don't await
// as this runs in a loop we do not await
SendSimulationData(ioTHubModuleClient);
}

private static async Task SendSimulationData(DeviceClient deviceClient)
private static async Task SendSimulationData(ModuleClient moduleClient)
{
while(true)
{
Expand All @@ -145,7 +108,7 @@ private static async Task SendSimulationData(DeviceClient deviceClient)
message.ContentEncoding = "utf-8";
message.ContentType = "application/json";

await deviceClient.SendEventAsync("temperatureOutput", message);
await moduleClient.SendEventAsync("temperatureOutput", message);
Console.WriteLine($"\t{DateTime.UtcNow.ToShortDateString()} {DateTime.UtcNow.ToLongTimeString()}> Sending message: {counter}, Body: {messageString}");

}
Expand Down
23 changes: 10 additions & 13 deletions v2/samples/azureiotedge-simulated-temperature-sensor/README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# Simulated Temperature Sensor for Azure IoT Edge

This module is an example of a temperature sensor simulation Azure IoT Edge module. You can see its usage in the [Azure IoT Edge documentation](https://docs.microsoft.com/en-us/azure/iot-edge/). It continuously creates simulated temperature data and sends the message to the `temperatureOutput` endpoint.
This Azure IoT Edge module simulates a temperature sensor. You can see its usage in the [Azure IoT Edge documentation](https://docs.microsoft.com/en-us/azure/iot-edge/). It continuously creates simulated temperature data and sends the message to the `temperatureOutput` endpoint.

## Note about this code

This code reflects the implementation details of the deployed `microsoft/azureiotedge-simulated-temperature-sensor:1.0-preview` container. It also extends the functionality to showcase the usage of direct methods and module twin property updates. To use the new features you have to build the container yourself and deploy it to a registry of your choice.

## Available Endpoints
## Code Structure

The project incorporates the same structure as the `aziotedgemodule` dotnet core template. The complete simulation logic lives in the `Simulation` folder.

## Building the container image

To build the container image you have to change the registry entry in the `module.json` file. After that you can use for instance Visual Studio Code and the corresponding [Azure IoT Edge tooling](https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-edge) to build and publish the image to the registry.

# Available Endpoints

You can interact with the Temperature Simulator in several ways.

Expand Down Expand Up @@ -93,17 +101,6 @@ Properties can be set during the set module process in the Azure Portal or via t
**SendInterval** = int value
- The interval in seconds between messages being pushed into the endpoint.

## Building Multi-Architecture Docker Container

This sample includes the file `multi-arch-manifest.yml` to showcase how to create a multi-architecture docker image.

1. Create for each architecture you want support a docker container using the Dockerfiles provided by the sample
2. Push the container to the registry of your choice
3. Replace the parameters `<registry>` and `<version>` with your values.
4. Get the [Manifest-Tool](https://github.com/estesp/manifest-tool)
5. Push `multi-arch-manifest.yml` to your registry using the manifest tool


# Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Newtonsoft.Json;

namespace AzureIotEdgeSimulatedTemperatureSensor
namespace AzureIotEdgeSimulatedTemperatureSensor.Simulation
{
public enum ControlCommandEnum
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;

namespace AzureIotEdgeSimulatedTemperatureSensor
namespace AzureIotEdgeSimulatedTemperatureSensor.Simulation
{
public class DataGenerationPolicy
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Azure.Devices.Shared;
using Newtonsoft.Json;

namespace AzureIotEdgeSimulatedTemperatureSensor
namespace AzureIotEdgeSimulatedTemperatureSensor.Simulation
{
public class DesiredPropertiesData
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using Newtonsoft.Json;

namespace AzureIotEdgeSimulatedTemperatureSensor
namespace AzureIotEdgeSimulatedTemperatureSensor.Simulation
{
public class MessageBody
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

using System;

namespace AzureIotEdgeSimulatedTemperatureSensor
namespace AzureIotEdgeSimulatedTemperatureSensor.Simulation
{
public class TemperatureDataFactory
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.6.0-preview-001" />
<PackageReference Include="Microsoft.Azure.Devices.Client" Version="1.17.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
<PackageReference Include="System.Runtime.Loader" Version="4.3.0" />
</ItemGroup>
</Project>

This file was deleted.

Loading