0

How can I draw (I mean, get data to draw) a timeline of IotHub device client connection state?

I would like to draw an availability status timeline from all my devices, for that I am doing the following:

  • Every one minute: Request all '$edgeHub' Module Identity Twin
  • Save the '$edgeHub' reported clients on a database
  • Get a timeline from this database

When my number of devices grows I will do a lot of requests, I was wondering if there is no other optimized way to do it using Azure IoT resources.

From '$edgeHub' module Twin I get the sample:

"reported": {
  "clients": {
    "iot/device": {
      "status": "Connected",
      "lastConnectedTimeUtc": "2020-11-30T12:00:41.5918442Z",
      "lastDisconnectedTimeUtc": "2020-11-30T12:00:41.5737114Z"
    }
  }

For API calls I am using https://github.com/amenzhinsky/iothub

Appreciate any response that helps me to investigate more about Azure monitoring device status.

Lohmann
  • 89
  • 2
  • 9

3 Answers3

2

1. Query

Instead of requesting all the module twins one by one, I would opt for using an IoT Hub query.

SELECT * FROM devices.modules WHERE is_defined(properties.reported.clients)

I don't know if your SDK supports that, but most (if not all) of the official SDKs have support to run queries. This will return every module twin that has the clients reported property defined. You could run that on a schedule and then save that output to a database as you had originally planned.

2. Route all module twin events to an endpoint

This one is a bit more tricky, but you can route device/module changes based on a query. You can then route all the events to a separate endpoint. The route would be something like:

IS_OBJECT($twin.properties.reported.clients)

You can read more on message routing here. The benefit of this approach is that you don't do any requests to IoT Hub and receive changes real-time. You can even consume these events using Azure Stream Analytics, which supports output to Power BI, Table storage and Cosmos DB natively. Result: you wrote no code and used only Azure services. You might want to consult the Azure pricing calculator if you want to leverage Azure Stream Analytics though.

Note: I did not thoroughly test solution #2, but theoretically this should work.

Matthijs van der Veer
  • 3,865
  • 2
  • 12
  • 22
  • Thanks for your answer, it helped me a lot. Unfornutally I did not found a good way to perform Query using Golang SDKs, so I will do the service worker using traditional API requests to solve the problem for while (we do not have many devices). I am starting the second option also, but I believe that I will take a time to put it to work, as soon as possible I will reply about the second option :). – Lohmann Dec 01 '20 at 01:32
  • I suspect with the second approach there will be no connected and disconnected events will be received.https://stackoverflow.com/questions/64369985/iot-hub-message-routing-with-devicelifecycleevents-source-does-not-work/64420097#64420097 – iAviator Dec 01 '20 at 08:52
  • I believe with some minor changes in the second option, it will work. This Microsoft doc helps a little https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-identity-registry#device-heartbeat – Lohmann Dec 03 '20 at 22:11
  • 1
    Absolutely, you could use device heartbeat. You could also use an event grid subscription on device connection/disconnection status. Many ways to go about it, but I'm glad it's a viable option, thanks for letting me know. – Matthijs van der Veer Dec 04 '20 at 06:46
0

To add to @matthijs-van-der-veer's answer you could also subscribe to device twin changes and update the counters on the twin change event.

tymtam
  • 31,798
  • 8
  • 86
  • 126
-1

Another approach, try sending the device life cycle events Device connected, Device Disconnected from Event Grid to Event Hub. And from Event Hub send this to any endpoint for processing the event i.e. may be a module that listens to the event from Event Hub.

So the flow will be like this-> IoT Hub Blade -> Events -> Add Subscription -> Add Event hub namespace endpoint

iAviator
  • 1,310
  • 13
  • 31