Skip to content

Commit

Permalink
fixed route retrieval, fail geocode if no geoapify api key is configured
Browse files Browse the repository at this point in the history
  • Loading branch information
myTselection committed Sep 1, 2024
1 parent d0d0b14 commit 17869c1
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 8 deletions.
2 changes: 1 addition & 1 deletion custom_components/carbu_com/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ async def handle_get_lowest_fuel_price(call):
GEO_API_KEY = config.get("GEO_API_KEY")

session = ComponentSession(GEO_API_KEY)
station_info = await hass.async_add_executor_job(lambda: session.getStationInfo(postalcode, country, fuel_type, town, max_distance, filter))
station_info = await hass.async_add_executor_job(lambda: session.getStationInfo(postalcode, country, fuel_type, town, max_distance, filter, True))

_LOGGER.debug(f"{NAME} get_lowest_fuel_price info found: {station_info}")
hass.bus.async_fire(f"{DOMAIN}_lowest_fuel_price", station_info)
Expand Down
66 changes: 59 additions & 7 deletions custom_components/carbu_com/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -1036,11 +1036,14 @@ def getOilPrediction(self):

@sleep_and_retry
@limits(calls=10, period=5)
def getStationInfo(self, postalcode, country, fuel_type: FuelType, town="", max_distance=0, filter=""):
def getStationInfo(self, postalcode, country, fuel_type: FuelType, town="", max_distance=0, filter="", townConfirmed = False):
locationinfo = None
single = True if max_distance == 0 else False
if country.lower() in ["be","fr","lu"]:
carbuLocationInfo = self.convertPostalCode(postalcode, country, town)
if townConfirmed:
carbuLocationInfo = self.convertPostalCodeMultiMatch(postalcode, country, town)
else:
carbuLocationInfo = self.convertPostalCode(postalcode, country)
if not carbuLocationInfo:
raise Exception(f"Location not found country: {country}, postalcode: {postalcode}, town: {town}")
town = carbuLocationInfo.get("n")
Expand Down Expand Up @@ -1234,7 +1237,7 @@ def getPriceOnRoute(self, country, fuel_type: FuelType, from_postalcode, to_post
to_country = country
to_location = self.geocode(to_country, to_postalcode)
assert to_location is not None
return self.getPriceOnRouteLatLon(fuel_type, from_location[0], from_location[1], to_location[0], to_location[1], filter)
return self.getPriceOnRouteLatLon(fuel_type, from_location[1], from_location[0], to_location[1], to_location[0], filter)


#USED BY Service: handle_get_lowest_fuel_price_on_route_coor
Expand Down Expand Up @@ -1267,7 +1270,11 @@ def getPriceOnRouteLatLon(self, fuel_type: FuelType, from_latitude, from_longitu
postal_code_country = self.reverseGeocode(route[i]['maneuver']['location'][0], route[i]['maneuver']['location'][1])
if postal_code_country.get('postal_code') is not None and postal_code_country.get('postal_code') not in processedPostalCodes:
_LOGGER.debug(f"Get route postalcode {postal_code_country.get('postal_code')}, processedPostalCodes {processedPostalCodes}")
bestAroundPostalCode = self.getStationInfo(postal_code_country.get('postal_code'), postal_code_country.get('country_code'), fuel_type, postal_code_country.get('town'), 3, filter)
bestAroundPostalCode = None
try:
bestAroundPostalCode = self.getStationInfo(postal_code_country.get('postal_code'), postal_code_country.get('country_code'), fuel_type, postal_code_country.get('town'), 3, filter, False)
except Exception as e:
_LOGGER.error(f"ERROR: getStationInfo failed : {e}")
if bestAroundPostalCode is None:
continue
processedPostalCodes.extend(bestAroundPostalCode.get('postalcodes'))
Expand Down Expand Up @@ -1329,7 +1336,8 @@ def geocode(self, country_code, postal_code):
# header = {"Accept-Language": "nl-BE"}
address = f"{postal_code}, {country_code}"


if self.API_KEY_GEOAPIFY in ["","GEO_API_KEY"]:
raise Exception("Geocode failed: GEO_API_KEY not set!")
# GEOCODIFY
# response = self.s.get(f"{self.GEOCODIFY_BASE_URL}geocode?api_key={self.API_KEY_GEOCODIFY}&q={address}")
# response = response.json()
Expand Down Expand Up @@ -1595,7 +1603,38 @@ def getOSMRoute(self, from_location, to_location):

# Request the route from OpenStreetMap API
# 'https://router.project-osrm.org/route/v1/driving/<from_lon>,<from_lat>;<to_lon>,<to_lat>?steps=true

header = {"Content-Type": "application/x-www-form-urlencoded"}
self.s.headers["User-Agent"] = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36"
url = f'https://router.project-osrm.org/route/v1/driving/{from_location[1]},{from_location[0]};{to_location[1]},{to_location[0]}?steps=true'
_LOGGER.debug(f"getOSMRoute: {url}")
response = self.s.get(url,headers=header, timeout=30)
route_data = response.json()
_LOGGER.debug(f"route_data {route_data}")


if route_data.get('routes') is None:
_LOGGER.error(f"ERROR: route not found: {route_data}")
return
# Extract the waypoints (towns) along the route
waypoints = route_data['routes'][0]['legs'][0]['steps']

_LOGGER.debug(f"waypoints {waypoints}")
return waypoints

# NOT USED by services on route
@sleep_and_retry
@limits(calls=1, period=15)
def getRoute(self, from_location, to_location):

#location expected (lat (50.XX), lon (4.XX))

# Request the route from GeoApify.com API

# 'https://router.project-osrm.org/route/v1/driving/<from_lon>,<from_lat>;<to_lon>,<to_lat>?steps=true
url = f'https://router.project-osrm.org/route/v1/driving/{from_location[1]},{from_location[0]};{to_location[1]},{to_location[0]}?steps=true'
url = f"https://api.geoapify.com/v1/routing?waypoints={from_location[0]},{from_location[1]}|{to_location[0]},{to_location[1]}&mode=drive&apiKey={self.API_KEY_GEOAPIFY}"
#NOT WORKING: steps contain no coordinates
response = self.s.get(url)
route_data = response.json()
_LOGGER.debug(f"route_data {route_data}")
Expand All @@ -1604,8 +1643,7 @@ def getOSMRoute(self, from_location, to_location):
waypoints = route_data['routes'][0]['legs'][0]['steps']

_LOGGER.debug(f"waypoints {waypoints}")
return waypoints

return waypoints


def haversine_distance(self, lat1, lon1, lat2, lon2):
Expand Down Expand Up @@ -1660,9 +1698,23 @@ def get_nested_element(self, json_obj, key_string):

# Example usage

# _LOGGER = logging.getLogger(__name__)
# _LOGGER.setLevel(logging.DEBUG)
# if not logging.getLogger().hasHandlers():
# logging.basicConfig(level=logging.DEBUG)
# _LOGGER.debug("Debug logging is now enabled.")

# session = ComponentSession("GEO_API_KEY")


#LOCAL TESTS

# session.geocode("BE", "1000")

# test route
# print(session.getPriceOnRoute("BE", FuelType.DIESEL, 1000, 2000))


# test nl official
# session.getFuelOfficial(FuelType.DIESEL_OFFICIAL_B7, "NL")
# session.getFuelOfficial(FuelType.LPG_OFFICIAL, "BE")
Expand Down

0 comments on commit 17869c1

Please sign in to comment.