/// <inheritdoc /> public async Task SubscribeAsync(string topic, Action <IPublishedMessage> handler, CancellationToken cancellationToken) { var topicHandler = new TopicHandler { Topic = topic, Handler = handler }; _topicHandlers.TryAdd(topicHandler, topicHandler); // TODO: need a better way. #pragma warning disable VSTHRD101 cancellationToken.Register(async() => { _topicHandlers.TryRemove(topicHandler, out _); if (_topicHandlers.Values.Count(t => t.Topic == topic) == 0) { await Task.WhenAll(Routers.Select(r => r.LeaveTopicAsync(topic, CancellationToken.None))) .ConfigureAwait(false); } }); #pragma warning restore VSTHRD101 // Tell routers if first time. Task Selector(IMessageRouter r) => r.JoinTopicAsync(topic, CancellationToken.None); if (_topicHandlers.Values.Count(t => t.Topic == topic) == 1) { await Task.WhenAll(Routers.Select(Selector)) .ConfigureAwait(false); } }
public void TestHandleMessage() { var handler = new TopicHandler(ReceivedMessage); var testTextMessage = new MockTextMessage("Test.Topic"); var testMessage = TextMessageUtil.CreateMessage("Test.Topic", "FieldName", "FieldValue"); testTextMessage.Text = testMessage; testTextMessage.Properties.SetString("BrokerUrl", "tcp://localhost:60606"); handler.OnMessageHandler(testTextMessage); }
private ServiceFacade() { DBHelper dbHelper = new DBHelper("dbHelper"); this.userHandler = new UserHandler(dbHelper); this.topicHandler = new TopicHandler(dbHelper); this.issueHandler = new IssueHandler(dbHelper); this.solutionHandler = new SolutionHandler(dbHelper); this.pictureHandler = new PictureHandler(dbHelper); //this.itemHandler = new ItemHandler(dbHelper); }
private void stopHandler (TopicHandler topicHandler) { if (topicHandler == null) { throw new ArgumentNullException(nameof(topicHandler)); } topicHandler.Stop(); Trace.TraceInformation($"{topicHandler.Name} - Stopped"); }
private void startHandler (TopicHandler topicHandler) { if (topicHandler == null) { throw new ArgumentNullException(nameof(topicHandler)); } Trace.TraceInformation($"{topicHandler.Name} - Starting"); topicHandler.Start(); Trace.TraceInformation($"{topicHandler.Name} - Started"); }
void RegisterTopicHandler(TopicHandler topicHandler) { osc.SetAddressHandler(topicHandler.topicName, topicHandler.OnTopic); InformServerOnRegistration(CreateRegisterJsonString(topicHandler.topicName)); }
static void StartRepoMockTest() { string _loggingPath = $"{System.Reflection.Assembly.GetExecutingAssembly().Location}Collector{Path.DirectorySeparatorChar}"; ILogger logger = new LoggerConfiguration() .WriteTo.Console().MinimumLevel.Information() .CreateLogger(); string IPAddress = "192.168.10.11"; string MAC = "00D0C9FCA9BA"; var _pressWriterInflux = new RepositoryWriterMachineMock( (machine, IsConnected) => { logger.Information($"Mock Connection Write: {machine.Line}-{machine.MAC}-{IsConnected}"); }, (machine, IsCycling) => { logger.Information($"Mock Status Write: {machine.Line}-{machine.MAC}-{IsCycling}"); return(DateTime.UtcNow); } ); var realPress = new PressAdam6051( new Repository.Model.MachineInfoInflux() { IPAddress = IPAddress, MAC = MAC, Line = "Y-1-1", Department = Tag.Value.DepartmentPartsProduction, Name = "BC-80020001-0000" }, _pressWriterInflux, new PressAdam6051PayloadConverterStatus(), logger ); var _pressList = new List <PressAdam6051>(); _pressList.Add(realPress); for (int i = 2; i < 13; i++) { _pressList.Add(new PressAdam6051(new Repository.Model.MachineInfoInflux() { IPAddress = IPAddress, MAC = "00D0C9FCA9BA", Line = $"Z-1-{i}", Department = Tag.Value.DepartmentPartsProduction, Name = $"BC-801{"0000":i}-0000", }, _pressWriterInflux, new PressAdam6051MessageConverterMock(i), logger) ); } //var _listOfTopicHandlers = new List<ITopicMessageHandler>(); var PressTopicHandlerForData = new TopicHandler(realPress.TopicForStatus, realPress); //_listOfTopicHandlers.Add(PressTopicHandlerForData); var _messageProcessor = new MessageProcessor(logger); _messageProcessor.AddMachineTopicHandler(PressTopicHandlerForData); foreach (var press in _pressList) { _messageProcessor.AddMachineTopicHandler(new TopicHandler(press.TopicForStatus, press)); } var _collector = new MQTTCollector(_messageProcessor, logger); _collector.Start(); _messageProcessor.StartProcessing(); _collector.WaitUntilStopped(); }
private static async Task RunTestNotLoadedFromFileTest(ILogger sublogger) { string IPAddress = "192.168.10.11"; string MAC = "00D0C9FCA9BA"; // var influxServerAddress = new Uri("http://192.168.1.208:8086"); var repo = new MachineRepository("192.168.1.208"); if (!repo.CanConnect()) { sublogger.Fatal("Can't Connect to DB Server. Exiting..."); return; } var _pressWriterInflux = new RepositoryWriterPressInflux(repo, sublogger); var realPress = new PressAdam6051( new Repository.Model.MachineInfoInflux() { IPAddress = IPAddress, MAC = MAC, Line = "Y-1-1", Department = Tag.Value.DepartmentPartsProduction, Name = "Y-1-1" }, _pressWriterInflux, new PressAdam6051PayloadConverterStatus(), sublogger ); var _pressList = new List <PressAdam6051>(); _pressList.Add(realPress); for (int i = 2; i < 13; i++) { _pressList.Add(new PressAdam6051(new Repository.Model.MachineInfoInflux() { IPAddress = IPAddress, MAC = "00D0C9FCA9BA", Line = $"Z-1-{i}", Department = Tag.Value.DepartmentPartsProduction, Name = $"Z-1-{i}", }, _pressWriterInflux, new PressAdam6051MessageConverterMock(i), sublogger) ); } //var _listOfTopicHandlers = new List<ITopicMessageHandler>(); var PressTopicHandlerForData = new TopicHandler(realPress.TopicForStatus, realPress); //_listOfTopicHandlers.Add(PressTopicHandlerForData); var _messageProcessor = new MessageProcessor(sublogger); _messageProcessor.AddMachineTopicHandler(PressTopicHandlerForData); foreach (var press in _pressList) { _messageProcessor.AddMachineTopicHandler(new TopicHandler(press.TopicForStatus, press)); } var _collector = new MQTTCollector(_messageProcessor, sublogger); Console.WriteLine("Starting Collection"); //await _collector.StartAsync(); _collector.Start(); _messageProcessor.StartProcessing(); Console.WriteLine("Waiting indefinitely"); await _collector.WaitUntilStoppedAsync(); }