Nagarajan.M
Nagarajan.M

Reputation: 93

How to get online and offline device status in azure iot Hub using azure functions

get online and offline device status in azure iot Hub using azure functions

Upvotes: 1

Views: 2159

Answers (3)

Florin Bobiș
Florin Bobiș

Reputation: 11

@claymodel I think that's actually the same as the internal implementation of the connectionState inside the IoT Devices section of Azure Portal.

The reason I believe it is so is because the DeviceDisconnected event reaches the Azure Function at approximately the same time it is shown in the DeviceTwin properties section.

This means that's it's basically as unreliable as the underlying implementation. I only got notified of the disconnection after a couple of minutes - so, useless

I now have to query the device each time before performing an operation, just to make sure the device is actually connected.

It's a real nightmare! Another thing to be aware of - there are two ways you can get notified of device lifecycle events:

  1. from the Message Routing section but that actually only sends the created and deleted events in some kind of format inside the EventData class
  2. from the Events section of the IoT Hub you create a subscription and point to you event hub for handling DeviceCreated, DeviceDeleted, DeviceConnected and DeviceDisconnected - which send a different kind of format inside the EventData class.

So there is a HUGE misalignment between sections on the IoT Hub.

To be honest, I have no idea if there is a good answer for checking device connectivity in a reliable manner. Hopefully they will actually do something about it in the near future

Upvotes: 1

claymodel
claymodel

Reputation: 49

First configure which events to publish from your IoT hub.More specifically DeviceConnected and DeviceDisconnected event will solve your purpose.

Then finally implement EventHub Trigger in Azure function to get the events.

Upvotes: 0

silent
silent

Reputation: 16208

The connectionState field is not reliable and should not be used to query device state. See here for details.

Instead it is recommended to either implement heart beats which are being sent from your devices. Or you can also create an Azure EventGrid subscription on device connection events and listen to those: https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-how-to-order-connection-state-events#configure-subscription-for-iot-hub-events

//edit: You could actually make use of the new stateful durable Function feature for your use case, in conjunction with EventGrid: https://learn.microsoft.com/de-de/azure/azure-functions/durable/durable-functions-preview

Upvotes: 1

Related Questions