IoT Edge Filter Solution

Description

This sample shows how to use custom code in a module to use message routing from another module (a simulated temperature sensor in this case), analyse the incoming messages and send them to a local blob storage module.

IoT Edge Filter Solution

You can find the full source code on my GitHub repository: ReneHezser/Edge-Filter-Blob-Solution.

Details

The solution contains of three containers (IoT edge modules). The Simulated Temperature and Blob Storage on IoT Edge are 1P modules from Microsoft, whereas the FilterModule has been adjusted to upload certain messages to a local blob storage.

static string containerName = "edge";
static BlobContainerClient containerClient;
...
static async Task ConnectToStorage()
{
    var connectionString = Environment.GetEnvironmentVariable("StorageAccountConnectionString");
    if (string.IsNullOrEmpty(connectionString)) throw new ArgumentNullException("StorageAccountConnectionString");

    var blobServiceClient = new BlobServiceClient(connectionString);
    containerClient = blobServiceClient.GetBlobContainerClient(containerName);
    await containerClient.CreateIfNotExistsAsync();
}
...
private static async Task UploadBlob(string messageString, byte[] messageBytes)
{
    JObject json = (JObject)JObject.Parse(messageString);
    if (json["machine"]?["temperature"]?.Value<float>() > 25.0)
    {
        // use the timestamp as filename
        var filename = ((DateTime)json["timeCreated"].Value<DateTime>()).ToString()...) + ".json";
        
        var binaryData = new BinaryData(messageBytes);
        var blobClient = containerClient.GetBlobClient(filename);
        // create a new blob without overwriting existing blobs
        await blobClient.UploadAsync(binaryData, false);
    }

    await Task.CompletedTask;
}

The code is pretty simple. My sample solution also shows how to deploy the two other modules and automate container creation with GitHub Actions.

Summary

The code sample on GitHub shows how to access messages sent from another module via IoT Edge message routing, filter them and store in a local blob storage container.

It is using Visual Studio Code extensions, devcontainers and GitHub actions to create a deployment manifest + docker images.