2222import ipaddress
2323import json
2424import urllib .parse
25+ from typing import Optional
2526
2627import aiohttp
2728
@@ -97,7 +98,7 @@ def normalize_url(url: str) -> str:
9798 return f'http://{ url } '
9899
99100
100- async def connect_to_duet (address : str , password : str ) -> dict :
101+ async def connect_to_duet (address : str , password : str ) -> Optional [ dict ] :
101102 """Connect to a printer using the specified IP address and password."""
102103 duet = RepRapFirmware (
103104 address = normalize_url (address ),
@@ -110,20 +111,28 @@ async def connect_to_duet(address: str, password: str) -> dict:
110111 board = board ['result' ]
111112 duet_name = await duet .rr_model (key = 'network.name' )
112113 duet_name = duet_name ['result' ]
113- webcam_uri = await get_webcam_url (duet )
114+
115+ try :
116+ webcam_uri = await get_webcam_url (duet )
117+ except aiohttp .client_exceptions .ClientResponseError :
118+ webcam_uri = None
119+
114120 try :
115121 cookie = await get_cookie (duet )
116122 except aiohttp .client_exceptions .ClientResponseError :
117123 cookie = None
124+
125+ except aiohttp .client_exceptions .ClientResponseError as e :
126+ print (e )
127+ return None
118128 except (
119129 aiohttp .client_exceptions .ClientConnectorError ,
120130 aiohttp .ClientError ,
121131 asyncio .exceptions .CancelledError ,
122132 asyncio .exceptions .TimeoutError ,
123133 OSError ,
124134 KeyError ,
125- ) as e :
126- print (e )
135+ ):
127136 return None
128137 finally :
129138 await duet .close ()
@@ -133,7 +142,7 @@ async def connect_to_duet(address: str, password: str) -> dict:
133142 'duet_uri' : normalize_url (f'{ address } ' ),
134143 'duet_password' : password ,
135144 'duet_unique_id' : f"{ board ['uniqueId' ]} " ,
136- 'webcam_uri' : normalize_url (webcam_uri ),
145+ 'webcam_uri' : normalize_url (webcam_uri ) if webcam_uri is not None else None ,
137146 'cookie' : cookie ,
138147 }
139148
0 commit comments