Exemplo n.º 1
0
        public override List <Field> ProcessReadoutRequest(ReadoutType Types, DateTime From, DateTime To)
        {
            String           Path   = this.resource;
            EditableTreeNode Node   = this.Parent;
            CoapServer       Server = null;
            CoapFolder       Folder;

            while (Node != null)
            {
                if ((Folder = Node as CoapFolder) != null)
                {
                    Path = Folder.Folder + "/" + Path;
                }
                else if ((Server = Node as CoapServer) != null)
                {
                    break;
                }

                Node = Node.Parent;
            }

            if (Server == null)
            {
                throw new Exception("No CoAP Server node found.");
            }

            CoapEndpoint Endpoint = Server.Endpoint;
            CoapResponse Response = Endpoint.GET(true, Server.Host, Server.Port, Path, this.query, 20000);

            return(this.ParseContent(Response.Response, Types, From, To));
        }
Exemplo n.º 2
0
 public override bool CanTakeNewNode(EditableTreeNode Child)
 {
     return(false);
 }
Exemplo n.º 3
0
 public override bool CanBeAddedTo(EditableTreeNode Parent)
 {
     return((Parent is CoapFolder) || (Parent is CoapServer));
 }
Exemplo n.º 4
0
 public override bool CanTakeNewNode(EditableTreeNode Child)
 {
     return((Child is CoapFolder) || (Child is CoapContent));
 }
 /// <summary>
 /// <see cref="Clayster.Library.Abstract.EditableTreeNode.CanTakeNewNode(EditableTreeNode)"/>
 /// </summary>
 public override bool CanTakeNewNode(EditableTreeNode Child)
 {
     return(Child is CoapServer);
 }
 /// <summary>
 /// <see cref="Clayster.Library.Abstract.EditableTreeNode.CanBeAddedTo"/>
 /// </summary>
 public override bool CanBeAddedTo(EditableTreeNode Parent)
 {
     return((Parent is Root) || (Parent is IpNetwork));
 }
 public override bool CanBeAddedTo(EditableTreeNode Parent)
 {
     return(Parent is CoapPort);
 }
        private void Scan(object Parameter)
        {
            object[] P            = (object[])Parameter;
            User     User         = (User)P[0];
            Language UserLanguage = (Language)P[1];

            try
            {
                CoapEndpoint       Endpoint = this.Endpoint;
                CoapResponse       Response;
                LinkFormatDocument LinkDoc;

                Response = Endpoint.GET(true, this.Host, this.Port, ".well-known/core", string.Empty, 20000);

                LinkDoc = Response.Response as LinkFormatDocument;
                if (LinkDoc == null)
                {
                    throw new Exception(CoapNode.String(28, "Unexpected response returned."));
                }

                foreach (Link Link in LinkDoc.Links)
                {
                    Uri              Uri = new Uri(Link.Url);
                    string[]         Parts = Uri.PathAndQuery.Split('/');
                    EditableTreeNode Node = this;
                    int              i, c = Parts.Length;
                    string           Segment;
                    object           Obj;
                    CoapContent      Content;
                    CoapFolder       Folder;
                    CoapNode         Found;
                    StringBuilder    Path = null;

                    for (i = 0; i < c - 1; i++)
                    {
                        Segment = Parts[i];
                        if (string.IsNullOrEmpty(Segment))
                        {
                            continue;
                        }

                        if (i <= 1 && Segment == ".well-known")
                        {
                            break;
                        }

                        Found = null;

                        if (Path == null)
                        {
                            Path = new StringBuilder();
                        }
                        else
                        {
                            Path.Append('/');
                        }

                        Path.Append(Segment);

                        foreach (EditableTreeNode Child in Node.AllChildren)
                        {
                            if ((Folder = Child as CoapFolder) != null && Folder.Folder == Segment)
                            {
                                Found = Folder;
                                break;
                            }
                        }

                        if (Found == null)
                        {
                            Parameters Param = EditableObject.GetParametersForNewObject(typeof(CoapFolder), true, User.AllPrivileges);
                            Param[Clayster.Library.Meters.Node.DefaultIdParameterId] = this.Id + "/" + Path.ToString();
                            Param["folder"] = Segment;

                            Found = (CoapNode)EditableObject.CreateNewObject(typeof(CoapFolder), Param, UserLanguage, true, Topology.Source, User);
                            Node.Add(Found);
                        }

                        Node = Found;
                    }

                    Segment = Parts[c - 1];
                    if (!string.IsNullOrEmpty(Segment))
                    {
                        Found = null;

                        if (Path == null)
                        {
                            Path = new StringBuilder();
                        }
                        else
                        {
                            Path.Append('/');
                        }

                        Path.Append(Segment);

                        foreach (EditableTreeNode Child in Node.AllChildren)
                        {
                            if ((Content = Child as CoapContent) != null && Content.Resource == Segment)
                            {
                                Found = Content;
                                break;
                            }
                        }

                        if (Found == null)
                        {
                            try
                            {
                                Type T = null;

                                Response = Endpoint.GET(true, this.Host, this.Port, Path.ToString(), string.Empty, 20000);
                                Obj      = Response.Response;

                                if (Obj is string)
                                {
                                    string            s = (string)Obj;
                                    PhysicalMagnitude M;
                                    Duration          D;
                                    DateTime          TP;
                                    double            d;
                                    int  NrDec;
                                    bool b;

                                    if (XmlUtilities.TryParseBoolean(s, out b))
                                    {
                                        T = typeof(CoapBoolean);
                                    }
                                    else if (XmlUtilities.TryParseDuration(s, out D))
                                    {
                                        T = typeof(CoapDuration);
                                    }
                                    else if (XmlUtilities.TryParseDateTimeXml(s, out TP) || (TP = Web.ParseDateTimeRfc822(s)) != DateTime.MinValue)
                                    {
                                        T = typeof(CoapDateTime);
                                    }
                                    else if (XmlUtilities.TryParseDouble(s, out d, out NrDec))
                                    {
                                        T = typeof(CoapNumber);
                                    }
                                    else if (PhysicalMagnitude.TryParse(s, out M))
                                    {
                                        T = typeof(CoapPhysicalMagnitude);
                                    }
                                    else
                                    {
                                        try
                                        {
                                            TurtleDocument TurtleDoc = new TurtleDocument((string)Obj, "coap://" + this.Host + ":" + this.Port.ToString() + "/" + Path.ToString());
                                        }
                                        catch (Exception)
                                        {
                                            T = typeof(CoapString);
                                        }
                                    }
                                }
                                else if (Obj is XmlDocument)
                                {
                                    XmlDocument Doc = (XmlDocument)Obj;

                                    if (Doc.DocumentElement.LocalName == "FieldsRoot" && Doc.DocumentElement.NamespaceURI == "http://clayster.com/schema/Fields/v1.xsd")
                                    {
                                        T = typeof(CoapFields);
                                    }
                                    else if ((Doc.DocumentElement.LocalName == "fields" || Doc.DocumentElement.LocalName == "failure") && Doc.DocumentElement.NamespaceURI == "urn:xmpp:iot:sensordata")
                                    {
                                        T = typeof(CoapXep0323);
                                    }
                                    else
                                    {
                                        T = typeof(CoapXml);
                                    }
                                }
                                else if (Obj is LinkFormatDocument)
                                {
                                    // Ignore
                                }
                                else if (Obj is byte[])
                                {
                                    // Ignore
                                }
                                else
                                {
                                    T = typeof(CoapJson);
                                }

                                if (T != null)
                                {
                                    Parameters Param = EditableObject.GetParametersForNewObject(T, true, User.AllPrivileges);
                                    Param[Clayster.Library.Meters.Node.DefaultIdParameterId] = this.Id + "/" + Path.ToString();
                                    Param["resource"] = Segment;

                                    if (Param.ContainsParameter("fieldName"))
                                    {
                                        Param["fieldName"] = Segment;
                                    }

                                    Found = (CoapNode)EditableObject.CreateNewObject(T, Param, UserLanguage, true, Topology.Source, User);
                                    Node.Add(Found);
                                }
                            }
                            catch (Exception ex)
                            {
                                this.LogException(ex);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.LogException(ex);
            }
            finally
            {
                this.scanning = false;
            }
        }