public IActionResult Post([FromBody] NormalizationTestRequest request) { // Validate Request. if (!IsValidRequest(request, out string errorMessage)) { return(this.BadRequest( new NormalizationTestResponse() { Result = TestResult.Fail.ToString(), Reason = errorMessage, })); } // Validate mapping semantic. var templateContext = collectionTemplateFactory.Create(request.DeviceMapping); if (!templateContext.IsValid(out string errors)) { return(this.BadRequest( new NormalizationTestResponse() { Result = TestResult.Fail.ToString(), Reason = errors, })); } // try normalizing and return result. try { var token = JToken.Parse(request.DeviceSample); var measurements = templateContext.Template.GetMeasurements(token); return(this.Ok( new NormalizationTestResponse() { Result = TestResult.Success.ToString(), NormalizedData = JToken.FromObject(measurements).ToString(), })); } catch (Exception ex) { return(this.BadRequest( new NormalizationTestResponse() { Result = TestResult.Fail.ToString(), Reason = ex.Message, })); } }
private async Task ConsumeAsyncImpl(IEnumerable <IEventMessage> events, string templateContent) { var templateContext = _collectionTemplateFactory.Create(templateContent); templateContext.EnsureValid(); var template = templateContext.Template; _logger.LogMetric( IomtMetrics.DeviceEvent(events.FirstOrDefault()?.PartitionId), events.Count()); IEnumerable <EventData> eventHubEvents = events .Select(x => { var eventData = new EventData(x.Body.ToArray()); eventData.SystemProperties = new SystemPropertiesCollection( x.SequenceNumber, x.EnqueuedTime.UtcDateTime, x.Offset.ToString(), x.PartitionId); if (x.Properties != null) { foreach (KeyValuePair <string, object> entry in x.Properties) { eventData.Properties[entry.Key] = entry.Value; } } if (x.SystemProperties != null) { foreach (KeyValuePair <string, object> entry in x.SystemProperties) { eventData.SystemProperties.TryAdd(entry.Key, entry.Value); } } return(eventData); }); var dataNormalizationService = new MeasurementEventNormalizationService(_logger, template, _exceptionTelemetryProcessor); await dataNormalizationService.ProcessAsync(eventHubEvents, _collector).ConfigureAwait(false); }
private IContentTemplate LoadDeviceTemplate(string deviceMappingContent, TemplateResult validationResult) { try { var templateContext = _collectionTemplateFactory.Create(deviceMappingContent); if (templateContext is TemplateContext <IContentTemplate> contentTemplateContext) { CaptureTemplateErrors(validationResult, contentTemplateContext, ValidationCategory.NORMALIZATION); } return(templateContext.Template); } catch (Exception e) { validationResult.CaptureException(e, ValidationCategory.NORMALIZATION); } return(null); }