Search code examples
node.jsazureazure-iot-hubazure-iot-hub-device-management

how to get "connectionStateUpdatedTime" from IOT HUB using node js sdk 'azure-iothub'


I am using "('azure-iothub').Registry" to get device twin data.

strQuery = `SELECT * FROM devices where deviceId IN [${deviceIds}]`;
query = registry.createQuery(strQuery, 500);

but device twin does not have "connectionStateUpdatedTime" property. As per MS document, it is in device identity. https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-identity-registry#device-identity-properties

is there a way to get "connectionStateUpdatedTime" property?


Solution

  • You will not get connectionStateUpdatedTime using above approach, as it returns below schema:

    {
        "deviceId": "myDeviceId",
        "etag": "AAAAAAAAAAc=",
        "status": "enabled",
        "statusUpdateTime": "0001-01-01T00:00:00",
        "connectionState": "Disconnected",
        "lastActivityTime": "0001-01-01T00:00:00",
        "cloudToDeviceMessageCount": 0,
        "authenticationType": "sas",
        "x509Thumbprint": {
            "primaryThumbprint": null,
            "secondaryThumbprint": null
        },
        "version": 2,
        "tags": {
            "location": {
                "region": "US",
                "plant": "Redmond43"
            }
        },
        "properties": {
            "desired": {
                "telemetryConfig": {
                    "configId": "db00ebf5-eeeb-42be-86a1-458cccb69e57",
                    "sendFrequencyInSecs": 300
                },
                "$metadata": {
                ...
                },
                "$version": 4
            },
            "reported": {
                "connectivity": {
                    "type": "cellular"
                },
                "telemetryConfig": {
                    "configId": "db00ebf5-eeeb-42be-86a1-458cccb69e57",
                    "sendFrequencyInSecs": 300,
                    "status": "Success"
                },
                "$metadata": {
                ...
                },
                "$version": 7
            }
        }
    }
    

    You are referring to identity registry which is a REST-capable collection of device or module identity resources. When you add an entry in the identity registry, IoT Hub creates a set of per-device resources such as the queue that contains in-flight cloud-to-device messages. For more information about the import and export APIs, see IoT Hub resource provider REST APIs.

    For your requirement, you can use Devices - Get Devices API:

    GET https://fully-qualified-iothubname.azure-devices.net/devices?api-version=2020-05-31-preview
    

    which will give you response as Device array which will have connectionStateUpdatedTime.

    Let me know if you have follow up questions or if I understood your question incorrectly.