Choosing the desired representation

As in HTTP, CoAP supports RESTful interfaces. This means that the same resource can be represented in different ways, based on the preferences of the client. We will create a momentary resource that returns all momentary values when called. While the previous resources only support plain text responses, the momentary resource will support XML and JSON as well. The actual representation will be chosen based on the Accept header provided by the client. If no Accept header is provided, plain text will be returned by default:

this.momentaryResource = this.coapEndpoint.Register("/Momentary", 
   (req, resp) => 
   { 
         if (req.IsAcceptable(Xml.ContentFormatCode)) 
               this.ReturnMomentaryAsXml(req, resp); 
         else if (req.IsAcceptable(Json.ContentFormatCode)) 
               this.ReturnMomentaryAsJson(req, resp); 
         else if (req.IsAcceptable(PlainText.ContentFormatCode)) 
               this.ReturnMomentaryAsPlainText(req, resp); 
         else if (req.Accept.HasValue) 
               throw new CoapException(CoapCode.NotAcceptable); 
         else 
               this.ReturnMomentaryAsPlainText(req, resp); 
   }, Notifications.Acknowledged, "Momentary values.", 
   null, null,new int[] { Xml.ContentFormatCode, 
   Json.ContentFormatCode, PlainText.ContentFormatCode }); 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset