Example #1
0
 public MqttTopic(MqttTopicNode Node, string FullTopic, string LocalTopic, MqttTopic Parent, MqttBroker Broker)
 {
     this.node       = Node;
     this.fullTopic  = FullTopic;
     this.localTopic = LocalTopic;
     this.parent     = Parent;
     this.broker     = Broker;
 }
Example #2
0
        public static MqttBroker GetBroker(MqttBrokerNode Node, string Key, string Host, int Port, bool Tls, string UserName, string Password,
                                           string WillTopic, string WillData, bool WillRetain, MqttQualityOfService WillQoS)
        {
            MqttBroker Broker;

            lock (brokers)
            {
                if (!brokers.TryGetValue(Key, out Broker))
                {
                    Broker = null;
                }
            }

            if (Broker != null)
            {
                Broker.SetWill(WillTopic, WillData, WillRetain, WillQoS);
                return(Broker);
            }
            else
            {
                Broker = new MqttBroker(Node, Host, Port, Tls, UserName, Password, WillTopic, WillData, WillRetain, WillQoS);

                lock (brokers)
                {
                    if (brokers.ContainsKey(Key))
                    {
                        Broker.Dispose();
                        return(brokers[Key]);
                    }
                    else
                    {
                        brokers[Key] = Broker;
                        return(Broker);
                    }
                }
            }
        }
Example #3
0
        internal async Task <MqttTopic> GetTopic(string[] Parts, int Index, bool CreateNew, MqttBroker Broker)
        {
            MqttTopic Topic;

            lock (this.topics)
            {
                if (!this.topics.TryGetValue(Parts[Index], out Topic))
                {
                    Topic = null;
                }
            }

            if (Topic == null)
            {
                if (System.Guid.TryParse(Parts[Index].Replace('_', '-'), out Guid _))
                {
                    return(null);
                }

                if (this.node.HasChildren)
                {
                    foreach (INode Child in await this.node.ChildNodes)
                    {
                        if (Child is MqttTopicNode Topic2 && Topic2.LocalTopic == Parts[Index])
                        {
                            Topic = new MqttTopic(Topic2, Topic2.FullTopic, Parts[Index], null, Broker);
                            break;
                        }
                    }
                }

                MqttTopicNode Node = null;

                if (Topic == null)
                {
                    if (!CreateNew)
                    {
                        return(null);
                    }

                    StringBuilder sb = new StringBuilder();
                    int           i;

                    for (i = 0; i < Index; i++)
                    {
                        sb.Append(Parts[i]);
                        sb.Append('/');
                    }

                    sb.Append(Parts[Index]);

                    string FullTopic = sb.ToString();

                    Node = new MqttTopicNode()
                    {
                        NodeId     = await MeteringNode.GetUniqueNodeId(FullTopic),
                        LocalTopic = Parts[Index]
                    };

                    Topic = new MqttTopic(Node, FullTopic, Parts[Index], this, Broker);
                }

                lock (this.topics)
                {
                    if (this.topics.ContainsKey(Parts[Index]))
                    {
                        Topic = this.topics[Parts[Index]];
                    }
                    else
                    {
                        this.topics[Parts[Index]] = Topic;
                    }
                }

                if (Node != null)
                {
                    if (Node != Topic.Node)
                    {
                        await Node.DestroyAsync();
                    }
                    else
                    {
                        await this.node.AddAsync(Node);
                    }
                }
            }

            Index++;
            if (Parts.Length == Index)
            {
                return(Topic);
            }
            else
            {
                return(await Topic.GetTopic(Parts, Index, CreateNew, Broker));
            }
        }