public void PrintInterfaceContent(DTInterfaceInfo dtif, IReadOnlyDictionary <Dtmi, DTEntityInfo> dtdlOM, int indent = 0)
        {
            var sb = new StringBuilder();

            for (int i = 0; i < indent; i++)
            {
                sb.Append("  ");
            }
            Console.WriteLine($"{sb}Interface: {dtif.Id} | {dtif.DisplayName}");
            Dictionary <string, DTContentInfo> contents = dtif.Contents;

            foreach (DTContentInfo item in contents.Values)
            {
                switch (item.EntityKind)
                {
                case DTEntityKind.Property:
                    DTPropertyInfo pi = item as DTPropertyInfo;
                    Console.WriteLine($"{sb}--Property: {pi.Name} with schema {pi.Schema}");
                    break;

                case DTEntityKind.Relationship:
                    DTRelationshipInfo ri = item as DTRelationshipInfo;
                    Console.WriteLine($"{sb}--Relationship: {ri.Name} with target {ri.Target}");
                    break;

                case DTEntityKind.Telemetry:
                    DTTelemetryInfo ti = item as DTTelemetryInfo;
                    Console.WriteLine($"{sb}--Telemetry: {ti.Name} with schema {ti.Schema}");
                    break;

                case DTEntityKind.Component:
                    DTComponentInfo ci = item as DTComponentInfo;
                    Console.WriteLine($"{sb}--Component: {ci.Id} | {ci.Name}");
                    DTInterfaceInfo component = ci.Schema;
                    PrintInterfaceContent(component, dtdlOM, indent + 1);
                    break;
                }
            }
        }
        public async Task <ActionResult> GetIoTHubDevice(string deviceId)
        {
            Device      device     = null;
            Twin        twin       = null;
            DEVICE_DATA deviceData = new DEVICE_DATA();

            deviceData.telemetry = new List <TELEMETRY_DATA>();

            // Retrieve device
            device = await _helper.GetIoTHubDevice(deviceId).ConfigureAwait(false);

            // Retrieve Deivce Twin for the device
            twin = await _helper.GetDeviceTwin(deviceId).ConfigureAwait(false);

            if (device == null || twin == null)
            {
                return(BadRequest());
            }

            deviceData.deviceId           = device.Id;
            deviceData.connectionState    = device.ConnectionState.ToString();
            deviceData.status             = device.Status.ToString();
            deviceData.authenticationType = device.Authentication.Type.ToString();

            if (device.Authentication.Type == AuthenticationType.Sas)
            {
                deviceData.primaryKey   = device.Authentication.SymmetricKey.PrimaryKey;
                deviceData.secondaryKey = device.Authentication.SymmetricKey.SecondaryKey;
            }

            JObject twinJson = (JObject)JsonConvert.DeserializeObject(twin.ToJson());

            // Check if this is IoT Plug and Play device or not
            if (twinJson.ContainsKey("modelId"))
            {
                deviceData.deviceModelId = twin.ModelId;

                // Resolve Device Model
                DTDLModelResolver resolver = new DTDLModelResolver(_modelRepoUrl, _gitToken, _logger);

                var modelData = await resolver.ParseModelAsync(deviceData.deviceModelId);

                if (modelData != null)
                {
                    try
                    {
                        /*
                         * Pick up telemetry
                         * */
                        var interfaces = modelData.Where(r => r.Value.EntityKind == DTEntityKind.Telemetry).ToList();
                        foreach (var dt in interfaces)
                        {
                            TELEMETRY_DATA  data          = new TELEMETRY_DATA();
                            DTTelemetryInfo telemetryInfo = dt.Value as DTTelemetryInfo;

                            switch (telemetryInfo.Schema.EntityKind)
                            {
                            case DTEntityKind.Integer:
                            case DTEntityKind.Long:
                                // for TSI in WebUI
                                data.dataType = "Long";
                                break;

                            case DTEntityKind.Double:
                            case DTEntityKind.Float:
                                // for TSI in WebUI
                                data.dataType = "Double";
                                break;

                            case DTEntityKind.Object:
                                // for now we support point object for location

                                if (telemetryInfo.Schema.Id.Versionless.Equals("dtmi:standard:schema:geospatial:point"))
                                {
                                    data.TelemetryType = telemetryInfo.Schema.Id.Versionless;
                                    break;
                                }
                                continue;

                            default:
                                continue;
                            }

                            if (telemetryInfo.SupplementalTypes.Count > 0)
                            {
                                foreach (var supplementalType in telemetryInfo.SupplementalTypes)
                                {
                                    data.TelemetryType = supplementalType.Versionless;
                                }
                            }

                            if (telemetryInfo.DisplayName.Count > 0)
                            {
                                data.TelemetryDisplayName = telemetryInfo.DisplayName["en"];
                            }

                            if (telemetryInfo.SupplementalProperties.Count > 0)
                            {
                                if (telemetryInfo.SupplementalProperties.ContainsKey("unit"))
                                {
                                    DTUnitInfo unitInfo = telemetryInfo.SupplementalProperties["unit"] as DTUnitInfo;
                                    data.unit = unitInfo.Symbol;
                                }
                            }
                            else
                            {
                                // No Unit
                                if (deviceData.deviceModelId.StartsWith("dtmi:seeedkk:wioterminal:wioterminal_co2checker"))
                                {
                                    if (telemetryInfo.Name.Equals("co2"))
                                    {
                                        data.unit = "PPM";
                                    }
                                    else if (telemetryInfo.Name.Equals("humi"))
                                    {
                                        data.unit = "%";
                                    }
                                    else if (telemetryInfo.Name.Equals("wbgt"))
                                    {
                                        data.unit = "°C";
                                    }
                                }
                            }

                            data.TelemetryName = telemetryInfo.Name;
                            deviceData.telemetry.Add(data);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError($"Exception in ParseAsync() : {e.Message}");
                    }
                }
            }
            return(Json(deviceData));
        }