コード例 #1
0
        public void publish(string topic, string payload, string mqttBrokerAddress)
        {
            if (mqttClient != null)
            {
                if (!mqttBrokerAddress.Equals(this.MqttBrokerAddressPublish) & mqttClient.IsConnected)
                {
                    mqttClient.Disconnect();
                }
            }
            mqttClient = new uPLibrary.Networking.M2Mqtt.MqttClient(mqttBrokerAddress, mqttBrokerPort, false, null, null, uPLibrary.Networking.M2Mqtt.MqttSslProtocols.None);
            string clientID = Guid.NewGuid().ToString();

            if (!mqttClient.IsConnected)
            {
                if (MqttUserName == null || MqttPassword == null)
                {
                    mqttClient.Connect(clientID);
                }
                else
                {
                    mqttClient.Connect(clientID, MqttUserName, MqttPassword);
                }
            }

            if (payload != null)
            {
                mqttClient.Publish(topic, Encoding.UTF8.GetBytes(payload), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, RetainMessages);
            }
            else
            {
                mqttClient.Publish(topic, null, MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, RetainMessages);
            }
        }
コード例 #2
0
 /// <summary>
 /// Publish data to the specified topic.
 /// </summary>
 /// <param name="topic">Topic.</param>
 /// <param name="data">Data.</param>
 public void Publish(string topic, string data)
 {
     if (_client.IsConnected)
     {
         _client.Publish(topic, Encoding.UTF8.GetBytes(data), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, true);
     }
 }
コード例 #3
0
        public void publish(string[] topic, string[] payload, string mqttBrokerAddress)
        {
            if (mqttClient != null)
            {
                if (!mqttBrokerAddress.Equals(this.MqttBrokerAddressPublish) & mqttClient.IsConnected)
                {
                    mqttClient.Disconnect();
                }
            }
            if (topic.Length != payload.Length)
            {
                throw new ArgumentOutOfRangeException("Array topic and payload must be the same size");
            }
            mqttClient = new uPLibrary.Networking.M2Mqtt.MqttClient(mqttBrokerAddress);
            string clientID = Guid.NewGuid().ToString();

            if (!mqttClient.IsConnected)
            {
                mqttClient.Connect(clientID);
            }
            for (int i = 0; i < payload.Length; i++)
            {
                mqttClient.Publish(topic[i], Encoding.UTF8.GetBytes(payload[i]), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
            }
        }
コード例 #4
0
        private static async Task <int> _publish()
        {
            Console.WriteLine("Type 'q' to quit");

            var client = new uPLibrary.Networking.M2Mqtt.MqttClient("grissom.klingon");

            client.Connect(Guid.NewGuid().ToString());
            Random r    = new Random((int)(DateTime.Now.Ticks + (long)Environment.CurrentManagedThreadId));
            string text = null;

            do
            {
                text = Console.ReadLine();
                if (text != "q")
                {
                    double d     = r.NextDouble() * 100.00;
                    var    bytes = System.Text.Encoding.UTF8.GetBytes(string.Format("{{ \"temp\":{0} }}", d));

                    client.Publish("house/serverroom/temp", bytes);

                    Console.WriteLine(1);
                }
            } while(text != "q");

            client.Disconnect();

            return(1);
        }
コード例 #5
0
        public void Send(string topic, string value)
        {
            if (!topic.StartsWith("/"))
            {
                topic = $"/{topic}";
            }

            var fullTopic = $"{baseTopic}{topic}";

            mqttClient.Publish($"{fullTopic}", Encoding.UTF8.GetBytes(value));
            logger.LogDebug($"Published '{value}' to '{fullTopic}");
        }
コード例 #6
0
        private void ProcessData(object param)
        {
            while (!shouldStop)
            {
                ReadOrder readOrder = (ReadOrder)param;
                lock (lockProcessData)
                {
                    if (readOrder.FunctionCode == FunctionCode.ReadCoils)
                    {
                        bool[] value = modbusClient.ReadCoils(readOrder.StartingAddress, readOrder.Quantity);
                        for (int i = 0; i < value.Length; i++)
                        {
                            if (readOrder.oldvalue[i] == null)
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            else if ((bool)readOrder.oldvalue[i] != value[i])
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            readOrder.oldvalue[i] = value[i];
                        }
                    }
                    if (readOrder.FunctionCode == FunctionCode.ReadDiscreteInputs)
                    {
                        bool[] value = modbusClient.ReadDiscreteInputs(readOrder.StartingAddress, readOrder.Quantity);
                        for (int i = 0; i < value.Length; i++)
                        {
                            if (readOrder.oldvalue[i] == null)
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            else if ((bool)readOrder.oldvalue[i] != value[i])
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            readOrder.oldvalue[i] = value[i];
                        }
                    }
                    if (readOrder.FunctionCode == FunctionCode.ReadHoldingRegisters)
                    {
                        int[] value = modbusClient.ReadHoldingRegisters(readOrder.StartingAddress, readOrder.Quantity);
                        for (int i = 0; i < value.Length; i++)
                        {
                            if (readOrder.oldvalue[i] == null)
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            else if ((int)readOrder.oldvalue[i] != value[i])
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            readOrder.oldvalue[i] = value[i];
                        }
                    }

                    if (readOrder.FunctionCode == FunctionCode.ReadInputRegisters)
                    {
                        int[] value = modbusClient.ReadInputRegisters(readOrder.StartingAddress, readOrder.Quantity);
                        for (int i = 0; i < value.Length; i++)
                        {
                            if (readOrder.oldvalue[i] == null)
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            else if ((int)readOrder.oldvalue[i] != value[i])
                            {
                                mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);
                            }
                            readOrder.oldvalue[i] = value[i];
                        }
                    }
                }
                System.Threading.Thread.Sleep(readOrder.CylceTime);
            }
        }
