-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.py
More file actions
62 lines (50 loc) · 1.77 KB
/
consumer.py
File metadata and controls
62 lines (50 loc) · 1.77 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
from typing import Dict
from src.common.models import DriverLocationEvent
from src.common.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 Exception 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
)