Esempio n. 1
0
        /// <summary>
        /// Send the request to get the temperature
        /// </summary>
        public static void SendRequest()
        {
            string      urlToCall = "coap://localhost:5683/sensors/temp/observe";
            UInt16      mId       = coapClient.GetNextMessageID();//Using this method to get the next message id takes care of pending CON requests
            CoAPRequest tempReq   = new CoAPRequest(CoAPMessageType.CON, CoAPMessageCode.GET, mId);

            tempReq.SetURL(urlToCall);
            //Important::Add the Observe option
            tempReq.AddOption(CoAPHeaderOption.OBSERVE, null);        //Value of observe option has no meaning in request
            tempReq.AddTokenValue(DateTime.Today.ToString("HHmmss")); //must be <= 8 bytes
            /*Uncomment the two lines below to use non-default values for timeout and retransmission count*/
            /*Dafault value for timeout is 2 secs and retransmission count is 4*/
            //tempReq.Timeout = 10;
            //tempReq.RetransmissionCount = 5;

            coapClient.Send(tempReq);
        }
Esempio n. 2
0
        /// Transfer data to server in blocks
        /// </summary>
        /// <returns>true if there are more blocks to be transferred</returns>
        private static void TransferToServerInBlocks()
        {
            CoAPRequest blockPUTReq = new CoAPRequest(CoAPMessageType.CON, CoAPMessageCode.PUT, coapClient.GetNextMessageID());

            blockPUTReq.SetURL("coap://localhost:5683/largedata/blockput");
            blockPUTReq.AddTokenValue(DateTime.Today.ToString("HHmm"));
            //Get needed bytes from source
            int copyBeginIdx = (int)(_blockSeqNo * BLOCK_SIZE_BYTES);
            int bytesToCopy  = ((copyBeginIdx + BLOCK_SIZE_BYTES) < _dataToTransfer.Length) ? BLOCK_SIZE_BYTES : (_dataToTransfer.Length - copyBeginIdx);

            byte[] blockToSend = new byte[bytesToCopy];
            Array.Copy(_dataToTransfer, copyBeginIdx, blockToSend, 0, bytesToCopy);
            //Calculate how many more bytes left to transfer
            bool hasMore = (_totalBytesTransferred + bytesToCopy < _dataToTransfer.Length);

            //Add the bytes to the payload
            blockPUTReq.Payload = new CoAPPayload(blockToSend);
            //Now add block option to the request
            blockPUTReq.SetBlockOption(CoAPHeaderOption.BLOCK1, new CoAPBlockOption(_blockSeqNo, hasMore, CoAPBlockOption.BLOCK_SIZE_16));
            //send
            coapClient.Send(blockPUTReq);
            //Updated bytes transferred
            _totalBytesTransferred += bytesToCopy;
        }