All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.rcll.refbox.OrderService Maven / Gradle / Ivy

There is a newer version: 0.1.19.1
Show newest version
package com.rcll.refbox;

import com.rcll.domain.*;
import lombok.Synchronized;
import org.robocup_logistics.llsf_msgs.OrderInfoProtos;

import java.util.List;
import java.util.stream.Collectors;

class OrderService {
    private OrderInfoProtos.OrderInfo orderInfo;
    private final MachineClient machineClient;
    private final TeamColor team;

    public OrderService(MachineClient machineClient, TeamColor team) {
        this.machineClient = machineClient;
        orderInfo = null;
        this.team = team;
    }

    @Synchronized
    public void update(OrderInfoProtos.OrderInfo orderInfo) {
        this.orderInfo = orderInfo;
    }

    @Synchronized
    public List getOrders() {
        return this.orderInfo.getOrdersList().stream()
                .map(this::protoOrderToOrder)
                .collect(Collectors.toList());
    }

    @Synchronized
    public Order getOrder(int id) {
        return getOrderNonLocked(id);
    }

    private Order getOrderNonLocked(int id) {
        var protoOrder = orderInfo.getOrdersList().stream()
                .filter(order -> order.getId() == id)
                .findFirst();
        if (protoOrder.isPresent()) {
            return protoOrderToOrder(protoOrder.get());
        } else {
            throw new RuntimeException("Unknown Order: " + id);
        }
    }

    private Order protoOrderToOrder(OrderInfoProtos.Order protoOrder) {
        int delivered = 0;
        switch (team) {
            case CYAN:
                delivered = protoOrder.getQuantityDeliveredCyan();
                break;
            case MAGENTA:
                delivered = protoOrder.getQuantityDeliveredMagenta();
                break;
        }
        Cap cap;
        switch (protoOrder.getCapColor()) {
            case CAP_BLACK:
                cap = Cap.Black;
                break;
            case CAP_GREY:
                cap = Cap.Grey;
                break;
            default:
                throw new RuntimeException("Unknown CapColor: " + protoOrder.getCapColor());
        }
        Base base;
        switch (protoOrder.getBaseColor()) {
            case BASE_RED:
                base = Base.Red;
                break;
            case BASE_BLACK:
                base = Base.Black;
                break;
            case BASE_SILVER:
                base = Base.Silver;
                break;
            default:
                throw new RuntimeException("Unknown BaseColor: " + protoOrder.getBaseColor());
        }
        Ring ring1 = ringForIndex(protoOrder, 0);
        Ring ring2 = ringForIndex(protoOrder, 1);
        Ring ring3 = ringForIndex(protoOrder, 2);
        return new Order(protoOrder.getId(), protoOrder.getQuantityRequested(), delivered,
                protoOrder.getDeliveryPeriodBegin(), protoOrder.getDeliveryPeriodEnd(),
                protoOrder.getDeliveryGate(), cap, ring1, ring2, ring3, base, protoOrder.getCompetitive());
    }

    private Ring ringForIndex(OrderInfoProtos.Order protoOrder, int index) {
        if (protoOrder.getRingColorsCount() <= index) {
            return null;
        }
        return machineClient.getRingForColor(machineClient.fromRefboxRingColor(protoOrder.getRingColors(index)));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy