Fix: Websocket communication misunderstanding error

This commit is contained in:
ziesorx 2025-09-13 00:19:41 +07:00
parent 9967bff6dc
commit 42a8325faf
8 changed files with 1109 additions and 63 deletions

View file

@ -598,7 +598,7 @@ class StreamManager:
async def start_stream(self, camera_id: str, payload: Dict[str, Any]) -> bool:
"""
Start a stream for WebSocket handler compatibility.
Start a stream for WebSocket handler compatibility with enhanced validation.
Args:
camera_id: Camera identifier
@ -608,35 +608,62 @@ class StreamManager:
True if stream started successfully, False otherwise
"""
try:
# Validate inputs
if not camera_id:
logger.error(f"Invalid camera_id provided: {camera_id}")
return False
if not payload:
logger.error(f"Empty payload provided for camera {camera_id}")
return False
# Create a subscription ID for this stream
subscription_id = f"ws_{camera_id}_{int(time.time() * 1000)}"
# Extract stream parameters from payload
# Extract stream parameters from payload with validation
rtsp_url = payload.get('rtspUrl')
snapshot_url = payload.get('snapshotUrl')
snapshot_interval = payload.get('snapshotInterval', 5000)
# Log payload details for debugging
logger.info(f"Starting stream for camera {camera_id} with payload: "
f"rtspUrl={rtsp_url}, snapshotUrl={snapshot_url}, "
f"snapshotInterval={snapshot_interval}")
# Validate URLs
if rtsp_url and not isinstance(rtsp_url, str):
logger.error(f"Invalid rtspUrl type for camera {camera_id}: {type(rtsp_url)}")
rtsp_url = None
if snapshot_url and not isinstance(snapshot_url, str):
logger.error(f"Invalid snapshotUrl type for camera {camera_id}: {type(snapshot_url)}")
snapshot_url = None
# Create a subscriber_id (for WebSocket compatibility, use the subscription_id)
subscriber_id = f"websocket_{int(time.time() * 1000)}"
# Create subscription based on available URL type
if rtsp_url:
if rtsp_url and rtsp_url.strip():
logger.info(f"Creating RTSP stream for camera {camera_id}: {rtsp_url}")
success = self.create_subscription(
subscription_id=subscription_id,
camera_id=camera_id,
subscriber_id=subscriber_id,
rtsp_url=rtsp_url
rtsp_url=rtsp_url.strip()
)
elif snapshot_url:
elif snapshot_url and snapshot_url.strip():
logger.info(f"Creating snapshot stream for camera {camera_id}: {snapshot_url}")
success = self.create_subscription(
subscription_id=subscription_id,
camera_id=camera_id,
subscriber_id=subscriber_id,
snapshot_url=snapshot_url,
snapshot_url=snapshot_url.strip(),
snapshot_interval=snapshot_interval
)
else:
logger.error(f"No valid stream URL provided for camera {camera_id}")
logger.error(f"No valid stream URL provided for camera {camera_id}. "
f"rtspUrl='{rtsp_url}', snapshotUrl='{snapshot_url}'. "
f"Payload keys: {list(payload.keys())}")
return False
if success:
@ -648,6 +675,8 @@ class StreamManager:
except Exception as e:
logger.error(f"Error starting stream for camera {camera_id}: {e}")
import traceback
traceback.print_exc()
return False
async def stop_stream(self, camera_id: str) -> bool: