Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/backend/apps/consumer/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ async def consume_queue(queue, name: str):
try:
await process_message(message)
except Exception as e:
logger.error(f"Error processing message from {name}: {e}")
logging.exception(f"Error processing message from {name}: {e}")

async def consume_from(rb_url: str, name: str):
while not stop_event.is_set():
Expand Down Expand Up @@ -318,6 +318,7 @@ def process_camera_rows(rows):
'cam_maintenanceis_on_demand': row.cam_maintenanceis_on_demand if hasattr(row, 'cam_maintenanceis_on_demand') else False,
'is_new': row.isnew if hasattr(row, 'isnew') else False,
'seq': row.seq if hasattr(row, 'seq') else 0,
'cam_locationsweather_station': row.cam_locationsweather_station if hasattr(row, 'cam_locationsweather_station') else '',

}
camera_list.append(camera_obj)
Expand Down Expand Up @@ -380,7 +381,7 @@ def watermark(webcam: any, image_data: bytes, tz: str, timestamp: str) -> bytes:
return buffer.read()

except Exception as e:
logger.error(f"Error processing image from camera: {e}")
logging.exception(f"Error processing image from camera: {e}")
return None

def blank_out_image(webcam: any, image_data: bytes, tz: str, timestamp: str) -> bytes:
Expand Down Expand Up @@ -426,7 +427,7 @@ def blank_out_image(webcam: any, image_data: bytes, tz: str, timestamp: str) ->
return buffer.read()

except Exception as e:
logger.error(f"Error processing image from camera: {e}")
logging.exception(f"Error processing image from camera: {e}")
return None


Expand All @@ -441,7 +442,7 @@ def save_original_image_to_pvc(camera_id: str, image_bytes: bytes):
with open(filepath, "wb") as f:
f.write(image_bytes)
except Exception as e:
logger.error(f"Error saving original image to PVC {filepath}: {e}")
logging.exception(f"Error saving original image to PVC {filepath}: {e}")
logger.info(f"Original image saved to PVC at {filepath}")

def save_watermarked_image_to_pvc(camera_id: str, image_bytes: bytes, timestamp: str, is_on: bool):
Expand All @@ -460,7 +461,7 @@ def save_watermarked_image_to_pvc(camera_id: str, image_bytes: bytes, timestamp:
else:
logger.info(f"Blank out image saved to PVC at {filepath}")
except Exception as e:
logger.error(f"Error saving image to PVC {filepath}: {e}")
logging.exception(f"Error saving image to PVC {filepath}: {e}")

def save_watermarked_image_to_drivebc_pvc(camera_id: str, image_bytes: bytes, is_on: bool):
os.makedirs(os.path.dirname(f'{DRIVEBC_PVC_WATERMARKED_PATH}'), exist_ok=True)
Expand All @@ -476,7 +477,7 @@ def save_watermarked_image_to_drivebc_pvc(camera_id: str, image_bytes: bytes, is
if is_on:
logger.info(f"Watermarked image saved to drivebc PVC at {filepath}")
except Exception as e:
logger.error(f"Error saving image to drivebc PVC {filepath}: {e}")
logging.exception(f"Error saving image to drivebc PVC {filepath}: {e}")

def delete_watermarked_image_from_pvc(camera_id: str):
save_dir = os.path.join(PVC_WATERMARKED_PATH, camera_id)
Expand All @@ -491,7 +492,7 @@ def delete_watermarked_image_from_pvc(camera_id: str):
if os.path.isfile(filepath):
os.remove(filepath)
except Exception as e:
logger.error(f"Error deleting watermarked images from PVC {save_dir}: {e}")
logging.exception(f"Error deleting watermarked images from PVC {save_dir}: {e}")

async def get_images_within(camera_id: str, hours: int = 720) -> list:
cutoff = datetime.now(timezone.utc) - timedelta(hours=hours)
Expand Down
1 change: 1 addition & 0 deletions src/backend/apps/webcam/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ class CameraSource(models.Model):
isnew = models.BooleanField(default=False)
cam_maintenanceis_on_demand = models.BooleanField(default=False)
seq = models.IntegerField(blank=True, null=True)
cam_locationsweather_station = models.CharField(max_length=10, blank=True, null=True)

class Meta:
db_table = 'Cams_Live'
Expand Down
25 changes: 20 additions & 5 deletions src/backend/apps/webcam/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def update_cam_from_sql_db(id: int, current_time: datetime.datetime):
credit=F('cam_internetcredit'),
dbc_mark=F('cam_internetdbc_mark'),
isNew=F('isnew'),
cam_locations_weather_station=F('cam_locationsweather_station'),
)
.values(
'id',
Expand All @@ -114,14 +115,17 @@ def update_cam_from_sql_db(id: int, current_time: datetime.datetime):
'credit',
'dbc_mark',
'isNew',
'cam_locations_weather_station',
)
.first()
)

if cam:
update_webcam_db(id, cam)
update_current_weather_code(id, cam)
return True
else:
logging.info(f"No mattching camera {id} was found in DBC database.")
return False

except Exception as e:
Expand All @@ -142,14 +146,26 @@ def format_region_name(region_name):
return ''.join(result)


def update_current_weather_code(cam_id: int, cam_data: dict):
cam = Webcam.objects.get(id=cam_id)
code = cam_data.get("cam_locations_weather_station", "").strip()

# Remove the link if there is no weather station code
if not code:
cam.local_weather_station = None
cam.save(update_fields=["local_weather_station_id"])

# Update the linked CurrentWeather record
CurrentWeather.objects.filter(
id=cam.local_weather_station_id
).update(code=code)

return True

def update_webcam_db(cam_id: int, cam_data: dict):
time_now_utc = datetime.datetime.now(datetime.timezone.utc).strftime("%Y%m%d%H%M%S%f")[:-3]
camera_status = calculate_camera_status(cam_id, time_now_utc)

# Unused
# ts_seconds = int(camera_status["timestamp"])
# dt_utc = datetime.datetime.fromtimestamp(ts_seconds, tz=ZoneInfo("UTC"))

existing_webcam = Webcam.objects.filter(id=cam_id).first()
if not existing_webcam:
return False
Expand Down Expand Up @@ -546,7 +562,6 @@ def update_camera_weather_station(camera, weather_class):

if weather_class == CurrentWeather:
stations_qs = stations_qs.filter(
distance__lte=D(km=30),
elevation__lte=camera.elevation + 300,
elevation__gte=camera.elevation - 300,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ def test_cam_found_calls_update_webcam_db_and_returns_true(

result = update_cam_from_sql_db(id=1, current_time=datetime.datetime.now())

self.assertTrue(result)
mock_update_webcam_db.assert_called_once_with(1, mock_cam)
mock_camera_source.objects.using.assert_called_once_with("mssql")

Expand Down