Skip to main content

Why Device Twins from Azure IoT Hub are important for us

This post is focusing on Device Twins that are available on Azure IoT Hub.
If it is your first time when you hear about Azure IoT Hub, than you should know that Azure IoT Hub is a powerfull serviced offered by Azure that can connect, manage and monitor your devices. More information about it can be found on Azure site

Why Device Twins?
When we are working with devices, we will find all the time information that is specific to individual device. Information like device serial number, customer information, location, device configuration is part of this type of information.
In an ideal world, we should have a unique location where all this data is stored. No custom code of functionality should be developed by us. Azure IoT Hub is offering this to us through Device Twins.

What are Device Twins?
It’s a collection of properties that specific for each individual device. Device twins contains 3 types of structure (where the last two are similar, but have different roles)

  • Tags: Device specific information that resides on the Azure IoT Hub and never reach the device
  • Desire Properties: Properties set on Azure IoT Hub (by backend) and are delivered to the device
  • Reported Properties: Properties set by Device and are delivered to Azure IoT Hub (to backend)

You might find odd that there are 2 types of properties/structures in device twin – tags and desired/reported properties. From the usability point of view, this is useful and handy.
Tags are used when you want to store information related to a device on the backend without sending it to the device. A good example is customer ID or device location. There is no need to send this data to device if the device doesn’t need this data.
In contrast, desired/reported properties are all the time send/received from device. Properties like device configuration, current status or different counters can be found inside them.

Format
All information related to Device Twins is stored in JSON format. Tags, Desired Properties and Reported Properties are stored separately in the same JSON, as different nodes.
"tags": {
  "$etag": "32423",
  "cliendId": "2323232",
  "location":  {
    "country": "Romania"
    "city": "Cluj-Napoca",
    "zipCode": "400001"
  }
},
"properties": {
  "desired": {
    "logsFrequency": 1000ms,
    "status": 1,
    "$metadata" : {...},
    "$version": 4
  },
  "reported": {
    "logsFrequency": 1000ms,
    "status": 1,
    "$metadata" : {...},
    "$version": 54
  }
}

Not all the time, the desired and reported properties will have the same value, we will see why later on.
The keys for properties are case-sensitive and can contain most of the characters (except ‘ ‘, ‘.’, ‘$’, C0 and C1 segments from Unicode control chars).
The values of properties can be any JSON type like string, int, boolean and even object. When you are playing with object you shall remember that the maximum depth of a property is 5. Arrays are not supported, but can be mapped easily as multiple properties.


Tracking of Device Twins
We can see Device Twin as a clone of our Device in the cloud. We can know all the time what is the current state of the device and what configuration was received by the device and what not.
For tracking, we will find under each type of Device Twin a node called $metadata, where information related to when was the last update of each property can be found. All the properties that are added by Azure IoT Hub are beginning with ‘$’ (‘$lastUpaded’, ‘version’).
The $version property is automatically added to each property structure like desired properties. The value of version is incremented with 1 each time when a change is done (a new value is added, updated or removed). Don’t forget that the version value is independently incremented for each structure. This means that the version of desired properties can be at 1400 and the reported property version can be at 4. There is no connection between them.
"properties": {
  "desired": {
    "logsFrequency": 1000ms,
    "status": 1,
    "$metadata" : {...},
    "$version": 3
  },
  "reported": {
    "logsFrequency": 5000ms,
    "status": 1,
    "securityLevel": 9
    "$metadata" : {...},
    "$version": 94
  }
}

In the above example we can obsever that the logsFrequency property value is not the same. Also, we have additional one new property in the reported properties - securityLevel.
The maximum size of properties value is 512 bytes. Overall, the maximum size of each structure can be maximum 8KB. 8KB for tags, 8KB for desired properties and 8KB for reported properties.

Actions
From the backend, we can retrieve the device twins based on device ID. Once we get this data, we can add or updates tags and desired properties. The reported properties can be only read on the backend.
At device level, we can retrieve the device twin for our current device, access desired properties and add or update the reported properties. At device level, we can device to be notified when a desired property is update or added (so called observe desired properties). Tags are not accessible on device.
To remove an existing property from tags, desired properties or reported properties we need to set the value to null.
"properties": {
  "desired": {
    "logsFrequency": 1000ms,
    "status": 1,
    "securityLevel": null
    "$metadata" : {...},
    "$version": 4
  },
  "reported": {
    "logsFrequency": 5000ms,
    "status": 1,
    "securityLevel": 0
    "$metadata" : {...},
    "$version": 94
  }
}

Communication and reconciliation
If we are on backend, the story is simple. We retrieve from Azure IoT Hub the last know device twins, with all the property’s values that are available. There is no magic required on this side.
On the device, the story is a little different. First of all, the communication for device twins is done over the same TLS connection. When a desired value is changed, the notification is sent to the device.
But we shall remember that notification is not queened in the command queue for C2D. It means that even if we have commands in the queue for that specific device, the desired properties update notification will be received by the device – using a different MQTT topic.
This is great, because we can use device twins when we want to send an action with a very high priority – like stopping the device.
The device twin notification is not persisted by Azure IoT Hub. If our device is offline when the notification is send, the notification will not reach the device. It means, that once a device state changes from offline to online, on the device we need to retrieve the desired properties and check the version of it. If the version is higher than the one that we have on the device, it means that one or more properties value changed and we need to check the values.

Conclusion
Device Twins are a powerful feature of Azure IoT Hub. If they are used smart, can change the way how you are using and communicate with your backend. Before jumping to code, try to understand them and what are the main characteristic of each of them.

Remember:

  1. Tags can be set, read and accessed only by backend 
  2. Reported Properties are set by device can be read by backend
  3. Desired Properties are set by backend and can be read by backend
  4. Use version and lastUpdated properties to detect updates when necessary


Comments

Popular posts from this blog

How to check in AngularJS if a service was register or not

There are cases when you need to check in a service or a controller was register in AngularJS.
For example a valid use case is when you have the same implementation running on multiple application. In this case, you may want to intercept the HTTP provider and add a custom step there. This step don’t needs to run on all the application, only in the one where the service exist and register.
A solution for this case would be to have a flag in the configuration that specify this. In the core you would have an IF that would check the value of this flag.
Another solution is to check if a specific service was register in AngularJS or not. If the service was register that you would execute your own logic.
To check if a service was register or not in AngularJS container you need to call the ‘has’ method of ‘inhector’. It will return TRUE if the service was register.
if ($injector.has('httpInterceptorService')) { $httpProvider.interceptors.push('httpInterceptorService&#…

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine:
threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration:

TeamCity.NET 4.51EF 6.0.2VS2013
It seems that there …

Run native .NET application in Docker (.NET Framework 4.6.2)

Scope
The main scope of this post is to see how we can run a legacy application written in .NET Framework in Docker.

Context
First of all, let’s define what is a legacy application in our context. By a legacy application we understand an application that runs .NET Framework 3.5 or higher in a production environment where we don’t have any more the people or documentation that would help us to understand what is happening behind the scene.
In this scenarios, you might want to migrate the current solution from a standard environment to Docker. There are many advantages for such a migration, like:

Continuous DeploymentTestingIsolationSecurity at container levelVersioning ControlEnvironment Standardization
Until now, we didn’t had the possibility to run a .NET application in Docker. With .NET Core, there was support for .NET Core in Docker, but migration from a full .NET framework to .NET Core can be costly and even impossible. Not only because of lack of features, but also because once you…