@Override public void deliver(IDeviceNestingContext nested, IDeviceAssignment assignment, IDeviceCommandExecution execution, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; LOGGER.info(parameters.toString()); LOGGER.info("Delivering command as " + parameters.getMethod().toUpperCase() + " to " + client.getURI()); if ("put".equalsIgnoreCase(parameters.getMethod())) { response = client.put(encoded, MediaTypeRegistry.APPLICATION_JSON); } else { response = client.post(encoded, MediaTypeRegistry.APPLICATION_JSON); } if (response != null) { LOGGER.info("Response from delivering command: " + response.getResponseText()); } else { LOGGER.info("No response from delivering command."); } }
@Override public void deliverSystemCommand(IDeviceNestingContext nested, IDeviceAssignment assignment, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; LOGGER.info(parameters.toString()); LOGGER.info("Delivering system command as " + parameters.getMethod().toUpperCase() + " to " + client.getURI()); if ("put".equalsIgnoreCase(parameters.getMethod())) { response = client.put(encoded, MediaTypeRegistry.APPLICATION_JSON); } else { response = client.post(encoded, MediaTypeRegistry.APPLICATION_JSON); } if (response != null) { LOGGER.info("Response from delivering system command: " + response.getResponseText()); } else { LOGGER.info("No response from delivering system command."); } }
@Override public Serializable doSend(Message message, MeasurementUnit measurementUnit) throws Exception { switch (method) { case GET: coapResponse = client.get(); break; case POST: coapResponse = client.post(message.getPayload().toString(), MediaTypeRegistry.TEXT_PLAIN); break; case PUT: coapResponse = client.put(message.getPayload().toString(), MediaTypeRegistry.TEXT_PLAIN); break; case DELETE: coapResponse = client.delete(); break; } return coapResponse.getResponseText(); }
case CoAPConstants.METHOD_POST: byte[] bodyPost = exchange.getIn().getBody(byte[].class); response = client.post(bodyPost, mediaType); break; case CoAPConstants.METHOD_PUT:
private static void testBlockwise(final CoapClient client, final Mode mode) { LOGGER.log(Level.FINE, "Test blockwise POST to {0}", client.getURI()); currentRequestText = LONG_REQUEST; String currentResponseText = LONG_RESPONSE; resource.setResponse(currentResponseText, mode); CoapResponse response = client.post(currentRequestText, MediaTypeRegistry.TEXT_PLAIN); assertThatResponseContainsValue(response, currentResponseText); }
/** Send Silectica rule as an ack (payload only version) */ @Test public void sendSilecticaRuleAckPayloadOnly() throws Exception { CoapClient client = createClientFor("devices/00173B1200210024/acks"); String rule = "{\"rule\":{\"r\":0,\"n\":\"netup\",\"v\":\"Networking Up\",\"s\":\"dis\",\"rc\":0,\"c\":[{\"n\":\"s/sys/net/ucup\",\"v\":1,\"r\":\"eq\",\"l\":\"or\",\"dt\":1}]},\"ruleQty\":6,\"OEId\":\"manualtest\"}"; handleResponse(client.post(rule.getBytes(), MediaTypeRegistry.APPLICATION_JSON)); }
System.out.println("---------------\nReboot Server"); CoapClient clientStimulus = new CoapClient(uri + "/obs-reset"); clientStimulus.post("sesame", MediaTypeRegistry.TEXT_PLAIN); try { Thread.sleep((timeout+6)*1000); } catch (InterruptedException e) { } relation4.proactiveCancel();
@Test public void testAsyncPostUpdatesResource() throws Exception { final CountDownLatch latch = new CountDownLatch(1); // Change the content to "two" and check client.post(new TestHandler("Test 3") { @Override public void onLoad(CoapResponse response) { if (CONTENT_1.equals(response.getResponseText())) { latch.countDown(); } } }, CONTENT_2, MediaTypeRegistry.TEXT_PLAIN); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertThat(resource.getContent(), is(CONTENT_2)); }
response = client.post(getLargeRequestPayload(), MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode() + " - " + response.getOptions().getLocationString()); System.out.println(response.getResponseText()); response = client.post(getLargeRequestPayload(), MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode()); System.out.println(response.getResponseText());
response = client.post("non-empty", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); response = client.post("non-empty", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.advanced().getType() + "-" + response.getCode()); System.out.println(response.getResponseText()); response = client.post("TD_COAP_CORE_18", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode() + "-" + response.getOptions().getLocationString()); System.out.println(response.getResponseText()); response = client.post("query", MediaTypeRegistry.TEXT_PLAIN); System.out.println(response.getCode() + "-" + response.getOptions().getLocationString()); System.out.println(response.getResponseText());
@Override public void deliverSystemCommand(IDeviceNestingContext nested, IDeviceAssignment assignment, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; getLogger().debug(parameters.toString()); getLogger().debug( "Delivering system command as " + parameters.getMethod().toUpperCase() + " to " + client.getURI()); if ("put".equalsIgnoreCase(parameters.getMethod())) { response = client.put(encoded, MediaTypeRegistry.APPLICATION_JSON); } else { response = client.post(encoded, MediaTypeRegistry.APPLICATION_JSON); } if (response != null) { getLogger().info("Response from delivering system command: " + response.getResponseText()); } else { getLogger().info("No response from delivering system command."); } }
/** Send Silectica rule as an ack */ protected void sendSilecticaRuleAck(String rule) throws Exception { CoapClient client = createClientFor(getBaseDeviceUrl() + "/acks"); DeviceCommandResponseCreateRequest ack = new DeviceCommandResponseCreateRequest(); ack.setOriginatingEventId(UUID.randomUUID()); ack.setResponse(rule); handleResponse(client.post(MarshalUtils.marshalJson(ack), MediaTypeRegistry.APPLICATION_JSON)); }
@Override public void deliver(IDeviceNestingContext nested, IDeviceAssignment assignment, IDeviceCommandExecution execution, byte[] encoded, CoapParameters parameters) throws SiteWhereException { CoapClient client = createCoapClient(parameters); CoapResponse response = null; getLogger().debug(parameters.toString()); getLogger().debug("Delivering command as " + parameters.getMethod().toUpperCase() + " to " + client.getURI()); if ("put".equalsIgnoreCase(parameters.getMethod())) { response = client.put(encoded, MediaTypeRegistry.APPLICATION_JSON); } else { response = client.post(encoded, MediaTypeRegistry.APPLICATION_JSON); } if (response != null) { getLogger().info("Response from delivering command: " + response.getResponseText()); } else { getLogger().info("No response from delivering command."); } }
@Test public void testAddAcknowledgement() throws Exception { CoapClient client = createClientFor(getBaseDeviceUrl() + "/acks"); DeviceCommandResponseCreateRequest ack = new DeviceCommandResponseCreateRequest(); ack.setOriginatingEventId(UUID.randomUUID()); ack.setResponse("Arbitrary string containing response content."); ack.setMetadata(new HashMap<String, String>()); ack.getMetadata().put("meta1", "value"); handleResponse(client.post(MarshalUtils.marshalJson(ack), MediaTypeRegistry.APPLICATION_JSON)); }
@Test public void testRegisterDevice() throws Exception { CoapClient client = createClientFor(getBaseDeviceUrl()); DeviceRegistrationRequest registration = new DeviceRegistrationRequest(); registration.setDeviceTypeToken("mega2560"); registration.setAreaToken("peachtree"); Map<String, String> metadata = new HashMap<String, String>(); metadata.put("ipaddress", "localhost"); registration.setMetadata(metadata); handleResponse(client.post(MarshalUtils.marshalJson(registration), MediaTypeRegistry.APPLICATION_JSON)); }
@Test public void testAddDeviceMeasurements() throws Exception { CoapClient client = createClientFor(getBaseDeviceUrl() + "/measurements"); DeviceMeasurementCreateRequest mx = new DeviceMeasurementCreateRequest(); mx.setName("pwr"); mx.setValue(38.23); mx.setEventDate(new Date()); handleResponse(client.post(MarshalUtils.marshalJson(mx), MediaTypeRegistry.APPLICATION_JSON)); }
@Test public void testAddDeviceAlert() throws Exception { CoapClient client = createClientFor(getBaseDeviceUrl() + "/alerts"); DeviceAlertCreateRequest alert = new DeviceAlertCreateRequest(); alert.setType("alert.test"); alert.setMessage("Danger! Danger!"); handleResponse(client.post(MarshalUtils.marshalJson(alert), MediaTypeRegistry.APPLICATION_JSON)); }
@Test public void testAddDeviceLocation() throws Exception { CoapClient client = createClientFor(getBaseDeviceUrl() + "/locations"); DeviceLocationCreateRequest location = new DeviceLocationCreateRequest(); location.setLatitude(33.7490); location.setLongitude(-84.3880); location.setElevation(0.0); handleResponse(client.post(MarshalUtils.marshalJson(location), MediaTypeRegistry.APPLICATION_JSON)); }
String resp3 = client.post(CONTENT_2, MediaTypeRegistry.TEXT_PLAIN).getResponseText(); Assert.assertEquals(CONTENT_1, resp3); String resp5 = client.post(CONTENT_3, MediaTypeRegistry.TEXT_PLAIN).getResponseText(); Assert.assertEquals(CONTENT_2, resp5); String resp7 = client.post(CONTENT_4, MediaTypeRegistry.TEXT_PLAIN).getResponseText(); Assert.assertEquals(CONTENT_3, resp7);