class DataHandler:
    def __init__(self, openrouteservice_api_key):
        self._database = SqlHandler()
        self._openrouteservice_handler = OpenRouteServiceHandler(
            openrouteservice_api_key)

    def close(self):
        self._database.close()

    def get_values_between_cities(self, city_1: str, city_2: str,
                                  option: str) -> float:

        for city in [city_1, city_2]:
            self.get_coordinates_from_city(
                city)  # insert into database if not already existing

        if not self._database.get_value(city_1, city_2, option):
            self._load_and_save_distance_and_duration(city_1, city_2)

        return self._database.get_value(city_1, city_2, option)

    def _load_and_save_distance_and_duration(self, city_1: str,
                                             city_2: str) -> None:
        coordinates_1 = self._database.get_coordinates_from_city(city_1)
        coordinates_2 = self._database.get_coordinates_from_city(city_2)
        distance, duration = self._openrouteservice_handler. \
            get_distance_duration_between_cities(coordinates_1,
                                                 coordinates_2)
        self._database.set_distance_duration(city_1, city_2, distance,
                                             duration)

    def get_coordinates_from_city(self, city: str) -> Coordinates:
        coordinates = self._database.get_coordinates_from_city(city)
        if not self._check_if_coordinates_are_valid(coordinates):
            coordinates = self._load_and_save_new_coordinates(city)

        return coordinates

    def _load_and_save_new_coordinates(self, city: str) -> Coordinates:
        coordinates = self._openrouteservice_handler.get_coordinate_of_city(
            city)
        self._database.set_coordinates_from_city(city, coordinates.longitude,
                                                 coordinates.latitude)
        return coordinates

    @staticmethod
    def _check_if_coordinates_are_valid(coordinates: Coordinates) -> bool:
        return not (coordinates.longitude == 0 and coordinates.latitude == 0)
예제 #2
0
        "Deve essere specificato il file 'CLIENTI.DBF'.\nPremere qualsiasi tasto per chiudere..."
    )
    input()
    exit(1)

socket = SocketHandler()
sql_handler = SqlHandler(config.path)
sql_handler.convert()
sql_data = sql_handler.get_to_send(config.limit)

if len(sql_data) == 0:
    print(
        "Nessun nuovo ordine da comunicare.\nPremere qualsiasi tasto per chiudere..."
    )
    input()
    exit(1)

data = message_maker(sql_data)
# data = sql_handler.get_test_data("", 5)

print(data)

sent = socket.start(data)
if sent:
    users = list(map(lambda d: str(d['Cellulare']), sql_data))
    sql_handler.set_sent(users)
    sql_handler.close()
    print("Messaggi inviati")

input()