示例#1
0
        private void btnPut_Click(object sender, EventArgs e)
        {
            CoApPut put = (CoApPut)NetworkInterface.Instance.ResourcePutter;

            put.IpAddress  = txtDevice.Text;
            put.ServerPort = GatewaySettings.Instance.CoApPort;
            // Drill into the device tree and fetch the related resource to fetch.
            put.URI = txtURI.Text;
            if (!(__Payload == null || __Payload.Length == 0))
            {
                put.Payload = new CoAPPayload(__Payload);
            }
            put.Send();
            string result = "";

            try
            {
                result = put.Response.ToString();
                if (put.Response.Payload.Value != null)
                {
                    if (put.Response.Payload.Value.Length != 0)
                    {
                        result += HdkUtils.BytesToHexView(put.Response.Payload.Value);
                    }
                }
            }
            catch
            {
                result = "Unable to retreive response from resource";
            }
            // Show the outcome from the PUT request.
            MessageBox.Show(result);
            Cursor.Current = Cursors.Default;
        }
        /// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        private void OnCoAPResponseReceived(CoAPResponse coapResp)
        {
            string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            FileLogger.Write("Received response from server - token = " + tokenRx);
            FileLogger.Write(coapResp.ToString());
            if (tokenRx == __Token)
            {
                if (BreakIfError(coapResp.Code.Value))
                {
                    return;
                }

                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                    if (options.Count > 0)
                    {
                        CoAPContentFormatOption ccformat = new CoAPContentFormatOption();
                        bool proceed = false;
                        foreach (CoAPHeaderOption o in options)
                        {
                            ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                            if (ccformat.IsValid())
                            {
                                proceed = true;
                                break;
                            }
                        }
                        if (proceed)
                        {
                            if (ccformat.Value == CoAPContentFormatOption.TEXT_PLAIN)
                            {
                                string result = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                                __PingResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_OCTET_STREAM)
                            {
                                string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                __PingResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_JSON)
                            {
                                string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                __PingResult = result;
                            }

                            __Response = coapResp;
                            __Done.Set();
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
        }
示例#3
0
 /// <summary>
 /// Capture the file contents.
 /// </summary>
 /// <param name="sender">the object dropped from</param>
 /// <param name="e">the arguments containing the data content</param>
 private void txtToPut_DragDrop(object sender, DragEventArgs e)
 {
     string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
     foreach (string file in files)
     {
         Console.WriteLine(file);
     }
     if (File.Exists(files[0]))
     {
         __Payload     = File.ReadAllBytes(files[0]);
         txtToPut.Text = HdkUtils.BytesToHexView(__Payload);
     }
 }
示例#4
0
        /// <summary>
        /// Handle GET request
        /// </summary>
        /// <param name="sender">the object generating the click</param>
        /// <param name="e">click-related arguments</param>
        private void btnGet_Click(object sender, EventArgs e)
        {
            // Set the wait cursor.
            Cursor.Current = Cursors.WaitCursor;

            // Hide any previous results
            pnlResults.Visible = false;
            lblResult.Visible  = false;

            CoApGet get = (CoApGet)NetworkInterface.Instance.ResourceGetter;

            // Use the device property for the target location.
            get.IpAddress = txtDevice.Text;

            // Use the pre-defined CoAP port number.
            get.ServerPort = GatewaySettings.Instance.CoApPort;

            // Use the resource property for the target resource.
            get.URI = txtURI.Text;

            // Send the request!
            get.Send();

            // Parse out the GET response.
            try
            {
                txtResult.Text = get.Response.ToString();
                if (get.Response.Payload.Value != null)
                {
                    if (get.Response.Payload.Value.Length != 0)
                    {
                        txtResult.Text += HdkUtils.BytesToHexView(get.Response.Payload.Value);
                    }
                }
            }
            catch
            {
                txtResult.Text = "Unable to retreive response from resource";
            }

            // Finally, make the results visible.
            pnlResults.Visible = true;
            lblResult.Visible  = true;

            // Reset the wait cursor.
            Cursor.Current = Cursors.Default;
        }
        public void SetGetResult(byte[] payload)
        {
            string discovery = AbstractByteUtils.ByteToStringUTF8(payload);

            Console.WriteLine("Discovery = " + discovery);
            __GetResult       = discovery;
            __InternalPayload = payload;
            CoAPResponse coapResp = new CoAPResponse();

            try
            {
                coapResp.FromByteStream(payload);
                string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";
                if (tokenRx == __Token)
                {
                    if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                    {
                        ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                        if (options.Count > 0)
                        {
                            CoAPContentFormatOption ccformat = new CoAPContentFormatOption();
                            bool proceed = false;
                            foreach (CoAPHeaderOption o in options)
                            {
                                ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                                if (ccformat.IsValid())
                                {
                                    proceed = true;
                                    break;
                                }
                            }
                            if (proceed)
                            {
                                if (ccformat.Value == CoAPContentFormatOption.TEXT_PLAIN)
                                {
                                    string result = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                                    Console.WriteLine("Get " + " = " + result);
                                    __GetResult = result;
                                }
                                if (ccformat.Value == CoAPContentFormatOption.APPLICATION_OCTET_STREAM)
                                {
                                    string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                    Console.WriteLine("Get " + " = " + result);
                                    __GetResult = result;
                                }
                                if (ccformat.Value == CoAPContentFormatOption.APPLICATION_JSON)
                                {
                                    string result = HdkUtils.BytesToHexView(coapResp.Payload.Value);
                                    Console.WriteLine("Get " + " = " + result);
                                    __GetResult = result;
                                }

                                __Response = coapResp;
                            }
                        }
                    }
                    else
                    {
                        //Will come here if an error occurred..
                    }
                }
            }
            catch
            { }
        }