Enhance location resolution in MessageHandler and RepeaterManager

- Implemented a mechanism in MessageHandler to resolve location names from the database using public keys, providing a more user-friendly display of locations.
- Added fallback logic to ensure coordinates are used if no resolved location is found.
- Updated comments in RepeaterManager to clarify the order of preference for city extraction and introduced county as a fallback for rural areas, improving location accuracy.
This commit is contained in:
agessaman
2026-01-01 18:52:32 -08:00
parent 7b2cdf411a
commit 5abf0a20dd
2 changed files with 56 additions and 6 deletions
+34 -1
View File
@@ -472,7 +472,40 @@ class MessageHandler:
name = advert_data.get('name', 'No name')
location = ""
if 'lat' in advert_data and 'lon' in advert_data:
location = f" at {advert_data['lat']:.4f},{advert_data['lon']:.4f}"
# Try to get resolved location from database if available
try:
if hasattr(self.bot, 'repeater_manager'):
# Look up the contact to get resolved location
public_key = advert_data.get('public_key')
if public_key:
contact_query = self.bot.db_manager.execute_query(
'SELECT city, state, country FROM complete_contact_tracking WHERE public_key = ?',
(public_key,)
)
if contact_query:
contact = contact_query[0]
city = contact.get('city')
state = contact.get('state')
if city and state:
location = f" at {city}, {state}"
elif city:
location = f" at {city}"
else:
# Fallback to coordinates if no resolved location
location = f" at {advert_data['lat']:.4f},{advert_data['lon']:.4f}"
else:
# No contact found yet, use coordinates
location = f" at {advert_data['lat']:.4f},{advert_data['lon']:.4f}"
else:
# No public key, use coordinates
location = f" at {advert_data['lat']:.4f},{advert_data['lon']:.4f}"
else:
# No repeater manager, use coordinates
location = f" at {advert_data['lat']:.4f},{advert_data['lon']:.4f}"
except Exception as e:
# If lookup fails, fallback to coordinates
self.logger.debug(f"Could not get resolved location for logging: {e}")
location = f" at {advert_data['lat']:.4f},{advert_data['lon']:.4f}"
# Show hop count in log
hop_count = signal_info.get('hops', 0)
+22 -5
View File
@@ -1327,7 +1327,7 @@ class RepeaterManager:
if location:
address = location.raw.get('address', {})
# Get city name from various fields
# Get city name from various fields (in order of preference)
city = (address.get('city') or
address.get('town') or
address.get('village') or
@@ -1335,6 +1335,15 @@ class RepeaterManager:
address.get('municipality') or
address.get('suburb'))
# If no city found, try county as fallback (for rural areas)
# Keep "County" in the name to disambiguate from cities with the same name
if not city:
county = address.get('county')
if county:
# Keep full county name to distinguish from cities (e.g., "Snohomish County" vs "Snohomish" city)
city = county # Keep "County" suffix to avoid ambiguity
self.logger.debug(f"Using county '{county}' as location name for coordinates {latitude}, {longitude}")
if city:
# For large cities, try to get neighborhood information
neighborhood = self._get_neighborhood_for_large_city(address, city)
@@ -1405,7 +1414,7 @@ class RepeaterManager:
address = location.raw.get('address', {})
self.logger.debug(f"Geocoding API returned address data: {list(address.keys())}")
# Get city name from various fields
# Get city name from various fields (in order of preference)
city = (address.get('city') or
address.get('town') or
address.get('village') or
@@ -1413,6 +1422,15 @@ class RepeaterManager:
address.get('municipality') or
address.get('suburb'))
# If no city found, try county as fallback (for rural areas)
# Keep "County" in the name to disambiguate from cities with the same name
if not city:
county = address.get('county')
if county:
# Keep full county name to distinguish from cities (e.g., "Snohomish County" vs "Snohomish" city)
city = county # Keep "County" suffix to avoid ambiguity
self.logger.debug(f"Using county '{county}' as location name for coordinates {latitude}, {longitude}")
if city:
# For large cities, try to get neighborhood information
neighborhood = self._get_neighborhood_for_large_city(address, city)
@@ -1422,11 +1440,10 @@ class RepeaterManager:
location_info['city'] = city
self.logger.debug(f"Extracted city: {location_info['city']}")
# Get state/province information
# Get state/province information (don't use county here since we may have used it for city)
state = (address.get('state') or
address.get('province') or
address.get('region') or
address.get('county'))
address.get('region'))
if state:
location_info['state'] = state
self.logger.debug(f"Extracted state: {state}")