/// <summary> /// Loads the most recent Device telemetry. /// </summary> /// <param name="deviceId"> /// The ID of the Device for which telemetry should be returned. /// </param> /// <param name="minTime"> /// The minimum time of record of the telemetry that should be returned. /// </param> /// <returns> /// Telemetry for the Device specified by deviceId, inclusively since /// minTime. /// </returns> public async Task<IEnumerable<DeviceTelemetryModel>> LoadLatestDeviceTelemetryAsync( string deviceId, IList<DeviceTelemetryFieldModel> telemetryFields, DateTime minTime) { IEnumerable<DeviceTelemetryModel> result = new DeviceTelemetryModel[0]; IEnumerable<DeviceTelemetryModel> blobModels; var telemetryBlobReader = await _blobStorageManager.GetReader(_telemetryDataPrefix, minTime); foreach (var telemetryStream in telemetryBlobReader) { try { blobModels = LoadBlobTelemetryModels(telemetryStream.Data, telemetryFields); } catch { continue; } if (blobModels == null) { break; } int preFilterCount = blobModels.Count(); blobModels = blobModels.Where( t => (t != null) && t.Timestamp.HasValue && t.Timestamp.Value >= minTime); if (preFilterCount == 0) { break; } result = result.Concat(blobModels); } if (!string.IsNullOrEmpty(deviceId)) { result = result.Where(t => t.DeviceId == deviceId); } return result; }
/// <summary> /// Loads the most recent Device telemetry. /// </summary> /// <param name="deviceId"> /// The ID of the Device for which telemetry should be returned. /// </param> /// <param name="minTime"> /// The minimum time of record of the telemetry that should be returned. /// </param> /// <returns> /// Telemetry for the Device specified by deviceId, inclusively since /// minTime. /// </returns> public async Task<IEnumerable<DeviceTelemetryModel>> LoadLatestDeviceTelemetryAsync( string deviceId, DateTime minTime) { IEnumerable<DeviceTelemetryModel> blobModels; IEnumerable<IListBlobItem> blobs; CloudBlockBlob blockBlob; CloudBlobContainer container; int preFilterCount; IEnumerable<DeviceTelemetryModel> result; minTime = minTime.ToUniversalTime(); result = new DeviceTelemetryModel[0]; container = await BlobStorageHelper.BuildBlobContainerAsync( this._telemetryStoreConnectionString, _telemetryContainerName); blobs = await BlobStorageHelper.LoadBlobItemsAsync( async (token) => { return await container.ListBlobsSegmentedAsync( _telemetryDataPrefix, true, BlobListingDetails.None, null, token, null, null); }); blobs = blobs.OrderByDescending( t => BlobStorageHelper.ExtractBlobItemDate(t)); foreach (IListBlobItem blob in blobs) { if ((blockBlob = blob as CloudBlockBlob) == null) { continue; } try { blobModels = await LoadBlobTelemetryModelsAsync(blockBlob); } catch { continue; } if (blobModels == null) { break; } preFilterCount = blobModels.Count(); blobModels = blobModels.Where( t => (t != null) && t.Timestamp.HasValue && t.Timestamp.Value.ToUniversalTime() >= minTime); if (preFilterCount == 0) { break; } result = result.Concat(blobModels); if (preFilterCount != blobModels.Count()) { break; } } if (!string.IsNullOrEmpty(deviceId)) { result = result.Where(t => t.DeviceId == deviceId); } return result; }