From 82dfeb112ad01f9e677a252053f0f480134d9cf9 Mon Sep 17 00:00:00 2001 From: Franoosh Date: Thu, 18 Jun 2026 07:23:18 +0200 Subject: webserver.py: - fix parameter extraction in validate_ functions - fix logging statement missing a parameter. client.py: - cosmetic changes: change multiple returns to building retval - logging fixes. --- client.py | 37 ++++++++++++++++++++++--------------- webserver.py | 10 +++++----- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/client.py b/client.py index 7b7a697..4a18d75 100644 --- a/client.py +++ b/client.py @@ -527,7 +527,7 @@ class ClientTask(Thread): self.video_threads[camera_id].device_threshold = _value # <-- integer! # configparser requires strings: cfg.set('cameras', f"{camera_id.decode('utf-8')}.contour_size_threshold", str(value)) - logger.info("Modified contour size threshold of camera %r to %d.", camera_id, value) + logger.info("Modified contour size threshold of camera %r to %d.", camera_id, _value) case b'modify_camera_grace_pd': try: _value = int(value) @@ -537,7 +537,7 @@ class ClientTask(Thread): CAMERAS[camera_id][b'movement_grace_period'] = _value # <-- integer! self.video_threads[camera_id].device_grace_period = _value # <-- integer! cfg.set('cameras', f"{camera_id.decode('utf-8')}.movement_grace_period", str(value)) - logger.info("Modified movement grace period of camera %r to %d.", camera_id, value) + logger.info("Modified movement grace period of camera %r to %d.", camera_id, _value) case b'add_camera': CAMERAS[camera_id] = {b'address': value} # vid_cl = ClientVideo(camera_id, CAMERAS[camera_id], self.queue) @@ -865,9 +865,10 @@ def validate_camera_address(address: str) -> bool: True if address is valid, False otherwise """ # Check if address is an integer (device index): + retval = False if address.startswith('/dev/video'): - return True - raise False + retval = True + return retval def validate_camera_threshold(threshold: str) -> bool: @@ -886,10 +887,12 @@ def validate_camera_threshold(threshold: str) -> bool: """ try: if int(threshold) > 0: - return True + retval = True + else: + retval = False except ValueError: - raise False - return False + retval = False + return retval def validate_camera_grace_pd(grace_pd: str) -> bool: @@ -908,10 +911,12 @@ def validate_camera_grace_pd(grace_pd: str) -> bool: """ try: if int(grace_pd) >= 0: - return True + retval = True + else: + retval = False except ValueError: - return False - return False + retval = False + return retval def camera_address_unique(cfg: ConfigParser, address: str) -> bool: @@ -930,14 +935,15 @@ def camera_address_unique(cfg: ConfigParser, address: str) -> bool: bool True if address is unique, False otherwise """ + retval = True for key, val in cfg.items('cameras'): try: _, param = key.split('.', 1) if param == 'address' and val == address: - return False + retval = False except ValueError: continue - return True + return retval def set_up_cameras(cameras_cfg_section: list[tuple[str, str]]) -> dict: @@ -1015,16 +1021,17 @@ def get_serial_number() -> bytes: bytes Serial number as string """ + serial = uuid.uuid4().hex.encode('utf-8') # Default to random uuid if we can't get serial try: with open('/proc/cpuinfo', 'r') as f: for line in f: if line.startswith('Serial'): serial = line.split(':')[1].strip() - return serial.encode('utf-8') + serial = serial.encode('utf-8') except Exception as exc: - logger.error("Error getting serial number: %r, using uuid.", exc) + logger.warning("Error getting serial number: %r, using uuid.", exc) - return str(uuid.getnode()).encode('utf-8') + return serial if __name__ == "__main__": diff --git a/webserver.py b/webserver.py index 1e1a068..4314b5e 100644 --- a/webserver.py +++ b/webserver.py @@ -412,13 +412,13 @@ def validate_directive(command: str, args: list, client_id: str, camera_id: str) # Validate arguments passed from the frontend: match command: case 'modify_camera_name': - retval = validate_camera_name(args, client_id, camera_id) + retval = validate_camera_name(args[0], client_id, camera_id) case 'modify_camera_threshold': - retval = validate_camera_threshold(args) + retval = validate_camera_threshold(args[0]) case 'modify_camera_grace_pd': - retval = validate_camera_grace_period(args) + retval = validate_camera_grace_period(args[0]) case 'modify_camera_address': - retval = validate_camera_address(args) + retval = validate_camera_address(args[0]) # case 'add_camera': # retval = validate_add_camera(args, client_id) case 'remove_camera': @@ -509,7 +509,7 @@ def validate_camera_grace_period(grace_period) -> bool: logger.error("Movement grace period cannot be negative: %r.", grace_period) retval = False except ValueError as exc: - logger.error("Invalid movement grace period: %r, exception: ", grace_period, exc) + logger.error("Invalid movement grace period: %r, exception: %r", grace_period, exc) retval = False return retval -- cgit v1.3