private static void CleanAll(string[] cmds)
        {
            CoapClient            c = new CoapClient(Program.Host);
            IEnumerable <WebLink> d = c.Discover("rt=core.rd*", MediaType.ApplicationLinkFormat);

            string endpointRegister = null;
            string endpointLookup   = null;
            string groupRegister    = null;
            string groupLookup      = null;
            string resourceLookup   = null;

            foreach (WebLink w in d)
            {
                foreach (string s in w.Attributes.GetResourceTypes())
                {
                    switch (s)
                    {
                    case "core.rd":
                        endpointRegister = w.Uri;
                        break;

                    case "core.rd-lookup-res":
                        resourceLookup = w.Uri;
                        break;

                    case "core.rd-group":
                        groupRegister = w.Uri;
                        break;

                    case "core.rd-lookup-gp":
                        groupLookup = w.Uri;
                        break;

                    case "core.rd-lookup-ep":
                        endpointLookup = w.Uri;
                        break;
                    }
                }
            }

            if (groupLookup != null)
            {
                d = c.Discover(groupLookup, null, MediaType.ApplicationLinkFormat);
                foreach (WebLink w in d)
                {
                    c.UriPath = w.Uri;
                    c.Delete();
                }
            }

            d = c.Discover(endpointLookup, null, MediaType.ApplicationLinkFormat);
            foreach (WebLink w in d)
            {
                c.UriPath = w.Uri;
                c.Delete();
            }
        }
        private static void RunTest2(string[] cmds)
        {
            //  Multicast Request
            //  Multicast Request
            //  NON GET /.well-known/core?rt=core.rd*
            // Accept=40

            Console.WriteLine("We don't do multicast for clients or servers at this time");

            CoapClient cl = new CoapClient(Program.Host)
            {
                UriPath  = "/.well-known/core",
                UriQuery = "rt=core.rd*",
                Timeout  = 20 * 1000
            };

            cl.UseNONs();

            IEnumerable <WebLink> d = cl.Discover("rt=core.r*", MediaType.ApplicationLinkFormat);

            foreach (WebLink w in d)
            {
                foreach (string s in w.Attributes.GetResourceTypes())
                {
                    switch (s)
                    {
                    case "core.rd":
                        ResourceRegister = w.Uri;
                        break;

                    case "core.rd-lookup-res":
                        ResourceLookup = w.Uri;
                        break;

                    case "core.rd-group":
                        GroupRegister = w.Uri;
                        break;

                    case "core.rd-lookup-gp":
                        GroupLookup = w.Uri;
                        break;

                    case "core.rd-lookup-ep":
                        EndpointLookup = w.Uri;
                        break;
                    }
                }
            }
        }
