Пример #1
0
        /// <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) : "";

            if (coapResp != null)
            {
                if (coapResp.Token != null)
                {
                    tokenRx = AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value);
                }
            }
            try
            {
                FileLogger.Write("Received response from server - token = " + tokenRx);
                FileLogger.Write(coapResp.ToString());
            }
            catch { }
            if (tokenRx == __Token)
            {
                if (BreakIfError(coapResp.Code.Value))
                {
                    return;
                }
                if (coapResp.Code.Value == CoAPMessageCode.CHANGED)
                {
                    __Response = coapResp;
                    __Done.Set();
                }
                else
                {
                    // Will come here if an error occurred.
                    // For now, let the request time out.
                }
            }
        }
        /// <summary>
        /// For some implementations, we need to retrieve the returned payload from a different source.
        /// This method allows us to push that payload into this object.
        /// </summary>
        /// <param name="payload"></param>
        public void SetDiscovery(byte[] payload)
        {
            string discovery = AbstractByteUtils.ByteToStringUTF8(payload);

            Console.WriteLine("Discovery = " + discovery);
            __DiscoveryResult = discovery;
        }
Пример #3
0
        private void OnCoAPResponseReceived(CoAPResponse coapResp)
        {
            string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (tokenRx == _mToken)
            {
                //This response is against the NON request for getting temperature we issued earlier
                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    //Get the temperature
                    string tempAsJSON = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);

                    //  Light light = Light.Deserialize(tempAsJSON);
                    // Hashtable tempValues = JSONResult.FromJSON(tempAsJSON);
                    // int temp = Convert.ToInt32(tempValues["temp"].ToString());
                    // int temp = Convert.ToInt32(tempAsJSON);
                    //Now do something with this temperature received from the server
                    Application.Current.Dispatcher.BeginInvoke((Action)(() => { gridError.Visibility = Visibility.Visible; }));
                    Application.Current.Dispatcher.BeginInvoke((Action)(() => { txtError.Text = $"Response received: {tempAsJSON}"; }));
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
        }
Пример #4
0
        /// <summary>
        /// Get the query parameter
        /// </summary>
        /// <param name="paramKey">The parameter key</param>
        /// <returns>string if key found else null</returns>
        public string GetQueryParam(string paramKey)
        {
            String qString = null;

            if (paramKey == null || paramKey.Trim().Length == 0)
            {
                return(null);
            }

            foreach (CoAPHeaderOption headerOption in this.Options)
            {
                if (headerOption.Number == CoAPHeaderOption.URI_QUERY)
                {
                    qString = AbstractByteUtils.ByteToStringUTF8(headerOption.Value);
                    if (qString.IndexOf("=") > 0)
                    {
                        string[] qpParts = qString.Split(new char[] { '=' });
                        //index 0 will have key and 1 will have value
                        if (paramKey.Trim().ToLower() == qpParts[0].Trim().ToLower())
                        {
                            return(AbstractURIUtils.UrlDecode(qpParts[1]));
                        }
                    }
                    else if (qString.Trim().ToLower() == paramKey.Trim().ToLower())
                    {
                        return("");//Only key no value
                    }
                }
            }
            return(null);
        }
        /// <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) : "";

            if (tokenRx == __Token)
            {
                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    ArrayList options = coapResp.Options.GetOptions((ushort)CoAPHeaderOption.CONTENT_FORMAT);
                    if (options.Count > 0)
                    {
                        bool proceed = false;
                        foreach (CoAPHeaderOption o in options)
                        {
                            CoAPContentFormatOption ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(o.Value));
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_LINK_FORMAT)
                            {
                                proceed = true;
                            }
                        }
                        if (proceed)
                        {
                            string discovery = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                            Console.WriteLine("Discovery " + __coapClient.EndPoint.ToString() + " = " + discovery);
                            __DiscoveryResult = discovery;
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
            __Done.Set();
        }
Пример #6
0
        /// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        private void OnCoAPSessionResponseReceived(CoAPResponse coapResp)
        {
            __TimedOut = false;
            string tokenRx = "";// (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (coapResp != null)
            {
                if (coapResp.Token != null)
                {
                    tokenRx = AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value);
                }
            }
            try
            {
                FileLogger.Write("Received response from server - token = " + tokenRx);
                FileLogger.Write(coapResp.ToString());
            }
            catch { }

            if (tokenRx == __Token)
            {
                int classCode = ((coapResp.Code.Value & 0xE0) >> 5);
                if (classCode == 2)
                {
                    __SessionRequestSucceeded = true;
                    FileLogger.Write("Session request completed successfully");
                }
                else
                {
                    FileLogger.Write("Session call code " + coapResp.Code.Value.ToString());
                    //Will come here if an error occurred..
                }
                __Done.Set();
            }
        }
        /// <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..
                }
            }
        }
