using Microcontroller; using TheXamlGuy.Framework.Core; using TheXamlGuy.Framework.Serial; namespace TheXamlGuy.Framework.Microcontroller; public class MicrocontrollerContext : IMicrocontrollerContext where TReadDeserializer : IMicrocontrollerModuleDeserializer, new() { private readonly IReadOnlyCollection modules; private readonly IEventAggregator eventAggregator; private readonly IMediator mediator; private readonly ISerialContext serialContext; public MicrocontrollerContext(IReadOnlyCollection modules, IEventAggregator eventAggregator, IMediator mediator, ISerialContext serialContext) { this.modules = modules; this.eventAggregator = eventAggregator; this.mediator = mediator; this.serialContext = serialContext; } public async Task InitializeAsync() { eventAggregator.Subscribe>(OnEvent, null, args => args.Context.Equals(serialContext)); serialContext.Open(); await Task.CompletedTask; } private async void OnEvent(SerialResponse args) { IMicrocontrollerModule? module = await mediator.HandleAsync(new TReadDeserializer { Read = args.Content }, modules); eventAggregator.Publish((dynamic?)module); } }