Search in sources :

Example 1 with Connection

use of it.unibo.dronesecurity.lib.Connection in project DroneSecurity by mirko-felice.

the class CourierShippingService method callback.

private void callback(@NotNull final MqttMessage msg) {
    final JsonObject json = new JsonObject(new String(msg.getPayload(), StandardCharsets.UTF_8));
    final String orderId = json.getString(MqttMessageParameterConstants.ORDER_ID_PARAMETER);
    final String statusValue = json.getString(MqttMessageParameterConstants.STATUS_PARAMETER);
    final OrderRepository orderRepository = OrderRepository.getInstance();
    CastHelper.safeCast(orderRepository.getOrderById(orderId), DeliveringOrder.class).ifPresent(order -> {
        final Connection connection = Connection.getInstance();
        if (MqttMessageValueConstants.DELIVERY_SUCCESSFUL_MESSAGE.equals(statusValue))
            orderRepository.confirmedDelivery(order.confirmDelivery());
        else if (MqttMessageValueConstants.DELIVERY_FAILED_MESSAGE.equals(statusValue))
            orderRepository.failedDelivery(order.failDelivery());
        // TODO maybe catch else branch that does NOT send this message
        final JsonNode message = new ObjectMapper().createObjectNode().put(MqttMessageParameterConstants.SYNC_PARAMETER, MqttMessageValueConstants.DRONE_CALLBACK_MESSAGE);
        connection.publish(MqttTopicConstants.ORDER_TOPIC, message);
    });
}
Also used : DeliveringOrder(it.unibo.dronesecurity.userapplication.shipping.courier.entities.DeliveringOrder) OrderRepository(it.unibo.dronesecurity.userapplication.shipping.courier.repo.OrderRepository) Connection(it.unibo.dronesecurity.lib.Connection) JsonObject(io.vertx.core.json.JsonObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Connection

use of it.unibo.dronesecurity.lib.Connection in project DroneSecurity by mirko-felice.

the class CourierShippingService method performDelivery.

private void performDelivery(@NotNull final RoutingContext routingContext) {
    final RequestParameters params = routingContext.get(ValidationHandler.REQUEST_CONTEXT_KEY);
    final JsonObject body = params.body().getJsonObject();
    final PlacedOrder order = body.getJsonObject(OrderConstants.ORDER_KEY).mapTo(PlacedOrder.class);
    if (order == null)
        routingContext.response().setStatusCode(CLIENT_ERROR_CODE).end();
    else {
        final DeliveringOrder deliveringOrder = order.deliver();
        OrderRepository.getInstance().delivering(deliveringOrder);
        final JsonNode messageJson = new ObjectMapper().createObjectNode().put(MqttMessageParameterConstants.SYNC_PARAMETER, MqttMessageValueConstants.PERFORM_DELIVERY_MESSAGE).put(MqttMessageParameterConstants.ORDER_ID_PARAMETER, order.getId()).put(MqttMessageParameterConstants.COURIER_PARAMETER, body.getString(OrderConstants.COURIER_KEY));
        final Connection connection = Connection.getInstance();
        connection.publish(MqttTopicConstants.ORDER_TOPIC, messageJson);
        connection.subscribe(MqttTopicConstants.LIFECYCLE_TOPIC, this::callback);
        routingContext.response().end(CORRECT_RESPONSE_TO_PERFORM_DELIVERY);
    }
}
Also used : DeliveringOrder(it.unibo.dronesecurity.userapplication.shipping.courier.entities.DeliveringOrder) Connection(it.unibo.dronesecurity.lib.Connection) JsonObject(io.vertx.core.json.JsonObject) JsonNode(com.fasterxml.jackson.databind.JsonNode) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RequestParameters(io.vertx.ext.web.validation.RequestParameters) PlacedOrder(it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder)

Aggregations

JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 JsonObject (io.vertx.core.json.JsonObject)2 Connection (it.unibo.dronesecurity.lib.Connection)2 DeliveringOrder (it.unibo.dronesecurity.userapplication.shipping.courier.entities.DeliveringOrder)2 RequestParameters (io.vertx.ext.web.validation.RequestParameters)1 PlacedOrder (it.unibo.dronesecurity.userapplication.shipping.courier.entities.PlacedOrder)1 OrderRepository (it.unibo.dronesecurity.userapplication.shipping.courier.repo.OrderRepository)1