Example #1
0
 public bool Start()
 {
     mqtt = new Mqttclient_handler();
     
     mqtt.MqttConnect(serveur, clientid);
     foreach (var tokv in topicsStr.Split(';'))
     {
         string topicname = tokv.Split('=')[0];
         string topicvalue = tokv.Split('=')[1];
         mqtt.subscribe(topicvalue, 0);
         // topicNames.Add(topicvalue, topicname);
     }
     int writeperiod = int.Parse(ConfigurationManager.AppSettings["writeperiod"]);
     int sendperiod = int.Parse(ConfigurationManager.AppSettings["sendperiod"]);
     timer_persistIncomingMessages = new Timer(WriteMessages, null, 0, writeperiod * 1000);
     timer_sendCommand = new Timer(SendMessages, null, 0, sendperiod * 1000);
     // Console.WriteLine("mqtt isconnected : {0}", mqtt.IsConnected);
     mqtt.OnMessageArrived += (Mqttclient_handler sender, MessPublishEventArgs e) =>
     {
         Console.WriteLine("at {0} from {1} : {2}", e.Message.Date, e.Message.Topic, e.Message.GetStringMessage());
         if (!messagesByTopic.ContainsKey(e.Message.Topic))
             messagesByTopic.Add(e.Message.Topic, new List<MqttMessage>());
         messagesByTopic[e.Message.Topic].Add(e.Message);
         if (messagesByTopic[e.Message.Topic].Count > 1000)
         {
             SaveMessages(messagesByTopic[e.Message.Topic], e.Message.Topic, FileType.csv);
             messagesByTopic[e.Message.Topic].Clear();
         }
     };
     return true;
     /*  do
        {
            Thread.Sleep(50);
        } while (true);*/
 }
Example #2
0
        static void Main(string[] args)
        {
            string serveur = ConfigurationManager.AppSettings["mqttserveur"];
            string clientid = ConfigurationManager.AppSettings["clientid"];
            string topicsStr = ConfigurationManager.AppSettings["topics"];
            //Dictionary<string, string> topicNames = new Dictionary<string, string>();
            Dictionary<string, List<MqttMessage>> messagesByTopic = new Dictionary<string, List<MqttMessage>>();
            //List<MqttMessage> messages = new List<MqttMessage>();
            Mqttclient_handler mqtt = new Mqttclient_handler();
            mqtt.MqttConnect(serveur, clientid);
            foreach (var tokv in topicsStr.Split(';'))
            {
                string topicname = tokv.Split('=')[0];
                string topicvalue = tokv.Split('=')[1];
                mqtt.subscribe(topicvalue, 0);
               // topicNames.Add(topicvalue, topicname);
            }


            Console.WriteLine("mqtt isconnected : {0}", mqtt.IsConnected);
            mqtt.OnMessageArrived += (Mqttclient_handler sender, MessPublishEventArgs e) =>
            {
                Console.WriteLine("at {0} from {1} : {2}", e.Message.Date, e.Message.Topic, e.Message.GetStringMessage());
                if (!messagesByTopic.ContainsKey(e.Message.Topic))
                    messagesByTopic.Add(e.Message.Topic, new List<MqttMessage>());
                messagesByTopic[e.Message.Topic].Add(e.Message);
                if (messagesByTopic[e.Message.Topic].Count > 20)
                {
                    SaveMessages(messagesByTopic[e.Message.Topic], e.Message.Topic, FileType.csv);
                    messagesByTopic[e.Message.Topic].Clear();
                }
            };
            ConsoleKeyInfo k;
            do
            {
                k = Console.ReadKey();
                if (k.KeyChar == 'w')
                {
                    foreach (var topic in messagesByTopic.Keys)
                    {
                        try
                        {
                            if (messagesByTopic[topic] != null && messagesByTopic[topic].Any())
                                SaveMessages(messagesByTopic[topic], topic, FileType.csv);
                        }
                        catch (Exception e)
                        {
                            throw new Exception(string.Format("Error in serialization topic {0}", topic), e);
                        }
                    }
                    messagesByTopic.Clear();
                    Console.WriteLine("Messages saved ");
                }
                //Console.WriteLine("press [Esc] to quit, [w] to write messages on disc");
            } while (true);// (k.Key != ConsoleKey.Escape);
            mqtt.Dispose();
        }