Skip to content

Commit 361abc4

Browse files
DBC22-5196: added camera back if removed from django db (#1108)
* DBC22-5196: added camera back if removed from django db * DBC22-5196: Fix for Location field parsing --------- Co-authored-by: Mulder <Willem.Mulder@gov.bc.ca>
1 parent 07b405a commit 361abc4

2 files changed

Lines changed: 64 additions & 32 deletions

File tree

src/backend/apps/consumer/processor.py

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,11 @@
2121
from contextlib import asynccontextmanager
2222
from aiormq.exceptions import ChannelInvalidStateError
2323
from asgiref.sync import sync_to_async
24-
from apps.webcam.models import Webcam
24+
from apps.webcam.models import Region, RegionHighway, Webcam
2525
from apps.consumer.models import ImageIndex
2626
from apps.shared.status import get_recent_timestamps, calculate_camera_status
2727
from botocore.config import Config
28+
from django.contrib.gis.geos import Point
2829

2930
tf = 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

479531
def 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

src/backend/apps/webcam/models.py

Lines changed: 6 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ def get_timezone(self):
8989
tzname = tf.timezone_at(lng=self.location.x, lat=self.location.y)
9090
return pytz.timezone(tzname) if tzname else timezone.utc
9191

92-
class Region(BaseModel):
93-
id = models.AutoField(primary_key=True)
92+
class Region(models.Model):
93+
id = models.CharField(primary_key=True)
9494
seq = models.IntegerField()
9595
name = models.CharField(max_length=100)
9696

@@ -99,7 +99,7 @@ class Meta:
9999
managed = False
100100

101101

102-
class Highway(BaseModel):
102+
class Highway(models.Model):
103103
id = models.AutoField(primary_key=True)
104104
hwy_number = models.CharField(max_length=20)
105105
section = models.CharField(max_length=100)
@@ -109,36 +109,16 @@ class Meta:
109109
managed = False
110110

111111

112-
class RegionHighway(BaseModel):
113-
id = models.AutoField(primary_key=True)
114-
region = models.ForeignKey(Region, on_delete=models.CASCADE)
115-
highway = models.ForeignKey(Highway, on_delete=models.CASCADE)
112+
class RegionHighway(models.Model):
113+
highway_id = models.CharField(max_length=20, primary_key=True)
114+
region_id = models.CharField(max_length=20)
116115
seq = models.IntegerField()
117116

118117
class Meta:
119118
db_table = 'Region_Highways_Live'
120119
managed = False
121120

122121

123-
# class CameraSource(models.Model):
124-
# id = models.AutoField(primary_key=True)
125-
# cam_internetname = models.CharField(max_length=100)
126-
# cam_internetcaption = models.CharField(max_length=255)
127-
# cam_locationsregion = models.ForeignKey(Region, on_delete=models.CASCADE)
128-
# cam_locationshighway = models.ForeignKey(Highway, on_delete=models.CASCADE)
129-
# cam_locationsorientation = models.CharField(max_length=50, blank=True, null=True)
130-
# cam_locationselevation = models.IntegerField(blank=True, null=True)
131-
# cam_controldisabled = models.BooleanField(default=False)
132-
# isnew = models.BooleanField(default=False)
133-
# cam_maintenanceis_on_demand = models.BooleanField(default=False)
134-
# cam_internetcredit = models.CharField(max_length=255, blank=True, null=True)
135-
# cam_internetdbc_mark = models.CharField(max_length=255, blank=True, null=True)
136-
# seq = models.IntegerField()
137-
138-
# class Meta:
139-
# db_table = 'Cams_Live'
140-
# managed = False
141-
142122
class CameraSource(models.Model):
143123
id = models.IntegerField(primary_key=True)
144124
cam_internetname = models.CharField(max_length=255, blank=True, null=True)

0 commit comments

Comments
 (0)