Java Code Examples for org.eclipse.californium.core.CoapResponse#getResponseText()

The following examples show how to use org.eclipse.californium.core.CoapResponse#getResponseText() . 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: ManagerTradfri.java    From helloiot with GNU General Public License v3.0 6 votes vote down vote up
String requestPostCOAP(String topic, String payload) throws TradfriException {
    LOGGER.log(Level.FINE, "request POST COAP: {0}, {1}", new Object[]{topic, payload});
    try {
        URI uri = new URI("coaps://" + coapIP + "/" + topic);
        CoapClient client = new CoapClient(uri);
        client.setEndpoint(coapEndPoint);
        CoapResponse response = client.post(payload, MediaTypeRegistry.TEXT_PLAIN);
        if (response == null || !response.isSuccess()) {
            LOGGER.log(Level.WARNING, "COAP GET error: Response error.");
            throw new TradfriException("Connection to gateway failed. Check host and PSK.");
        }
        String result = response.getResponseText();
        client.shutdown();
        return result;
    } catch (URISyntaxException ex) {
        LOGGER.log(Level.WARNING, "COAP GET error: {0}", ex.getMessage());
        throw new TradfriException(ex);
    }
}
 
Example 2
Source File: ManagerTradfri.java    From helloiot with GNU General Public License v3.0 6 votes vote down vote up
String requestGetCOAP(String topic) throws TradfriException {
    LOGGER.log(Level.FINE, "requext GET COAP: {0}", new Object[]{topic});
    try {
        URI uri = new URI("coaps://" + coapIP + "/" + topic);
        CoapClient client = new CoapClient(uri);
        client.setEndpoint(coapEndPoint);
        CoapResponse response = client.get();
        if (response == null || !response.isSuccess()) {
            LOGGER.log(Level.WARNING, "COAP GET error: Response error.");
            throw new TradfriException("Connection to gateway failed. Check host and PSK.");
        }
        String result = response.getResponseText();
        client.shutdown();
        return result;
    } catch (URISyntaxException ex) {
        LOGGER.log(Level.WARNING, "COAP GET error: {0}", ex.getMessage());
        throw new TradfriException(ex);
    }
}
 
Example 3
Source File: TradfriCoapHandler.java    From smarthome with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void onLoad(CoapResponse response) {
    logger.debug("CoAP response\noptions: {}\npayload: {}", response.getOptions(), response.getResponseText());
    if (response.isSuccess()) {
        if (callback != null) {
            try {
                callback.onUpdate(parser.parse(response.getResponseText()));
                callback.setStatus(ThingStatus.ONLINE, ThingStatusDetail.NONE);
            } catch (JsonParseException e) {
                logger.warn("Observed value is no valid json: {}, {}", response.getResponseText(), e.getMessage());
            }
        }
        if (future != null) {
            String data = response.getResponseText();
            future.complete(data);
        }
    } else {
        logger.debug("CoAP error {}", response.getCode());
        if (callback != null) {
            callback.setStatus(ThingStatus.OFFLINE, ThingStatusDetail.COMMUNICATION_ERROR);
        }
        if (future != null) {
            future.completeExceptionally(new RuntimeException("Response " + response.getCode().toString()));
        }
    }
}
 
Example 4
Source File: AuavRoutines.java    From SoftwarePilot with MIT License 5 votes vote down vote up
AUAVHandler () {
		ch = new CoapHandler() {
						@Override public void onLoad(CoapResponse response) {
								resp = response.getResponseText();
								auavLock("continue");
						}
						@Override public void onError() {
								resp = "CoapHandler Error";
								System.out.println("RoutineLock: " +auavLock+"-FAILED");
								auavLock("continue");
						}};
}
 
