示例#1
0
        private void PerformSubscription(bool Req, IqEventArgs e, string Id, Dictionary <string, FieldSubscriptionRule> FieldNames,
                                         ThingReference[] Nodes, FieldType FieldTypes, string ServiceToken, string DeviceToken, string UserToken,
                                         Duration MaxAge, Duration MinInterval, Duration MaxInterval)
        {
            DateTime     Now = DateTime.Now;
            Subscription Subscription;

            if (Req)
            {
                string   Key = e.From + " " + Id;
                string[] Fields2;

                if (FieldNames == null)
                {
                    Fields2 = null;
                }
                else
                {
                    Fields2 = new string[FieldNames.Count];
                    FieldNames.Keys.CopyTo(Fields2, 0);
                }

                SensorDataServerRequest Request = new SensorDataServerRequest(Id, this, e.From, e.From,
                                                                              Nodes, FieldTypes, Fields2, DateTime.MinValue, DateTime.MaxValue, DateTime.MinValue,
                                                                              ServiceToken, DeviceToken, UserToken);
                bool NewRequest;

                lock (this.requests)
                {
                    if (NewRequest = !this.requests.ContainsKey(Key))
                    {
                        this.requests[Key] = Request;
                    }
                }

                if (Request.When > Now)
                {
                    e.IqResult("<accepted xmlns='" + SensorClient.NamespaceSensorData + "' id='" + XML.Encode(Id) + "' queued='true'/>");
                    Request.When = this.scheduler.Add(Request.When, this.StartReadout, Request);
                }
                else
                {
                    e.IqResult("<accepted xmlns='" + SensorClient.NamespaceSensorData + "' id='" + XML.Encode(Id) + "'/>");
                    this.PerformReadout(Request);
                }
            }

            if (Nodes == null)
            {
                Nodes = new ThingReference[] { ThingReference.Empty }
            }
            ;

            lock (this.subscriptionsByThing)
            {
                Subscription = new Subscription(Id, e.From, Nodes, FieldNames, FieldTypes, MaxAge, MinInterval, MaxInterval,
                                                ServiceToken, DeviceToken, UserToken);

                foreach (ThingReference Thing in Nodes)
                {
                    if (!subscriptionsByThing.TryGetValue(Thing, out LinkedList <Subscription> Subscriptions))
                    {
                        Subscriptions = new LinkedList <Subscription>();
                        subscriptionsByThing[Thing] = Subscriptions;
                    }

                    LinkedListNode <Subscription> Next, Loop = Subscriptions.First;
                    while (Loop != null)
                    {
                        Next = Loop.Next;

                        if (Loop.Value.From == e.From)
                        {
                            if (Loop.Value.RemoveNode(Thing))
                            {
                                this.RemoveSubscriptionLocked(e.From, Loop.Value.Id, false);
                            }

                            Subscriptions.Remove(Loop);
                            break;
                        }

                        Loop = Next;
                    }

                    Subscriptions.AddLast(Subscription);
                }

                if (!subscriptionsByJID.TryGetValue(e.From, out Dictionary <string, Subscription> Subscriptions2))
                {
                    Subscriptions2             = new Dictionary <string, Subscription>();
                    subscriptionsByJID[e.From] = Subscriptions2;
                }

                Subscriptions2[Subscription.Id] = Subscription;
            }

            if (!Req)
            {
                e.IqResult("<accepted xmlns='" + SensorClient.NamespaceSensorData + "' id='" + XML.Encode(Id) + "'/>");
            }

            this.UpdateSubscriptionTimers(Now, Subscription);
        }