@@ -30,11 +30,10 @@ def basic_auth_str(user: str, pwd: str) -> str:
3030
3131DataClass = enum (
3232 TEMPERATURE = "temperature" ,
33- HUMIDITY = "humidity" ,
3433 PRESSURE = "pressure" ,
35- LIGHT_INTENSITY = "light intensity" ,
3634 COMMENT = "comment" ,
37- BATTERY = "remaining battery"
35+ BATTERY = "remaining battery" ,
36+ BEACON = "beacon challenge"
3837)
3938
4039class DataPoint ():
@@ -69,39 +68,30 @@ class Temperature(DataPoint):
6968 def __init__ (self , value ):
7069 super ().__init__ (DataClass .TEMPERATURE , value )
7170
72- class Humidity (DataPoint ):
73- """A class for DataPoints that store humidity values"""
74- UNIT = "%"
75- def __init__ (self , value ):
76- super ().__init__ (DataClass .HUMIDITY , value )
77-
7871class Pressure (DataPoint ):
7972 """A class for DataPoints that store barometric pressure values"""
8073 UNIT = "inHg"
8174 def __init__ (self , value ):
8275 super ().__init__ (DataClass .PRESSURE , value )
8376
84- class Intensity (DataPoint ):
85- """A class for DataPoints that store light intensity values"""
86- UNIT = "lux"
87- def __init__ (self , value ):
88- super ().__init__ (DataClass .LIGHT_INTENSITY , value )
89-
9077class Text (DataPoint ):
9178 """A class reserved for DataPoints that are intended as a text comment"""
9279 UNIT = "" # No unit for regular strings of text
9380 def __init__ (self , value : str ):
9481 super ().__init__ (DataClass .COMMENT , value )
9582
83+ class BeaconChallenge (DataPoint ):
84+ """A class reserved for DataPoints that are in response to a message from the beacon"""
85+ UNIT = "" # No unit for regular strings of text
86+ def __init__ (self , value : str ):
87+ super ().__init__ (DataClass .BEACON , value )
88+
9689class BatteryLevel (DataPoint ):
9790 """A class reserved for DataPoints that are intended as an indication of battery level"""
9891 UNIT = "%" # No unit for regular strings of text
9992 def __init__ (self , value : int ):
10093 super ().__init__ (DataClass .BATTERY , value )
10194
102- class ConnectionError (Exception ):
103- """Indicates an issue with the server connection"""
104-
10595class AuthorizationError (ConnectionError ):
10696 """Indicates an issue with the team credentials"""
10797
@@ -165,7 +155,7 @@ def __init__(
165155 self .context .load_verify_locations (cadata = server_cert )
166156 self .connect_wifi ()
167157
168- def connect_wifi (self , attempts = 10 ) -> None :
158+ def connect_wifi (self , attempts = 50 ) -> None :
169159 """Creates the wifi connection to the access point"""
170160 wifi .radio .enabled = True
171161 if self .v :
@@ -179,7 +169,7 @@ def connect_wifi(self, attempts=10) -> None:
179169 if self .v :
180170 print (e .with_traceback )
181171 attempts -= 1
182- time .sleep (1 )
172+ time .sleep (. 1 )
183173 if self .v :
184174 print ("Initializing socket pool..." )
185175 self .pool = socketpool .SocketPool (wifi .radio )
@@ -218,6 +208,7 @@ def radio(self):
218208 return wifi .radio
219209
220210 def rx_bytes (self ) -> bytes :
211+ total_length : int = None
221212 response = b""
222213 while True :
223214 buf = bytearray (256 )
@@ -228,12 +219,21 @@ def rx_bytes(self) -> bytes:
228219 recvd = 0
229220 else :
230221 raise
231- response += buf
222+ response += bytes ( buf ). replace ( b' \x00 ' , b'' )
232223 del buf
233224 collect ()
234225 if self .v :
235226 print (f"Received { recvd } bytes" )
236- if recvd == 0 :
227+ if response .endswith (b'\r \n \r \n ' ):
228+ header_chunks = response .split (b'\r \n ' )
229+ for header in header_chunks :
230+ if header .startswith (b'Content-Length: ' ):
231+ total_length = len (response ) + int (header .split (b' ' )[1 ])
232+ break
233+ if recvd == 0 or (
234+ total_length is not None and \
235+ len (response ) >= total_length
236+ ):
237237 del recvd
238238 collect ()
239239 break
@@ -267,8 +267,8 @@ def _do_request(
267267 f"{ method } { path } HTTP/1.1\r \n " +
268268 "Host: api.local\r \n " +
269269 "Connection: close\r \n " +
270- f"Authorization: Basic { auth_str } " +
271- '\r \n ' .join (headers ) + " \r \n "
270+ f"Authorization: Basic { auth_str } \r \n " +
271+ '\r \n ' .join (headers )
272272 )
273273 del auth_str
274274 collect ()
@@ -283,7 +283,8 @@ def _do_request(
283283 f"Content-Length: { len (body )} \r \n " +
284284 f"\r \n { body } \r \n "
285285 )
286- req_text += '\r \n '
286+ else :
287+ req_text += '\r \n \r \n '
287288
288289 if self .v :
289290 print ("Sending request..." )
@@ -302,7 +303,7 @@ def _do_request(
302303 collect ()
303304 if self .v :
304305 print ("Receiving response..." )
305- self .wrapped_socket .setblocking ( False )
306+ self .wrapped_socket .settimeout ( self . conf . TIMEOUT )
306307 response = self .rx_bytes ()
307308 except Exception as e :
308309 if self .v :
@@ -388,7 +389,7 @@ def get_status(self) -> GameStatus:
388389 resp_json = loads (resp [1 ])
389390 if self .v :
390391 print (f"It is { resp_json ['unix_time' ]} seconds since the epoch." )
391- return GameStatus (Time (resp_json ['unix_time' ]), resp_json ['status' ]['score' ])
392+ return GameStatus (Time (resp_json ['unix_time' ]), resp_json ['status' ]['score' ], resp_json [ 'CubeServer_version' ] )
392393
393394 def sync_time (self ) -> bool :
394395 """Syncs the current clock against the server"""
0 commit comments