Example #1
0
        private void connect_Click(object sender, EventArgs e)
        {
            client = new MqttClient(broker_textBox.Text);
            try
            {
                client.Connect(clientid_textBox.Text, user_textBox.Text, password_textBox.Text);
            }
            catch
            {
                MessageBox.Show("Connect error");
            }
            broker_textBox.Enabled   = false;
            clientid_textBox.Enabled = false;
            user_textBox.Enabled     = false;
            password_textBox.Enabled = false;
            Connect.Enabled          = false;

            GroupBox[] group_all = { group1, group2, group3, group4, group5, group6, group7, group8 };
            foreach (var group in group_all)
            {
                group.Enabled = true;
            }
        }
Example #2
0
        static void Main(string[] args)
        {
            string clientid = Guid.NewGuid().ToString();

            MqttClient client = null;

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

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

                if (Properties.Settings.Default.Topic.Length == 0)
                {
                    Tools.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)
                {
                    Tools.Log("Connecting with credentials: " + Properties.Settings.Default.MQTTHost);
                    client.Connect(clientid, Properties.Settings.Default.Name, Properties.Settings.Default.Password);
                }
                else
                {
                    Tools.Log("Connecting without credentials: " + Properties.Settings.Default.MQTTHost);
                    client.Connect(clientid);
                }
                Tools.Log("Connected!");
            }
            catch (Exception ex)
            {
                Tools.Log(ex.Message);
            }

            string lastjson = "-";

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

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

                    string temp = System.IO.File.ReadAllText("/etc/teslalogger/current_json.txt");
                    if (temp != lastjson)
                    {
                        lastjson = temp;
                        client.Publish(Properties.Settings.Default.Topic, Encoding.UTF8.GetBytes(lastjson));
                    }
                }
                catch (Exception ex)
                {
                    System.Threading.Thread.Sleep(30000);
                    Tools.Log(ex.ToString());
                }
            }
        }
Example #3
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());
                }
            }
        }
Example #4
0
        static void Main(string[] args)
        {
            string clientid  = "6333abad-51f4-430d-9ba5-0047602612d1";
            int    MQTTPort  = MqttSettings.MQTT_BROKER_DEFAULT_PORT;
            bool   subtopics = false;

            MqttClient client = null;

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

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

                if (Properties.Settings.Default.MQTTPort.Length > 0)
                {
                    try
                    {
                        int.TryParse(Properties.Settings.Default.MQTTPort, out MQTTPort);
                        Logfile.Log("MQTT: Using user specific port: " + Properties.Settings.Default.MQTTPort);
                    }
                    catch (Exception ex)
                    {
                        Logfile.Log(ex.Message);
                    }
                }

                if (Properties.Settings.Default.ClientID.Length > 0)
                {
                    clientid = Properties.Settings.Default.ClientID;
                    Logfile.Log("MQTT: Using user specific ClientID: " + clientid);
                }

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

                if (Properties.Settings.Default.Subtopics.Length > 0)
                {
                    try
                    {
                        Boolean.TryParse(Properties.Settings.Default.Subtopics, out subtopics);

                        if (subtopics)
                        {
                            Logfile.Log("MQTT: Subtopics enabled");
                        }
                    }
                    catch (Exception ex)
                    {
                        Logfile.Log(ex.Message);
                    }
                }

                client = new MqttClient(Properties.Settings.Default.MQTTHost, MQTTPort, false, null, null, MqttSslProtocols.None);

                if (Properties.Settings.Default.Name.Length > 0 && Properties.Settings.Default.Password.Length > 0)
                {
                    Logfile.Log("MQTT: Connecting with credentials: " + Properties.Settings.Default.MQTTHost + ":" + MQTTPort);
                    client.Connect(clientid, Properties.Settings.Default.Name, Properties.Settings.Default.Password);
                }
                else
                {
                    Logfile.Log("MQTT: Connecting without credentials: " + Properties.Settings.Default.MQTTHost + ":" + MQTTPort);
                    client.Connect(clientid);
                }
                if (client.IsConnected)
                {
                    Logfile.Log("MQTT: Connected!");
                }
                else
                {
                    Logfile.Log("MQTT: Connection failed!");
                }
            }
            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(1000);

                    if (!client.IsConnected)
                    {
                        Logfile.Log("MQTT: 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);

                            if (subtopics)
                            {
                                var topics = JsonConvert.DeserializeObject <Dictionary <string, string> >(temp);
                                foreach (var keyvalue in topics)
                                {
                                    client.Publish(topic + "/" + keyvalue.Key, Encoding.UTF8.GetBytes(keyvalue.Value),
                                                   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());
                }
            }
        }
Example #5
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());
                }
            }
        }