using Azaion.Common.Database; using Azaion.Common.DTO; using Azaion.Common.Events; using MediatR; using MessagePack; using Microsoft.Extensions.Logging; namespace Azaion.Common.Services.Inference; public class InferenceServiceEventHandler(IInferenceService inferenceService, IAnnotationService annotationService, IMediator mediator, ILogger logger) : INotificationHandler, INotificationHandler { public async Task Handle(InferenceDataEvent e, CancellationToken ct) { try { if (e.Command.Message == "DONE") { await inferenceService.InferenceCancelTokenSource.CancelAsync(); return; } var annImage = MessagePackSerializer.Deserialize(e.Command.Data, cancellationToken: ct); var annotation = await annotationService.SaveAnnotation(annImage, ct); await mediator.Publish(new AnnotationAddedEvent(annotation), ct); } catch (Exception ex) { logger.LogError(ex, ex.Message); } } public async Task Handle(AIAvailabilityStatusEvent e, CancellationToken ct) { e.Status = AIAvailabilityEnum.Enabled; } }