Beispiel #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;
 }
Beispiel #2
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));
            }
        }
Beispiel #3
0
        public async Task <MqttTopic> GetTopic(string TopicString, bool CreateNew)
        {
            if (string.IsNullOrEmpty(TopicString))
            {
                return(null);
            }

            string[]  Parts = TopicString.Split('/');
            MqttTopic Topic;

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

            if (Topic == null)
            {
                if (System.Guid.TryParse(Parts[0].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[0])
                        {
                            Topic = new MqttTopic(Topic2, Parts[0], Parts[0], null, this);
                            break;
                        }
                    }
                }

                MqttTopicNode Node = null;

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

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

                    Topic = new MqttTopic(Node, Parts[0], Parts[0], null, this);
                }

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

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

            if (Parts.Length == 1)
            {
                return(Topic);
            }
            else
            {
                return(await Topic.GetTopic(Parts, 1, CreateNew, this));
            }
        }