Example 5
Source File: CaptureImageV2Driver.java    From SoftwarePilot with MIT License 5 votes vote down vote up
public String invokeDriver(String dn, String params, CoapHandler ch) {
	params = params + "-dp=" + "AUAVsim";
	if(this.d2p == null) {
		try {
			URI portStr = new URI("coap://127.0.0.1:5117/cr");
			CoapClient e = new CoapClient(portStr);
			CoapResponse client = e.put("dn=list", 0);
			String ls = client.getResponseText();
			this.d2p = new HashMap();
			String[] lines = ls.split("\n");

			for(int x = 0; x < lines.length; ++x) {
				String[] data = lines[x].split("-->");
				if(data.length == 2) {
					this.d2p.put(data[0].trim(), data[1].trim());
				}
			}
		} catch (Exception var12) {
			this.d2p = null;
			System.out.println("AUAVRoutine invokeDriver error");
			var12.printStackTrace();
			return "Invoke Error";
		}
	}

	if(this.d2p != null) {
		String var13 = (String)this.d2p.get(dn);
		if(var13 != null) {
			try {
				URI var14 = new URI("coap://127.0.0.1:" + var13 + "/cr");
				CoapClient var15 = new CoapClient(var14);
				var15.put(ch, params, 0);
				return "Success";
			} catch (Exception var11) {
				return "Unable to reach driver " + dn + "  at port: " + var13;
			}
		} else {
			return "Unable to find driver: " + dn;
		}
	} else {
		return "InvokeDriver: Unreachable code touched";
	}
}
 
Example 6
Source File: CaptureImageV2Driver.java    From SoftwarePilot with MIT License 4 votes vote down vote up
@Override public void onLoad(CoapResponse response) {
		resp = response.getResponseText();
		drvUnsetLock();
}
 
Example 7
Source File: CameraRoutine.java    From SoftwarePilot with MIT License 4 votes vote down vote up
@Override public void onLoad(CoapResponse response) {
		resp = response.getResponseText();
		rtnLock("barrier-1");
}
 
Example 8
Source File: LightBulb.java    From ThingML-Tradfri with Apache License 2.0 4 votes vote down vote up
protected void parseResponse(CoapResponse response) {
	boolean updateListeners = false;
               gateway.getLogger().log(Level.INFO, response.getResponseText());
	try {
		JSONObject json = new JSONObject(response.getResponseText());
		jsonObject = json;
                       String new_name = json.getString(TradfriConstants.NAME);
                       if (name == null || !name.equals(new_name)) updateListeners = true;
		name = new_name;
                       
                       dateInstalled = new Date(json.getLong(TradfriConstants.DATE_INSTALLED)*1000);
                       dateLastSeen = new Date(json.getLong(TradfriConstants.DATE_LAST_SEEN)*1000);
                       
                       boolean new_online = json.getInt(TradfriConstants.DEVICE_REACHABLE) != 0;
                       if (new_online != online) updateListeners = true;
                       online = new_online;
		
                       manufacturer = json.getJSONObject("3").getString("0");
		type = json.getJSONObject("3").getString("1");
                       firmware = json.getJSONObject("3").getString("3");
                       
		JSONObject light = json.getJSONArray(TradfriConstants.LIGHT).getJSONObject(0);
		
		if (light.has(TradfriConstants.ONOFF) && light.has(TradfriConstants.DIMMER)) {
			boolean new_on = (light.getInt(TradfriConstants.ONOFF) != 0);
			int new_intensity = light.getInt(TradfriConstants.DIMMER);
			if (on != new_on) updateListeners = true;
			if (intensity != new_intensity) updateListeners = true;
			on = new_on;
			intensity = new_intensity;
		}
		else {
			if (online) updateListeners = true;
			online = false;
		}
		if (light.has(TradfriConstants.COLOR)) {
			String new_color = light.getString(TradfriConstants.COLOR);
			if (color == null || !color.equals(new_color)) updateListeners = true;
			color = new_color;
		}
	} catch (JSONException e) {
		System.err.println("Cannot update bulb info: error parsing the response from the gateway.");
		e.printStackTrace();
	}
	if (updateListeners) {
		for (TradfriBulbListener l : listners) l.bulb_state_changed(this);
	}
}