Пример #8
0
        /// <summary>
        /// Get the payload as a string (A UTF-8 formatted string)
        /// </summary>
        /// <returns>The payload as string</returns>
        public string GetPayload()
        {
            if (this.Payload == null || this.Payload.Value == null || this.Payload.Value.Length == 0)
            {
                return(null);
            }

            return(AbstractByteUtils.ByteToStringUTF8(this.Payload.Value));
        }
        ///// <summary>
        ///// Called when a request is received...
        ///// </summary>
        ///// <param name="coapReq">The CoAPRequest object</param>
        //private void OnCoAPRequestReceived(CoAPRequest coapReq)
        //{
        //    Console.WriteLine(coapReq.QueryString);
        //}

        /// <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) : "";

            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 on Olimex " + __coapClient.EndPoint.ToString() + " = " + result);
                                __GetResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_OCTET_STREAM)
                            {
                                string result = SSNUtils.Conversion.BytesToHexView(coapResp.Payload.Value);
                                Console.WriteLine("Get on Olimex " + __coapClient.EndPoint.ToString() + " = " + result);
                                __GetResult = result;
                            }
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_JSON)
                            {
                                string result = SSNUtils.Conversion.BytesToHexView(coapResp.Payload.Value);
                                Console.WriteLine("Get on Olimex " + __coapClient.EndPoint.ToString() + " = " + result);
                                __GetResult = result;
                            }

                            __Response = coapResp;
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
            __Done.Set();
        }
Пример #10
0
 /// <summary>
 /// Convert to a string representation
 /// </summary>
 /// <returns>string</returns>
 public override string ToString()
 {
     if (this.Value == null || this.Value.Length == 0)
     {
         return("");
     }
     else
     {
         return(AbstractByteUtils.ByteToStringUTF8(this.Value));
     }
 }
Пример #11
0
 /// <summary>
 /// Convert to a string representation
 /// </summary>
 /// <returns>string</returns>
 public override string ToString()
 {
     if (this.Length > 0)
     {
         return("Token : Length =" + this.Length + ", Value=" + AbstractByteUtils.ByteToStringUTF8(this.Value));
     }
     else
     {
         return("Token : Length = 0, Value = NULL");
     }
 }
Пример #12
0
 /// <summary>
 /// We should receive the temperature from sever in the response
 /// </summary>
 /// <param name="coapResp">CoAPResponse</param>
 private static void OnCoAPResponseReceived(CoAPResponse coapResp)
 {
     if (coapResp.MessageType.Value == CoAPMessageType.ACK &&
         coapResp.Code.Value == CoAPMessageCode.CONTENT)
     {
         //We got the temperature..it will be in payload in JSON
         string    payload = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
         Hashtable keyVal  = JSONResult.FromJSON(payload);
         int       temp    = Convert.ToInt32(keyVal["temp"].ToString());
         //do something with the temperature now
     }
 }
Пример #13
0
        private void OnCoAPResponseReceived(CoAPResponse coapResp)
        {
            string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (tokenRx == _mToken)
            {
                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    tempAsJSON = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                }
                else
                {
                }
            }
        }
 /// <summary>
 /// Get the first matching option from the list that has the value
 /// which starts with the given input value
 /// </summary>
 /// <param name="optionNumber">The option number to get</param>
 /// <param name="valueStartsWith">The option value must start with this value</param>
 /// <returns>CoAPHeaderOption if available else null</returns>
 public CoAPHeaderOption GetOption(UInt16 optionNumber, string valueStartsWith)
 {
     foreach (CoAPHeaderOption headerOption in this)
     {
         if (headerOption.Number == optionNumber)
         {
             string optionValue = AbstractByteUtils.ByteToStringUTF8(headerOption.Value);
             if (optionValue.Trim().IndexOf(valueStartsWith) == 0)
             {
                 return(headerOption);
             }
         }
     }
     return(null);
 }
Пример #15
0
        /// <summary>
        /// Get the request path
        /// </summary>
        /// <returns>The coap path associated with the request</returns>
        public string GetPath()
        {
            string path = "";

            //Path components
            foreach (CoAPHeaderOption headerOption in this.Options)
            {
                if (headerOption.Number == CoAPHeaderOption.URI_PATH)
                {
                    if (path.Trim().Length > 0)
                    {
                        path += "/";
                    }
                    path += AbstractByteUtils.ByteToStringUTF8(headerOption.Value);
                }
            }
            return(path);
        }
