public async Task NotifyRegisteredOwner(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            ILogger logger
            )
        {
            StorageBlobCreatedEventData blobCreatedEventData =
                ((JObject)eventGridEvent.Data).ToObject <StorageBlobCreatedEventData>();

            string blobName = BlobHelper.GetBlobName(blobCreatedEventData.Url);

            #region Logging

            logger.LogInformation(
                new EventId((int)LoggingConstants.EventId.NotifyVehicleOwnerStarted),
                LoggingConstants.Template,
                LoggingConstants.EventId.NotifyVehicleOwnerStarted.ToString(),
                blobName,
                LoggingConstants.ProcessingFunction.NotifyRegisteredOwner.ToString(),
                LoggingConstants.ProcessStatus.Started.ToString(),
                "Execution Started"
                );

            #endregion


            try
            {
                var speedingTicket = await _dmvDbHandler
                                     .GetSpeedingTicketInfoAsync(blobName)
                                     .ConfigureAwait(false);

                var registeredOwnerInfo = await _dmvDbHandler
                                          .GetOwnerInformationAsync(
                    vehicleRegistrationNumber : speedingTicket.VehicleResgistrationNumber)
                                          .ConfigureAwait(false);

                var infractionImage = await _blobHandler
                                      .DownloadBlobAsync(blobCreatedEventData.Url)
                                      .ConfigureAwait(false);


                var attachments = new List <OwnerNotificationMessageAttachment>
                {
                    new OwnerNotificationMessageAttachment
                    {
                        Name        = speedingTicket.TicketNumber,
                        Content     = Convert.ToBase64String(infractionImage),
                        ContentType = "image/jpeg"
                    }
                };

                var notificationMessage =
                    CreateNotificationMessage
                    (
                        vehicleOwnerInfo: registeredOwnerInfo,
                        attachments: attachments,
                        ticketNumber: speedingTicket.TicketNumber,
                        infractionDate: speedingTicket.Date,
                        infractionDistrict: speedingTicket.District
                    );

                await _ownerNotificationHandler
                .NotifyOwnerAsync(notificationMessage)
                .ConfigureAwait(false);

                #region Logging

                logger.LogInformation(
                    new EventId((int)LoggingConstants.EventId.NotifyVehicleOwnerFinished),
                    LoggingConstants.Template,
                    LoggingConstants.EventId.NotifyVehicleOwnerFinished.ToString(),
                    blobName,
                    LoggingConstants.ProcessingFunction.NotifyRegisteredOwner.ToString(),
                    LoggingConstants.ProcessStatus.Finished.ToString(),
                    "Execution Finished"
                    );

                #endregion
            }
            catch (Exception ex)
            {
                CustomEventData customEventData = new CustomEventData
                {
                    ImageUrl = blobCreatedEventData.Url,

                    CustomEvent = CustomEvent.Exceptioned.ToString()
                };

                await _eventHandler.PublishEventToTopicAsync(customEventData)
                .ConfigureAwait(false);

                #region Logging

                logger.LogInformation(
                    new EventId((int)LoggingConstants.EventId.NotifyVehicleOwnerFinished),
                    LoggingConstants.Template,
                    LoggingConstants.EventId.NotifyVehicleOwnerFinished.ToString(),
                    blobName,
                    LoggingConstants.ProcessingFunction.NotifyRegisteredOwner.ToString(),
                    LoggingConstants.ProcessStatus.Failed.ToString(),
                    $"Execution Failed. Reason: {ex.Message}"
                    );

                #endregion
            }
        }
示例#2
0
        public async Task ExtractRegistrationNumber(
            [EventGridTrigger] EventGridEvent eventGridEvent,
            ILogger logger
            )
        {
            StorageBlobCreatedEventData blobCreatedEventData =
                ((JObject)eventGridEvent.Data).ToObject <StorageBlobCreatedEventData>();

            string blobName = BlobHelper.GetBlobName(blobCreatedEventData.Url);


            #region Logging

            logger.LogInformation(
                new EventId((int)LoggingConstants.EventId.ExtractRegistrationNumberStarted),
                LoggingConstants.Template,
                LoggingConstants.EventId.ExtractRegistrationNumberStarted.ToString(),
                blobName,
                LoggingConstants.ProcessingFunction.ExtractRegistrationNumber.ToString(),
                LoggingConstants.ProcessStatus.Started.ToString(),
                "Execution Started"
                );

            #endregion

            CustomEventData customEventData = new CustomEventData
            {
                ImageUrl     = blobCreatedEventData.Url,
                TicketNumber = blobName,
            };

            try
            {
                string registrationNumber = await _computerVisionHandler
                                            .ExtractRegistrationNumberWithUrlAsync(blobCreatedEventData.Url)
                                            .ConfigureAwait(false);

                var metadata = await _blobHandler
                               .GetBlobMetadataAsync(blobCreatedEventData.Url);


                customEventData.DistrictOfInfraction = metadata["districtofinfraction"];
                customEventData.DateOfInfraction     = eventGridEvent.EventTime.ToString("dd-MM-yyyy");

                if (!string.IsNullOrWhiteSpace(registrationNumber))
                {
                    customEventData.VehicleRegistrationNumber = registrationNumber;
                    customEventData.CustomEvent = CustomEvent.NumberExtractionCompleted.ToString();

                    #region Logging

                    logger.LogInformation(
                        new EventId((int)LoggingConstants.EventId.ExtractRegistrationNumberFinished),
                        LoggingConstants.Template,
                        LoggingConstants.EventId.ExtractRegistrationNumberFinished.ToString(),
                        blobName,
                        LoggingConstants.ProcessingFunction.ExtractRegistrationNumber.ToString(),
                        LoggingConstants.ProcessStatus.Finished.ToString(),
                        "Execution Finished"
                        );

                    #endregion
                }
                else
                {
                    customEventData.CustomEvent = CustomEvent.Exceptioned.ToString();

                    #region Logging

                    logger.LogInformation(
                        new EventId((int)LoggingConstants.EventId.ExtractRegistrationNumberFinished),
                        LoggingConstants.Template,
                        LoggingConstants.EventId.ExtractRegistrationNumberFinished.ToString(),
                        blobName,
                        LoggingConstants.ProcessingFunction.ExtractRegistrationNumber.ToString(),
                        LoggingConstants.ProcessStatus.Failed.ToString(),
                        "Execution Failed. Reason: Failed to extract number plate from the image"
                        );

                    #endregion
                }
            }
            catch (Exception ex)
            {
                customEventData.CustomEvent = CustomEvent.Exceptioned.ToString();

                #region Logging

                logger.LogInformation(
                    new EventId((int)LoggingConstants.EventId.ExtractRegistrationNumberFinished),
                    LoggingConstants.Template,
                    LoggingConstants.EventId.ExtractRegistrationNumberFinished.ToString(),
                    blobName,
                    LoggingConstants.ProcessingFunction.ExtractRegistrationNumber.ToString(),
                    LoggingConstants.ProcessStatus.Failed.ToString(),
                    $"Execution Failed. Reason: {ex.Message}"
                    );

                #endregion
            }

            await _eventHandler
            .PublishEventToTopicAsync(customEventData)
            .ConfigureAwait(false);
        }