Esempio n. 1
0
 public RootObject2()
 {
     fields2 = new Fields2();
 }
Esempio n. 2
0
        /// <summary>
        /// Checks if a readout can be performed.
        /// </summary>
        /// <param name="RequestFromBareJid">Readout request came from this bare JID.</param>
        /// <param name="FieldTypes">Field types requested.</param>
        /// <param name="Nodes">Any nodes included in the request.</param>
        /// <param name="FieldNames">And field names included in the request. If null, all field names are requested.</param>
        /// <param name="ServiceTokens">Any service tokens provided.</param>
        /// <param name="DeviceTokens">Any device tokens provided.</param>
        /// <param name="UserTokens">Any user tokens provided.</param>
        /// <param name="Callback">Method to call when result is received.</param>
        /// <param name="State">State object to pass on to the callback method.</param>
        public void CanRead(string RequestFromBareJid, FieldType FieldTypes, IEnumerable <ThingReference> Nodes, IEnumerable <string> FieldNames,
                            string[] ServiceTokens, string[] DeviceTokens, string[] UserTokens, CanReadCallback Callback, object State)
        {
            if ((!string.IsNullOrEmpty(this.ownerJid) && string.Compare(RequestFromBareJid, this.ownerJid, true) == 0) ||
                string.Compare(RequestFromBareJid, this.provisioningServerAddress, true) == 0)
            {
                if (Callback != null)
                {
                    try
                    {
                        ThingReference[] Nodes2 = Nodes as ThingReference[];
                        if (Nodes2 == null && Nodes != null)
                        {
                            List <ThingReference> List = new List <ThingReference>();
                            List.AddRange(Nodes);
                            Nodes2 = List.ToArray();
                        }

                        string[] FieldNames2 = FieldNames as string[];
                        if (FieldNames2 == null && FieldNames != null)
                        {
                            List <string> List = new List <string>();
                            List.AddRange(FieldNames);
                            FieldNames2 = List.ToArray();
                        }

                        IqResultEventArgs        e0 = new IqResultEventArgs(null, string.Empty, this.client.FullJID, this.provisioningServerAddress, true, State);
                        CanReadResponseEventArgs e  = new CanReadResponseEventArgs(e0, State, RequestFromBareJid, true, FieldTypes, Nodes2, FieldNames2);

                        Callback(this.client, e);
                    }
                    catch (Exception ex)
                    {
                        Log.Critical(ex);
                    }
                }

                return;
            }

            StringBuilder Xml = new StringBuilder();

            Xml.Append("<canRead xmlns='");
            Xml.Append(NamespaceProvisioningDevice);
            Xml.Append("' jid='");
            Xml.Append(XML.Encode(RequestFromBareJid));

            this.AppendTokens(Xml, "st", ServiceTokens);
            this.AppendTokens(Xml, "dt", DeviceTokens);
            this.AppendTokens(Xml, "ut", UserTokens);

            if ((FieldTypes & FieldType.All) == FieldType.All)
            {
                Xml.Append("' all='true");
            }
            else
            {
                if (FieldTypes.HasFlag(FieldType.Momentary))
                {
                    Xml.Append("' m='true");
                }

                if (FieldTypes.HasFlag(FieldType.Peak))
                {
                    Xml.Append("' p='true");
                }

                if (FieldTypes.HasFlag(FieldType.Status))
                {
                    Xml.Append("' s='true");
                }

                if (FieldTypes.HasFlag(FieldType.Computed))
                {
                    Xml.Append("' c='true");
                }

                if (FieldTypes.HasFlag(FieldType.Identity))
                {
                    Xml.Append("' i='true");
                }

                if (FieldTypes.HasFlag(FieldType.Historical))
                {
                    Xml.Append("' h='true");
                }
            }

            if (Nodes == null && FieldNames == null)
            {
                Xml.Append("'/>");
            }
            else
            {
                Xml.Append("'>");

                if (Nodes != null)
                {
                    foreach (ThingReference Node in Nodes)
                    {
                        Xml.Append("<nd id='");
                        Xml.Append(XML.Encode(Node.NodeId));

                        if (!string.IsNullOrEmpty(Node.SourceId))
                        {
                            Xml.Append("' src='");
                            Xml.Append(XML.Encode(Node.SourceId));
                        }

                        if (!string.IsNullOrEmpty(Node.Partition))
                        {
                            Xml.Append("' pt='");
                            Xml.Append(XML.Encode(Node.Partition));
                        }

                        Xml.Append("'/>");
                    }
                }

                if (FieldNames != null)
                {
                    foreach (string FieldName in FieldNames)
                    {
                        Xml.Append("<f n='");
                        Xml.Append(XML.Encode(FieldName));
                        Xml.Append("'/>");
                    }
                }

                Xml.Append("</canRead>");
            }

            this.CachedIqGet(Xml.ToString(), (sender, e) =>
            {
                XmlElement E = e.FirstElement;
                List <ThingReference> Nodes2 = null;
                List <string> Fields2        = null;
                FieldType FieldTypes2        = (FieldType)0;
                string Jid = string.Empty;
                string NodeId;
                string SourceId;
                string Partition;
                bool b;
                bool CanRead;

                if (e.Ok && E.LocalName == "canReadResponse" && E.NamespaceURI == NamespaceProvisioningDevice)
                {
                    CanRead = XML.Attribute(E, "result", false);

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

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

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

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

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

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

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

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

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

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

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

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

                            Fields2.Add(XML.Attribute((XmlElement)N, "n"));
                            break;
                        }
                    }
                }
                else
                {
                    CanRead = false;
                }

                CanReadResponseEventArgs e2 = new CanReadResponseEventArgs(e, State, Jid, CanRead, FieldTypes2, Nodes2?.ToArray(), Fields2?.ToArray());

                try
                {
                    Callback(this, e2);
                }
                catch (Exception ex)
                {
                    Log.Critical(ex);
                }
            }, null);
        }