Пример #16
0
 /// <summary>
 /// Going forward, we will receive temperature notifications from
 /// server in a CON request
 /// </summary>
 /// <param name="coapReq">CoAPRequest</param>
 static void OnCoAPRequestReceived(CoAPRequest coapReq)
 {
     if (coapReq.MessageType.Value == CoAPMessageType.CON)
     {
         //Extract the temperature..but first, check if the notification is fresh
         //The server sends a 4-digit sequence number
         int newObsSeq = AbstractByteUtils.ToUInt16(coapReq.Options.GetOption(CoAPHeaderOption.OBSERVE).Value);
         if ((lastObsSeq < newObsSeq && ((newObsSeq - lastObsSeq) < (System.Math.Pow(2.0, 23.0)))) ||
             (lastObsSeq > newObsSeq && ((lastObsSeq - newObsSeq) > (System.Math.Pow(2.0, 23.0)))) ||
             DateTime.Today > lastObsRx.AddSeconds(128))
         {
             //The value received from server is new....read the new temperature
             //We got the temperature..it will be in payload in JSON
             string    payload = AbstractByteUtils.ByteToStringUTF8(coapReq.Payload.Value);
             Hashtable keyVal  = JSONResult.FromJSON(payload);
             int       temp    = Convert.ToInt32(keyVal["temp"].ToString());
             //do something with the temperature now
             Debug.WriteLine(coapReq.ToString());
         }
         //update how many notifications received
         countOfNotifications++;
         if (countOfNotifications > 5)
         {
             //We are no longer interested...send RST to de-register
             CoAPResponse resp = new CoAPResponse(CoAPMessageType.RST, CoAPMessageCode.EMPTY, coapReq.ID.Value);
             resp.RemoteSender = coapReq.RemoteSender;
             resp.Token        = coapReq.Token;//Do not forget this...this is how messages are correlated
             coapClient.Send(resp);
         }
         else
         {
             //we are still interested...send ACK
             CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.EMPTY, coapReq.ID.Value);
             resp.RemoteSender = coapReq.RemoteSender;
             resp.Token        = coapReq.Token;//Do not forget this...this is how messages are correlated
             coapClient.Send(resp);
         }
         lastObsSeq = newObsSeq;
         lastObsRx  = DateTime.Today;
     }
 }
        /// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        static void OnCoAPResponseReceived(CoAPResponse coapResp)
        {
            string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (tokenRx == _mToken)
            {
                //This response is against the NON request for getting temperature we issued earlier
                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    //Get the temperature
                    string    tempAsJSON = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                    Hashtable tempValues = JSONResult.FromJSON(tempAsJSON);
                    int       temp       = Convert.ToInt32(tempValues["temp"].ToString());
                    //Now do something with this temperature received from the server
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
        }
Пример #18
0
        /// <summary>
        /// Get the request URL
        /// </summary>
        /// <returns>The coap URL associated with the request</returns>
        public string GetURL()
        {
            string scheme = (this.IsSecure) ? "coaps" : "coap";
            string host   = "localhost";
            UInt16 port   = 5683;

            if (this.Options.HasOption(CoAPHeaderOption.URI_HOST))
            {
                host = AbstractByteUtils.ByteToStringUTF8(this.Options.GetOption(CoAPHeaderOption.URI_HOST).Value);
            }
            if (this.Options.HasOption(CoAPHeaderOption.URI_PORT))
            {
                port = AbstractByteUtils.ToUInt16(this.Options.GetOption(CoAPHeaderOption.URI_PORT).Value);
            }
            ;

            string path    = this.GetPath();
            string qString = this.QueryString;
            string url     = scheme + "://" + host + ":" + port;

            url += (path != null && path.Trim().Length > 0) ? "/" + path : "";
            url += (qString != null && qString.Trim().Length > 0) ? "?" + qString : "";
            return(url);
        }
        /// <summary>
        /// Called when a response is received against a sent request
        /// </summary>
        /// <param name="coapResp">The CoAPResponse object</param>
        private void OnCoAPDiscoveryResponseReceived(CoAPResponse coapResp)
        {
            __TimedOut = false;
            string tokenRx = "";// (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";

            if (coapResp != null)
            {
                if (coapResp.Token != null)
                {
                    tokenRx = AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value);
                }
            }
            try
            {
                FileLogger.Write("Received response from server - token = " + tokenRx);
                FileLogger.Write(coapResp.ToString());
            }
            catch { }

            if (tokenRx == __Token)
            {
                if (BreakIfError(coapResp.Code.Value))
                {
                    this.ErrorResult = coapResp.Code.ToString();
                    return;
                }

                if (coapResp.Code.Value == CoAPMessageCode.CONTENT)
                {
                    // Looks like we have the expected response.
                    // Check the content format and figure out how to store the response.
                    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;
                            }
                        }
                        // The header options match what we expected.
                        // Set the discovery result.
                        if (proceed)
                        {
                            if (ccformat.Value == CoAPContentFormatOption.APPLICATION_LINK_FORMAT)
                            {
                                string discovery = AbstractByteUtils.ByteToStringUTF8(coapResp.Payload.Value);
                                __DiscoveryResult = discovery;
                            }

                            __Response = coapResp;
                            __Done.Set();
                        }
                    }
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
        }
