List of usage examples for io.vertx.core.json JsonObject getDouble
public Double getDouble(String key)
From source file:com.baldmountain.depot.models.Product.java
License:Open Source License
public Product(JsonObject json) { super("products", json); title = json.getString("title"); description = json.getString("description"); imageUrl = json.getString("imageUrl"); if (json.containsKey("price")) { price = new BigDecimal(json.getDouble("price")).setScale(2, RoundingMode.CEILING); } else {//w w w . j ava 2 s. co m price = BigDecimal.ZERO.setScale(2, RoundingMode.CEILING); } }
From source file:com.funmix.service.TruckServiceImpl.java
@Override public Future<Optional<Truck>> insert(JsonObject json) { log.info(json.toString());/*from w ww . j a v a 2s. c o m*/ Future<Optional<Truck>> result = Future.future(); JsonArray params = new JsonArray(); //licenseplate,truck_type,tonnage,volume_length,,volume_width,volume_height,status params.add(json.getString("licenseplate")); params.add(json.getString("truck_type")); params.add(json.getDouble("tonnage")); params.add(json.getDouble("volume_length")); params.add(json.getDouble("volume_width")); params.add(json.getDouble("volume_height")); log.info(params.toString()); client.getConnection(connHandler(result, connection -> { connection.updateWithParams(SQL_INSERT, params, r -> { if (r.failed()) { result.fail(r.cause()); log.info(r.cause()); } else { UpdateResult urs = r.result(); params.clear(); params.add(urs.getKeys().getInteger(0)); log.info(urs.getKeys().getInteger(0)); connection.queryWithParams(SQL_QUERY, params, rs -> { if (rs.failed()) { result.fail(rs.cause()); log.info(rs.cause()); } else { List<JsonObject> list = rs.result().getRows(); if (list == null || list.isEmpty()) { result.complete(Optional.empty()); } else { result.complete(Optional.of(new Truck(list.get(0)))); } } }); } connection.close(); }); })); return result; }
From source file:com.github.ithildir.airbot.service.GeoServiceVertxProxyHandler.java
License:Apache License
public void handle(Message<JsonObject> msg) { try {// www. j a v a 2s. c om JsonObject json = msg.body(); String action = msg.headers().get("action"); if (action == null) { throw new IllegalStateException("action not specified"); } accessed(); switch (action) { case "getLocationByCoordinates": { service.getLocationByCoordinates( json.getValue("latitude") == null ? null : (json.getDouble("latitude").doubleValue()), json.getValue("longitude") == null ? null : (json.getDouble("longitude").doubleValue()), res -> { if (res.failed()) { if (res.cause() instanceof ServiceException) { msg.reply(res.cause()); } else { msg.reply(new ServiceException(-1, res.cause().getMessage())); } } else { msg.reply(res.result() == null ? null : res.result().toJson()); } }); break; } case "getLocationByQuery": { service.getLocationByQuery((java.lang.String) json.getValue("query"), res -> { if (res.failed()) { if (res.cause() instanceof ServiceException) { msg.reply(res.cause()); } else { msg.reply(new ServiceException(-1, res.cause().getMessage())); } } else { msg.reply(res.result() == null ? null : res.result().toJson()); } }); break; } default: { throw new IllegalStateException("Invalid action: " + action); } } } catch (Throwable t) { msg.reply(new ServiceException(500, t.getMessage())); throw t; } }
From source file:com.github.ithildir.airbot.service.impl.MapQuestGeoServiceImpl.java
License:Open Source License
private Location _getLocation(JsonObject jsonObject) { JsonArray resultsJsonArray = jsonObject.getJsonArray("results"); JsonObject resultJsonObject = resultsJsonArray.getJsonObject(0); JsonArray locationsJsonArray = resultJsonObject.getJsonArray("locations"); JsonObject locationJsonObject = locationsJsonArray.getJsonObject(0); JsonObject latLngJsonObject = locationJsonObject.getJsonObject("latLng"); double latitude = latLngJsonObject.getDouble("lat"); double longitude = latLngJsonObject.getDouble("lng"); String country = locationJsonObject.getString("adminArea1"); return new Location(latitude, longitude, country); }
From source file:com.github.ithildir.airbot.service.impl.WaqiMeasurementServiceImpl.java
License:Open Source License
private Measurement _getMeasurement(JsonObject jsonObject) { String status = jsonObject.getString("status"); if (!"ok".equals(status)) { _logger.warn("Unable to use response {0}", jsonObject); return null; }//from w ww.j a v a 2 s . c o m JsonObject dataJsonObject = jsonObject.getJsonObject("data"); JsonObject cityJsonObject = dataJsonObject.getJsonObject("city"); String city = cityJsonObject.getString("name"); JsonObject timeJsonObject = dataJsonObject.getJsonObject("time"); String dateTime = timeJsonObject.getString("s"); String dateTimeOffset = timeJsonObject.getString("tz"); String date = dateTime.substring(0, 10); String time = dateTime.substring(11); TemporalAccessor temporalAccessor = DateTimeFormatter.ISO_OFFSET_DATE_TIME .parse(date + "T" + time + dateTimeOffset); Instant instant = Instant.from(temporalAccessor); int aqi = dataJsonObject.getInteger("aqi"); String mainPollutant = dataJsonObject.getString("dominentpol"); Map<String, Double> values = new HashMap<>(); JsonObject valuesJsonObject = dataJsonObject.getJsonObject("iaqi"); for (String pollutant : valuesJsonObject.fieldNames()) { JsonObject pollutantJsonObject = valuesJsonObject.getJsonObject(pollutant); double value = pollutantJsonObject.getDouble("v"); values.put(pollutant, value); } return new Measurement(city, instant.toEpochMilli(), aqi, mainPollutant, values, null); }
From source file:com.github.ithildir.airbot.service.MeasurementServiceVertxProxyHandler.java
License:Apache License
public void handle(Message<JsonObject> msg) { try {/*from w w w. j ava 2 s.co m*/ JsonObject json = msg.body(); String action = msg.headers().get("action"); if (action == null) { throw new IllegalStateException("action not specified"); } accessed(); switch (action) { case "getMeasurement": { service.getMeasurement( json.getValue("latitude") == null ? null : (json.getDouble("latitude").doubleValue()), json.getValue("longitude") == null ? null : (json.getDouble("longitude").doubleValue()), res -> { if (res.failed()) { if (res.cause() instanceof ServiceException) { msg.reply(res.cause()); } else { msg.reply(new ServiceException(-1, res.cause().getMessage())); } } else { msg.reply(res.result() == null ? null : res.result().toJson()); } }); break; } case "getName": { service.getName(createHandler(msg)); break; } case "init": { service.init(createHandler(msg)); break; } default: { throw new IllegalStateException("Invalid action: " + action); } } } catch (Throwable t) { msg.reply(new ServiceException(500, t.getMessage())); throw t; } }
From source file:com.github.ithildir.airbot.service.UserServiceVertxProxyHandler.java
License:Apache License
public void handle(Message<JsonObject> msg) { try {/*from w w w .jav a 2 s .c o m*/ JsonObject json = msg.body(); String action = msg.headers().get("action"); if (action == null) { throw new IllegalStateException("action not specified"); } accessed(); switch (action) { case "getUserLocation": { service.getUserLocation((java.lang.String) json.getValue("userId"), res -> { if (res.failed()) { if (res.cause() instanceof ServiceException) { msg.reply(res.cause()); } else { msg.reply(new ServiceException(-1, res.cause().getMessage())); } } else { msg.reply(res.result() == null ? null : res.result().toJson()); } }); break; } case "updateUserLocation": { service.updateUserLocation((java.lang.String) json.getValue("userId"), json.getValue("latitude") == null ? null : (json.getDouble("latitude").doubleValue()), json.getValue("longitude") == null ? null : (json.getDouble("longitude").doubleValue()), (java.lang.String) json.getValue("country"), createHandler(msg)); break; } default: { throw new IllegalStateException("Invalid action: " + action); } } } catch (Throwable t) { msg.reply(new ServiceException(500, t.getMessage())); throw t; } }
From source file:com.github.jackygurui.vertxredissonrepository.repository.index.LongScoreResolver.java
License:Apache License
@Override public Double resolve(Object value, JsonObject root, String id, String fieldName, RedissonIndex index) { if (StringUtils.isBlank(index.scoreField())) { return ((Long) value).doubleValue(); } else {/*from w w w .j a v a 2 s .c o m*/ return root.getDouble(index.scoreField()); } }
From source file:hu.elte.ik.robotika.futar.vertx.backend.verticle.HTTPVerticle.java
@Override public void start() { init();/* www.j a va 2 s. co m*/ HttpServer http = vertx.createHttpServer(); Router router = Router.router(vertx); // Setup websocket connection handling router.route("/eventbus/*").handler(eventBusHandler()); router.route("/ws").handler(this::handleWebSocketConnection); // Handle robot position data router.route("/api/robotposition/:data").handler(this::handleRobotPositionData); // Setup http session auth handling router.route().handler(CookieHandler.create()); router.route().handler(BodyHandler.create()); router.route().handler(SessionHandler.create(LocalSessionStore.create(vertx))); // Simple auth service which uses a properties file for user/role info AuthProvider authProvider = ShiroAuth.create(vertx, ShiroAuthRealmType.PROPERTIES, new JsonObject()); // We need a user session handler too to make sure the user is stored in // the session between requests router.route().handler(UserSessionHandler.create(authProvider)); // Any requests to URI starting '/rest/' require login router.route("/rest/*").handler(SimpleAuthHandler.create(authProvider)); // Serve the static private pages from directory 'rest' // user/getAll TEST page router.route("/rest/user/getAll").handler(this::getAllUser); // Preload router.route("/rest/info").handler(this::getInfo); router.route("/loginhandler").handler(SimpleLoginHandler.create(authProvider)); router.route("/logout").handler(context -> { context.clearUser(); // Status OK context.response().setStatusCode(HttpStatus.SC_OK).end(); }); router.route().handler(StaticHandler.create().setWebRoot(webRoot)); http.websocketHandler(ws -> { String id = java.util.UUID.randomUUID().toString(); ws.handler(buffer -> { try { JsonObject response = new JsonObject(buffer.toString()); if (response.getString("action") != null && response.getString("action").equals("login.robot") && sockets.get(id) == null) { log.info("robot logged in:" + id); sockets.put(id, ws); JsonObject pb = new JsonObject(); pb.put("robotId", id); vertx.eventBus().publish("new.robot", Json.encode(pb)); } else if (response.getString("action") != null && response.getString("action").equals("found.bluetooth")) { log.info("found.bluetooth"); JsonObject pb = response.getJsonObject("data"); if (placedBTDevices.get(pb.getString("address")) == null && newBTDevices.get(pb.getString("address")) == null) { JsonObject data = new JsonObject(Json.encode(pb)); data.remove("rssi"); newBTDevices.put(pb.getString("address"), data); log.info("New bt device: " + buffer); vertx.eventBus().publish("new.bt.device", Json.encode(data)); } btData.get(id).remove(pb.getString("address")); log.info("Update bt data: " + id + " " + pb.getString("address") + " " + pb.getInteger("rssi")); double x = (pb.getInteger("rssi") - (-40.0)) / ((-10.0) * 2.0); log.info("sub calc res: " + x); double d = Math.pow(10.0, x); //RSSI (dBm) = -10n log10(d) + A log.info("the calculated distance is around: " + d + "m"); btData.get(id).put(pb.getString("address"), d * 27); } else if (response.getString("action") != null && response.getString("action").equals("start.bluetooth.scan")) { log.info("start.bluetooth.scan"); btData.remove(id); btData.put(id, new LinkedHashMap<String, Double>()); } else if (response.getString("action") != null && response.getString("action").equals("finished.bluetooth.scan")) { log.info("finished.bluetooth.scan"); if (btData.get(id).size() >= 3) { double x0 = 0, y0 = 0, r0 = 0, x1 = 0, y1 = 0, r1 = 0, x2 = 0, y2 = 0, r2 = 0; int i = 0; for (Map.Entry<String, Double> entry : btData.get(id).entrySet()) { String key = entry.getKey(); JsonObject placedBT = placedBTDevices.get(key); if (placedBT == null) { log.info("placedBT is null, the key was: " + key); continue; } double value = entry.getValue(); if (i == 0) { x0 = placedBT.getDouble("x"); y0 = placedBT.getDouble("y"); r0 = value; log.info("fill first circle x: " + x0 + " y: " + y0 + " r: " + r0); } else if (i == 1) { x1 = placedBT.getDouble("x"); y1 = placedBT.getDouble("y"); r1 = value; log.info("fill second circle x: " + x1 + " y: " + y1 + " r: " + r1); } else if (i == 2) { x2 = placedBT.getDouble("x"); y2 = placedBT.getDouble("y"); r2 = value; log.info("fill third circle x: " + x2 + " y: " + y2 + " r: " + r2); } else { break; } i++; } if (i == 3) { log.info("start calculation"); if (calculateThreeCircleIntersection(x0, y0, r0, x1, y1, r1, x2, y2, r2, id)) { log.info("solved circle intersection"); } else { log.info("cannot solve circle interseciton"); } } else { log.info("there was not enough BT device: " + i); } } else { log.info("There is not enough BT data to calculate the location of the robot."); } } log.info("got the following message from " + id + ": " + Json.encode(response)); } catch (Exception e) { log.info("Cannot process the following buffer: " + buffer); log.error(e); } }); ws.endHandler(e -> { JsonObject response = new JsonObject(); response.put("robotId", id); vertx.eventBus().publish("logout.robot", Json.encode(response)); sockets.remove(id); positionedRobots.remove(id); btData.remove(id); log.info("The following robot logged out: " + id); }); }).requestHandler(router::accept).listen(Integer.getInteger("http.port"), System.getProperty("http.address", "0.0.0.0")); }