/// <summary> /// Called when a CoAP request is received...we will only support CON requests /// of type GET... the path is sensors/temp /// </summary> /// <param name="coapReq">CoAPRequest object</param> private static void OnCoAPRequestReceived(CoAPRequest coapReq) { string reqPath = (coapReq.GetPath() != null) ? coapReq.GetPath().ToLower() : ""; if (coapReq.MessageType.Value == CoAPMessageType.CON) { if (coapReq.Code.Value != CoAPMessageCode.GET) { CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.METHOD_NOT_ALLOWED, coapReq /*Copy all necessary values from request in the response*/); //When you use the constructor that accepts a request, then automatically //the message id , token and remote sender values are copied over to the response coapServer.Send(resp); } else if (reqPath != "sensors/temp") { //We do not understand this.. CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.NOT_FOUND, coapReq /*Copy all necessary values from request in the response*/); coapServer.Send(resp); } else { Debug.WriteLine(coapReq.ToString()); CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.CONTENT, coapReq /*Copy all necessary values from request in the response*/); //The payload will be JSON Hashtable ht = new Hashtable(); ht.Add("temp", GetRoomTemperature()); string jsonStr = JSONResult.ToJSON(ht); resp.AddPayload(jsonStr); //Tell recipient about the content-type of the response resp.AddOption(CoAPHeaderOption.CONTENT_FORMAT, AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_JSON)); //send it coapServer.Send(resp); } } }
/// <summary> /// Called when a CoAP request is received (NON, CON) /// </summary> /// <param name="coapReq">CoAPRequest object</param> private static void OnCoAPRequestReceived(CoAPRequest coapReq) { string reqPath = (coapReq.GetPath() != null) ? coapReq.GetPath().ToLower() : ""; /* * For well-know path, we should support both CON and NON. * For NON request, we send back the details in another NON message * For CON request, we send back the details in an ACK */ /*Well known should be a GET*/ if (coapReq.Code.Value != CoAPMessageCode.GET) { if (coapReq.MessageType.Value == CoAPMessageType.CON) { CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.METHOD_NOT_ALLOWED, coapReq /*Copy all necessary values from request in the response*/); //When you use the constructor that accepts a request, then automatically //the message id , token and remote sender values are copied over to the response coapServer.Send(resp); } else { //For NON, we can only send back a RST CoAPResponse resp = new CoAPResponse(CoAPMessageType.RST, CoAPMessageCode.METHOD_NOT_ALLOWED, coapReq /*Copy all necessary values from request in the response*/); //When you use the constructor that accepts a request, then automatically //the message id , token and remote sender values are copied over to the response coapServer.Send(resp); } } else { //Message type is GET...check the path..this server only supports well-known path if (reqPath != ".well-known/core") { if (coapReq.MessageType.Value == CoAPMessageType.CON) { CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.NOT_FOUND, coapReq /*Copy all necessary values from request in the response*/); coapServer.Send(resp); } else { //For NON, we can only send back a RST CoAPResponse resp = new CoAPResponse(CoAPMessageType.RST, CoAPMessageCode.NOT_FOUND, coapReq /*Copy all necessary values from request in the response*/); coapServer.Send(resp); } } else { //Request is GET and path is right if (coapReq.MessageType.Value == CoAPMessageType.CON) { CoAPResponse resp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.CONTENT, coapReq /*Copy all necessary values from request in the response*/); //Add response payload resp.AddPayload(GetSupportedResourceDescriptions()); //Tell recipient about the content-type of the response resp.AddOption(CoAPHeaderOption.CONTENT_FORMAT, AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_LINK_FORMAT)); coapServer.Send(resp); } else { //Its a NON, send a NON back...in CoAPSharp, NON is always considered as request CoAPResponse resp = new CoAPResponse(CoAPMessageType.NON, CoAPMessageCode.CONTENT, coapReq.ID.Value); //Copy over other needed values from the reqeust resp.Token = coapReq.Token; resp.RemoteSender = coapReq.RemoteSender; resp.AddPayload(GetSupportedResourceDescriptions()); //Tell recipient about the content-type of the response resp.AddOption(CoAPHeaderOption.CONTENT_FORMAT, AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_LINK_FORMAT)); //send it coapServer.Send(resp); } } } }