/// <summary> /// Подходит ли обработчик для обработки данных. /// </summary> /// <param name="command"></param> /// <param name="data4Handle"></param> /// <returns></returns> public bool IsDataHandler(Command4Device command, IEnumerable <AdInputType> data4Handle) { return((command == Command4Device.None) && (data4Handle != null) && (data4Handle.Any())); }
/// <summary> /// Подходит ли обработчик для обработки команды. /// </summary> /// <param name="command">имя команды</param> /// <param name="handlerName">имя обработчика</param> /// <returns></returns> public bool IsCommandHandler(Command4Device command, string handlerName) { var commandName = $"Command_{command.ToString()}"; //Command_On, Command_Off, Command_Restart, Command_Clear return(commandName.Equals(handlerName)); }
/// <summary> /// Отправить данные или команду на выбранный обмен. /// </summary> /// <param name="keyExchange"></param> /// <param name="dataAction"></param> /// <param name="inData"></param> /// <param name="command4Device"></param> public async Task Send2ConcreteExchanges(string keyExchange, DataAction dataAction, List <TIn> inData, Command4Device command4Device = Command4Device.None) { var exchange = Exchanges.FirstOrDefault(exch => exch.KeyExchange == keyExchange); if (exchange == null) { await Send2Produder(Option.TopicName4MessageBroker, $"Обмен не найденн для этого ус-ва {keyExchange}"); return; } await SendDataOrCommand(exchange, dataAction, inData, command4Device); }
/// <summary> /// Отправить данные или команду. /// </summary> /// <param name="exchange"></param> /// <param name="dataAction"></param> /// <param name="inData"></param> /// <param name="command4Device"></param> private async Task SendDataOrCommand(IExchange <TIn> exchange, DataAction dataAction, List <TIn> inData, Command4Device command4Device = Command4Device.None) { if (!exchange.IsStartedTransportBg) { await Send2Produder(Option.TopicName4MessageBroker, $"Отправка данных НЕ удачна, Бекграунд обмена {exchange.KeyExchange} НЕ ЗАПЦУЩЕН"); return; } if (!exchange.IsOpen) { await Send2Produder(Option.TopicName4MessageBroker, $"Отправка данных НЕ удачна, соединение транспорта для обмена {exchange.KeyExchange} НЕ ОТКРЫТО"); return; } switch (dataAction) { case DataAction.OneTimeAction: exchange?.SendOneTimeData(inData); break; case DataAction.CycleAction: if (!exchange.IsStartedCycleExchange) { await Send2Produder(Option.TopicName4MessageBroker, $"Отправка данных НЕ удачна, Цикл. обмен для обмена {exchange.KeyExchange} НЕ ЗАПУЩЕН"); return; } exchange?.SendCycleTimeData(inData); break; case DataAction.CommandAction: exchange?.SendCommand(command4Device); break; default: throw new ArgumentOutOfRangeException(nameof(dataAction), dataAction, null); } }
/// <summary> /// Отправить данные или команду на все обмены. /// </summary> /// <param name="dataAction"></param> /// <param name="inData"></param> /// <param name="command4Device"></param> public async Task Send2AllExchanges(DataAction dataAction, List <TIn> inData, Command4Device command4Device = Command4Device.None) { var tasks = new List <Task>(); foreach (var exchange in Exchanges) { tasks.Add(SendDataOrCommand(exchange, dataAction, inData, command4Device)); } await Task.WhenAll(tasks); }