Ejemplo n.º 1
0
        private async void Process(MqttContent Content)
        {
            try
            {
                while (true)
                {
                    MqttTopic Topic = await this.GetTopic(Content.Topic, true);

                    Topic?.DataReported(Content);

                    lock (this.queue)
                    {
                        if (this.queue.First == null)
                        {
                            this.processing = false;
                            break;
                        }
                        else
                        {
                            Content = this.queue.First.Value;
                            this.queue.RemoveFirst();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Critical(ex);
                this.processing = false;
            }
        }
Ejemplo n.º 2
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;
 }
Ejemplo n.º 3
0
        private MqttTopic[] GetChildNodes()
        {
            if (this.topics == null)
            {
                return(new MqttTopic[0]);
            }
            else
            {
                MqttTopic[] Result;

                lock (this.topics)
                {
                    Result = new MqttTopic[this.topics.Count];
                    this.topics.Values.CopyTo(Result, 0);
                }

                return(Result);
            }
        }
Ejemplo n.º 4
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));
            }
        }
Ejemplo n.º 5
0
        public void DataReported(MqttContent Content)
        {
            int Len = Content.Data.Length;

            if (Len == 0)
            {
                this.data = null;
                return;
            }

            this.dataCount += Len;

            try
            {
                if (this.data == null)
                {
                    this.data = this.FindDataType(Content);
                }
                else
                {
                    this.data.DataReported(Content);
                }

                this.SetOk();
            }
            catch (Exception)
            {
                try
                {
                    this.data = this.FindDataType(Content);
                }
                catch (Exception ex2)
                {
                    this.Exception(ex2);
                    return;
                }
            }

            if (this.broker.Client?.HasSniffers ?? false)
            {
                this.data.SnifferOutput(this.broker.Client);
            }

            if (Types.TryGetModuleParameter("Sensor", out object Obj) && Obj is SensorServer SensorServer)
            {
                try
                {
                    InternalReadoutRequest Request = new InternalReadoutRequest(string.Empty,
                                                                                new ThingReference[] { this.node }, FieldType.All, null, DateTime.MinValue, DateTime.MaxValue,
                                                                                (sender, e) =>
                    {
                        SensorServer.NewMomentaryValues(this.node, e.Fields);

                        MqttTopic Current = this;
                        MqttTopic Parent  = this.parent;

                        while (Parent != null)
                        {
                            foreach (Field F in e.Fields)
                            {
                                if (F.Name == "Value")
                                {
                                    F.Name = Current.localTopic;
                                }
                                else
                                {
                                    F.Name = Current.localTopic + ", " + F.Name;
                                }

                                SensorServer.NewMomentaryValues(Parent.node, F);
                            }

                            Current = Parent;
                            Parent  = Parent.parent;
                        }

                        return(Task.CompletedTask);
                    },
                                                                                (sender, e) =>
                    {
                        return(Task.CompletedTask);
                    }, null);

                    this.StartReadout(Request);
                }
                catch (Exception ex)
                {
                    this.Exception(ex);
                }
            }
        }
Ejemplo n.º 6
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));
            }
        }
Ejemplo n.º 7
0
        public async Task DataReceived(MqttContent Content)
        {
            MqttTopic Topic = await this.GetTopic(Content.Topic, true);

            Topic?.DataReported(Content);
        }