Пример #20
0
        /// <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());
            __Response = coapResp;
            if (tokenRx == __Token)
            {
                if (BreakIfError(coapResp.Code.Value))
                {
                    return;
                }

                if (coapResp.Code.Value == CoAPMessageCode.DELETED)
                {
                    __Done.Set();
                }
                else
                {
                    //Will come here if an error occurred..
                }
            }
        }
Пример #21
0
        /// <summary>
        /// Convert to a string representation
        /// </summary>
        /// <returns>string</returns>
        public override string ToString()
        {
            string optionValueAsString = (this.Value != null) ? AbstractByteUtils.ByteToStringUTF8(this.Value) : "";

            switch (this.Number)
            {
            case CoAPHeaderOption.ACCEPT:
                return("Accept : " + AbstractByteUtils.ToUInt16(this.Value).ToString());

            case CoAPHeaderOption.BLOCK1:
                CoAPBlockOption cbo1 = new CoAPBlockOption(this.Value);
                return("Block1 : " + cbo1.ToString());

            case CoAPHeaderOption.BLOCK2:
                CoAPBlockOption cbo2 = new CoAPBlockOption(this.Value);
                return("Block2 : " + cbo2.ToString());

            case CoAPHeaderOption.CONTENT_FORMAT:
                CoAPContentFormatOption ccformat = new CoAPContentFormatOption(AbstractByteUtils.ToUInt16(this.Value));
                return("Content-Format : " + ccformat.ToString());

            case CoAPHeaderOption.ETAG:
                return("ETag : " + optionValueAsString);

            case CoAPHeaderOption.IF_MATCH:
                return("If-Match : " + optionValueAsString);

            case CoAPHeaderOption.IF_NONE_MATCH:
                return("If-None-Match : ");

            case CoAPHeaderOption.OBSERVE:
                return("Observe : " + AbstractByteUtils.ToUInt64(this.Value).ToString());   //We have no data structure for 3-bytes

            case CoAPHeaderOption.LOCATION_PATH:
                return("Location-Path : " + optionValueAsString);

            case CoAPHeaderOption.LOCATION_QUERY:
                return("Location-Query : " + optionValueAsString);

            case CoAPHeaderOption.MAX_AGE:
                return("Max-Age : " + AbstractByteUtils.ToUInt64(this.Value).ToString());

            case CoAPHeaderOption.PROXY_SCHEME:
                return("Proxy-Scheme : " + optionValueAsString);

            case CoAPHeaderOption.PROXY_URI:
                return("Proxy-Uri : " + optionValueAsString);

            case CoAPHeaderOption.SIZE1:
                return("Size1 : " + AbstractByteUtils.ToUInt64(this.Value).ToString());

            case CoAPHeaderOption.SIZE2:
                return("Size2 : " + AbstractByteUtils.ToUInt64(this.Value).ToString());

            case CoAPHeaderOption.URI_HOST:
                return("Uri-Host : " + optionValueAsString);

            case CoAPHeaderOption.URI_PATH:
                return("Uri-Path : " + optionValueAsString);

            case CoAPHeaderOption.URI_PORT:
                return("Uri-Port : " + AbstractByteUtils.ToUInt16(this.Value));

            case CoAPHeaderOption.URI_QUERY:
                return("Uri-Query : " + optionValueAsString);

            default:
                return("Unknown : " + optionValueAsString);
            }
        }
Пример #22
0
        /// <summary>
        /// For implementations whose communications approach doesn't fit in the standard model,
        /// this allows an inheriting class to set the response payload.
        /// </summary>
        /// <param name="payload"></param>
        public void SetDeleteResult(byte[] payload)
        {
            __InternalPayload = payload;
            CoAPResponse coapResp = new CoAPResponse();

            try
            {
                string tokenRx = (coapResp.Token != null && coapResp.Token.Value != null) ? AbstractByteUtils.ByteToStringUTF8(coapResp.Token.Value) : "";
                if (tokenRx == __Token)
                {
                    if (coapResp.Code.Value == CoAPMessageCode.DELETED)
                    {
                        __Response = coapResp;
                    }
                    else
                    {
                        //Will come here if an error occurred..
                    }
                }
            }
            catch
            { }
        }
        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
            { }
        }