Java Code Examples for org.eclipse.californium.core.server.resources.CoapExchange#respond()
The following examples show how to use
org.eclipse.californium.core.server.resources.CoapExchange#respond() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CoapReceiverResource.java From datacollector with Apache License 2.0 | 6 votes |
public void handle(CoapExchange exchange) { if (shuttingDown) { LOG.debug("Shutting down, discarding incoming request from '{}'", exchange.getSourceAddress()); exchange.respond(CoAP.ResponseCode.SERVICE_UNAVAILABLE); } else { long start = System.currentTimeMillis(); LOG.debug("Request accepted from '{}'", exchange.getSourceAddress()); try { if (receiver.process(exchange.getRequestPayload())) { exchange.respond(CoAP.ResponseCode.VALID); exchange.accept(); requestMeter.mark(); } else { exchange.respond(CoAP.ResponseCode.INTERNAL_SERVER_ERROR); exchange.accept(); errorRequestMeter.mark(); } } catch (IOException ex) { exchange.reject(); errorQueue.offer(ex); errorRequestMeter.mark(); } finally { requestTimer.update(System.currentTimeMillis() - start, TimeUnit.MILLISECONDS); } } }
Example 2
Source File: CoapTransportResource.java From iotplatform with Apache License 2.0 | 6 votes |
@Override public void handlePOST(CoapExchange exchange) { Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest()); if (!featureType.isPresent()) { log.trace("Missing feature type parameter"); exchange.respond(ResponseCode.BAD_REQUEST); } else { switch (featureType.get()) { case ATTRIBUTES: processRequest(exchange, MsgType.POST_ATTRIBUTES_REQUEST); break; case TELEMETRY: processRequest(exchange, MsgType.POST_TELEMETRY_REQUEST); break; case RPC: Optional<Integer> requestId = getRequestId(exchange.advanced().getRequest()); if (requestId.isPresent()) { processRequest(exchange, MsgType.TO_DEVICE_RPC_RESPONSE); } else { processRequest(exchange, MsgType.TO_SERVER_RPC_REQUEST); } break; } } }
Example 3
Source File: CoapTransportResource.java From Groza with Apache License 2.0 | 6 votes |
@Override public void handlePOST(CoapExchange exchange) { Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest()); if (!featureType.isPresent()) { log.trace("Missing feature type parameter"); exchange.respond(CoAP.ResponseCode.BAD_REQUEST); } else { switch (featureType.get()) { case ATTRIBUTES: processRequest(exchange, SessionMsgType.POST_ATTRIBUTES_REQUEST); break; case TELEMETRY: processRequest(exchange, SessionMsgType.POST_TELEMETRY_REQUEST); break; case RPC: Optional<Integer> requestId = getRequestId(exchange.advanced().getRequest()); if (requestId.isPresent()) { processRequest(exchange, SessionMsgType.TO_DEVICE_RPC_RESPONSE); } else { processRequest(exchange, SessionMsgType.TO_SERVER_RPC_REQUEST); } break; } } }
Example 4
Source File: BatteryDriver.java From SoftwarePilot with MIT License | 5 votes |
@Override public void handlePUT(CoapExchange ce) { // Split on & and = then on ' ' String outLine = ""; byte[] payload = ce.getRequestPayload(); String inputLine = new String(payload); int AUAVsim = 0; if (inputLine.contains("dp=AUAVsim")) { AUAVsim = 1; } String[] args = inputLine.split("-");//??? switch (args[0]) { case "dc=help": ce.respond(getUsageInfo()); break; case "dc=qry": String qry = args[1].substring(3); ce.respond(queryH2(qry)); break; case "dc=dji": System.out.println("Battery Value is: " + djiLastReading); System.out.println("Battery MAH is: " + djiLastMAH); System.out.println("Battery Current is: "+djiCurrent); System.out.println("Battery Voltage is: "+djiVoltage); ce.respond("Percent=" + djiLastReading+", MAH="+djiLastMAH); case "dc=lcl": if (AUAVsim == 1) { lclLastReading--; addReadingH2(lclLastReading, "lcl"); ce.respond("Battery: " + Integer.toString(lclLastReading)); break; } try { Class<?> c = Class.forName("android.app.ActivityThread"); android.app.Application app = (android.app.Application) c.getDeclaredMethod("currentApplication").invoke(null); android.content.Context context = app.getApplicationContext(); BatteryManager bm = (BatteryManager)context.getSystemService("batterymanager"); int batLevel = bm.getIntProperty(4); lclLastReading = batLevel; addReadingH2(batLevel, "lcl"); ce.respond("Battery: " + Integer.toString(batLevel)); } catch (Exception e) { ce.respond("Battery: Error"); } break; case "dc=cfg": if(AUAVsim != 1){ initBatteryCallback(); } ce.respond("Battery: Configured"); default: ce.respond("Error: BatteryDriver unknown command\n"); } }
Example 5
Source File: HCoapServer.java From SI with BSD 2-Clause "Simplified" License | 5 votes |
private void receiveRequest(CoapExchange exchange) { if(listener != null) { listener.receiveCoapRequest(exchange); } else { exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, "Not found listener"); } }
Example 6
Source File: InCse.java From SI with BSD 2-Clause "Simplified" License | 5 votes |
private boolean sendCoapResponse(OneM2mResponse resMessage, CoapExchange exchange) { if (exchange == null) { return false; } coapMap.remove(resMessage.getRequestIdentifier()); try { Response response = CoapResponseCodec.encode(resMessage, exchange); log.debug("<< SEND CoAP MESSAGE:"); log.debug(response.toString()); log.debug(response.getPayloadString()); exchange.respond(response); // added in 2017-10-31 to support CSE-relative Unstructured addressing //System.out.println("############### uripath-size=" + exchange.getRequestOptions().getUriPath().size()); //System.out.println("############### resMessage.getResponseStatusCode()=" + resMessage.getResponseStatusCode()); ///System.out.println("############### resourceId=" + ((Resource)resMessage.getContentObject()).getResourceID()); int len = exchange.getRequestOptions().getUriPath().size(); int resCode = resMessage.getResponseStatusCode(); if(resCode == 2001 && len == 1) { String resourceId = ((Resource)resMessage.getContentObject()).getResourceID(); coapServer.add(coapServer.new HCoapResource(resourceId)); } //coapServer.add(resources) return true; } catch (Exception e) { e.printStackTrace(); exchange.respond(ResponseCode.INTERNAL_SERVER_ERROR, "Respose encoding failed"); } return false; }
Example 7
Source File: WritableResource.java From IOT-Technical-Guide with Apache License 2.0 | 5 votes |
@Override public void handlePUT(CoapExchange exchange) { byte[] payload = exchange.getRequestPayload(); try { value = new String(payload, "UTF-8"); exchange.respond(CHANGED, value); } catch (Exception e) { e.printStackTrace(); exchange.respond(BAD_REQUEST, "Invalid String"); } }
Example 8
Source File: CoapTransportResource.java From iotplatform with Apache License 2.0 | 5 votes |
@Override public void handleGET(CoapExchange exchange) { Optional<FeatureType> featureType = getFeatureType(exchange.advanced().getRequest()); if (!featureType.isPresent()) { log.trace("Missing feature type parameter"); exchange.respond(ResponseCode.BAD_REQUEST); } else if (featureType.get() == FeatureType.TELEMETRY) { log.trace("Can't fetch/subscribe to timeseries updates"); exchange.respond(ResponseCode.BAD_REQUEST); } else if (exchange.getRequestOptions().hasObserve()) { boolean unsubscribe = exchange.getRequestOptions().getObserve() == 1; MsgType msgType; if (featureType.get() == FeatureType.RPC) { msgType = unsubscribe ? MsgType.UNSUBSCRIBE_RPC_COMMANDS_REQUEST : MsgType.SUBSCRIBE_RPC_COMMANDS_REQUEST; } else { msgType = unsubscribe ? MsgType.UNSUBSCRIBE_ATTRIBUTES_REQUEST : MsgType.SUBSCRIBE_ATTRIBUTES_REQUEST; } Optional<SessionId> sessionId = processRequest(exchange, msgType); if (sessionId.isPresent()) { if (exchange.getRequestOptions().getObserve() == 1) { exchange.respond(ResponseCode.VALID); } } } else if (featureType.get() == FeatureType.ATTRIBUTES) { processRequest(exchange, MsgType.GET_ATTRIBUTES_REQUEST); } else { log.trace("Invalid feature type parameter"); exchange.respond(ResponseCode.BAD_REQUEST); } }
Example 9
Source File: LocationDriver.java From SoftwarePilot with MIT License | 5 votes |
@Override public void handlePUT(CoapExchange ce) { // Split on & and = then on ' ' String outLine = ""; byte[] payload = ce.getRequestPayload(); String inputLine = new String(payload); int AUAVsim = 0; if (inputLine.contains("dp=AUAVsim")) { AUAVsim = 1; } String[] args = inputLine.split("-");//??? switch (args[0]) { case "dc=help": ce.respond(getUsageInfo()); break; case "dc=qry": String qry = args[1].substring(3); ce.respond("Location: "+queryH2(qry)); break; case "dc=tme": ce.respond("Location: time="+getStartTime()); break; case "dc=set": String Xpos = args[1].substring(3); String Ypos = args[2].substring(3); String Zpos = args[3].substring(3); addPositionH2(Xpos,Ypos,Zpos); Xcur = Xpos;Ycur=Ypos; Zcur=Zpos; ce.respond("Location: set"); break; case "dc=get": ce.respond("Location: x="+Xcur+"-y="+Ycur+"-z="+Zcur); break; default: ce.respond("Error: LocationDriver unknown command\n"); } }
Example 10
Source File: HelloWorld2.java From hands-on-coap with Eclipse Public License 1.0 | 4 votes |
@Override public void handleGET(CoapExchange exchange) { exchange.respond(String.valueOf(System.currentTimeMillis())); }
Example 11
Source File: CoapServer.java From SI with BSD 2-Clause "Simplified" License | 4 votes |
@Override public void handleGET(CoapExchange exchange) { exchange.respond(ResponseCode.CONTENT, msg); }
Example 12
Source File: TimeResource.java From IOT-Technical-Guide with Apache License 2.0 | 4 votes |
@Override public void handleGET(CoapExchange exchange) { exchange.respond(String.valueOf(System.currentTimeMillis())); }
Example 13
Source File: HelloWorld1.java From hands-on-coap with Eclipse Public License 1.0 | 4 votes |
@Override public void handleGET(CoapExchange exchange) { exchange.respond("Hello world!"); }
Example 14
Source File: HelloWorld2.java From hands-on-coap with Eclipse Public License 1.0 | 4 votes |
@Override public void handleGET(CoapExchange exchange) { exchange.respond("Fun with CoAP!"); }
Example 15
Source File: WritableResource.java From IOT-Technical-Guide with Apache License 2.0 | 4 votes |
@Override public void handleGET(CoapExchange exchange) { exchange.respond(value); }
Example 16
Source File: HelloResource.java From IOT-Technical-Guide with Apache License 2.0 | 4 votes |
@Override public void handleGET(CoapExchange exchange) { exchange.respond("Hello world!"); }
Example 17
Source File: CoapResource.java From SI with BSD 2-Clause "Simplified" License | 2 votes |
/** * Handles the GET request in the given CoAPExchange. By default it responds * with a 4.05 (Method Not Allowed). Override this method to respond * differently to GET requests. Possible response codes for GET requests are * Content (2.05) and Valid (2.03). * * @param exchange the CoapExchange for the simple API */ public void handleGET(CoapExchange exchange) { exchange.respond(ResponseCode.METHOD_NOT_ALLOWED); }
Example 18
Source File: CoapResource.java From SI with BSD 2-Clause "Simplified" License | 2 votes |
/** * Handles the POST request in the given CoAPExchange. By default it * responds with a 4.05 (Method Not Allowed). Override this method to * respond differently to POST requests. Possible response codes for POST * requests are Created (2.01), Changed (2.04), and Deleted (2.02). * * @param exchange the CoapExchange for the simple API */ public void handlePOST(CoapExchange exchange) { exchange.respond(ResponseCode.METHOD_NOT_ALLOWED); }
Example 19
Source File: CoapResource.java From SI with BSD 2-Clause "Simplified" License | 2 votes |
/** * Handles the PUT request in the given CoAPExchange. By default it responds * with a 4.05 (Method Not Allowed). Override this method to respond * differently to PUT requests. Possible response codes for PUT requests are * Created (2.01) and Changed (2.04). * * @param exchange the CoapExchange for the simple API */ public void handlePUT(CoapExchange exchange) { exchange.respond(ResponseCode.METHOD_NOT_ALLOWED); }
Example 20
Source File: CoapResource.java From SI with BSD 2-Clause "Simplified" License | 2 votes |
/** * Handles the PUT request in the given CoAPExchange. By default it responds * with a 4.05 (Method Not Allowed). Override this method to respond * differently to PUT requests. Possible response codes for PUT requests are * Created (2.01) and Changed (2.04). * * @param exchange the CoapExchange for the simple API */ public void handlePUT(CoapExchange exchange) { exchange.respond(ResponseCode.METHOD_NOT_ALLOWED); }