Esempio n. 3
0
 /// <summary>
 /// Acquire All Resources
 /// </summary>
 /// <returns></returns>
 public List <WebLink> GetResources()
 {
     return(_coapClient.Discover().ToList());
 }
        static void RunTest2X(string[] cmds)
        {
            int useContentFormat = MediaType.ApplicationLinkFormat;

            if (cmds.Length > 2)
            {
                switch (cmds[2].ToLower())
                {
#if false // Dead code?
                case "cbor":
                    useContentFormat = MediaType.ApplicationLinkFormatCbor;
                    break;

                case "json":
                    useContentFormat = MediaType.ApplicationLinkFormatJson;
                    break;
#endif

                default:
                    Console.WriteLine("Unrecognized content type");
                    return;
                }
            }
            CoapClient            c = new CoapClient(Program.Host);
            IEnumerable <WebLink> d = c.Discover("rt=core.r*", MediaType.ApplicationLinkFormat);

            string endpointRegister = null;
            string endpointLookup   = null;
            string groupRegister    = null;
            string groupLookup      = null;
            string resourceLookup   = null;

            foreach (WebLink w in d)
            {
                foreach (string s in w.Attributes.GetResourceTypes())
                {
                    switch (s)
                    {
                    case "core.rd":
                        endpointRegister = w.Uri;
                        break;

                    case "core.rd-lookup-res":
                        resourceLookup = w.Uri;
                        break;

                    case "core.rd-group":
                        groupRegister = w.Uri;
                        break;

                    case "core.rd-lookup-gp":
                        groupLookup = w.Uri;
                        break;

                    case "core.rd-lookup-ep":
                        endpointLookup = w.Uri;
                        break;
                    }
                }
            }

            if (endpointRegister == null)
            {
                Console.WriteLine("No endpoint registration resource found");
                return;
            }

            //  Register three endpoints and get their locations

            Uri uri1 = new Uri(Program.Host, endpointRegister);
            c          = new CoapClient(uri1);
            c.UriQuery = "ep=endpoint1&con=coap://sensor1&lt=240";

            Response r =
                c.Post(EncodeResources(
                           "</sensors/temp>;ct=41;rt=\"temperature-c\";if=sensor,</sensors/light>;ct=41;rt=\"light-lux\";if=sensor;obs",
                           useContentFormat),
                       useContentFormat);
            string endpoint1Location = r.Location;

            c.UriQuery = "ep=endpoint2&d=floor3&con=coap://sensor2&lt=120";
            r          = c.Post(EncodeResources(
                                    "</sensors/temp>;ct=41;rt=\"temperature-c\";if=sensor,</sensors/light>;ct=41;rt=\"light-lux\";if=sensor",
                                    useContentFormat), useContentFormat);
            string endpoint2Location = r.Location;

            c.UriQuery = "ep=endpoint3&con=coaps://door1&even=yes&lt=120";
            r          = c.Post(EncodeResources(
                                    "</door/state>;ct=41;rt=doorx;if=senseor,</door/lock>,</door/desc>;ct=0;anchor=\"https://doorcomany/locks?locktype=1\";rt=description",
                                    useContentFormat), useContentFormat);
            string endpoint3Location = r.Location;

            //  do end point queries
            if (endpointLookup == null)
            {
                Console.WriteLine("No endpoint lookup resource found");
            }
            else
            {
                uri1 = new Uri(Program.Host, endpointLookup);
                c    = new CoapClient(uri1);
                d    = c.Discover(endpointLookup, null, useContentFormat);
                Console.WriteLine("Query Endpoint all - expect 3");
                foreach (WebLink w in d)
                {
                    Console.WriteLine("  " + w.ToString());
                }

                d = c.Discover(endpointLookup, "if=sensor", useContentFormat);
                Console.WriteLine("Query for if=sensor");
                foreach (WebLink w in d)
                {
                    Console.WriteLine("  " + w.ToString());
                }

                d = c.Discover(endpointLookup, "even=yes", useContentFormat);
                Console.WriteLine("Query for even=yes");
                foreach (WebLink w in d)
                {
                    Console.WriteLine("  " + w.ToString());
                }
            }

            if (resourceLookup == null)
            {
                Console.WriteLine("No resource lookup resource found");
            }
            else
            {
                uri1 = new Uri(Program.Host, resourceLookup);
                c    = new CoapClient(uri1);

                d = c.Discover(resourceLookup, null, useContentFormat);
                Console.WriteLine("Query resources all");
                foreach (WebLink w in d)
                {
                    Console.WriteLine("  " + w.ToString());
                }

                d = c.Discover(resourceLookup, "if=sensor", useContentFormat);
                Console.WriteLine("Query resource - if=sensor");
                foreach (WebLink w in d)
                {
                    Console.WriteLine("  " + w.ToString());
                }

                d = c.Discover(resourceLookup, "ep=endpoint1", useContentFormat);
                Console.WriteLine("Query resource - endpoint1");
                foreach (WebLink w in d)
                {
                    Console.WriteLine("  " + w.ToString());
                }
            }


            if (groupRegister != null)
            {
                uri1       = new Uri(Program.Host, groupRegister);
                c          = new CoapClient(uri1);
                c.UriQuery = "gp=lights";
                r          = c.Post($"<{endpoint1Location}>,<{endpoint2Location}>", MediaType.ApplicationLinkFormat);

                string group1Location = r.Location;

                c.UriQuery = "gp=all&con=coap://[MD1]:8080&odd=no";
                r          = c.Post($"<{endpoint1Location}>,<{endpoint2Location}>,<{endpoint3Location}>",
                                    MediaType.ApplicationLinkFormat);

                if (groupLookup != null)
                {
                    uri1 = new Uri(Program.Host, groupLookup);
                    c    = new CoapClient(uri1);

                    //  Get all of the groups
                    d = c.Discover(groupLookup, null, useContentFormat);
                    Console.WriteLine("Retrieve all groups - expect 3");
                    foreach (WebLink w in d)
                    {
                        Console.WriteLine("  " + w.ToString());
                    }

                    // Get all groups w/ doors
                    d = c.Discover(groupLookup, "rt=doorx", useContentFormat);
                    Console.WriteLine("Retrieve groups w/ rt=doors - expect 1");
                    foreach (WebLink w in d)
                    {
                        Console.WriteLine("  " + w.ToString());
                    }
                }
            }
        }
