Repository navigation
Expand file tree
/
Copy pathconsumer.py
More file actions
56 lines (44 loc) · 1.73 KB
/
Copy pathconsumer.py
File metadata and controls
56 lines (44 loc) · 1.73 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
from models import DriverLocationEvent
from utils import get_logger
logger = get_logger("DriverLocationConsumer")
class DriverLocationConsumer:
"""
Listens for incoming driver telemetry updates and stores them in
DriverLocationStore.
Expected event format:
{
"driver_id": "d123",
"lat": 40.712,
"lon": -74.005,
"timestamp": "...",
"status": "available"
}
"""
def __init__(self, event_bus, store):
self.event_bus = event_bus
self.store = store
self.logger = logger
# ------------------------------------------------------------
# Handle Incoming Driver Location Updates
# ------------------------------------------------------------
async def handle_driver_location(self, data: dict):
"""
Convert dictionary → Pydantic model → update store.
"""
try:
event = DriverLocationEvent(**data)
except (TypeError, ValueError) as e:
self.logger.error(f"Invalid driver location event: {e}")
return
# Update the driver store
self.store.upsert_driver(event)
self.logger.info(f"Driver update processed: {event.driver_id} @ ({event.lat}, {event.lon})")
# ------------------------------------------------------------
# Subscribe to EventBus Topic
# ------------------------------------------------------------
async def start(self):
"""
Begins listening to driver_location_updates topic.
"""
self.logger.info("DriverLocationConsumer listening for driver updates...")
await self.event_bus.subscribe("driver_location_updates", self.handle_driver_location)