-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocator_agent.py
More file actions
153 lines (131 loc) · 6.02 KB
/
locator_agent.py
File metadata and controls
153 lines (131 loc) · 6.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
from uagents import Agent, Context, Model, Protocol
from typing import Dict, Optional
import aiohttp
import os
from shared_protocol import health_protocol, LocationData, emergency_protocol
import json
import asyncio
from datetime import datetime
from pydantic import BaseModel
# Data models
class HospitalInfo(Model):
name: str
address: str
distance: float
travel_time: int # in minutes
class LocationInfo(Model):
address: str
coordinates: LocationData
nearest_hospital: HospitalInfo
timestamp: str
class GeoInfo(Model):
patient_address: str
emergency_contact: str
nearest_hospital: str
estimated_travel_time: int
coordinates: Dict[str, float]
# Create locator agent
locator_agent = Agent(
name="locator-agent",
seed="locator-agent-seed",
)
# Initialize API keys and endpoints on startup
@locator_agent.on_event("startup")
async def startup(ctx: Context):
ctx.logger.info("Locator agent starting up")
ctx.logger.info(f"Agent address: {ctx.agent.address}")
# Store API keys and endpoints
ctx.storage.set('GOOGLE_MAPS_API_KEY', os.getenv('GOOGLE_MAPS_API_KEY'))
ctx.storage.set('last_location_update', None)
async def get_address_from_coordinates(ctx: Context, lat: float, lon: float) -> str:
"""Get street address from coordinates using Google Maps Geocoding API"""
api_key = ctx.storage.get('GOOGLE_MAPS_API_KEY')
url = f"https://maps.googleapis.com/maps/api/geocode/json?latlng={lat},{lon}&key={api_key}"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.json()
if data['status'] == 'OK':
return data['results'][0]['formatted_address']
else:
ctx.logger.error(f"Geocoding API error: {data['status']}")
return "Address not found"
except Exception as e:
ctx.logger.error(f"Error getting address: {e}")
return "Error getting address"
async def find_nearest_hospital(ctx: Context, lat: float, lon: float) -> HospitalInfo:
"""Find nearest hospital using Google Places API"""
api_key = ctx.storage.get('GOOGLE_MAPS_API_KEY')
url = f"https://maps.googleapis.com/maps/api/place/nearbysearch/json?location={lat},{lon}&radius=5000&type=hospital&key={api_key}"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.json()
if data['status'] == 'OK' and data['results']:
hospital = data['results'][0]
# Get travel time
travel_time = await get_travel_time(
ctx,
lat,
lon,
hospital['geometry']['location']['lat'],
hospital['geometry']['location']['lng']
)
return HospitalInfo(
name=hospital['name'],
address=hospital['vicinity'],
distance=0.0, # Could calculate if needed
travel_time=travel_time
)
else:
ctx.logger.error(f"Places API error: {data['status']}")
return None
except Exception as e:
ctx.logger.error(f"Error finding hospital: {e}")
return None
async def get_travel_time(ctx: Context, orig_lat: float, orig_lon: float, dest_lat: float, dest_lon: float) -> int:
"""Get estimated travel time in minutes using Google Distance Matrix API"""
api_key = ctx.storage.get('GOOGLE_MAPS_API_KEY')
url = f"https://maps.googleapis.com/maps/api/distancematrix/json?origins={orig_lat},{orig_lon}&destinations={dest_lat},{dest_lon}&mode=driving&key={api_key}"
try:
async with aiohttp.ClientSession() as session:
async with session.get(url) as response:
data = await response.json()
if data['status'] == 'OK':
duration = data['rows'][0]['elements'][0]['duration']['value']
return int(duration / 60) # Convert seconds to minutes
else:
ctx.logger.error(f"Distance Matrix API error: {data['status']}")
return 0
except Exception as e:
ctx.logger.error(f"Error getting travel time: {e}")
return 0
@health_protocol.on_message(model=LocationData)
async def handle_location_update(ctx: Context, sender: str, msg: LocationData):
"""Handle incoming location data and send geo information to decision maker"""
ctx.logger.info(f"Received location update from {sender}")
try:
# Get address from coordinates
address = await get_address_from_coordinates(ctx, msg.latitude, msg.longitude)
# Find nearest hospital
hospital_info = await find_nearest_hospital(ctx, msg.latitude, msg.longitude)
if hospital_info:
# Prepare geo information
geo_info = GeoInfo(
patient_address=address,
emergency_contact="", # Would be fetched from user profile
nearest_hospital=hospital_info.name,
estimated_travel_time=hospital_info.travel_time,
coordinates={"latitude": msg.latitude, "longitude": msg.longitude}
)
# Send to decision maker
DECISION_MAKER_ADDRESS = os.getenv("DECISION_MAKER_ADDRESS")
await ctx.send(DECISION_MAKER_ADDRESS, geo_info)
ctx.logger.info(f"Sent geo information to decision maker")
# Update last location timestamp
ctx.storage.set('last_location_update', datetime.now().isoformat())
except Exception as e:
ctx.logger.error(f"Error processing location update: {e}")
# Include the shared protocol
locator_agent.include(health_protocol)
locator_agent.include(emergency_protocol)