예제 #1
0
        public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel)
        {
            var deviceMessage = new DeviceMessage
            {
                Topic    = topic,
                Payload  = payload,
                QosLevel = qosLevel
            };

            MessageReceived?.Invoke(this, new DeviceMessageReceivedEventArgs(deviceMessage));
        }
예제 #2
0
        public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel)
        {
            try
            {
                var message = new MqttApplicationMessage(topic, payload, (MqttQualityOfServiceLevel)qosLevel, false);
                _client.PublishAsync(message).Wait();

                _log.Verbose($"Published message '{topic}' [{Encoding.UTF8.GetString(payload)}].");
            }
            catch (Exception exception)
            {
                _log.Error(exception, $"Failed to publish message '{topic}' [{Encoding.UTF8.GetString(payload)}].");
            }
        }
예제 #3
0
        public static void Publish(this IDeviceMessageBrokerService deviceMessageBrokerService, string topic,
                                   string message, MqttQosLevel qosLevel)
        {
            if (deviceMessageBrokerService == null)
            {
                throw new ArgumentNullException(nameof(deviceMessageBrokerService));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            deviceMessageBrokerService.Publish(topic, Encoding.UTF8.GetBytes(message), qosLevel);
        }
예제 #4
0
        public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel, bool retain)
        {
            try
            {
                var message = new MqttApplicationMessageBuilder()
                              .WithTopic(topic)
                              .WithPayload(payload)
                              .WithQualityOfServiceLevel((MqttQualityOfServiceLevel)qosLevel)
                              .WithRetainFlag(retain)
                              .Build();

                _server.PublishAsync(message).GetAwaiter().GetResult();

                _log.Verbose($"Published message '{topic}' [{Encoding.UTF8.GetString(payload)}].");
            }
            catch (Exception exception)
            {
                _log.Error(exception, $"Failed to publish message '{topic}' [{Encoding.UTF8.GetString(payload)}].");
            }
        }
예제 #5
0
 protected internal override Task SubscribeAsync(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce)
 {
     return(Task.Factory.StartNew(() => Subscribe(topic, qos)));
 }
예제 #6
0
 protected internal override void Subscribe(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce)
 {
     mqttClient.Subscribe(new string[] { topic }, new byte[] { (byte)qos });
 }
예제 #7
0
 public static void Subscribe(this MysoftMqttClient client, IMqttTopic topic, MqttQosLevel qos)
 {
     client.Subscribe(topic.Build(), qos);
 }
        public TopicSubscriber(IMqttTopic topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce, bool shared = false) : this(topic.Build(), qos, shared)
        {

        }
 public TopicSubscriber(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce, bool shared = false)
 {
     this.Topic = topic;
     this.Qos = qos;
     this.Shared = shared;
 }
예제 #10
0
 public MqttMessageBuilder WithQos(MqttQosLevel qos)
 {
     _qos = qos;
     return(this);
 }
예제 #11
0
        public static void Publish(this IDeviceMessageBrokerService deviceMessageBrokerService, string topic, string payload, MqttQosLevel qosLevel, bool retain)
        {
            if (deviceMessageBrokerService == null)
            {
                throw new ArgumentNullException(nameof(deviceMessageBrokerService));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }

            deviceMessageBrokerService.Publish(topic, Encoding.UTF8.GetBytes(payload), qosLevel, retain);
        }
예제 #12
0
        public static void Publish(this IDeviceMessageBrokerService deviceMessageBrokerService, string topic, JObject payload, MqttQosLevel qosLevel, bool retain)
        {
            if (deviceMessageBrokerService == null)
            {
                throw new ArgumentNullException(nameof(deviceMessageBrokerService));
            }
            if (topic == null)
            {
                throw new ArgumentNullException(nameof(topic));
            }
            if (payload == null)
            {
                throw new ArgumentNullException(nameof(payload));
            }

            deviceMessageBrokerService.Publish(topic, payload.ToString(Formatting.None), qosLevel, retain);
        }
 /// <summary>
 /// 异步订阅
 /// </summary>
 /// <param name="topic"></param>
 /// <param name="qos"></param>
 /// <returns></returns>
 protected internal abstract Task SubscribeAsync(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce);
 /// <summary>
 /// 订阅主题
 /// </summary>
 /// <param name="topic"></param>
 /// <param name="qos"></param>
 protected internal abstract void Subscribe(string topic, MqttQosLevel qos = MqttQosLevel.AtMostOnce);
예제 #15
0
 public void Publish(string topic, byte[] payload, MqttQosLevel qosLevel)
 {
     _client.PublishAsync(new MqttApplicationMessage(topic, payload, (MqttQualityOfServiceLevel)qosLevel, false)).Wait();
     _log.Verbose($"Published message '{topic}' [{Encoding.UTF8.GetString(payload)}].");
 }