Esempio n. 5
0
 public List <WebLink> GetResources()
 {
     return(cc.Discover().ToList());
 }
Esempio n. 6
0
        static void Main(string[] args)
        {
            Com.AugustCellars.CoAP.Log.LogManager.Level = LogLevel.None;
            ;
            String Server = "192.168.53.55:5684";

            OneKey userKey = new OneKey();

            userKey.Add(CoseKeyKeys.KeyType, GeneralValues.KeyType_Octet);
            //userKey.Add(CoseKeyParameterKeys.Octet_k, CBORObject.FromObject(Encoding.UTF8.GetBytes("sesame")));
            // userKey.Add(CoseKeyKeys.KeyIdentifier, CBORObject.FromObject(Encoding.UTF8.GetBytes("password")));

            CoapClient client = new CoapClient(new Uri($"coaps://{Server}/.well-known/core"));



            CoAPEndPoint ep = new DTLSClientEndPoint(userKey);

            client.EndPoint = ep;
            ep.Start();

            //

            Response r1 = client.Get();

            Console.WriteLine("Links = " + r1.PayloadString);

            //
            //           string str = "<//15001/65536>;ct=0;obs,<//15001/65537>;ct=0;obs,<//15004/136834>;ct=0;obs,<//15005/136834/217609>;ct=0;obs,<//15005/136834/218326>;ct=0;obs,<//15005/136834/224384>;ct=0;obs,<//15005/136834>;ct=0;obs,<//15001>;ct=0;obs,<//15001/reset>;ct=0,<//status>;ct=0;obs,<//15005>;ct=0;obs,<//15004>;ct=0;obs,<//15004/add>;ct=0,<//15004/remove>;ct=0,<//15006>;ct=0;obs,<//15011/15012>;ct=0;obs,<//15011/9034>;ct=0,<//15011/9030>;ct=0,<//15011/9031>;ct=0,<//15011/9063>;ct=0,<//15011/9033>;ct=0,<//15010>;ct=0;obs";
            //           IEnumerable<WebLink> links = LinkFormat.Parse(str);
            //           foreach(var item in links) Console.WriteLine(item);
            //

            LogManager.Level = LogLevel.None;


            IEnumerable <WebLink> items = client.Discover();

            foreach (var node in items)
            {
                Console.WriteLine($"Resource = {node}");

                client.UriPath = node.Uri;

                if (false && node.Attributes.Observable)
                {
                    CoapClient c2 = new CoapClient()
                    {
                        EndPoint = client.EndPoint,
                        Uri      = client.Uri,
                        UriPath  = node.Uri
                    };
                    Console.WriteLine("Observe it");
                    CoapObserveRelation relation1 = c2.Observe(r => { EventIn(node.Uri, r); });
                }
                else
                {
                    Response response = client.Get();

                    Console.WriteLine("   Payload: " + response.PayloadString);
                    Console.WriteLine();
                }
            }

            client.Uri     = new Uri($"coaps://{Server}");
            client.UriPath = "/15004/166412";
            client.Get();
            Response rep = client.Put("{ \"5850\":1}");

            Thread.Sleep(3000);

            //rep = client.Get();
            Console.WriteLine(rep.PayloadString);

            client.UriPath = "/15001/65537";
            ;

            for (int i = 0; i < 10; i++)
            {
                Thread.Sleep(3000);
                client.Put("{ \"5851\":127}");

                Thread.Sleep(3000);
                client.Put("{ \"3311\":[{ \"5851\":0}]}");

                Thread.Sleep(3000);
                client.Put("{ \"3311\":[{ \"5851\":255}]}");
            }


            ep.Stop();
        }