public async Task <ActionResult> GetDevice(string deviceId) { Device device = null; Twin twin = null; DEVICE_DATA deviceData = new DEVICE_DATA(); try { device = await _helper.GetDevice(deviceId).ConfigureAwait(false); twin = await _helper.GetTwin(deviceId).ConfigureAwait(false); if (device == null) { return(BadRequest()); } var jsonData = JsonConvert.SerializeObject(device); 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; } if (twin != null) { JObject twinJson = (JObject)JsonConvert.DeserializeObject(twin.ToJson()); if (twinJson.ContainsKey("modelId")) { deviceData.modelId = twin.ModelId; } } } catch (Exception ex) { _logger.LogError($"Error {ex}"); } return(Json(deviceData)); }
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)); }