Search in sources :

Example 1 with Client

use of io.enmasse.iot.transport.Client 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

AmqpClient (io.enmasse.iot.transport.AmqpClient)1 Client (io.enmasse.iot.transport.Client)1 MqttClient (io.enmasse.iot.transport.MqttClient)1 DecodeException (io.vertx.core.json.DecodeException)1 JsonObject (io.vertx.core.json.JsonObject)1