public new void Send()
        {
            string serverIP = __IpAddress;//coapsharp.Properties.Settings.Default.IpAddress;// "10.90.202.182";//"localhost";

            if (HdkUtils.IsIPv4Address(serverIP))
            {
                CoAPSettings.Instance.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork;
            }
            else
            {
                CoAPSettings.Instance.AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6;
            }

            __coapClient.Initialize(serverIP, __ServerPort);
            __coapClient.CoAPResponseReceived += new CoAPResponseReceivedHandler(OnCoAPResponseReceived);

            __coapClient.CoAPError += new CoAPErrorHandler(OnCoAPError);

            coapReq = new CoAPRequest(this.ConfirmableMessageType, //CoAPMessageType.NON,
                                      CoAPMessageCode.GET,
                                      100);                        //hardcoded message ID as we are using only once
            string uriToCall = "coap://" + serverIP + ":" + __ServerPort + __URI;

            coapReq.SetURL(uriToCall);
            __Token       = DateTime.Now.ToString("HHmmss"); //Token value must be less than 8 bytes
            coapReq.Token = new CoAPToken(__Token);          //A random token
            __coapClient.Send(coapReq);
            __Done.WaitOne();
            __Done.Reset();
            __Done.Close();
            __Done = null;
            __coapClient.Shutdown();
            __coapClient = null;
        }
Exemplo n.º 2
0
        /// <summary>
        /// Load devices based on the network interface settings.
        /// For each valid device, load a list of related CoAP resources.
        /// </summary>
        private void LoadNodes()
        {
            Cursor.Current = Cursors.WaitCursor;    // Show as buys while loading the resources.

            FileLogger.Write("Loading all known devices");

            // Load the correct device class based on current network option
            CoApDeviceFinder finder  = (CoApDeviceFinder)NetworkInterface.Instance.NodeFinder;
            CoApDevices      devices = new CoApDevices();

            try
            {
                // With the Ping change, we are capturing the gateway credentials earlier than we had planned.
                //SetGatewayCoapApiCredentials();
                devices = finder.LoadNodes();
            }
            catch (Exception devFail)
            {
                MessageBox.Show("Device fetch failed - " + devFail.Message);
                return;
            }

            // Loop through all fetched devices.
            // If the device was successfully pinged, then we can load related resources.
            // Otherwise, we ignore it.
            foreach (CoApDevice d in devices)
            {
                TreeNode deviceNode = treeDevices.Nodes.Add(d.Name);
                if (d.Reachable)
                {
                    deviceNode.ForeColor = treeDevices.ForeColor;
                    if (NetworkInterface.Instance.UsingGateway)
                    {
                        CoAPSettings.Instance.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork;
                    }
                    if (NetworkInterface.Instance.UsingSimulator)
                    {
                        CoAPSettings.Instance.AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6;
                    }
                    // We should never get here, but this is added in case we add other communication methods later.
                    if (!NetworkInterface.Instance.UsingGateway && !NetworkInterface.Instance.UsingSimulator)
                    {
                        if (HdkUtils.IsIPv4Address(d.Name))
                        {
                            CoAPSettings.Instance.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork;
                        }
                        else
                        {
                            CoAPSettings.Instance.AddressFamily = System.Net.Sockets.AddressFamily.InterNetworkV6;
                        }
                    }

                    // We need to load the credentials if they have not already been set.
                    bool credentialsOk = true;
                    //if (NetworkInterface.Instance.UsingGateway)
                    //{
                    //    credentialsOk = SetGatewayCoapApiCredentials();
                    //}

                    // Check to see if we are ready to load resources related to a device.
                    if (__LoadResources && credentialsOk)
                    {
                        //d.Resources = LoadResources(deviceNode);
                    }
                }
                // Unreachable devices will be grayed out.
                else
                {
                    deviceNode.ForeColor = Color.Gray;
                }
            }

            // Expand the tree so that all loaded devices and resources are visible.
            treeDevices.ExpandAll();

            Cursor.Current = Cursors.Default;   // Clear the "busy" cursor
        }