Files
annotations/Azaion.Common/Services/Inference/InferenceServiceEventHandler.cs
T
Oleksandr Bezdieniezhnykh 067f02cc63 update AI initializing
rework AIAvailabilityStatus events to mediatr
2025-09-01 20:12:13 +03:00

43 lines
1.3 KiB
C#

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<InferenceServiceEventHandler> logger) :
INotificationHandler<InferenceDataEvent>,
INotificationHandler<AIAvailabilityStatusEvent>
{
public async Task Handle(InferenceDataEvent e, CancellationToken ct)
{
try
{
if (e.Command.Message == "DONE")
{
await inferenceService.InferenceCancelTokenSource.CancelAsync();
return;
}
var annImage = MessagePackSerializer.Deserialize<AnnotationImage>(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;
}
}