-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamera_config.py
214 lines (182 loc) · 7.5 KB
/
camera_config.py
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# camera_config.py
from dataclasses import dataclass
from typing import Dict, List, Optional
from pathlib import Path
import json
import time
from datetime import datetime, timezone
from ..logger_config import logger
@dataclass
class Zone:
"""Represents a monitored zone in a camera's view"""
id: str
name: str
coordinates: List[Dict[str, float]] # List of lat/lng points defining the zone
active: bool = True
@dataclass
class CameraLocation:
"""Represents a camera location and configuration"""
id: str
name: str
lat: float
lng: float
stream_url: str
fps: int = 30
direction: int = 0
roadway: Optional[str] = None
zones: List[Zone] = None
last_updated: Optional[str] = None
class CameraConfigManager:
"""Manages camera configuration and zones"""
def __init__(self, config_path: str = 'config/camera_locations.json'):
self.config_path = Path(config_path)
self.config_dir = self.config_path.parent
self.cameras: Dict[str, CameraLocation] = {}
self.last_refresh = time.time()
self._config_check_interval = 300 # 5 minutes
async def initialize(self):
"""Initialize the camera configuration"""
try:
self.config_dir.mkdir(parents=True, exist_ok=True)
await self.load_config()
logger.info("Camera configuration initialized")
except Exception as e:
logger.error(f"Error initializing camera config: {e}")
raise
async def load_config(self) -> bool:
"""Load camera configuration from file"""
try:
if not self.config_path.exists():
logger.warning("Config file not found, will create new one")
return False
with open(self.config_path) as f:
data = json.load(f)
self.cameras = {
camera_id: CameraLocation(
id=camera_id,
name=cam_data.get('name', camera_id),
lat=float(cam_data['lat']),
lng=float(cam_data['lng']),
stream_url=cam_data['stream_url'],
fps=cam_data.get('fps', 30),
direction=cam_data.get('direction', 0),
roadway=cam_data.get('roadway'),
zones=[
Zone(
id=f"{camera_id}_{i}",
name=zone.get('name', f"Zone {i}"),
coordinates=zone['coordinates'],
active=zone.get('active', True)
)
for i, zone in enumerate(cam_data.get('zones', []))
],
last_updated=datetime.now(timezone.utc).isoformat()
)
for camera_id, cam_data in data.items()
if self._validate_camera_data(cam_data)
}
self.last_refresh = time.time()
logger.info(f"Loaded configuration for {len(self.cameras)} cameras")
return True
except Exception as e:
logger.error(f"Error loading config: {e}")
return False
def get_config(self) -> Dict[str, CameraLocation]:
"""Get current camera configuration"""
current_time = time.time()
if current_time - self.last_refresh > self._config_check_interval:
asyncio.create_task(self.load_config())
return self.cameras
def _validate_camera_data(self, data: dict) -> bool:
"""Validate individual camera data"""
required_fields = {'lat', 'lng', 'name', 'stream_url'}
if not all(key in data and data[key] is not None for key in required_fields):
return False
# Validate zones if present
if 'zones' in data:
for zone in data['zones']:
if not self._validate_zone_data(zone):
return False
return True
def _validate_zone_data(self, zone: dict) -> bool:
"""Validate zone configuration data"""
if 'coordinates' not in zone:
return False
coordinates = zone['coordinates']
if not isinstance(coordinates, list) or len(coordinates) < 3:
return False
for point in coordinates:
if not isinstance(point, dict) or 'lat' not in point or 'lng' not in point:
return False
return True
async def save_config(self) -> bool:
"""Save current configuration to file"""
try:
config_data = {}
for camera_id, camera in self.cameras.items():
camera_data = {
'name': camera.name,
'lat': camera.lat,
'lng': camera.lng,
'stream_url': camera.stream_url,
'fps': camera.fps,
'direction': camera.direction,
'roadway': camera.roadway,
'zones': [
{
'name': zone.name,
'coordinates': zone.coordinates,
'active': zone.active
}
for zone in (camera.zones or [])
]
}
config_data[camera_id] = camera_data
with open(self.config_path, 'w') as f:
json.dump(config_data, f, indent=4)
logger.info("Camera configuration saved successfully")
return True
except Exception as e:
logger.error(f"Error saving config: {e}")
return False
async def add_zone(self, camera_id: str, name: str, coordinates: List[Dict[str, float]]) -> Optional[Zone]:
"""Add a new zone to a camera"""
try:
if camera_id not in self.cameras:
return None
camera = self.cameras[camera_id]
if camera.zones is None:
camera.zones = []
zone = Zone(
id=f"{camera_id}_{len(camera.zones)}",
name=name,
coordinates=coordinates
)
camera.zones.append(zone)
await self.save_config()
return zone
except Exception as e:
logger.error(f"Error adding zone: {e}")
return None
async def update_zone(self, camera_id: str, zone_id: str,
coordinates: Optional[List[Dict[str, float]]] = None,
active: Optional[bool] = None) -> bool:
"""Update an existing zone"""
try:
if camera_id not in self.cameras:
return False
camera = self.cameras[camera_id]
if not camera.zones:
return False
for zone in camera.zones:
if zone.id == zone_id:
if coordinates is not None:
zone.coordinates = coordinates
if active is not None:
zone.active = active
await self.save_config()
return True
return False
except Exception as e:
logger.error(f"Error updating zone: {e}")
return False