示例#1
0
        static void Main(string[] args)
        {
            string clientid = Guid.NewGuid().ToString();

            MqttClient client = null;

            try
            {
                Logfile.Log("MqttClient Version: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);

                if (Properties.Settings.Default.MQTTHost.Length == 0)
                {
                    Logfile.Log("No MQTTHost settings -> MQTT disabled!");
                    return;
                }

                if (Properties.Settings.Default.Topic.Length == 0)
                {
                    Logfile.Log("No Topic settings -> MQTT disabled!");
                    return;
                }

                client = new MqttClient(Properties.Settings.Default.MQTTHost);

                if (Properties.Settings.Default.Name.Length > 0 && Properties.Settings.Default.Password.Length > 0)
                {
                    Logfile.Log("Connecting with credentials: " + Properties.Settings.Default.MQTTHost);
                    client.Connect(clientid, Properties.Settings.Default.Name, Properties.Settings.Default.Password);
                }
                else
                {
                    Logfile.Log("Connecting without credentials: " + Properties.Settings.Default.MQTTHost);
                    client.Connect(clientid);
                }
                Logfile.Log("Connected!");
            }
            catch (Exception ex)
            {
                Logfile.Log(ex.Message);
            }

            string lastjson = "-";

            while (true)
            {
                try
                {
                    System.Threading.Thread.Sleep(5000);

                    if (!client.IsConnected)
                    {
                        Logfile.Log("Reconnect");
                        client.Connect(clientid);
                    }

                    // string temp = System.IO.File.ReadAllText("/etc/teslalogger/current_json_1.txt");
                    string temp = null;
                    using (WebClient wc = new WebClient())
                    {
                        temp = wc.DownloadString("http://localhost:5000/currentjson/1");
                    }

                    if (temp != lastjson)
                    {
                        lastjson = temp;
                        client.Publish(Properties.Settings.Default.Topic, Encoding.UTF8.GetBytes(lastjson),
                                       uPLibrary.Networking.M2Mqtt.Messages.MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, true);
                    }
                }
                catch (WebException wex)
                {
                    Logfile.Log(wex.Message);
                    System.Threading.Thread.Sleep(60000);
                }
                catch (Exception ex)
                {
                    System.Threading.Thread.Sleep(30000);
                    Logfile.Log(ex.ToString());
                }
            }
        }
示例#2
0
        static void Main(string[] args)
        {
            string clientid = "6333abad-51f4-430d-9ba5-0047602612d1";

            MqttClient client = null;

            try
            {
                Logfile.Log("MqttClient Version: " + System.Reflection.Assembly.GetExecutingAssembly().GetName().Version);

                if (Properties.Settings.Default.MQTTHost.Length == 0)
                {
                    Logfile.Log("No MQTTHost settings -> MQTT disabled!");
                    return;
                }

                if (Properties.Settings.Default.Topic.Length == 0)
                {
                    Logfile.Log("No Topic settings -> MQTT disabled!");
                    return;
                }

                client = new MqttClient(Properties.Settings.Default.MQTTHost);

                if (Properties.Settings.Default.Name.Length > 0 && Properties.Settings.Default.Password.Length > 0)
                {
                    Logfile.Log("Connecting with credentials: " + Properties.Settings.Default.MQTTHost);
                    client.Connect(clientid, Properties.Settings.Default.Name, Properties.Settings.Default.Password);
                }
                else
                {
                    Logfile.Log("Connecting without credentials: " + Properties.Settings.Default.MQTTHost);
                    client.Connect(clientid);
                }
                Logfile.Log("Connected!");
            }
            catch (Exception ex)
            {
                Logfile.Log(ex.Message);
            }

            System.Collections.Generic.HashSet <int>            allCars  = GetAllcars();
            System.Collections.Generic.Dictionary <int, string> lastjson = new Dictionary <int, string>();


            while (true)
            {
                try
                {
                    System.Threading.Thread.Sleep(5000);

                    if (!client.IsConnected)
                    {
                        Logfile.Log("Reconnect");
                        client.Connect(clientid);
                    }

                    foreach (int car in allCars)
                    {
                        string temp = null;
                        using (WebClient wc = new WebClient())
                        {
                            temp = wc.DownloadString("http://localhost:5000/currentjson/" + car);
                        }

                        if (!lastjson.ContainsKey(car) || temp != lastjson[car])
                        {
                            lastjson[car] = temp;
                            string topic = Properties.Settings.Default.Topic;

                            if (allCars.Count > 1)
                            {
                                topic += "-" + car;
                            }

                            client.Publish(topic, Encoding.UTF8.GetBytes(lastjson[car]),
                                           uPLibrary.Networking.M2Mqtt.Messages.MqttMsgBase.QOS_LEVEL_AT_LEAST_ONCE, true);
                        }
                    }
                }
                catch (WebException wex)
                {
                    Logfile.Log(wex.Message);
                    System.Threading.Thread.Sleep(60000);
                }
                catch (Exception ex)
                {
                    System.Threading.Thread.Sleep(30000);
                    Logfile.Log(ex.ToString());
                }
            }
        }