public static async Task GetMessages() { var fluksoIP = FluksoConfig.getConfig("FluksoServer:FluksoIP"); var options = new ManagedMqttClientOptionsBuilder() .WithAutoReconnectDelay(TimeSpan.FromSeconds(5)) .WithClientOptions(new MqttClientOptionsBuilder() .WithClientId("FluksoCore") .WithTcpServer(fluksoIP) .Build()) .Build(); var mqttClient = new MqttFactory().CreateManagedMqttClient(); await mqttClient.SubscribeAsync(new TopicFilterBuilder().WithTopic("/sensor/+/+").Build()); await mqttClient.StartAsync(options); Console.WriteLine("Starting FluksoCore"); Console.WriteLine($"Listening on {fluksoIP}"); if (FluksoConfig.getConfig("Output:Console") == "true") { mqttClient.ApplicationMessageReceived += MessageHandler.ConsoleHandler; Console.WriteLine("Output Console enabled"); } if (FluksoConfig.getConfig("Output:EventHubs") == "true") { mqttClient.ApplicationMessageReceived += MessageHandler.EventHubsHandler; Console.WriteLine("Output EventHubs enabled"); } await Task.Delay(Timeout.Infinite); }
static MessageHandler() { //Open EventHubs connection var connectionStringBuilder = new EventHubsConnectionStringBuilder(FluksoConfig.getConfig("EventHubs:EHConnectionString")) { EntityPath = FluksoConfig.getConfig("EventHubs:EHEntityPath") }; eventHubClient = EventHubClient.CreateFromConnectionString(connectionStringBuilder.ToString()); //Close the connection gracefully in case program gets stopped AppDomain.CurrentDomain.ProcessExit += MessageHandler_Close; }
private void deriveNameFromTopicName() { if (_topicName.IndexOf(FluksoConfig.getConfig("Sensors:Electricity")) > 0) { _name = "Electricity"; } else if (_topicName.IndexOf(FluksoConfig.getConfig("Sensors:Water")) > 0) { _name = "Water"; } else if (_topicName.IndexOf(FluksoConfig.getConfig("Sensors:Gas")) > 0) { _name = "Gas"; } else { _name = "Unknown"; } }