2121from contextlib import asynccontextmanager
2222from aiormq .exceptions import ChannelInvalidStateError
2323from asgiref .sync import sync_to_async
24- from apps .webcam .models import Webcam
24+ from apps .webcam .models import Region , RegionHighway , Webcam
2525from apps .consumer .models import ImageIndex
2626from apps .shared .status import get_recent_timestamps , calculate_camera_status
2727from botocore .config import Config
28+ from django .contrib .gis .geos import Point
2829
2930tf = TimezoneFinder ()
3031
@@ -211,13 +212,14 @@ def process_camera_rows(rows):
211212 'cam_locations_region' : row .cam_locationsregion if hasattr (row , 'cam_locationsregion' ) else '' ,
212213 'cam_locations_highway' : row .cam_locationshighway if hasattr (row , 'cam_locationshighway' ) else '' ,
213214 'cam_locations_highway_section' : row .cam_locationshighway_section if hasattr (row , 'cam_locationshighway_section' ) else '' ,
214- 'cam_locations_elevation' : row .cam_locationsorientation if hasattr (row , 'cam_locationsorientation ' ) else '' ,
215+ 'cam_locations_elevation' : row .cam_locationselevation if hasattr (row , 'cam_locationselevation ' ) else '' ,
215216 'update_period_mean' : 300 ,
216217 'update_period_stddev' : 60 ,
217218 'dbc_mark' : row .cam_internetdbc_mark if hasattr (row , 'cam_internetdbc_mark' ) else '' ,
218219 'is_on' : not row .cam_controldisabled if hasattr (row , 'cam_controldisabled' ) else True ,
219220 'cam_maintenanceis_on_demand' : row .cam_maintenanceis_on_demand if hasattr (row , 'cam_maintenanceis_on_demand' ) else False ,
220221 'is_new' : row .isnew if hasattr (row , 'isnew' ) else False ,
222+ 'seq' : row .seq if hasattr (row , 'seq' ) else 0 ,
221223
222224 }
223225 camera_list .append (camera_obj )
@@ -362,8 +364,57 @@ def generate_local_timestamp(db_data: list, camera_id: str, timestamp: str):
362364 return timestamp
363365
364366@sync_to_async
365- def insert_image_and_update_webcam (camera_id , timestamp ):
366- camera = Webcam .objects .get (id = camera_id )
367+ def insert_image_and_update_webcam (camera_id , timestamp , webcam ):
368+ region = webcam .get ('cam_locations_region' , '' )
369+ region_obj = Region .objects .using ("mssql" ).filter (id = region ).first ()
370+ region_number = region_obj .seq if region_obj else None
371+ region_name = region_obj .name if region_obj else ''
372+ cam_locations_geo_latitude = webcam .get ('cam_locations_geo_latitude' , None )
373+ cam_locations_geo_longitude = webcam .get ('cam_locations_geo_longitude' , None )
374+
375+ geometry = None
376+ if cam_locations_geo_latitude and cam_locations_geo_longitude :
377+ try :
378+ geometry = Point (float (cam_locations_geo_longitude ), float (cam_locations_geo_latitude ))
379+ except (ValueError , TypeError ):
380+ geometry = None
381+
382+ elevation = webcam .get ('cam_locations_elevation' , None )
383+ raw_hw = webcam .get ("cam_locations_highway" ) or ""
384+ parts = raw_hw .split ("_" , 1 )
385+ highway_number = parts [0 ]
386+ highway_description = parts [1 ] if len (parts ) > 1 else ""
387+ highway_id = (
388+ f"{ highway_number } _{ highway_description } "
389+ if highway_description
390+ else highway_number
391+ )
392+
393+ region_id = region_obj .id if region_obj else None
394+ highway_group = RegionHighway .objects .using ("mssql" ).filter (
395+ highway_id = highway_id ,
396+ region_id = region_id
397+ ).first ().seq if region_id else None
398+
399+ camera , created = Webcam .objects .get_or_create (
400+ id = camera_id ,
401+ defaults = {
402+ "name" : f"{ webcam .get ('cam_internet_name' , '' )} " ,
403+ "caption" : f"{ webcam .get ('cam_internet_caption' , '' )} " ,
404+ "region" : region_number ,
405+ "region_name" : f"{ region_name } " ,
406+ "highway" : highway_number ,
407+ "highway_description" : highway_description ,
408+ "highway_group" : highway_group ,
409+ "highway_cam_order" : f"{ webcam .get ('seq' , 0 )} " ,
410+ "location" : geometry ,
411+ "orientation" : webcam .get ('cam_locations_orientation' , '' ),
412+ "elevation" : elevation ,
413+ "dbc_mark" : webcam .get ('dbc_mark' , '' ),
414+ "update_period_mean" : webcam .get ('update_period_mean' , 300 ),
415+ "update_period_stddev" : webcam .get ('update_period_stddev' , 60 ),
416+ }
417+ )
367418
368419 ImageIndex .objects .create (
369420 camera_id = camera_id ,
@@ -473,7 +524,8 @@ async def handle_image_message(camera_id: str, db_data: any, body: bytes, timest
473524 # Insert record into DB
474525 await insert_image_and_update_webcam (
475526 camera_id ,
476- utc_dt
527+ utc_dt ,
528+ webcam
477529 )
478530
479531def verify_image (image_data : bytes , camera_id : str ) -> bool :
@@ -484,4 +536,4 @@ def verify_image(image_data: bytes, camera_id: str) -> bool:
484536 return True
485537 except Exception as e :
486538 logger .warning (f"Invalid image for camera { camera_id } : { e } " )
487- return False
539+ return False
0 commit comments