com.nasa.ESWorkerVerticle.java Source code

Java tutorial

Introduction

Here is the source code for com.nasa.ESWorkerVerticle.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nasa;

import com.google.gson.Gson;
import com.nasa.dto.Feedback;
import com.nasa.dto.Pollution;
import com.nasa.dto.Weather;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.eventbus.EventBus;
import io.vertx.core.json.Json;
import io.vertx.core.json.JsonArray;
import io.vertx.core.json.JsonObject;

public class ESWorkerVerticle extends AbstractVerticle {

    ElasticSearchAdapter es = new ElasticSearchAdapter();

    @Override
    public void start() throws Exception {

        EventBus eb = vertx.eventBus();

        eb.consumer("bus.symptoms").handler(message -> {

            String text = ((JsonObject) message.body()).getString("text");

            JsonObject result = null;

            try {
                if (text != null) {
                    result = es.getSymptoms(text);
                }
            } catch (Exception ex) {
                result = new JsonObject();
                result.put("symptoms", new JsonArray());
            }

            message.reply(result);
        });

        eb.consumer("bus.conditions").handler(message -> {

            String text = ((JsonObject) message.body()).getString("text");

            JsonObject result = null;

            try {
                if (text != null) {
                    result = es.getConditions(text);
                }
            } catch (Exception ex) {
                result = new JsonObject();
                result.put("conditions", new JsonArray());
            }

            message.reply(result);
        });

        eb.consumer("bus.map.weather").handler(message -> {

            String lat = ((JsonObject) message.body()).getString("lat");
            String lon = ((JsonObject) message.body()).getString("lon");
            String zoom = ((JsonObject) message.body()).getString("zoom");
            String count = ((JsonObject) message.body()).getString("count");

            JsonObject result = null;

            try {
                if (lat != null && lon != null && zoom != null) {
                    result = Transformer.weatherToMap(es.getMap(lat, lon, zoom, count));
                } else {
                    throw new IllegalArgumentException("incorrect weather params: " + message.body().toString());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                result = new JsonObject();
                result.put("type", "FeatureCollection");
                result.put("features", new JsonArray());
            }

            message.reply(result);
        });

        eb.consumer("bus.map.pollution").handler(message -> {

            String lat = ((JsonObject) message.body()).getString("lat");
            String lon = ((JsonObject) message.body()).getString("lon");
            String zoom = ((JsonObject) message.body()).getString("zoom");
            String count = ((JsonObject) message.body()).getString("count");

            System.out.println("Worker: " + message.body());

            JsonObject result = null;

            try {
                if (lat != null && lon != null && zoom != null) {
                    result = Transformer.pollutionToMap(es.getMap(lat, lon, zoom, count));
                } else {
                    throw new IllegalArgumentException("incorrect pollution params: " + message.body().toString());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                result = new JsonObject();
                result.put("type", "FeatureCollection");
                result.put("features", new JsonArray());
            }

            message.reply(result);
        });

        eb.consumer("bus.map.condition").handler(message -> {

            String lat = ((JsonObject) message.body()).getString("lat");
            String lon = ((JsonObject) message.body()).getString("lon");
            String zoom = ((JsonObject) message.body()).getString("zoom");
            String count = ((JsonObject) message.body()).getString("count");

            JsonObject result = null;

            try {
                if (lat != null && lon != null && zoom != null) {
                    result = Transformer.conditionToMap(es.getMap(lat, lon, zoom, count));
                } else {
                    throw new IllegalArgumentException("incorrect condition params: " + message.body().toString());
                }
            } catch (Exception ex) {
                ex.printStackTrace();
                result = new JsonObject();
                result.put("type", "FeatureCollection");
                result.put("features", new JsonArray());
            }

            message.reply(result);
        });

        eb.consumer("bus.check").handler(message -> {

            Float lat = Float.parseFloat(((JsonObject) message.body()).getString("lat"));
            Float lon = Float.parseFloat(((JsonObject) message.body()).getString("lon"));

            JsonObject result = new JsonObject();

            Gson gson = new Gson();

            try {
                JsonArray matchesArr = new JsonArray();

                if (lat != null && lon != null) {
                    matchesArr = es.getCheck(lat, lon);
                    result.put("conditions", matchesArr);
                }

                OtherOmwClient owm = new OtherOmwClient();
                BreezoMeterClient bm = new BreezoMeterClient();

                JsonObject weatherJ = owm.currentWeather(lat, lon);
                JsonObject pollutionJ = bm.currentAirQualityAtPoint(lat, lon);

                Weather weather = owm.toWeather(weatherJ);
                if (weather != null) {
                    result.put("weather", new JsonObject(gson.toJson(weather)));
                }

                Pollution pollution = bm.toPollution(pollutionJ);
                if (pollution != null) {
                    result.put("pollution", new JsonObject(gson.toJson(pollution)));
                }

            } catch (Exception ex) {
                result = new JsonObject();
                result.put("conditions", new JsonArray());
                result.put("weather", new JsonObject());
                result.put("pollution", new JsonObject());
            }

            message.reply(result);
        });

        eb.consumer("bus.feedback").handler(message -> {

            JsonObject userFeedback = (JsonObject) message.body();

            JsonObject result = new JsonObject();

            try {
                Gson gson = new Gson();
                Feedback feedback = gson.fromJson(userFeedback.toString(), Feedback.class);

                float lat = feedback.getLocation().getLat();
                float lon = feedback.getLocation().getLon();

                OtherOmwClient owm = new OtherOmwClient();
                JsonObject owmData = owm.currentWeather(lat, lon);
                feedback.setWeather(owm.toWeather(owmData));

                BreezoMeterClient bm = new BreezoMeterClient();
                JsonObject bmData = bm.currentAirQualityAtPoint(lat, lon);
                feedback.setPollution(bm.toPollution(bmData));

                result = es.postFeedback(new JsonObject(gson.toJson(feedback)));
            } catch (Exception ex) {
                ex.printStackTrace();
                result.put("success", false);
            }

            message.reply(result);
        });
    }
}