コード例 #7
0
        private void ProcessData(object param)
        {
            while (!shouldStop)
            {
                try
                {
                    if (!mqttClient.IsConnected)
                    {
                        mqttClient = new uPLibrary.Networking.M2Mqtt.MqttClient(mqttBrokerAddress, mqttBrokerPort, false, null, null, uPLibrary.Networking.M2Mqtt.MqttSslProtocols.None);
                        string clientID = Guid.NewGuid().ToString();
                        if (MqttUserName == null || MqttPassword == null)
                        {
                            mqttClient.Connect(clientID);
                        }
                        else
                        {
                            mqttClient.Connect(clientID, MqttUserName, MqttPassword);
                        }
                    }
                }
                catch (Exception exc)
                {
                    if (!this.AutomaticReconnect)
                    {
                        throw exc;
                    }
                }
                ReadOrder readOrder = (ReadOrder)param;
                lock (lockProcessData)
                {
                    try
                    {
                        if (readOrder.FunctionCode == FunctionCode.ReadCoils)
                        {
                            bool[] value = modbusClient.ReadCoils(readOrder.StartingAddress, readOrder.Quantity);
                            for (int i = 0; i < value.Length; i++)
                            {
                                if (readOrder.oldvalue[i] == null)
                                {
                                    mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                }
                                else if ((bool)readOrder.oldvalue[i] != value[i])
                                {
                                    mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                }
                                readOrder.oldvalue[i] = value[i];
                            }
                        }
                        if (readOrder.FunctionCode == FunctionCode.ReadDiscreteInputs)
                        {
                            bool[] value = modbusClient.ReadDiscreteInputs(readOrder.StartingAddress, readOrder.Quantity);
                            for (int i = 0; i < value.Length; i++)
                            {
                                if (readOrder.oldvalue[i] == null)
                                {
                                    mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                }
                                else if ((bool)readOrder.oldvalue[i] != value[i])
                                {
                                    mqttClient.Publish(readOrder.Topic[i], Encoding.UTF8.GetBytes(value[i].ToString()), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                }
                                readOrder.oldvalue[i] = value[i];
                            }
                        }
                        if (readOrder.FunctionCode == FunctionCode.ReadHoldingRegisters)
                        {
                            int[] value = modbusClient.ReadHoldingRegisters(readOrder.StartingAddress, readOrder.Quantity);
                            for (int i = 0; i < value.Length; i++)
                            {
                                float scale = readOrder.Scale != null ? (readOrder.Scale[i] == 0) ? 1 : readOrder.Scale[i] : 1;
                                if (readOrder.oldvalue[i] == null)
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale) + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                                else if (((int)readOrder.oldvalue[i] != value[i]) && (readOrder.Hysteresis != null ? ((value[i] < (int)readOrder.oldvalue[i] - (int)readOrder.Hysteresis[i]) | (value[i] > (int)readOrder.oldvalue[i] + (int)readOrder.Hysteresis[i])) : true))
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString() + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                            }
                        }

                        if (readOrder.FunctionCode == FunctionCode.ReadInputRegisters)
                        {
                            int[] value = modbusClient.ReadInputRegisters(readOrder.StartingAddress, readOrder.Quantity);
                            for (int i = 0; i < value.Length; i++)
                            {
                                float scale = readOrder.Scale != null ? (readOrder.Scale[i] == 0) ? 1 : readOrder.Scale[i] : 1;
                                if (readOrder.oldvalue[i] == null)
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString() + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                                else if (((int)readOrder.oldvalue[i] != value[i]) && (readOrder.Hysteresis != null ? ((value[i] < (int)readOrder.oldvalue[i] - (int)readOrder.Hysteresis[i]) | (value[i] > (int)readOrder.oldvalue[i] + (int)readOrder.Hysteresis[i])) : true))
                                {
                                    mqttClient.Publish(readOrder.Topic[i], (readOrder.Unit == null ? Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString()) : Encoding.UTF8.GetBytes(((float)value[i] * scale).ToString() + " " + readOrder.Unit[i])), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, readOrder.Retain[i]);
                                    readOrder.oldvalue[i] = value[i];
                                }
                            }
                        }
                    }
                    catch (Exception exc)
                    {
                        modbusClient.Disconnect();
                        Thread.Sleep(2000);
                        if (!AutomaticReconnect)
                        {
                            throw exc;
                        }
                        if (!modbusClient.Connected)
                        {
                            try
                            {
                                modbusClient.Connect();
                            }
                            catch (Exception) { }
                        }
                    }
                }
                System.Threading.Thread.Sleep(readOrder.CylceTime);
            }
        }