Example #1
0
 internal bool Remove(SensorDataServerRequest Request)
 {
     lock (this.requests)
     {
         return(this.requests.Remove(Request.Key));
     }
 }
Example #2
0
        private void PerformReadout(SensorDataServerRequest Request)
        {
            Request.Started = true;

            SensorDataReadoutEventHandler h = this.OnExecuteReadoutRequest;

            if (h == null)
            {
                this.client.SendMessage(MessageType.Normal, Request.RemoteJID, "<done xmlns='" + SensorClient.NamespaceSensorData + "' id='" +
                                        XML.Encode(Request.Id) + "'/>", string.Empty, string.Empty, string.Empty, string.Empty, string.Empty);

                lock (this.requests)
                {
                    this.requests.Remove(Request.Key);
                }
            }
            else
            {
                try
                {
                    h(this, Request);
                }
                catch (Exception ex)
                {
                    Request.ReportErrors(true, new ThingError(string.Empty, string.Empty, string.Empty, DateTime.Now, ex.Message));
                }
            }
        }
Example #3
0
        private void StartReadout(object P)
        {
            SensorDataServerRequest Request = (SensorDataServerRequest)P;

            this.client.SendMessage(MessageType.Normal, Request.RemoteJID, "<started xmlns='" + SensorClient.NamespaceSensorData + "' id='" +
                                    XML.Encode(Request.Id) + "'/>", string.Empty, string.Empty, string.Empty, string.Empty, string.Empty);

            this.PerformReadout(Request);
        }
Example #4
0
        private void TriggerSubscription(Subscription Subscription)
        {
            string Key = Subscription.From + " " + Subscription.Id;

            SensorDataServerRequest Request = new SensorDataServerRequest(Subscription.Id, this, Subscription.From,
                                                                          Subscription.From, Subscription.NodeReferences, Subscription.FieldTypes, Subscription.FieldNames,
                                                                          DateTime.MinValue, DateTime.MaxValue, DateTime.MinValue, Subscription.ServiceToken, Subscription.DeviceToken,
                                                                          Subscription.UserToken);
            bool NewRequest;

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

            this.PerformReadout(Request);
            this.UpdateSubscriptionTimers(Subscription.LastTrigger, Subscription);
        }
Example #5
0
        private void AcceptRequest(SensorDataServerRequest Request, IqEventArgs e, string Id)
        {
            string Key = e.From + " " + Id;
            bool   NewRequest;

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

            if (Request.When > DateTime.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);
            }
        }
Example #6
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);
        }
Example #7
0
        private void ReqHandler(object Sender, IqEventArgs e)
        {
            List <ThingReference> Nodes      = null;
            List <string>         Fields     = null;
            XmlElement            E          = e.Query;
            FieldType             FieldTypes = (FieldType)0;
            DateTime From         = DateTime.MinValue;
            DateTime To           = DateTime.MaxValue;
            DateTime When         = DateTime.MinValue;
            string   ServiceToken = string.Empty;
            string   DeviceToken  = string.Empty;
            string   UserToken    = string.Empty;
            string   NodeId;
            string   SourceId;
            string   Partition;
            string   Id = string.Empty;
            bool     b;

            foreach (XmlAttribute Attr in E.Attributes)
            {
                switch (Attr.Name)
                {
                case "id":
                    Id = Attr.Value;
                    break;

                case "from":
                    if (!XML.TryParse(Attr.Value, out From))
                    {
                        From = DateTime.MinValue;
                    }
                    break;

                case "to":
                    if (!XML.TryParse(Attr.Value, out To))
                    {
                        To = DateTime.MaxValue;
                    }
                    break;

                case "when":
                    if (!XML.TryParse(Attr.Value, out When))
                    {
                        When = DateTime.MinValue;
                    }
                    break;

                case "st":
                    ServiceToken = Attr.Value;
                    break;

                case "dt":
                    DeviceToken = Attr.Value;
                    break;

                case "ut":
                    UserToken = Attr.Value;
                    break;

                case "all":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.All;
                    }
                    break;

                case "h":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Historical;
                    }
                    break;

                case "m":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Momentary;
                    }
                    break;

                case "p":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Peak;
                    }
                    break;

                case "s":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Status;
                    }
                    break;

                case "c":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Computed;
                    }
                    break;

                case "i":
                    if (CommonTypes.TryParse(Attr.Value, out b) && b)
                    {
                        FieldTypes |= FieldType.Identity;
                    }
                    break;
                }
            }

            foreach (XmlNode N in E.ChildNodes)
            {
                switch (N.LocalName)
                {
                case "nd":
                    if (Nodes == null)
                    {
                        Nodes = new List <ThingReference>();
                    }

                    E         = (XmlElement)N;
                    NodeId    = XML.Attribute(E, "id");
                    SourceId  = XML.Attribute(E, "src");
                    Partition = XML.Attribute(E, "pt");

                    Nodes.Add(new ThingReference(NodeId, SourceId, Partition));
                    break;

                case "f":
                    if (Fields == null)
                    {
                        Fields = new List <string>();
                    }

                    Fields.Add(XML.Attribute((XmlElement)N, "n"));
                    break;
                }
            }

            SensorDataServerRequest Request = new SensorDataServerRequest(Id, this, e.From, e.From, Nodes?.ToArray(), FieldTypes,
                                                                          Fields?.ToArray(), From, To, When, ServiceToken, DeviceToken, UserToken);

            if (this.provisioningClient != null)
            {
                this.provisioningClient.CanRead(e.FromBareJid, Request.Types, Request.Nodes, Request.FieldNames,
                                                Request.ServiceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                Request.DeviceToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                Request.UserToken.Split(space, StringSplitOptions.RemoveEmptyEntries),
                                                (sender2, e2) =>
                {
                    if (e2.Ok && e2.CanRead)
                    {
                        Request.Nodes      = e2.Nodes;
                        Request.FieldNames = e2.FieldsNames;
                        Request.Types      = e2.FieldTypes;

                        this.AcceptRequest(Request, e, Id);
                    }
                    else
                    {
                        e.IqError("<error type='cancel'><forbidden xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/>" +
                                  "<text xmlns='urn:ietf:params:xml:ns:xmpp-stanzas' xml:lang='en'>Access denied.</text></error>");
                    }
                }, null);
            }
            else
            {
                this.AcceptRequest(Request, e, Id);
            }
        }