0

I'm trying to find out which telemetry-producing sensors (aka singals/time series') are attached to a device using the REST API. The deviceTemplates endpoint has all this information but there is no JSON schema for it's response anywhere. The responses are highly variable and hard to parse. I need a robust solution for any kind of device.

Any help/advice? Could the IoT Hub SDK help?

dax
  • 591
  • 2
  • 6
  • 16
  • I do recommend to read the https://learn.microsoft.com/en-us/azure/iot-develop/overview-iot-plug-and-play – Roman Kiss Sep 22 '22 at 21:42
  • While it is hard to generalize the device template format, for the IoT type devices in IoT central, you can target the contents section of the device template JSON file to extract telemetry data. You may refer to this JavaScript code on [How to parse JSON object](https://stackoverflow.com/questions/9991805/javascript-how-to-parse-json-array) and parse the telemetry data in the contents section. Refer to this resource to get more understanding on Device Template -- [Different sections in Device Template](https://learn.microsoft.com/en-us/azure/iot-central/core/howto-set-up-template) – LeelaRajesh_Sayana Oct 18 '22 at 21:33

1 Answers1

0

I would recommend using Json.Net Schema framework. It lets you determine your own custom JSON schema that would fit your needs.

Please refer the following documentation that helps you on how to Load your schema. Once you have the schema determined you can validate it against a device template and confirm if it follows the same pattern.

You can then parse the JSON file using Json.Net to extract the data you may need. Here is sample of the code that extracts contents from a device template. I have used "RS40 Occupancy Sensor.json" device template in my case.

    public static void LoadJson()
    {
        using (StreamReader r = new StreamReader(@"Path to your JSON device template file"))
        {
            string json = r.ReadToEnd();
            try
            {
                dynamic? array = JsonConvert.DeserializeObject(json);
                if (array != null)
                {
                    foreach (var item in array)
                    {
                        var contents = item.contents;
                        //filter contents to get telemetry data or other information
                    }
                }

            }                
            catch (Exception ex) { Console.WriteLine(ex); }
    }