Search in sources :

Example 41 with DecodeException

use of io.vertx.core.json.DecodeException in project okapi by folio-org.

the class OkapiToken method getPayload.

private JsonObject getPayload() {
    String encodedJson;
    try {
        encodedJson = this.token.split("\\.")[1];
    } catch (ArrayIndexOutOfBoundsException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
    String decodedJson = new String(Base64.getDecoder().decode(encodedJson));
    JsonObject j;
    try {
        j = new JsonObject(decodedJson);
    } catch (DecodeException e) {
        throw new IllegalArgumentException(e.getMessage());
    }
    return j;
}
Also used : JsonObject(io.vertx.core.json.JsonObject) DecodeException(io.vertx.core.json.DecodeException)

Example 42 with DecodeException

use of io.vertx.core.json.DecodeException in project enmasse-workshop by EnMasseProject.

the class HeatingDevice method connected.

private void connected(AsyncResult<Client> done) {
    if (done.succeeded()) {
        log.info("Connected to the service");
        Client client = done.result();
        client.receivedHandler(messageDelivery -> {
            try {
                JsonObject json = new JsonObject(Buffer.buffer(messageDelivery.message()));
                log.info("Received message on {} with payload {}", messageDelivery.address(), json);
                String deviceId = json.getString("device-id");
                if (!deviceId.equals(this.config.getProperty(DeviceConfig.DEVICE_ID))) {
                    log.error("Received control message for some other device with id {}", deviceId);
                } else {
                    String operation = json.getString("operation");
                    if ("open".equals(json)) {
                        valve.open();
                    } else if ("close".equals(operation)) {
                        valve.close();
                    }
                }
            } catch (DecodeException e) {
                log.error("Error decoding message, discarded !", e);
            }
        });
        String controlAddress = this.config.getProperty(DeviceConfig.CONTROL_ADDRESS);
        log.info("Registering to receive on {}", controlAddress);
        client.receive(controlAddress);
        int updateInterval = Integer.valueOf(this.config.getProperty(DeviceConfig.UPDATE_INTERVAL));
        String temperatureAddress = this.config.getProperty(DeviceConfig.TEMPERATURE_ADDRESS);
        this.vertx.setPeriodic(updateInterval, t -> {
            int temperature = this.dht22.getTemperature();
            JsonObject json = new JsonObject();
            json.put("device-id", this.config.getProperty(DeviceConfig.DEVICE_ID));
            json.put("temperature", temperature);
            log.info("Sending temperature value = {} ...", temperature);
            client.send(temperatureAddress, json.toString().getBytes(), v -> {
                log.info("... sent {}", v);
            });
        });
    } else {
        log.error("Error connecting to the service", done.cause());
    }
}
Also used : JsonObject(io.vertx.core.json.JsonObject) AmqpClient(io.enmasse.iot.transport.AmqpClient) MqttClient(io.enmasse.iot.transport.MqttClient) Client(io.enmasse.iot.transport.Client) DecodeException(io.vertx.core.json.DecodeException)

Aggregations

DecodeException (io.vertx.core.json.DecodeException)42 JsonObject (io.vertx.core.json.JsonObject)17 Failure (org.folio.okapi.common.Failure)13 Test (org.junit.Test)8 File (java.io.File)6 DeploymentDescriptor (org.folio.okapi.bean.DeploymentDescriptor)6 TenantModuleDescriptor (org.folio.okapi.bean.TenantModuleDescriptor)6 JsonArray (io.vertx.core.json.JsonArray)4 ArrayList (java.util.ArrayList)4 ModuleDescriptor (org.folio.okapi.bean.ModuleDescriptor)4 Buffer (io.vertx.rxjava.core.buffer.Buffer)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 Tuple2 (io.vavr.Tuple2)2 Vertx (io.vertx.core.Vertx)2 Buffer (io.vertx.core.buffer.Buffer)2 FileNotFoundException (java.io.FileNotFoundException)2 ByteBuffer (java.nio.ByteBuffer)2 Scanner (java.util.Scanner)2 AmqpClient (io.enmasse.iot.transport.AmqpClient)1