org.eclipse.californium.core.CoapObserveRelation Java Examples

The following examples show how to use org.eclipse.californium.core.CoapObserveRelation. 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
@Override
public void connect() {

    connectBridge();

    try {
        String response = requestGetCOAP(TradfriConstants.DEVICES);
        JsonArray devices = gsonparser.parse(response).getAsJsonArray();
        for (JsonElement d : devices) {
            watching.add(subscribeCOAP(TradfriConstants.DEVICES + "/" + d.getAsInt()));
        }
        response = requestGetCOAP(TradfriConstants.GROUPS);
        JsonArray groups = gsonparser.parse(response).getAsJsonArray();
        for (JsonElement g : groups) {
            watching.add(subscribeCOAP(TradfriConstants.GROUPS + "/" + g.getAsInt()));
        }
    } catch (TradfriException | JsonParseException ex) {
        throw new RuntimeException(ex.getLocalizedMessage(), ex);
    }

    registrations = CompletableAsync.scheduleTask(120000, 120000, () -> {
        watching.forEach(CoapObserveRelation::reregister);
    });
}
 
Example #2
Source File: ManagerTradfri.java    From helloiot with GNU General Public License v3.0 6 votes vote down vote up
private CoapObserveRelation subscribeCOAP(String topic) throws TradfriException {

        try {
            URI uri = new URI("coaps://" + coapIP + "/" + topic);

            CoapClient client = new CoapClient(uri);
            client.setEndpoint(coapEndPoint);
            CoapHandler handler = new CoapHandler() {
                @Override
                public void onLoad(CoapResponse response) {
                    receivedCOAP(topic, response.getResponseText());
                }

                @Override
                public void onError() {
                    LOGGER.log(Level.WARNING, "COAP subscription error");
                }
            };
            return client.observe(handler);
        } catch (URISyntaxException ex) {
            LOGGER.log(Level.WARNING, "COAP SEND error: {0}", ex.getMessage());
            throw new TradfriException(ex);
        }
    }
 
Example #3
Source File: TradfriCoapClient.java    From smarthome with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Starts observation of the resource and uses the given callback to provide updates.
 *
 * @param callback the callback to use for updates
 */
public CoapObserveRelation startObserve(CoapCallback callback) {
    return observe(new TradfriCoapHandler(callback));
}