/// <summary> /// Initializes a new instance of the <see cref="Brick"/> class. /// </summary> /// <param name="communicationFactory">Object implementing the <see cref="ICommunicationFactory"/> interface.</param> /// <param name="fileProvider">Object implementing the <see cref="IFileProvider"/> interface.</param> /// <param name="alwaysSendEvents">Send events when data changes, or at every poll</param> public Brick(ICommunicationFactory communicationFactory, IFileProvider fileProvider, bool alwaysSendEvents = false) { DirectCommandFactory = new DirectCommandFactory(this); SystemCommandFactory = new SystemCommandFactory(this, fileProvider); BatchCommandFactory = new BatchCommandFactory(this); Buttons = new BrickButtons(); _communicationFactory = communicationFactory; _alwaysSendEvents = alwaysSendEvents; int index = 0; Ports = new Dictionary <InputPort, Port>(); foreach (InputPort i in Enum.GetValues(typeof(InputPort))) { Ports[i] = new Port { InputPort = i, Index = index++, Name = i.ToString(), }; } }
private async Task ConnectAsyncInternal(TimeSpan pollingTime) { _tokenSource = new CancellationTokenSource(); await _comm.ConnectAsync(); await DirectCommandFactory.StopAllAsync(); if (pollingTime != TimeSpan.Zero) { Task.Factory.StartNew( async() => { while (!_tokenSource.IsCancellationRequested) { await PollSensorsAsync(); await Task.Delay(pollingTime, _tokenSource.Token); } await DirectCommandFactory.StopAllAsync(); }, _tokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Current).GetAwaiter(); } }